Skip to content

Commit 7061c08

Browse files
committed
Add tests for utility.js
1 parent fe8b68a commit 7061c08

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

test/utility.coffee

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
###
2+
Copyright (C) 2016 Thomas Rosenau
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are met:
6+
7+
* Redistributions of source code must retain the above copyright
8+
notice, this list of conditions and the following disclaimer.
9+
* Redistributions in binary form must reproduce the above copyright
10+
notice, this list of conditions and the following disclaimer in the
11+
documentation and/or other materials provided with the distribution.
12+
13+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS'
14+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16+
ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
17+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22+
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23+
###
24+
25+
'use strict'
26+
27+
utility = require '../lib/utility'
28+
expect = require('chai').expect
29+
30+
describe 'utility:', ->
31+
describe 'deepCopy:', ->
32+
it 'copies objects', ->
33+
o = {}
34+
copy = utility.deepCopy o
35+
expect(JSON.stringify copy).to.equal JSON.stringify o
36+
expect(copy).not.to.equal o
37+
38+
a = []
39+
copy = utility.deepCopy a
40+
expect(JSON.stringify copy).to.equal JSON.stringify a
41+
expect(copy).not.to.equal a
42+
43+
o2 = {a: 1, b: 2, c: [{d: true}]}
44+
copy = utility.deepCopy o2
45+
expect(JSON.stringify copy).to.equal JSON.stringify o2
46+
expect(copy).not.to.equal o2
47+
expect(copy.c).not.to.equal o2.c
48+
expect(copy.c[0]).not.to.equal o2.c[0]
49+
50+
it 'filters internal properties', ->
51+
o = {__foo: true, bar: [{__baz: 1}, 2]}
52+
copy = utility.deepCopy o
53+
expect(copy.__foo).to.equal undefined
54+
expect(copy.bar[0].__baz).to.equal undefined
55+
expect(JSON.stringify copy).to.equal JSON.stringify {bar: [{}, 2]}
56+
57+
describe 'generateNextName:', ->
58+
it 'generates names', ->
59+
#XXX not valid identifiers - should we disallow these input values by throwing an error?
60+
expect(utility.generateNextName '').to.equal '0'
61+
expect(utility.generateNextName '0').to.equal '1'
62+
expect(utility.generateNextName '8').to.equal '9'
63+
64+
expect(utility.generateNextName '9').to.equal 'a'
65+
expect(utility.generateNextName 'a').to.equal 'b'
66+
expect(utility.generateNextName 'b').to.equal 'c'
67+
expect(utility.generateNextName 'z').to.equal 'A'
68+
expect(utility.generateNextName 'A').to.equal 'B'
69+
expect(utility.generateNextName 'Z').to.equal '_'
70+
expect(utility.generateNextName '_').to.equal '$'
71+
expect(utility.generateNextName '$').to.equal 'a0'
72+
expect(utility.generateNextName 'a0').to.equal 'a1'
73+
expect(utility.generateNextName 'az').to.equal 'aA'
74+
expect(utility.generateNextName 'aZ').to.equal 'a_'
75+
expect(utility.generateNextName 'a_').to.equal 'a$'
76+
expect(utility.generateNextName 'a$').to.equal 'b0'
77+
expect(utility.generateNextName '$$').to.equal 'a00'
78+
expect(utility.generateNextName 'Z$$').to.equal '_00'
79+
expect(utility.generateNextName '_$$').to.equal '$00'
80+
expect(utility.generateNextName '$$_').to.equal '$$$'
81+
expect(utility.generateNextName '$$$').to.equal 'a000'
82+
expect(utility.generateNextName 'correcthorsebatterystaple').to.equal 'correcthorsebatterystaplf'

0 commit comments

Comments
 (0)