Issues (204)

test/ClassBuilder/SetterValidationTest.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 the metadata validation of generated setters.
9
 */
10
test('ClassBuilder:SetterValidationTest', function(assert) {
11
12
    // Declaration of a class with a set of properties with defined types.
13
    // When a generated setter is called with a mismatched type as value, an
14
    // exception should be thrown.
15
    var MyClass = 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...
16
        'public number a_number': 1,
17
        'protected string b_protected_string': 'foo',
18
        'public object c_object': {'foo':'bar'},
19
20
        'public function setBString' : function(str) {
21
            this.setBProtectedString(str);
22
        },
23
24
        'public function getBString' : function() {
25
            return this.getBProtectedString();
26
        }
27
    });
28
29
    var a = new MyClass();
30
31
    // Verify that all getters and setters are properly generated with correct
32
    // visibility set on them.
33
    assert.equal(typeof(a.getANumber), 'function', 'Getter for property "a_number" exists.');
34
    assert.equal(typeof(a.getBProtectedString), 'undefined', 'Getter for property "b_protected_string" is private.');
35
    assert.equal(typeof(a.getCObject), 'function', 'Getter for property "c_object" exists.');
36
    assert.equal(typeof(a.getANumber), 'function', 'Setter for property "a_number" exists.');
37
    assert.equal(typeof(a.setBProtectedString), 'undefined', 'Setter for property "b_protected_string" is private.');
38
    assert.equal(typeof(a.setCObject), 'function', 'Setter for property "c_object" exists.');
39
40
    // Verify integrity of values through (custom) getters.
41
    assert.strictEqual(a.getANumber(), 1, 'Return value of getANumber is OK.');
42
    assert.strictEqual(a.getBString(), 'foo', 'Return value of getBString is OK.');
43
    assert.deepEqual(a.getCObject(), {'foo':'bar'}, 'Return value of getCObject is OK.');
44
45
    // Test calling setBProtectedString using the custom setter.
46
    a.setBString('rawr');
47
    assert.strictEqual(a.getBString(), 'rawr', 'Return value of getBString after modification is OK.');
48
49
    // If everything above works correctly, it's time to test the validation of
50
    // the property types in the generated setters. If a property is defined as
51
    // a string and a number-type value is passed to a setter, an exception is
52
    // thrown.
53
    assert.throws(function() { a.setANumber('This is not a number');
54
    }, function(err) { return err === 'setANumber expects number, string given.'; }, 'Setter type mismatch validator for setANumber is OK.');
55
    assert.throws(function() { a.setBString(123);
56
    }, function(err) { return err === 'setBProtectedString expects string, number given.'; }, 'Setter type mismatch validator for setBString is OK.');
57
    assert.throws(function() { a.setCObject('This is not an object');
58
    }, function(err) { return err === 'setCObject expects object, string given.'; }, 'Setter type mismatch validator for setCObject is OK.');
59
});
60