diff --git a/lib/index.js b/lib/index.js index 2562df8..1b88a34 100644 --- a/lib/index.js +++ b/lib/index.js @@ -24,7 +24,7 @@ exports.chain = function(fn) { } } -exports.inspect = function(obj) { +exports.stringify = function(obj) { var root = obj switch (kindof(obj)) { diff --git a/must.js b/must.js index 23ebccd..799b8c4 100644 --- a/must.js +++ b/must.js @@ -2,10 +2,11 @@ var AssertionError = require("./lib/assertion_error") var kindof = require("kindof") var defineGetter = require("./lib").defineGetter var lookupGetter = require("./lib").lookupGetter -var inspect = require("./lib").inspect +var stringify = require("./lib").stringify var chain = require("./lib").chain exports = module.exports = Must exports.AssertionError = AssertionError +exports.stringify = stringify /** * The main class that wraps the asserted object and that you call matchers on. @@ -324,7 +325,7 @@ Must.prototype.instanceof = function(expected) { } function instanceofMessage(expected) { - var type = expected.displayName || expected.name || inspect(expected) + var type = expected.displayName || expected.name || stringify(expected) return "be an instance of " + type } @@ -748,7 +749,7 @@ Must.prototype.property = function(property, expected) { if (ok && arguments.length > 1) ok = this.actual[property] === expected var msg = "have property \"" + property + "\"" - if (arguments.length > 1) msg += " equal to " + inspect(expected) + if (arguments.length > 1) msg += " equal to " + stringify(expected) insist.call(this, ok, msg) } @@ -772,7 +773,7 @@ Must.prototype.ownProperty = function(property, expected) { if (ok && arguments.length > 1) ok = this.actual[property] === expected var msg = "have own property \"" + property + "\"" - if (arguments.length > 1) msg += " equal to " + inspect(expected) + if (arguments.length > 1) msg += " equal to " + stringify(expected) insist.call(this, ok, msg) } @@ -1024,7 +1025,7 @@ Must.prototype.gte = Must.prototype.least */ Must.prototype.between = function(begin, end) { insist.call(this, begin <= this.actual && this.actual <= end, function() { - return "be between " + inspect(begin) + " and " + inspect(end) + return "be between " + stringify(begin) + " and " + stringify(end) }) } @@ -1032,10 +1033,10 @@ function insist(ok, message, expected, opts) { if (!this.negative ? ok : !ok) return var not = this.negative ? "not " : "" - var msg = inspect(this.actual) + " must " + not + var msg = stringify(this.actual) + " must " + not msg += typeof message == "function" ? message(expected) : message if (typeof message != "function" && arguments.length >= 3) - msg += " " + inspect(expected) + msg += " " + stringify(expected) opts = opts ? Object.create(opts) : {} opts.actual = this.actual diff --git a/test/lib/index_test.js b/test/lib/index_test.js index 883b8d7..30882f4 100644 --- a/test/lib/index_test.js +++ b/test/lib/index_test.js @@ -1,96 +1,96 @@ -var inspect = require("../../lib").inspect var assert = require("assert") +var stringify = require("../..").stringify -describe("inspect", function() { +describe("Must.stringify", function() { it("must return undefined", function() { - assert.strictEqual(inspect(undefined), "undefined") + assert.strictEqual(stringify(undefined), "undefined") }) it("must return null", function() { - assert.strictEqual(inspect(null), "null") + assert.strictEqual(stringify(null), "null") }) describe("given Number", function() { it("must show 42 as 42", function() { - assert.strictEqual(inspect(42), "42") + assert.strictEqual(stringify(42), "42") }) it("must show Infinity as Infinity", function() { - assert.strictEqual(inspect(Infinity), "Infinity") + assert.strictEqual(stringify(Infinity), "Infinity") }) it("must show -Infinity as -Infinity", function() { - assert.strictEqual(inspect(-Infinity), "-Infinity") + assert.strictEqual(stringify(-Infinity), "-Infinity") }) it("must show NaN as NaN", function() { - assert.strictEqual(inspect(NaN), "NaN") + assert.strictEqual(stringify(NaN), "NaN") }) }) describe("given Date", function() { it("must show ISO string representation", function() { var date = new Date(Date.UTC(1987, 5, 18, 2)) - assert.strictEqual(inspect(date), "1987-06-18T02:00:00.000Z") + assert.strictEqual(stringify(date), "1987-06-18T02:00:00.000Z") }) }) describe("given RegExp", function() { it("must show source with flags", function() { var regexp = /abc[de]./i - assert.strictEqual(inspect(regexp), "/abc[de]./i") + assert.strictEqual(stringify(regexp), "/abc[de]./i") }) }) describe("given Function", function() { it("must show source", function() { function awesome() { return 42 } - assert.strictEqual(inspect(awesome), "function awesome() { return 42 }") + assert.strictEqual(stringify(awesome), "function awesome() { return 42 }") }) }) describe("given Object", function() { it("must show recursively", function() { var obj = {a: {cool: 42}} - assert.strictEqual(inspect(obj), '{"a":{"cool":42}}') + assert.strictEqual(stringify(obj), '{"a":{"cool":42}}') }) it("must show inherited properties", function() { var obj = Object.create({a: 42}) - assert.strictEqual(inspect(obj), '{"a":42}') + assert.strictEqual(stringify(obj), '{"a":42}') }) it("must show circular objects as [Circular]", function() { var obj = {name: "John", likes: {sex: true}} obj.self = obj var str = '{"name":"John","likes":{"sex":true},"self":"[Circular]"}' - assert.strictEqual(inspect(obj), str) + assert.strictEqual(stringify(obj), str) }) it("must show nested circular objects as [Circular]", function() { var obj = {name: "John", likes: {}} obj.likes.likes = obj.likes var str = '{"name":"John","likes":{"likes":"[Circular]"}}' - assert.strictEqual(inspect(obj), str) + assert.strictEqual(stringify(obj), str) }) it("must show circular arrays as [Circular]", function() { var obj = [1, 2, 3] obj.push(obj) obj.push(5) - assert.strictEqual(inspect(obj), '[1,2,3,"[Circular]",5]') + assert.strictEqual(stringify(obj), '[1,2,3,"[Circular]",5]') }) it("must show circular inherited objects as [Circular]", function() { var obj = Object.create({name: "John"}) obj.self = obj var str = '{"self":"[Circular]","name":"John"}' - assert.strictEqual(inspect(obj), str) + assert.strictEqual(stringify(obj), str) }) it("must include undefined values", function() { var obj = {name: "John", age: undefined} - assert.strictEqual(inspect(obj), '{"name":"John","age":"[Undefined]"}') + assert.strictEqual(stringify(obj), '{"name":"John","age":"[Undefined]"}') }) }) })