From 7061c0848b105b48e1e832519b6f2fce642c2fcb Mon Sep 17 00:00:00 2001 From: ThomasR Date: Sat, 27 Feb 2016 13:55:40 +0100 Subject: [PATCH 1/2] Add tests for utility.js --- test/utility.coffee | 82 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 test/utility.coffee diff --git a/test/utility.coffee b/test/utility.coffee new file mode 100644 index 0000000..3a9e385 --- /dev/null +++ b/test/utility.coffee @@ -0,0 +1,82 @@ +### +Copyright (C) 2016 Thomas Rosenau + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +### + +'use strict' + +utility = require '../lib/utility' +expect = require('chai').expect + +describe 'utility:', -> + describe 'deepCopy:', -> + it 'copies objects', -> + o = {} + copy = utility.deepCopy o + expect(JSON.stringify copy).to.equal JSON.stringify o + expect(copy).not.to.equal o + + a = [] + copy = utility.deepCopy a + expect(JSON.stringify copy).to.equal JSON.stringify a + expect(copy).not.to.equal a + + o2 = {a: 1, b: 2, c: [{d: true}]} + copy = utility.deepCopy o2 + expect(JSON.stringify copy).to.equal JSON.stringify o2 + expect(copy).not.to.equal o2 + expect(copy.c).not.to.equal o2.c + expect(copy.c[0]).not.to.equal o2.c[0] + + it 'filters internal properties', -> + o = {__foo: true, bar: [{__baz: 1}, 2]} + copy = utility.deepCopy o + expect(copy.__foo).to.equal undefined + expect(copy.bar[0].__baz).to.equal undefined + expect(JSON.stringify copy).to.equal JSON.stringify {bar: [{}, 2]} + + describe 'generateNextName:', -> + it 'generates names', -> + #XXX not valid identifiers - should we disallow these input values by throwing an error? + expect(utility.generateNextName '').to.equal '0' + expect(utility.generateNextName '0').to.equal '1' + expect(utility.generateNextName '8').to.equal '9' + + expect(utility.generateNextName '9').to.equal 'a' + expect(utility.generateNextName 'a').to.equal 'b' + expect(utility.generateNextName 'b').to.equal 'c' + expect(utility.generateNextName 'z').to.equal 'A' + expect(utility.generateNextName 'A').to.equal 'B' + expect(utility.generateNextName 'Z').to.equal '_' + expect(utility.generateNextName '_').to.equal '$' + expect(utility.generateNextName '$').to.equal 'a0' + expect(utility.generateNextName 'a0').to.equal 'a1' + expect(utility.generateNextName 'az').to.equal 'aA' + expect(utility.generateNextName 'aZ').to.equal 'a_' + expect(utility.generateNextName 'a_').to.equal 'a$' + expect(utility.generateNextName 'a$').to.equal 'b0' + expect(utility.generateNextName '$$').to.equal 'a00' + expect(utility.generateNextName 'Z$$').to.equal '_00' + expect(utility.generateNextName '_$$').to.equal '$00' + expect(utility.generateNextName '$$_').to.equal '$$$' + expect(utility.generateNextName '$$$').to.equal 'a000' + expect(utility.generateNextName 'correcthorsebatterystaple').to.equal 'correcthorsebatterystaplf' From 681651754f0c9daa26137bdbb8f74f6e324db8aa Mon Sep 17 00:00:00 2001 From: ThomasR Date: Sun, 28 Feb 2016 07:51:57 +0100 Subject: [PATCH 2/2] Remove test cases for undefined behavior --- test/utility.coffee | 6 ------ 1 file changed, 6 deletions(-) diff --git a/test/utility.coffee b/test/utility.coffee index 3a9e385..db6d037 100644 --- a/test/utility.coffee +++ b/test/utility.coffee @@ -56,12 +56,6 @@ describe 'utility:', -> describe 'generateNextName:', -> it 'generates names', -> - #XXX not valid identifiers - should we disallow these input values by throwing an error? - expect(utility.generateNextName '').to.equal '0' - expect(utility.generateNextName '0').to.equal '1' - expect(utility.generateNextName '8').to.equal '9' - - expect(utility.generateNextName '9').to.equal 'a' expect(utility.generateNextName 'a').to.equal 'b' expect(utility.generateNextName 'b').to.equal 'c' expect(utility.generateNextName 'z').to.equal 'A'