Issues (204)

test/ClassBuilder/InheritanceTest.js (1 issue)

Severity
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 getters, setters and inheritance of instantiated class defintions.
9
 */
10
test('ClassBuilder:InheritanceTest', function(assert) {
11
12
    // BaseClass declaration with 3 public properties. We'll be extending on
13
    // this class to see if the integrity of these values stay intact within
14
    // the correct context.
15
    var BaseClass = JOII.ClassBuilder({}, {a: 1, b: 'foo', c: {'foo':'bar'}});
0 ignored issues
show
This function should enable strict mode with use strict.

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.

Loading history...
16
    var Child1    = JOII.ClassBuilder({ 'extends' : BaseClass }, {});
17
    var Child2    = JOII.ClassBuilder({ 'extends' : BaseClass }, {});
18
19
    // Verify that getters and setters exist within the instantiated children.
20
    var c1 = new Child1();
21
    var c2 = new Child2();
22
23
    assert.equal(typeof(c1.getA), 'function', 'Getter "getA" in Child1 exists.');
24
    assert.equal(typeof(c1.getB), 'function', 'Getter "getB" in Child1 exists.');
25
    assert.equal(typeof(c1.getC), 'function', 'Getter "getC" in Child1 exists.');
26
    assert.equal(typeof(c1.setA), 'function', 'Setter "setA" in Child1 exists.');
27
    assert.equal(typeof(c1.setB), 'function', 'Setter "setB" in Child1 exists.');
28
    assert.equal(typeof(c1.setC), 'function', 'Setter "setC" in Child1 exists.');
29
    assert.equal(typeof(c2.getA), 'function', 'Getter "getA" in Child2 exists.');
30
    assert.equal(typeof(c2.getB), 'function', 'Getter "getB" in Child2 exists.');
31
    assert.equal(typeof(c2.getC), 'function', 'Getter "getC" in Child2 exists.');
32
    assert.equal(typeof(c2.setA), 'function', 'Setter "setA" in Child2 exists.');
33
    assert.equal(typeof(c2.setB), 'function', 'Setter "setB" in Child2 exists.');
34
    assert.equal(typeof(c2.setC), 'function', 'Setter "setC" in Child2 exists.');
35
36
    // Update the values in Child1. Check if Child2 is still intact with its
37
    // original values inherited from BaseClass.
38
    c1.setA(2);
39
    c1.setB('bar');
40
    c1.setC({'foo' : 123});
41
42
    assert.strictEqual(c1.getA(), 2, 'Updated value of "a" in Child1 OK.');
43
    assert.strictEqual(c2.getA(), 1, 'Original value of "a" in Child2 OK.');
44
    assert.strictEqual(c1.getB(), 'bar', 'Updated value of "b" in Child1 OK.');
45
    assert.strictEqual(c2.getB(), 'foo', 'Original value of "b" in Child2 OK.');
46
    assert.deepEqual(c1.getC(), {'foo' : 123}, 'Updated value of "c" in Child1 OK.');
47
    assert.deepEqual(c2.getC(), {'foo' : 'bar' }, 'Original value of "c" in Child2 OK.');
48
49
    // Instantiate the BaseClass and verify if the original values of BaseClass
50
    // are still intact.
51
    var b = new BaseClass();
52
    assert.strictEqual(b.getA(), 1, 'Original value of "a" in BaseClass OK.');
53
    assert.strictEqual(b.getB(), 'foo', 'Original value of "b" in BaseClass OK.');
54
    assert.deepEqual(b.getC(), {'foo' : 'bar' }, 'Original value of "c" in BaseClass OK.');
55
56
    // Create a child class with a custom setter which calles the parent setter after
57
    // adjusting the value a little.
58
    var SuperClass = JOII.ClassBuilder({'extends': BaseClass}, {
59
        setA: function(value) {
60
            this['super']('setA', value + 1);
61
        },
62
        setB: function(value) {
63
            this.b = value;
64
        },
65
        getC: function() {
66
            return this.c;
67
        }
68
    });
69
70
    var s = new SuperClass();
71
72
    s.setA(2);
73
    s.setB('hello');
74
    assert.strictEqual(s.getA(), 3, 'Value of property "a" set correctly using custom setter.');
75
    assert.strictEqual(s.getB(), 'hello', 'Value of property "b" set correctly using custom setter.');
76
    assert.deepEqual(s.getC(), {'foo': 'bar'}, 'Value of property "c" get correctly using custom getter.');
77
78
    s.setC({'foo':'rawr'});
79
    assert.deepEqual(s.getC(), {'foo':'rawr'}, 'Value of property "c" get correctly using custom getter after update.');
80
81
    // Since we've been modifying some things now, check the integrity of the
82
    // previously created instances again. No properties must be referenced
83
    // back to eachother!
84
    assert.strictEqual(c1.getA(), 2, 'Updated value of "a" in Child1 is still OK.');
85
    assert.strictEqual(c2.getA(), 1, 'Original value of "a" in Child2 is still OK.');
86
    assert.strictEqual(c1.getB(), 'bar', 'Updated value of "b" in Child1 is still OK.');
87
    assert.strictEqual(c2.getB(), 'foo', 'Original value of "b" in Child2 is still OK.');
88
    assert.deepEqual(c1.getC(), {'foo' : 123}, 'Updated value of "c" in Child1 is still OK.');
89
    assert.deepEqual(c2.getC(), {'foo' : 'bar' }, 'Original value of "c" in Child2 is still OK.');
90
    assert.strictEqual(b.getA(), 1, 'Original value of "a" in BaseClass is still OK.');
91
    assert.strictEqual(b.getB(), 'foo', 'Original value of "b" in BaseClass is still OK.');
92
    assert.deepEqual(b.getC(), {'foo' : 'bar' }, 'Original value of "c" in BaseClass is still OK.');
93
94
    // Extend on SuperClass to see if the 'super'-method still functions
95
    // correctly midway the chain.
96
    var ChildOfSuper = JOII.ClassBuilder({'extends': SuperClass}, {});
97
98
    var cos = new ChildOfSuper();
99
    cos.setA(10);
100
    assert.strictEqual(cos.getA(), 11, 'Value of property "a" set correctly using custom setter in ChildOfSuper.');
101
});
102