mirror of
https://github.com/atom/atom.git
synced 2026-04-06 03:02:13 -04:00
Change assertion API to pass error to callback
Don’t assume anything about metadata. Just give assertion callers access to the error object. Signed-off-by: Max Brunsfeld <maxbrunsfeld@gmail.com>
This commit is contained in:
committed by
Max Brunsfeld
parent
a4d716c491
commit
54549f916a
@@ -127,7 +127,7 @@ describe "the `atom` global", ->
|
||||
column: 3
|
||||
originalError: error
|
||||
|
||||
describe ".assert(condition, message, metadata)", ->
|
||||
describe ".assert(condition, message, callback)", ->
|
||||
errors = null
|
||||
|
||||
beforeEach ->
|
||||
@@ -142,17 +142,11 @@ describe "the `atom` global", ->
|
||||
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 passed a callback function", ->
|
||||
it "calls the callback with the assertion failure's error object", ->
|
||||
error = null
|
||||
atom.assert(false, "a == b", (e) -> error = e)
|
||||
expect(error).toBe errors[0]
|
||||
|
||||
describe "if the condition is true", ->
|
||||
it "does nothing", ->
|
||||
|
||||
@@ -721,17 +721,12 @@ class Atom extends Model
|
||||
Section: Private
|
||||
###
|
||||
|
||||
assert: (condition, message, metadata) ->
|
||||
assert: (condition, message, callback) ->
|
||||
return true 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
|
||||
callback?(error)
|
||||
|
||||
@emitter.emit 'did-fail-assertion', error
|
||||
|
||||
|
||||
@@ -296,10 +296,12 @@ class TokenizedBuffer extends Model
|
||||
# undefined. This should paper over the problem but we want to figure out
|
||||
# what is happening:
|
||||
tokenizedLine = @tokenizedLineForRow(row)
|
||||
atom.assert tokenizedLine?, "TokenizedLine is defined", =>
|
||||
metadata:
|
||||
atom.assert tokenizedLine?, "TokenizedLine is defined", (error) =>
|
||||
error.metadata = {
|
||||
row: row
|
||||
rowCount: @tokenizedLines.length
|
||||
}
|
||||
|
||||
return false unless tokenizedLine?
|
||||
|
||||
return false if @buffer.isRowBlank(row) or tokenizedLine.isComment()
|
||||
|
||||
Reference in New Issue
Block a user