/
test.js
99 lines (91 loc) · 2.12 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
var underscore = require( 'underscore' );
var Interfascist = require( 'interfascist' );
var Mixism = require( '../index' );
var assert = require( 'assert' );
describe( 'mixism', function(){
var geometryInterfaces = {
Point: {
x: 'Number',
y: 'Number'
},
Line: {
start: 'Point',
end: 'Point'
},
Size: {
width: 'Number',
height: 'Number'
}
};
var validator = new Interfascist( geometryInterfaces );
var mixism = new Mixism( validator );
var _ = mixism.underscore;
var mixins = {
Point: {
square: function( point ) {
return {
x: point.x * point.x,
y: point.y * point.y
};
},
squareAdd: function( point, value ) {
return {
x: point.x * point.x + value,
y: point.y * point.y + value
};
}
},
Line: {
square: function( line ) {
return {
start: _( line.start ).square(),
end: _( line.end ).square()
}
}
},
Number: {
square: function( value ) {
return value * value;
}
}
};
mixism.mixin( mixins );
var p = {
x: 5,
y: 3.5
};
var l = {
start: p,
end: {
x: 2,
y: -1
}
};
var s = {
width: 4,
height: 3.5
};
describe( 'simple extension', function() {
it( 'should extend the Point interface', function(){
var sqP = _( p ).square();
assert( sqP.x === 25 && sqP.y === 12.25 );
});
it( 'should extend the Point interface with args', function(){
var sqP = _( p ).squareAdd( 2 );
assert( sqP.x === 27 && sqP.y === 14.25 );
});
it( 'should extend the Line interface', function(){
var sqL = _( l ).square();
assert( sqL.start.x === 25 && sqL.start.y === 12.25 && sqL.end.x === 4 && sqL.end.y === 1 );
});
it( 'should not extend the Size interface', function(){
assert.throws( function(){
var sqS = _( s ).square();
} );
});
it( 'should extend a Number', function(){
var sq = _( 5 ).square();
assert( sq === 25 );
});
});
});