Skip to content

Instantly share code, notes, and snippets.

@vsavkin
Created January 20, 2012 00:34
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 vsavkin/1644056 to your computer and use it in GitHub Desktop.
Save vsavkin/1644056 to your computer and use it in GitHub Desktop.
Testing Framework in IO
AssertionFailed := Exception clone
// --------------------------------------------------------------------------
SpecDefinition := Object clone
SpecDefinition new := method(name,
r := SpecDefinition clone
r name := name
r examples := Map clone
r
)
SpecDefinition spockify := method(body,
curr := body
afterExpect := false
(curr name == "expect") ifTrue(afterExpect = true)
while(curr = curr next,
(curr name == "expect") ifTrue(afterExpect = true)
(afterExpect and curr isEndOfLine) ifTrue(
assertion := Message fromString("raiseAssertIfFalse")
curr previous setNext(assertion)
assertion setNext(curr)
)
(afterExpect and (curr next == nil)) ifTrue(
assertion := Message fromString("raiseAssertIfFalse")
curr setNext(assertion)
curr = assertion
)
)
)
SpecDefinition should := method(name,
body := call message argAt(1)
spockify(body)
examples atPut(name, body)
)
// --------------------------------------------------------------------------
SpecResults := Object clone
SpecResults new := method(specName,
r := SpecResults clone
r results := Map clone
r specName := specName
r
)
SpecResults record := method(exampleName, description,
results atPut(exampleName, description)
)
SpecResults printResults := method(
("Running ".. specName .. "...") println
results foreach(testName,status,
(specName .. " should " .. testName .."...") println
status println
"" println
)
)
// --------------------------------------------------------------------------
SpecRunner := Object clone
SpecRunner new := method(specDef, results,
r := SpecRunner clone
r specDefinition := specDef
r results := results
r
)
SpecRunner expect := method(true)
SpecRunner assert := method(
bool := call sender doMessage(call message argAt(0))
str := call message asSimpleString
bool ifFalse(AssertionFailed raise(str))
)
SpecRunner runExample := method(testName, body,
e := try(doMessage(body))
(e != nil) ifTrue(
(e type == "AssertionFailed") ifTrue(
results record(testName, ">> Failed: " .. e error)) ifFalse(
results record(testName, ">> Error: " .. e error)
)
) ifFalse (
results record(testName, ">> Passed")
)
)
SpecRunner run := method(
specDefinition examples foreach(testName, body,
runExample(testName, body)
)
)
// --------------------------------------------------------------------------
raiseAssertIfFalse := method(
call target ifFalse(AssertionFailed raise("False != True"))
)
describe := method(specName,
d := SpecDefinition new(specName)
body := call message argAt(1)
d doMessage(body)
res := SpecResults new(specName)
r := SpecRunner new(d, res)
r appendProto(d)
r run
res printResults
)
// --------------------------------------------------------------------------
describe("Example Specification",
"We describe spec here"
should("use assert keyword",
assert(1 == 1)
)
should("treat everything after expect as an assertion",
expect
1 == 1
2 == 2
)
x := 2
should("use context variables",
y := 2
expect
x + y == 4
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment