Issues (204)

test/Reflection/ReflectionTest.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
 * Test the behavior of the Reflection mechanism which retrieves metadata from
9
 * a defined JOII-class, its properties and methods.
10
 */
11
test('Reflection:ReflectionTest', function(assert) {
12
13
    var BaseClass = JOII.ClassBuilder({}, {
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...
14
        'final protected nullable number a_number' : 1
15
    });
16
17
    var ChildClass = JOII.ClassBuilder({ 'extends' : BaseClass }, {
18
        'protected nullable string a_string' : 'Hello World'
19
    });
20
21
    var EndClass = JOII.ClassBuilder({ 'extends' : ChildClass }, {
22
        'public setNumber' : function(i) {
23
            this.a_number = i + 10;
24
        }
25
    });
26
27
    var base_reflector  = new JOII.Reflection.Class(BaseClass),
28
        child_reflector = new JOII.Reflection.Class(ChildClass),
29
        end_reflector   = new JOII.Reflection.Class(EndClass);
30
31
    assert.equal(base_reflector.getProperties().length, 1, 'Amount of properties in BaseClass is 1.');
32
    assert.equal(child_reflector.getProperties().length, 2, 'Amount of properties in ChildClass is 2.');
33
    assert.equal(end_reflector.getProperties().length, 2, 'Amount of properties in EndClass is 2.');
34
    assert.equal(base_reflector.getMethods().length, 2, 'Amount of methods in BaseClass is 3.');
35
    assert.equal(child_reflector.getMethods().length, 4, 'Amount of methods in ChildClass is 5.');
36
    assert.equal(end_reflector.getMethods().length, 5, 'Amount of methods in EndClass is 6.');
37
38
    assert.equal(base_reflector.hasParent(), false, 'BaseClass does not have a parent.');
39
    assert.equal(child_reflector.hasParent(), true, 'ChildClass has a parent.');
40
    assert.equal(end_reflector.hasParent(), true, 'EndClass has have a parent.');
41
42
    assert.equal(base_reflector.getProperty('a_number').getName(), 'a_number', 'a_number reflects name OK from BaseClass.');
43
    assert.equal(child_reflector.getProperty('a_string').getName(), 'a_string', 'a_string reflects name OK from ChildClass.');
44
    assert.equal(end_reflector.getProperty('a_number').getName(), 'a_number', 'a_number reflects name OK from EndClass.');
45
46
    var prop_a = base_reflector.getProperty('a_number'),
47
        prop_b = child_reflector.getProperty('a_string'),
48
        method = end_reflector.getMethod('setNumber');
49
50
    assert.equal(prop_a.isType('number'), true, 'prop_a is a number.');
51
    assert.equal(prop_a.isAbstract(), false, 'prop_a is not abstract.');
52
    assert.equal(prop_a.isFinal(), true, 'prop_a is final.');
53
    assert.equal(prop_a.isProtected(), true, 'prop_a is protected.');
54
    assert.equal(prop_a.isPublic(), false, 'prop_a is not public.');
55
    assert.equal(prop_a.isNullable(), true, 'prop_a is nullable.');
56
57
    assert.equal(prop_b.isType('string'), true, 'prop_b is a string.');
58
    assert.equal(prop_b.isAbstract(), false, 'prop_b is not abstract.');
59
    assert.equal(prop_b.isFinal(), false, 'prop_b is not final.');
60
    assert.equal(prop_b.isProtected(), true, 'prop_b is protected.');
61
    assert.equal(prop_b.isPublic(), false, 'prop_b is not public.');
62
    assert.equal(prop_b.isNullable(), true, 'prop_b is nullable.');
63
64
    assert.equal(method.isType('function'), true, 'method is a function.');
65
    assert.equal(method.isAbstract(), false, 'method is not abstract.');
66
    assert.equal(method.isFinal(), false, 'method is not final.');
67
    assert.equal(method.isProtected(), false, 'method is not protected.');
68
    assert.equal(method.isPublic(), true, 'method is public.');
69
    assert.equal(method.isNullable(), false, 'method is not nullable.');
70
71
    // Test parent reflectors.
72
    var parent_base = child_reflector.getParent(),
73
        child_base  = end_reflector.getParent();
74
75
    assert.equal(parent_base.getProperties().length, 1, 'Amount of properties in BaseClass (via Child.parent) is 1.');
76
    assert.equal(child_base.getProperties().length, 2, 'Amount of properties in ChildClass (via End.parent) is 2.');
77
    assert.equal(parent_base.getMethods().length, 2, 'Amount of methods in BaseClass (via Child.parent) is 2.');
78
    assert.equal(child_base.getMethods().length, 4, 'Amount of methods in ChildClass (via End.parent) is 4.');
79
80
});
81