TestableClosure.java

/**
* Copyright 2010,2021 Nikolas S Boyd.
Permission is granted to copy this work provided this copyright statement is retained in all copies.

*/
package Hoot.Tests;

import Hoot.Runtime.Functions.*;
import Hoot.Runtime.Faces.*;
import Hoot.Runtime.Values.*;
import Hoot.Runtime.Blocks.*;
import Smalltalk.Core.*;
import Smalltalk.Blocks.*;
import Smalltalk.Magnitudes.*;
import Hoot.Behaviors.*;
import Hoot.Behaviors.Nil;
import Hoot.Behaviors.Object;
import Hoot.Behaviors.True;
import Hoot.Behaviors.False;
import Hoot.Behaviors.Boolean;
import Hoot.Magnitudes.*;
import Hoot.Magnitudes.Integer;
import Hoot.Magnitudes.Character;
import Hoot.Magnitudes.Float;
import Hoot.Magnitudes.Double;
import Hoot.Collections.*;
import Hoot.Collections.String;
import java.lang.Runtime;
import Hoot.Streams.Transcript;
import static Hoot.Magnitudes.SmallInteger.*;
import Hoot.Runtime.Blocks.MultiValuable;
import Hoot.Metrics.MetricsSnapshot;

public class TestableClosure extends Object implements Testable, MultiValuable
{

  protected Closure closure;
    protected String name = String.emptyString();
    public String name() { return this.name; }
    public Object getName() { return (Object)this.name; }

    public TestableClosure name(String value) { this.name = value; return this; }
    public void setName(Object value) { this.name = (String)value; }

  ;
  protected MetricsSnapshot beforeTest = new MetricsSnapshot();
  protected MetricsSnapshot afterTest = new MetricsSnapshot();
  protected static final String MsecsReport = String.from("%s %s = %s msecs");
  protected static final String SecsReport = String.from("%s %s = %s secs");

  /**
   * 
   */
  public    TestableClosure()
  {
    java.lang.String exitID = "TestableClosure>>TestableClosure";
    Frame f0 = new Frame(exitID);
  }

  /**
   * @return 
   */
  public   TestableClosure with(final Closure c)
  {
    java.lang.String exitID = "TestableClosure>>with";
    Frame f0 = new Frame(exitID);
     closure = c;
    return (TestableClosure)this;
  }

  /**
   * @return 
   */
  public static   TestableClosure closure(final Closure c)
  {
    java.lang.String exitID = "TestableClosure>>$closure";
    Frame f0 = new Frame(exitID);
    return (TestableClosure)new TestableClosure(c);
  }

  /**
   * @return 
   */
  @Override public   TestableClosure value()
  {
    java.lang.String exitID = "TestableClosure>>value";
    Frame f0 = new Frame(exitID);
    this.runTest();
    return (TestableClosure)this;
  }

  /**
   * @return 
   */
  protected   Closure createClosure()
  {
    java.lang.String exitID = "TestableClosure>>createClosure";
    Frame f0 = new Frame(exitID);
    return (Closure)Closure.with(f2 -> {
      this.runTest();
    }, "");
  }

  /**
   * @return 
   */
  public   Closure testableClosure()
  {
    java.lang.String exitID = "TestableClosure>>testableClosure";
    Frame f0 = new Frame(exitID);
    if ((closure == null)) {
       closure = this.createClosure();
    };
    return (Closure)closure;
  }

  /**
   * @return 
   */
  public static   TestableClosure named(final String name)
  {
    java.lang.String exitID = "TestableClosure>>$named";
    Frame f0 = new Frame(exitID);
    return (TestableClosure)new TestableClosure(name);
  }

  /**
   * @return 
   */
  public   String description()
  {
    java.lang.String exitID = "TestableClosure>>description";
    Frame f0 = new Frame(exitID);
    return (String)name.notEmpty().ifFalse_ifTrue(Closure.with(f2 -> {
      return String.from(this.getClass().getSimpleName());
    }, ""), Closure.with(f2 -> {
      return name;
    }, ""));
  }

  /**
   * 
   */
  protected    TestableClosure(final String nameString)
  {
    java.lang.String exitID = "TestableClosure>>TestableClosure";
    Frame f0 = new Frame(exitID);
     name = nameString;
  }

  /**
   * 
   */
  protected    TestableClosure(final Closure c)
  {
    java.lang.String exitID = "TestableClosure>>TestableClosure";
    Frame f0 = new Frame(exitID);
     closure = c;
  }

