Skip to content

Instantly share code, notes, and snippets.

@gsmaverick
Created January 18, 2015 02:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gsmaverick/e0a2b9424d507b3a8faf to your computer and use it in GitHub Desktop.
Save gsmaverick/e0a2b9424d507b3a8faf to your computer and use it in GitHub Desktop.
Simple unit testing in wren.
// Simple unit testing in wren.
class Spec {
new(description, body) {
_description = description
_body = body
}
description { _description }
run {
var expectations = []
while (!_body.isDone) {
var yieldResult = _body.try
// When a fiber finishes the last yield returns `null`.
if (yieldResult != null) {
expectations.add(yieldResult)
}
}
return expectations
}
}
class Suite {
new(name, block) {
_name = name
// Stores all the test bodies defined in this suite.
_tests = []
block.call(this)
}
expect(condition) {
Fiber.yield(condition)
}
it(description, body) {
var spec = new Spec(description, body)
_tests.add(spec)
}
run {
IO.print("Running ", _name)
for (test in _tests) {
var testResult = test.run
var succeeded = testResult.all { |r| r == true }
var str
if (succeeded) {
str = "[passed]"
} else {
str = "[failed]"
}
IO.print(" " + str + " " + test.description)
}
IO.print("Finished ", _name)
}
}
var suite = new Suite("strings", new Fn { |s|
s.it("should concatenate with another string", new Fiber {
s.expect(("a" + "b") == "ab")
})
s.it("should runtime error if concatenated with a non-string", new Fiber {
var fiber = new Fiber { "a" + 123 }
s.expect(fiber.try == "Right operand must be a string.")
})
// Classiness.
s.it("should be of class String", new Fiber {
s.expect("a" is String)
})
})
suite.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment