myst-lang / myst

A structured, dynamic, general-purpose language.
http://myst-lang.org
MIT License
118 stars 17 forks source link

Add `assert` library for making assertions. #169

Closed faultyserver closed 6 years ago

faultyserver commented 6 years ago

Instead of the simple assert and refute methods for making basic assertions, the assert library consists of two classes: Assertion and BlockAssertion. These classes define various methods for making the actual assertions, all of which use a fluid design to make writing assertions in test code clean and easy.

The assert library is automatically included with spec, and the entire set of Myst specs have been refactored to use it appropriately.

In total, the methods added by this PR are:

To access these methods, the library also adds two clauses of an assert method, which takes care of instantiating the right type of object for the assertions being made.

For assertions on static values, just use assert(some_argument), then call the appropriate method for the assertion. For example:

assert(1).equals(1)
assert(1) != 2

For assertions on dynamic values, or about what happens during the execution of a method, use the block form of assert{ some_block_code }:

assert{ raise :foo }.raises(:foo)
assert{ :return_value }.returns(:return_value)

When an assertion does not pass, it will raise an AssertionFailure object with relevant information about the failure. Inside of the spec library, this is automatically printed out to the console with additional location information.