This repository was archived by the owner on Jun 9, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.js
131 lines (130 loc) · 3.79 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
var assert = require('assert'),
$ = require('./index')
module.exports = {
'test .format() is undefined': function() {
assert.ok(typeof String.prototype.format === 'undefined')
},
'test installs .format()': function() {
$.extendString()
assert.ok(typeof String.prototype.format === 'function')
},
'test installs as .zzzformat()': function() {
$.extendString('zzzformat')
assert.ok(typeof String.prototype.zzzformat === 'function')
delete String.prototype.zzzformat
},
'test standalone': function() {
assert.equal($("Test [{0}]", 42.5), "Test [42.5]")
},
'test as method': function() {
assert.equal("Test [{0}]".format(42.5), "Test [42.5]")
},
'test integer modifier': function() {
assert.equal($("Test [{0:i}]", 42.5), "Test [43]")
},
'test padding': function() {
assert.equal($("Test [{0:8}]", 42.5), "Test [ 42.5]")
},
'test with a function': function() {
assert.equal($("Test [{0}]", function() { return "xxx" }), "Test [xxx]")
},
'test with a function and vars': function() {
var x = 0
assert.equal($("Test [{0}] [{0}] [{0}]", function() { return ++x }), "Test [1] [2] [3]")
},
'test JSON modifier': function() {
assert.equal($("Test [{0:j}]", {foo:1, bar:2}), 'Test [{"foo":1,"bar":2}]')
},
'test with an object and named properties': function() {
assert.equal($("Test [{foo:4}]", {foo:1}), 'Test [ 1]')
assert.equal($("Test [{foo.bar:4}]", {foo:{bar:1}}), 'Test [ 1]')
var Foo = function() {this.foo = 1}
assert.equal($("Test [{foo:4}]", new Foo), 'Test [ 1]')
Foo.prototype.bar = function() { return this.foo + 1 }
assert.equal($("Test [{bar:4}]", new Foo), 'Test [ 2]')
},
'test with multiple objects and named properties': function() {
assert.equal($("Test [{0|foo.bar:4}] [{1|foo.bar:4}]", {foo:{bar:4}}, {foo:{bar:8}}), 'Test [ 4] [ 8]')
},
'test with negative padding': function() {
assert.equal($("Test [{0:-4}]", 12), 'Test [12 ]')
},
'test with padding width too small': function() {
assert.equal($("Test [{0:1}]", 9999), 'Test [9999]')
},
'test fancy test': function() {
assert.equal(
$("The string {0:j} is {length}-character long", "Hello, World!"),
'The string "Hello, World!" is 13-character long'
)
},
'test with NaN': function() {
var x
assert.equal($("{0:8i}", x), " NaN")
assert.equal($("{0:8i}", "xxx"), " NaN")
assert.equal($("{0:8i}"), " NaN")
assert.equal($("{foo.bar.baz:i}"), "NaN")
},
'test with undefined properties (and no i modifier)': function() {
assert.equal($("{foo.bar.baz}", {foo:{bar:{}}}), "{foo.bar.baz}")
assert.equal($("{foo.bar.baz}"), "{foo.bar.baz}")
}
,
'test with escaped replacements': function() {
assert.equal($("{{0}} is [{0}]", 1), "{0} is [1]")
},
'test with multiple functions in property chain': function() {
assert.equal(
$("{foo.bar.baz.boom}", {
foo: function() {
return {
bar: function() {
return {
baz: function() {
return {
boom: function() {
return 1
}
}
}
}
}
}
}
}),
"1"
)
},
'test with multiple functions in property chain (2)': function() {
assert.equal(
$("{foo.bar.baz}", {
foo: function() {
return {
bar: {
value: 1,
baz: function() {
return this.value
}
}
}
}
}),
"1"
)
},
'test with zero padding': function() {
assert.equal($("{0:05}", 12), "00012")
},
'test with zero padding and a string': function() {
assert.equal($("{0:05}", "two"), " two")
},
'test with non-matching accolades 1': function() {
assert.equal($("{0}}}", "two"), "two}}")
},
'test with non-matching accolades 2': function() {
assert.equal($("{{{0}", "two"), "{{two")
},
'test with non-matching accolades, issue #1': function() {
assert.equal($("{1{test}}", { test: 1 }), "{11}")
}
}