Skip to content

test.unit.wren

clsource edited this page Nov 10, 2020 · 13 revisions

Unit testing

These are some classes For doing TDD with Wren and Dome Used to make assertions about values in test cases. Loosely inspired on Assert.hx and Please.wren


This is an example test to demostrate the creation of a new test suite. Is not required to be child of this class. This is just an example.

class ExampleTest {
  /// Required property that stores all the tests to run
  static all {[thatExampleTestWorks]}

  /// Individual test. Can be any name.
  /// Could return a List, List of List or a Single Fiber.

  /// Single Fiber Test
  static thatExampleTestWorks {
    Fn.new {|assert|
      // Run your tests here.
    }
  }

  /// Single Fiber Test with Description
  static testThatEpsilonExists {[
    "epsilon value exists",
    Fn.new { |assert|
      assert.isNotNull(Ss.epsilon)
    }
  ]}

  /// Multi Fiber Test with Description
  static testThatSumWorks {[
    ["can get the sum of two numbers",
    Fn.new{ |assert|
      assert.equal(Ss.sum([1,2]), 3)
    }],
    ["the sum of no numbers is zero",
      Fn.new{ |assert|
        assert.equal(Ss.sum([]), 0)
    }]
  ]}

  /// Multi Fiber Test with Group Description
  static testThatSumWorks2 {[
    "Ss.sum()", [
      ["can get the sum of two numbers",
      Fn.new{ |assert|
        assert.equal(Ss.sum([1,2]), 3)
      }],
      ["the sum of no numbers is zero",
        Fn.new{ |assert|
          assert.equal(Ss.sum([]), 0)
      }]
    ]
  ]}

  /// Optional methods and properties
  /// They can be omited from the test suite
  static describe { "optional description of the test suite" }
  static setup() {}
  static teardown() {}
}

API

Optional description of the test suite. It defaults to the class name if not provided.

  • Signature: static var describe:String? = "%(Class)"

Required static list of all the test methods that should be executed in this test suite.

  • Signature: static var all:List

Optional method that is called before running the test suite.

  • Signature: static func setup() -> Void

Optional method that is called after running the test suite.

  • Signature: static func teardown() -> Void

Every test should return at least a Fn.new{} object to make the test assertions. assert object is automatically injected by the test runner

  • Signature: static var thatExampleTestWorks:<Fiber|List>

This is a simple test runner. It will execute the test lifecycle of setup, execution and teardown for each Test Suite.

Example

import "./unit" for Runner
import "./misc/emoji.test" for EmojiTests

class Game {
    static init() {
      // Add your tests here
      Runner.run(EmojiTests)
      Runner.end()
    }
}
  • Since: 1.0.0

API

It runs all the tests contained in the test Class.

  • Since: 1.0.0
  • Parameter Class: A class which adopts the test suite spec.
  • Signature: static func run(Class:Class) -> Void
  • Throws: Fiber.abort(error) if test fails.

Ends the testing process and optionally executes a end block.

  • Example:
  import "dome" for Process
  Runner.end {
    Process.exit()
  }
  • Since: 1.0.0
  • Signature: static func end<T:Fiber|Fn>(exit:T?) -> Void
  • Parameter exit: An optional block that is executed at the end of the process.

Assertion class provides methods that throws Fiber.abort on failure.

import "./test/unit" for Assert
  • Since: 1.0.0

API

Terminates the execution by throwing a Fiber.abort()

  • Since: 1.0.0
  • Signature: static abort(message:String?) -> Void
  • Parameter message: The message that will show in the abort.
  • Throws: Fiber.abort(message) on assertion error.

Assert that two variables have the same value

  • Since: 1.0.0
  • Signature: static func equal(value:Any, expected:Any, message:String?) -> Void
  • Parameter value: The first variable.
  • Parameter expected: The second variable.
  • Parameter message: Optional mesage to show on assertion error.
  • Throws: Fiber.abort() on assertion error.

Assert that two variables have the different values

  • Since: 1.0.0
  • Signature: static func isNotEqual(value:Any, expected:Any, message:String?) -> Void
  • Parameter value: The first variable.
  • Parameter expected: The second variable.
  • Parameter message: Optional mesage to show on assertion error.
  • Throws: Fiber.abort() on assertion error.

Assert that two floating point variables have the same value. This is needed due to Floating-Point arithmetic. Thanks to @Dr.Henwig at Wren's Discord for the help.

  • See: Floating Point Arithmetic
  • See: Floating Point Guide
  • Since: 1.0.0
  • Signature: static func nearlyEqual(value:Num, expected:Num, epsilon:Num, message:String?) -> Void
  • Parameter value: The first variable.
  • Parameter expected: The second variable.
  • Parameter epsilon: The precision needed to consider values equal. e.g. 0.001
  • Parameter message: Optional mesage to show on assertion error.
  • Throws: Fiber.abort() on assertion error.

Assert that two floating point variables have different values. This is needed due to Floating-Point arithmetic.

  • See: Floating Point Arithmetic
  • Since: 1.0.0
  • Signature: static func isNotNearlyEqual(value:Num, expected:Num, epsilon:Num, message:String?) -> Void
  • Parameter value: The first variable.
  • Parameter expected: The second variable.
  • Parameter epsilon: The precision needed to consider values equal. e.g. 0.001
  • Parameter message: Optional mesage to show on assertion error.
  • Throws: Fiber.abort() on assertion error.

Assert that one variable is above the expected.

  • Since: 1.0.0
  • Signature: static func above(value:Any, expected:Any, message:String?) -> Void
  • Parameter value: The first variable.
  • Parameter expected: The second variable.
  • Parameter message: Optional mesage to show on assertion error.
  • Throws: Fiber.abort() on assertion error.

Assert that one variable is below the expected.

  • Since: 1.0.0
  • Signature: static func below(value:Any, expected:Any, message:String?) -> Void
  • Parameter value: The first variable.
  • Parameter expected: The second variable.
  • Parameter message: Optional mesage to show on assertion error.
  • Throws: Fiber.abort() on assertion error.

Verifies that the value belongs to a certain kind of class.

  • Since: 1.0.0
  • Signature: static func isKind(value:Any, Kind:Class, message:String?) -> Void
  • Parameter value: The value that will be checked.
  • Parameter Kind: The Class that would be checked against the value.
  • Parameter message: Optional parameter with a message to show on assertion error.
  • Throws: Fiber.abort() on assertion error.

Verifies that the value not belongs to a certain kind of class.

  • Since: 1.0.0
  • Signature: static func isNotKind(value:Any, Kind:Class, message:String?) -> Void
  • Parameter value: The value that will be checked.
  • Parameter Kind: The Class that would be checked against the value.
  • Parameter message: Optional parameter with a message to show on assertion error.
  • Throws: Fiber.abort() on assertion error.

Assert that a value is null.

  • Since: 1.0.0
  • Signature: static func isNull(value:Any, message:String?) -> Void
  • Parameter value: The value that would be checked.
  • Parameter message: Optional message to show on assertion error.
  • Throws: Fiber.abort() on assertion error.

Assert that a value is not null.

  • Since: 1.0.0
  • Signature: static func isNull(value:Any, message:String?) -> Void
  • Parameter value: The value that would be checked.
  • Parameter message: Optional message to show on assertion error.
  • Throws: Fiber.abort() on assertion error.

Asserts that a value is kind of String.

  • Since: 1.0.0
  • Signature: static func isString(value:Any, message:String?) -> Void
  • Parameter value: A value to check.
  • Parameter message: Optional message to show on assertion error.
  • Throws: Fiber.abort() on assertion error.

Asserts that a value is not kind of String.

  • Since: 1.0.0
  • Signature: static func isString(value:Any, message:String?) -> Void
  • Parameter value: A value to check.
  • Parameter message: Optional message to show on assertion error.
  • Throws: Fiber.abort() on assertion error.

Assert that a block of code (Fiber or Fn) fails (Throws Fiber.abort()).

  • Since: 1.0.0
  • Signature: static func fail<T:Fiber|Fn>(block:T, message:String?) -> Void
  • Parameter value: The value that would be checked.
  • Parameter message: Optional message to show on assertion error.
  • Throws: Fiber.abort() on assertion error.

Assert that a block of code (Fiber or Fn) succeeds (not fails) (Not throws Fiber.abort()).

  • Since: 1.0.0
  • Signature: static func fail<T:Fiber|Fn>(block:T, message:String?) -> Void
  • Parameter value: The value that would be checked.
  • Parameter message: Optional message to show on assertion error.
  • Throws: Fiber.abort() on assertion error.
Clone this wiki locally