Skip to content

Commit

Permalink
Add atom.assert and atom.onDidFailAssertion
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Sobo committed Jun 20, 2015
1 parent cd35107 commit a1f6a15
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
31 changes: 31 additions & 0 deletions spec/atom-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,37 @@ describe "the `atom` global", ->
expect(atom.openDevTools).not.toHaveBeenCalled()
expect(atom.executeJavaScriptInDevTools).not.toHaveBeenCalled()

describe ".assert(condition, message, metadata)", ->
errors = null

beforeEach ->
errors = []
atom.onDidFailAssertion (error) -> errors.push(error)

describe "if the condition is false", ->
it "notifies onDidFailAssertion handlers with an error object based on the call site of the assertion", ->
atom.assert(false, "a == b")
expect(errors.length).toBe 1
expect(errors[0].message).toBe "Assertion failed: a == b"
expect(errors[0].stack).toContain('atom-spec')

describe "if metadata is an object", ->
it "assigns the object on the error as `metadata`", ->
metadata = {foo: 'bar'}
atom.assert(false, "a == b", metadata)
expect(errors[0].metadata).toBe metadata

describe "if metadata is a function", ->
it "assigns the function's return value on the error as `metadata`", ->
metadata = {foo: 'bar'}
atom.assert(false, "a == b", -> metadata)
expect(errors[0].metadata).toBe metadata

describe "if the condition is true", ->
it "does nothing", ->
atom.assert(true, "a == b")
expect(errors).toEqual []

describe "saving and loading", ->
afterEach -> atom.mode = "spec"

Expand Down
17 changes: 17 additions & 0 deletions src/atom.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,9 @@ class Atom extends Model
onDidThrowError: (callback) ->
@emitter.on 'did-throw-error', callback

onDidFailAssertion: (callback) ->
@emitter.on 'did-fail-assertion', callback

###
Section: Atom Details
###
Expand Down Expand Up @@ -711,6 +714,20 @@ class Atom extends Model
Section: Private
###

assert: (condition, message, metadata) ->
return if condition

error = new Error("Assertion failed: " + message)
Error.captureStackTrace(error, @assert)

if metadata?
if typeof metadata is 'function'
error.metadata = metadata()
else
error.metadata = metadata

@emitter.emit 'did-fail-assertion', error

deserializeProject: ->
Project = require './project'

Expand Down

0 comments on commit a1f6a15

Please sign in to comment.