1 | /* Javascript Object Inheritance Implementation ______ ________ |
||
2 | * (c) 2016 <[email protected]> __ / / __ \/ _/ _/ |
||
3 | * Licensed under MIT. / // / /_/ // /_/ / |
||
4 | * ------------------------------------------------------ \___/\____/___/__*/ |
||
5 | var JOII = require('../../dist/joii').JOII; |
||
6 | |||
7 | /** |
||
8 | * Tests instanceOf functionality. |
||
9 | */ |
||
10 | test('EnumBulder:EnumBuilderTest', function(assert) { |
||
11 | |||
12 | /** |
||
13 | * Tests if the given Class (c) throws an exception due to |
||
14 | * interface validation with the given expected exception string. |
||
15 | * |
||
16 | * @param {Function} fn |
||
17 | * @param {String} expected_exception_string |
||
18 | */ |
||
19 | var testException = function(fn, expected_exception_string) { |
||
0 ignored issues
–
show
|
|||
20 | assert.throws(fn, function(err) { |
||
21 | return err === expected_exception_string ? true : err; |
||
22 | }, 'Error thrown: ' + expected_exception_string); |
||
23 | }; |
||
24 | |||
25 | // Plain object enum. |
||
26 | var plain = JOII.EnumBuilder('test1', { ONE: 1, TWO: 2}); |
||
27 | |||
28 | assert.strictEqual(plain.ONE, 1, 'plainObj: plain.ONE exists'); |
||
29 | assert.strictEqual(plain.TWO, 2, 'plainObj: plain.TWO exists'); |
||
30 | |||
31 | // Class enum |
||
32 | var cls = JOII.ClassBuilder({ 'enum': 'EnumTest2', expose_enum: true }, { 'const ONE' : 1, 'const TWO' : 2 }); |
||
33 | |||
34 | assert.strictEqual(cls.ONE, 1, 'classObj: cls.ONE exists'); |
||
35 | assert.strictEqual(cls.TWO, 2, 'classObj: cls.TWO exists'); |
||
36 | |||
37 | // Test exposure of test2. |
||
38 | assert.strictEqual(typeof(EnumTest2), 'object', 'EnumTest2 exists in the global namespace'); |
||
39 | assert.strictEqual(EnumTest2.contains(cls.ONE), true, 'cls.ONE exists within EnumTest2'); |
||
40 | |||
41 | // Test validation |
||
42 | assert.strictEqual(typeof(JOII.EnumRegistry.test1), 'object', 'Test1 correctly registered'); |
||
43 | assert.strictEqual(JOII.EnumRegistry.test1.contains(1), true, 'Test1 contains 1'); |
||
44 | assert.strictEqual(JOII.EnumRegistry.test1.contains(2), true, 'Test1 contains 2'); |
||
45 | assert.strictEqual(JOII.EnumRegistry.test1.contains(3), false, 'Test1 does not contain 3'); |
||
46 | |||
47 | // Test duplicate enum name |
||
48 | testException(function() { |
||
49 | JOII.EnumBuilder('test1', { ONE: 1, TWO: 2}); |
||
50 | }, 'Enumerator "test1" already exists.'); |
||
51 | |||
52 | // Test invalid content: function |
||
53 | testException(function() { |
||
54 | JOII.EnumBuilder('test3', { ONE: function() {}, TWO: 2}); |
||
55 | }, 'An enumerator cannot contain functions. "ONE" is a function.'); |
||
56 | |||
57 | // Test invalid content: object |
||
58 | testException(function() { |
||
59 | JOII.EnumBuilder('test4', { ONE: {}, TWO: 2}); |
||
60 | }, 'An enumerator cannot contain objects. "ONE" is an object.'); |
||
61 | |||
62 | // Test interface |
||
63 | var I1 = JOII.InterfaceBuilder({ 'enum': 'InterfaceEnum', expose_enum: true }, { |
||
64 | 'const ONE' : 1, |
||
65 | 'const TWO' : 2 |
||
66 | }); |
||
67 | |||
68 | assert.strictEqual(I1.ONE, 1, 'interfaceObj: I1.ONE exists'); |
||
69 | assert.strictEqual(I1.TWO, 2, 'interfaceObj: I1.TWO exists'); |
||
70 | |||
71 | assert.strictEqual(InterfaceEnum.ONE, 1, 'interfaceObj: InterfaceEnum.ONE exists'); |
||
72 | assert.strictEqual(InterfaceEnum.TWO, 2, 'interfaceObj: InterfaceEnum.TWO exists'); |
||
73 | |||
74 | var I2 = JOII.InterfaceBuilder({ 'enum': 'InterfaceEnum2', expose_enum: true, 'extends' : I1 }, { |
||
75 | 'const THREE' : 3, |
||
76 | 'const FOUR' : 4 |
||
77 | }); |
||
78 | |||
79 | assert.strictEqual(I2.ONE, 1, 'interfaceObj: I1.ONE exists'); |
||
80 | assert.strictEqual(I2.TWO, 2, 'interfaceObj: I1.TWO exists'); |
||
81 | assert.strictEqual(I2.THREE, 3, 'interfaceObj: I1.THREE exists'); |
||
82 | assert.strictEqual(I2.FOUR, 4, 'interfaceObj: I1.FOUR exists'); |
||
83 | |||
84 | assert.strictEqual(InterfaceEnum2.ONE, 1, 'interfaceObj: InterfaceEnum.ONE exists'); |
||
85 | assert.strictEqual(InterfaceEnum2.TWO, 2, 'interfaceObj: InterfaceEnum.TWO exists'); |
||
86 | assert.strictEqual(InterfaceEnum2.THREE, 3, 'interfaceObj: InterfaceEnum.THREE exists'); |
||
87 | assert.strictEqual(InterfaceEnum2.FOUR, 4, 'interfaceObj: InterfaceEnum.FOUR exists'); |
||
88 | }); |
||
89 |
Strict mode is a way to opt-in to a restricted variant of JavaScript. It eliminates some common pitfalls by being less lenient and raising more errors.
Besides, it is also used to fix certain mistakes which made it difficult for JavaScript runtimes to perform certain optimizations.
We generally recommend to only enable strict mode on the function scope and not in the global scope as that might break third-party scripts on your website.