-
Notifications
You must be signed in to change notification settings - Fork 287
/
Copy pathtest_validation.js
84 lines (71 loc) · 2.62 KB
/
test_validation.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
/* global describe, it */
'use strict'
var assert = require('assert'),
sprintfjs = require('../src/sprintf.js'),
sprintf = sprintfjs.sprintf,
vsprintf = sprintfjs.vsprintf
function should_throw(format,args,err) {
assert.throws(function() { vsprintf(format,args) }, err)
}
function should_not_throw(format,args) {
assert.doesNotThrow(function() { vsprintf(format,args) })
}
describe('sprintfjs cache', function() {
it('should not throw Error (cache consistency)', function() {
// redefine object properties to ensure that is not affect to the cache
sprintf('hasOwnProperty')
sprintf('constructor')
should_not_throw('%s', ['caching...'])
should_not_throw('%s', ['crash?'])
})
})
describe('sprintfjs', function() {
it('should throw SyntaxError for placeholders', function() {
should_throw('%', [], SyntaxError)
should_throw('%A', [], SyntaxError)
should_throw('%s%', [], SyntaxError)
should_throw('%(s', [], SyntaxError)
should_throw('%)s', [], SyntaxError)
should_throw('%$s', [], SyntaxError)
should_throw('%()s', [], SyntaxError)
should_throw('%(12)s', [], SyntaxError)
})
var numeric = 'bcdiefguxX'.split('')
numeric.forEach(function(specifier) {
var fmt = sprintf('%%%s',specifier)
it(fmt + ' should throw TypeError for invalid numbers', function() {
should_throw(fmt, [], TypeError)
should_throw(fmt, ['str'], TypeError)
should_throw(fmt, [{}], TypeError)
should_throw(fmt, ['s'], TypeError)
})
it(fmt + ' should not throw TypeError for something implicitly castable to number', function() {
should_not_throw(fmt, [1/0])
should_not_throw(fmt, [true])
should_not_throw(fmt, [[1]])
should_not_throw(fmt, ['200'])
should_not_throw(fmt, [null])
})
})
it('should not throw Error for expression which evaluates to undefined', function() {
should_not_throw("%(x.y)s", {x : {}})
})
it('should throw own Error when expression evaluation would raise TypeError', function() {
var fmt = "%(x.y)s"
try {
sprintf(fmt, {})
} catch (e) {
assert(e.message.indexOf('[sprintf]') !== -1)
}
})
it('should not throw when accessing properties on the prototype', function() {
function C() { }
C.prototype = {
get x() { return 2 },
set y(v) { /*Noop */}
}
var c = new C()
should_not_throw("%(x)s", c)
should_not_throw("%(y)s", c)
})
})