Skip to content

Commit

Permalink
Make sure something is used in the right context.
Browse files Browse the repository at this point in the history
  • Loading branch information
RubenVerborgh committed Jan 9, 2013
1 parent 4a8c518 commit 1f5ffb3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
6 changes: 6 additions & 0 deletions lib/chai-things.js
Expand Up @@ -22,6 +22,12 @@

/* `something` chain property */
function chainSomething() {
// `include` or `contains` should have been called before
if (!utils.flag(this, "contains"))
throw new Error("cannot use something without include or contains");
// `something` has already been used
if (utils.flag(this, "something"))
throw new Error("cannot use something twice in an assertion");
// flag that this is a something chain
utils.flag(this, "something", true);
}
Expand Down
18 changes: 15 additions & 3 deletions test/something.coffee
Expand Up @@ -3,15 +3,27 @@ describe "include.something", ->
describe "without negation", ->
it "should assert whether the object is an array", ->
(() -> "not an array".should.include.something.that.deep.equals { a: 1 }).
should.throw("expected 'not an array' to be an array")
should.throw "expected 'not an array' to be an array"

it "should assert whether the array is non-empty", ->
(() -> [].should.include.something.that.deep.equals { a: 1 }).
should.throw("expected [] not to be empty")
should.throw "expected [] not to be empty"

it "passes if an equal element exists", ->
[{ a: 1 }, { b: 2 }].should.include.something.that.deep.equals { b: 2 }

it "does not pass if an equal element does not exist", ->
(() -> [{ a: 1 }, { b: 2 }].should.include.something.that.deep.equals { c: 3 }).
should.throw("expected an element of [ { a: 1 }, { b: 2 } ] to deeply equal { c: 3 }")
should.throw "expected an element of [ { a: 1 }, { b: 2 } ] to deeply equal { c: 3 }"

describe "something in wrong context", ->

describe "without include", ->
it "should throw an error", ->
(() -> [{ a: 1 }, { b: 2 }].should.something.that.deep.equals { b: 2 }).
should.throw "cannot use something without include or contains"

describe "when something has already been used", ->
it "should throw an error", ->
(() -> [].should.include.something.that.something).
should.throw "cannot use something twice in an assertion"

0 comments on commit 1f5ffb3

Please sign in to comment.