Skip to content

Instantly share code, notes, and snippets.

@gsmaverick
Created January 18, 2015 06:05
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/6715d5c98d4a8be3977b to your computer and use it in GitHub Desktop.
Save gsmaverick/6715d5c98d4a8be3977b to your computer and use it in GitHub Desktop.
// Simple unit testing in wren.
class Spec {
new(description, body) {
_description = description
if (_body.type == Fiber){
_body = body
} else {
_body = new Fiber(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 BaseMatcher {
new(value) {
_value = value
}
not {
_negate = true
return this
}
toBe(klass) {
yield_(_value is klass)
}
toBeFalse {
yield_(_value == false)
}
toBeTrue {
yield_(_value == true)
}
toEqual(other) {
yield_(_value == other)
}
toBeARuntimeError(errorMessage) {
// Run the fiber to generate the possible error.
_value.try
yield_(_value.error == errorMessage)
}
yield_ (result) {
Fiber.yield(_negate ? !result : result)
}
}
class Suite {
new(name, block) {
_name = name
// Stores all the test bodies defined in this suite.
_tests = []
block.call(this)
run
}
new(name, expectFn, block) {
_name = name
_expectFn = expectFn
// Stores all the test bodies defined in this suite.
_tests = []
block.call(this)
run
}
defaultExpect(condition) { new BaseMatcher(condition) }
expect(condition) {
if (_expectFn) {
return _expectFn.call(condition)
}
return defaultExpect(condition)
}
it(description, body) {
var spec = new Spec(description, body)
_tests.add(spec)
}
run {
IO.print(_name)
for (test in _tests) {
var testResult = test.run
var succeeded = testResult.all { |r| r == true }
var str
if (succeeded) {
str = "\e[0;32m[passed]\e[0;0m"
} else {
str = "\e[0;31m[failed]\e[0;0m"
}
IO.print(" " + str + " " + test.description)
if (!succeeded) {
var message = testResult[testResult.count - 1].toString
IO.print(" " + message)
}
}
}
}
var suite = new Suite("string") { |s|
s.it("should be of class String") {
s.expect("a").toBe(String)
}
s.it("should be a descendant of Object") {
s.expect("a").toBe(Object)
}
s.it("should not be descendants of any other classes") {
s.expect("a").not.toBe(Num)
}
s.it("should return the correct type value") {
s.expect("a".type).toEqual(String)
}
s.it("should concatenate with another string") {
s.expect("a" + "b").toEqual("ab")
}
s.it("should runtime error if concatenated with a non-string") {
var fiber = new Fiber { "a" + 123 }
s.expect(fiber).toBeARuntimeError("Right operand must be a string.")
}
s.it("contains#should return true if both strings are empty") {
s.expect("".contains("")).toBeTrue
}
s.it("contains#return true if the search string is empty") {
s.expect("anything".contains("")).toBeTrue
}
s.it("contains#return true if the search string is a substring") {
s.expect("something".contains("meth")).toBeTrue
s.expect("something".contains("some")).toBeTrue
s.expect("something".contains("ing")).toBeTrue
}
s.it("contains#should return false if the search string is not a substring") {
s.expect("something".contains("math")).toBeFalse
}
s.it("contains#should runtime error if contains is called with non-string") {
var fiber = new Fiber { "foo".contains(1) }
s.expect(fiber).toBeARuntimeError("Argument must be a string.")
}
}
@munificent
Copy link

❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment