class TestRunner

class TestRunner

lib/unittest/test_runner.tya:2

TestRunner provides the unittest/TestRunner standard library API.

Source
# TestRunner provides the unittest/TestRunner standard library API.
class TestRunner
  # TestRunner.default provides the unittest/TestRunner standard library operation.
  # @return Any the resulting value.
  default: ->
    TestRunner()

  # TestRunner.error_message provides the unittest/TestRunner standard library operation.
  # @param err Any err value.
  # @return Any the resulting value.
  error_message: err ->
    msg = err
    try
      msg = err["message"]
    catch _
      msg = err
    msg

  # TestRunner.print_error provides the unittest/TestRunner standard library operation.
  # @param label Any label value.
  # @param err Any err value.
  # @return Any the resulting value.
  print_error: label, err ->
    msg = self.error_message(err)
    println("  ERROR {label}")
    println("        {msg}")

  # TestRunner.print_fail provides the unittest/TestRunner standard library operation.
  # @param label Any label value.
  # @param err Any err value.
  # @return Any the resulting value.
  print_fail: label, err ->
    msg = self.error_message(err)
    println("  FAIL  {label}")
    println("        {msg}")

  # TestRunner.print_summary provides the unittest/TestRunner standard library operation.
  # @param result Any result value.
  # @return Any the resulting value.
  print_summary: result ->
    tests = result["tests"]
    passes = result["passes"]
    failures = result["failures"]
    errors = result["errors"]
    if errors > 0
      println(
        "{tests} tests, {passes} passed, {failures} failed, {errors} errors"
      )
    else
      println("{tests} tests, {passes} passed, {failures} failed")

  # TestRunner.run provides the unittest/TestRunner standard library operation.
  # @param suite Any suite value.
  # @return Any the resulting value.
  run: suite ->
    result = { tests: 0, passes: 0, failures: 0, errors: 0 }
    self.run_suite(suite, result)
    self.print_summary(result)
    result

  # TestRunner.run_and_exit provides the unittest/TestRunner standard library operation.
  # @param suite Any suite value.
  # @return Any the resulting value.
  run_and_exit: suite ->
    result = self.run(suite)
    if result["failures"] > 0 or result["errors"] > 0
      exit(1)
    result

  # TestRunner.run_case provides the unittest/TestRunner standard library operation.
  # @param test Any test value.
  # @param result Any result value.
  # @return Any the resulting value.
  run_case: test, result ->
    klass = test
    methods = []
    try
      methods = test.unittest_test_methods
    catch _
      klass = test.class
      methods = klass.unittest_test_methods
    for method in methods
      instance = test
      try
        instance = klass()
      catch _
        instance = test
      self.run_method(instance, method, result)

  # TestRunner.run_method provides the unittest/TestRunner standard library operation.
  # @param instance Any instance value.
  # @param method String method value.
  # @param result Any result value.
  # @return Any the resulting value.
  run_method: instance, method, result ->
    result["tests"] = result["tests"] + 1
    label = "{instance.class.name}.{method}"
    setup_ok = true
    failed = false
    errored = false
    if instance.class.unittest_has_setup
      try
        instance["setup"]()
      catch err
        setup_ok = false
        errored = true
        self.print_error(label, err)
    if setup_ok
      try
        instance[method]()
      catch err
        kind = ""
        try
          kind = err["kind"]
        catch _
          kind = ""
        if kind == "unittest_fail"
          failed = true
          self.print_fail(label, err)
        else
          errored = true
          self.print_error(label, err)
      if instance.class.unittest_has_teardown
        try
          instance["teardown"]()
        catch err
          errored = true
          self.print_error(label, err)
    if failed
      result["failures"] = result["failures"] + 1
    elseif errored
      result["errors"] = result["errors"] + 1
    else
      result["passes"] = result["passes"] + 1
      println("  PASS  {label}")

  # TestRunner.run_suite provides the unittest/TestRunner standard library operation.
  # @param suite Any suite value.
  # @param result Any result value.
  # @return Any the resulting value.
  run_suite: suite, result ->
    for test in suite.tests
      is_suite = false
      try
        is_suite = test.class == TestSuite
      catch _
        is_suite = false
      if is_suite
        self.run_suite(test, result)
      else
        self.run_case(test, result)

Methods

default

TestRunner.default()

lib/unittest/test_runner.tya:5

TestRunner.default provides the unittest/TestRunner standard library operation.

Source
  # TestRunner.default provides the unittest/TestRunner standard library operation.
  # @return Any the resulting value.
  default: ->
    TestRunner()

error_message

TestRunner.error_message(err)

lib/unittest/test_runner.tya:11

TestRunner.error_message provides the unittest/TestRunner standard library operation.

Source
  # TestRunner.error_message provides the unittest/TestRunner standard library operation.
  # @param err Any err value.
  # @return Any the resulting value.
  error_message: err ->
    msg = err
    try
      msg = err["message"]
    catch _
      msg = err
    msg

print_error

TestRunner.print_error(label, err)

lib/unittest/test_runner.tya:23

TestRunner.print_error provides the unittest/TestRunner standard library operation.

Source
  # TestRunner.print_error provides the unittest/TestRunner standard library operation.
  # @param label Any label value.
  # @param err Any err value.
  # @return Any the resulting value.
  print_error: label, err ->
    msg = self.error_message(err)
    println("  ERROR {label}")
    println("        {msg}")

print_fail

TestRunner.print_fail(label, err)

lib/unittest/test_runner.tya:32

TestRunner.print_fail provides the unittest/TestRunner standard library operation.

Source
  # TestRunner.print_fail provides the unittest/TestRunner standard library operation.
  # @param label Any label value.
  # @param err Any err value.
  # @return Any the resulting value.
  print_fail: label, err ->
    msg = self.error_message(err)
    println("  FAIL  {label}")
    println("        {msg}")

print_summary

TestRunner.print_summary(result)

lib/unittest/test_runner.tya:40

TestRunner.print_summary provides the unittest/TestRunner standard library operation.

Source
  # TestRunner.print_summary provides the unittest/TestRunner standard library operation.
  # @param result Any result value.
  # @return Any the resulting value.
  print_summary: result ->
    tests = result["tests"]
    passes = result["passes"]
    failures = result["failures"]
    errors = result["errors"]
    if errors > 0
      println(
        "{tests} tests, {passes} passed, {failures} failed, {errors} errors"
      )
    else
      println("{tests} tests, {passes} passed, {failures} failed")

run

TestRunner.run(suite)

lib/unittest/test_runner.tya:55

TestRunner.run provides the unittest/TestRunner standard library operation.

Source
  # TestRunner.run provides the unittest/TestRunner standard library operation.
  # @param suite Any suite value.
  # @return Any the resulting value.
  run: suite ->
    result = { tests: 0, passes: 0, failures: 0, errors: 0 }
    self.run_suite(suite, result)
    self.print_summary(result)
    result

run_and_exit

TestRunner.run_and_exit(suite)

lib/unittest/test_runner.tya:64

TestRunner.run_and_exit provides the unittest/TestRunner standard library operation.

Source
  # TestRunner.run_and_exit provides the unittest/TestRunner standard library operation.
  # @param suite Any suite value.
  # @return Any the resulting value.
  run_and_exit: suite ->
    result = self.run(suite)
    if result["failures"] > 0 or result["errors"] > 0
      exit(1)
    result

run_case

TestRunner.run_case(test, result)

lib/unittest/test_runner.tya:74

TestRunner.run_case provides the unittest/TestRunner standard library operation.

Source
  # TestRunner.run_case provides the unittest/TestRunner standard library operation.
  # @param test Any test value.
  # @param result Any result value.
  # @return Any the resulting value.
  run_case: test, result ->
    klass = test
    methods = []
    try
      methods = test.unittest_test_methods
    catch _
      klass = test.class
      methods = klass.unittest_test_methods
    for method in methods
      instance = test
      try
        instance = klass()
      catch _
        instance = test
      self.run_method(instance, method, result)

run_method

TestRunner.run_method(instance, method, result)

lib/unittest/test_runner.tya:95

TestRunner.run_method provides the unittest/TestRunner standard library operation.

Source
  # TestRunner.run_method provides the unittest/TestRunner standard library operation.
  # @param instance Any instance value.
  # @param method String method value.
  # @param result Any result value.
  # @return Any the resulting value.
  run_method: instance, method, result ->
    result["tests"] = result["tests"] + 1
    label = "{instance.class.name}.{method}"
    setup_ok = true
    failed = false
    errored = false
    if instance.class.unittest_has_setup
      try
        instance["setup"]()
      catch err
        setup_ok = false
        errored = true
        self.print_error(label, err)
    if setup_ok
      try
        instance[method]()
      catch err
        kind = ""
        try
          kind = err["kind"]
        catch _
          kind = ""
        if kind == "unittest_fail"
          failed = true
          self.print_fail(label, err)
        else
          errored = true
          self.print_error(label, err)
      if instance.class.unittest_has_teardown
        try
          instance["teardown"]()
        catch err
          errored = true
          self.print_error(label, err)
    if failed
      result["failures"] = result["failures"] + 1
    elseif errored
      result["errors"] = result["errors"] + 1
    else
      result["passes"] = result["passes"] + 1
      println("  PASS  {label}")

run_suite

TestRunner.run_suite(suite, result)

lib/unittest/test_runner.tya:141

TestRunner.run_suite provides the unittest/TestRunner standard library operation.

Source
  # TestRunner.run_suite provides the unittest/TestRunner standard library operation.
  # @param suite Any suite value.
  # @param result Any result value.
  # @return Any the resulting value.
  run_suite: suite, result ->
    for test in suite.tests
      is_suite = false
      try
        is_suite = test.class == TestSuite
      catch _
        is_suite = false
      if is_suite
        self.run_suite(test, result)
      else
        self.run_case(test, result)