Issues (204)

test/InterfaceBuilder/InterfaceValidationTest.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 validation functionality of interfaces.
9
 */
10
test('InterfaceBuilder:InterfaceValidationTest', function(assert) {
11
12
    // An interface must never declare abstract types.
13
    assert.throws(function() {
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
        JOII.InterfaceBuilder('Test', {'abstract public string foobar': 'foobar'});
15
    }, function(err) { return err === 'An interface may not contain abstract definitions. Property foobar is abstract in interface Test.'; }, 'Exception is thrown when an interface declares abstract properties.');
16
17
    // An interface must never declare final types.
18
    assert.throws(function() {
19
        JOII.InterfaceBuilder('Test', {'final public string foobar': 'foobar'});
20
    }, function(err) { return err === 'An interface may not contain final definitions. Property foobar is final in interface Test.'; }, 'Exception is thrown when an interface declares final properties.');
21
22
    /**
23
     * Tests if the given Class (c) throws an exception due to
24
     * interface validation with the given expected exception string.
25
     *
26
     * @param {Interface} i
27
     * @param {Class}     body
28
     * @param {String}    expected_exception_string
29
     */
30
    var testException = function(i, body, expected_exception_string) {
31
        assert.throws(function() {
32
            JOII.ClassBuilder({ 'implements' : i }, body);
33
        }, function(err) { return err === expected_exception_string; }, 'Validator throws: ' + expected_exception_string);
34
    };
35
36
    var TestInterface = JOII.InterfaceBuilder('TestInterface', {
37
        'public nullable string name' : null,
38
        'protected number age' : null,
39
        'public function myTest' : function(a) {}
40
    });
41
42
    var ChildTestInterface = JOII.InterfaceBuilder('TestInterface2', {'extends': TestInterface}, {
43
        'public nullable number age2' : null
44
    });
45
46
    // Test the base interface.
47
    testException('TestInterface', {'protected nullable string name': ''}, 'Property name cannot be protected because the interface declared it public.');
48
    testException('TestInterface', {'public nullable string name': ''}, 'Class must implement protected number "age" as defined in the interface TestInterface.');
49
    testException('TestInterface', {'public nullable string name': '', 'public number age': null}, 'Property age cannot be public because the interface declared it protected.');
50
    testException('TestInterface', {'public nullable string name': '', 'protected number age': null, 'public myTest': function(a, b) {}}, 'Method myTest does not match the parameter count as defined in the interface TestInterface.');
51
52
    // Test the interface that extends on TestInterface.
53
    testException('TestInterface2', {'public nullable number age': null}, 'Class must implement public nullable number "age2" as defined in the interface TestInterface2.'); // From ChildTestInterface
54
    testException('TestInterface2', {'public nullable number age2': null}, 'Class must implement public nullable string "name" as defined in the interface TestInterface2.'); // From TestInterface2
55
56
    // An interface can be used to enforce types.
57
    var MTI = JOII.ClassBuilder({'implements': TestInterface}, {
58
        'public nullable string name' : null,
59
        'protected number age' : null,
60
        'public function myTest' : function(a) {}
61
    });
62
63
    var MyClass = JOII.ClassBuilder({
64
        'public nullable TestInterface ti' : null
65
    });
66
67
    assert.throws(function() {
68
        var m = new MyClass(); m.setTi(1);
69
    }, function(err) { return err === 'setTi expects an instance of TestInterface, number given.'; }, 'Exception is thrown when a setter is called and an argument with the wrong type was given.');
70
});
71