Skip to content

Commit

Permalink
Rename inspect to stringify and export it.
Browse files Browse the repository at this point in the history
  • Loading branch information
moll committed Jun 13, 2015
1 parent b0bacb9 commit b7f6167
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 26 deletions.
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ exports.chain = function(fn) {
}
}

exports.inspect = function(obj) {
exports.stringify = function(obj) {
var root = obj

switch (kindof(obj)) {
Expand Down
15 changes: 8 additions & 7 deletions must.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand Down Expand Up @@ -1024,18 +1025,18 @@ 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)
})
}

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
Expand Down
36 changes: 18 additions & 18 deletions test/lib/index_test.js
Original file line number Diff line number Diff line change
@@ -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]"}')
})
})
})

0 comments on commit b7f6167

Please sign in to comment.