  /**
   * @return 
   */
  public static   TestableClosure named_repeat_closure(final String name, final SmallInteger count, final Closure c)
  {
    java.lang.String exitID = "TestableClosure>>$named_repeat_closure";
    Frame f0 = new Frame(exitID);
    return (TestableClosure)new TestableClosure(name).with(c).reportAfterRepeated(count);
  }

  /**
   * @return 
   */
  public   void recycleGarbage()
  {
    java.lang.String exitID = "TestableClosure>>recycleGarbage";
    Frame f0 = new Frame(exitID);
    java.lang.Runtime.getRuntime().gc();
  }

  /**
   * @return 
   */
  public   void snapshotBeforeTest()
  {
    java.lang.String exitID = "TestableClosure>>snapshotBeforeTest";
    Frame f0 = new Frame(exitID);
     beforeTest = new MetricsSnapshot();
  }

  /**
   * @return 
   */
  public   void snapshotAfterTest()
  {
    java.lang.String exitID = "TestableClosure>>snapshotAfterTest";
    Frame f0 = new Frame(exitID);
     afterTest = new MetricsSnapshot();
  }

  /**
   * @return 
   */
  public   Array<MetricsSnapshot> snapshots()
  {
    java.lang.String exitID = "TestableClosure>>snapshots";
    Frame f0 = new Frame(exitID);
    return (Array<MetricsSnapshot>)((Array)Array.withAll(new Object[]{
      beforeTest, afterTest
    }));
  }

  /**
   * @return 
   */
  public   TestableClosure reportSnapshots()
  {
    java.lang.String exitID = "TestableClosure>>reportSnapshots";
    Frame f0 = new Frame(exitID);
    beforeTest.reportUsage(String.from("afore test"));
    afterTest.reportUsage(String.from("after test"));
    afterTest.reportHeapUsage();
    this.log().cr();
    return (TestableClosure)this;
  }

  /**
   * @return 
   */
  public   TestableClosure doIt()
  {
    java.lang.String exitID = "TestableClosure>>doIt";
    Frame f0 = new Frame(exitID);
    this.snapshotBeforeTest();
    this.runTest();
    this.snapshotAfterTest();
    return (TestableClosure)this;
  }

  /**
   * @return 
   */
  public   void prepare()
  {
    java.lang.String exitID = "TestableClosure>>prepare";
    Frame f0 = new Frame(exitID);
  }

  /**
   * @return 
   */
  @Override public   void runTest()
  {
    java.lang.String exitID = "TestableClosure>>runTest";
    Frame f0 = new Frame(exitID);
  }

  /**
   * @return 
   */
  public   void cleanUp()
  {
    java.lang.String exitID = "TestableClosure>>cleanUp";
    Frame f0 = new Frame(exitID);
  }

  /**
   * @return 
   */
  public   Transcript log()
  {
    java.lang.String exitID = "TestableClosure>>log";
    Frame f0 = new Frame(exitID);
    return (Transcript)Transcript.log();
  }

  /**
   * @return 
   */
  public   TestableClosure reportAfterRun()
  {
    java.lang.String exitID = "TestableClosure>>reportAfterRun";
    Frame f0 = new Frame(exitID);
    this.prepare();
    this.snapshotBeforeTest();

    LongInteger duration = LongInteger.from(this.testableClosure().millisecondsToRun());
    this.snapshotAfterTest();
    this.cleanUp();
    this.log().printLine(MsecsReport.formatWith(Unity.printString(), this.description().rightPadded(SmallInteger.from(20)), duration.printString()));
    return (TestableClosure)this;
  }

  /**
   * @return 
   */
  public   TestableClosure reportAfterRepeated(final SmallInteger count)
  {
    java.lang.String exitID = "TestableClosure>>reportAfterRepeated";
    Frame f0 = new Frame(exitID);
    this.prepare();
    this.snapshotBeforeTest();

    LongInteger duration = LongInteger.from(this.testableClosure().millisecondsToRepeat(count));
    this.snapshotAfterTest();
    this.cleanUp();
    this.log().printLine(SecsReport.formatWith(count.printString(), this.description().rightPadded(SmallInteger.from(20)), (duration.divideWith(SmallInteger.from(1000))).asScaledDecimal(SmallInteger.from(3)).printString()));
    return (TestableClosure)this;
  }
}