TestableCode.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.util.*;
import Hoot.Collections.*;
import Hoot.Compiler.Scopes.*;
import Hoot.Runtime.Names.TypeName;
import Hoot.Runtime.Names.Primitive;

public class TestableCode extends TestableClosure
{

  protected OrderedCollection<String> codeBlocks = OrderedCollection.type().$new();
  protected static final String CodeReport = String.from("%s = %s, %s");

  /**
   * 
   */
  protected    TestableCode(final ArrayedCollection<String> blocks)
  {
    super();
    java.lang.String exitID = "TestableCode>>TestableCode";
    Frame f0 = new Frame(exitID);
    codeBlocks.addAll(blocks);
  }

  /**
   * @return 
   */
  public static   TestableCode withAll(final ArrayedCollection<String> blocks)
  {
    java.lang.String exitID = "TestableCode>>$withAll";
    Frame f0 = new Frame(exitID);
    return (TestableCode)new TestableCode(blocks);
  }

  /**
   * @return 
   */
  public static   TestableCode withAll(final List<String> blocks)
  {
    java.lang.String exitID = "TestableCode>>$withAll";
    Frame f0 = new Frame(exitID);
    return (TestableCode)TestableCode.withAll(OrderedCollection.type().from(blocks));
  }

  /**
   * @return 
   */
  public static   TestableCode withAll(final String ... blocks)
  {
    java.lang.String exitID = "TestableCode>>$withAll";
    Frame f0 = new Frame(exitID);
    return (TestableCode)TestableCode.withAll(Utils.wrap(blocks));
  }

  /**
   * @return 
   */
  public   void runTests()
  {
    java.lang.String exitID = "TestableCode>>runTests";
    Frame f0 = new Frame(exitID);
    codeBlocks.$do(Closure.with(f2 -> {
      String each = f2.getValue(0).value();
      this.showCode(each);
    }, "each"));
  }

  /**
   * @return 
   */
  public   java.lang.Object evaluateCode(final String code)
  {
    java.lang.String exitID = "TestableCode>>evaluateCode";
    Frame f0 = new Frame(exitID);
    return (java.lang.Object)ClosureCompiler.evaluate(code.asPrimitive());
  }

  /**
   * @return 
   */
  public   TestableCode showCode(final String code)
  {
    java.lang.String exitID = "TestableCode>>showCode";
    Frame f0 = new Frame(exitID);
    if ((code.size().intValue() == 0)) {
      this.log().cr();
      return this;
    };

    java.lang.Object result = this.evaluateCode(code);

    java.lang.Class resultType = Primitive.JavaRoot;
    if ((result == null)) {
       result = "null";
    }
    else {
       resultType = result.getClass();
    };
    this.reportCode_result_type(code, result, result.getClass());
    return (TestableCode)this;
  }

  /**
   * @return 
   */
  public   TestableCode reportCode_result_type(final String code, final java.lang.Object result, final java.lang.Class resultType)
  {
    java.lang.String exitID = "TestableCode>>reportCode_result_type";
    Frame f0 = new Frame(exitID);
    this.log().printLine(CodeReport.formatWith(code, String.from(result.toString()), String.from(resultType.getSimpleName())));
    return (TestableCode)this;
  }
}