Skip to content

Commit

Permalink
Added a basic toThrow matcher for exceptions #3
Browse files Browse the repository at this point in the history
  • Loading branch information
follesoe committed Jul 1, 2012
1 parent 8e971a2 commit 6e80c35
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
19 changes: 15 additions & 4 deletions jasmine.io
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ Matcher toBeNil := method(
false
)

Matcher toThrow := method(
ex := try(actual call)
if(ex == nil,
self message := "Expected an exception to be thrown, but no exception was thrown."
return false,
return true
)
)

Matcher message := method(inverted,
"Expected " .. actual .. if(inverted, " not ", " ") .. expectiation fromCamelCaseToSentence .. " " .. expected
)
Expand All @@ -42,11 +51,11 @@ Sequence fromCamelCaseToSentence := method(
output
)

expect := method(actual,
expect := method(actual,
wrapper := Object clone
wrapper actual := actual
wrapper inverted := false

wrapper actual := actual

wrapper not := method(
self inverted := true
self
Expand All @@ -58,10 +67,12 @@ expect := method(actual,
matcher expected := call message arguments at(0)
matcher expectiation := call message name
matcher success := matcher doMessage(call message, actual)
if(inverted, matcher success := if(matcher success, false, true))

if(inverted, matcher success := if(matcher success, false, true))
if(matcher success == false, Exception raise(matcher message(inverted)))
matcher
)

wrapper
)

Expand Down
29 changes: 20 additions & 9 deletions matcher_spec.io
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ describe("A custom matcher",
)
)

describe("toThrow matcher",
it("should be possible to test that an exception is thrown",
matcher := expect(block(Exception raise("Something crashed"))) toThrow()
expect(matcher success) toBe(true)
),

it("should fail if an exception is not thrown",
ex := try(expect(block(1)) toThrow())
expect(ex error) toBe("Expected an exception to be thrown, but no exception was thrown.")
)
)

describe("Matcher tests for toEqual",
it("should expect true toEqual true to be true",
matcher := expect(true) toEqual(true)
Expand Down Expand Up @@ -61,7 +73,6 @@ describe("Matcher tests for toEqual",
)

describe("toBeNil Matcher",

it("should expect nil toBeNil",
result := expect(nil) toBeNil()
expect(result success) toBe(true)
Expand All @@ -86,13 +97,13 @@ describe("toBe Matcher for strings",

describe("Not matcher",

it("inverts expectation",
result := expect(1) not toEqual(2)
expect(result success) toBe(true)
),
it("inverts expectation",
result := expect(1) not toEqual(2)
expect(result success) toBe(true)
),

it("inverts default error messages",
ex := try(expect(1) not toBeLessThan(2))
expect(ex error) toEqual("Expected 1 not to be less than 2")
)
it("inverts default error messages",
ex := try(expect(1) not toBeLessThan(2))
expect(ex error) toEqual("Expected 1 not to be less than 2")
)
)

0 comments on commit 6e80c35

Please sign in to comment.