This repository was archived by the owner on Jun 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
126 lines (104 loc) · 3.88 KB
/
test.js
File metadata and controls
126 lines (104 loc) · 3.88 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
'use strict';
import { assert } from 'chai';
import KindaInstantiable from './src';
suite('KindaInstantiable', function() {
test('instantiation', function() {
let Company = KindaInstantiable.extend('Company', function() {
this.initializer = function() {
this.employees = [];
};
});
let company1 = Company.instantiate();
assert.isArray(company1.employees);
assert.strictEqual(company1.employees.length, 0);
company1.employees.push('Jean Dupont');
assert.strictEqual(company1.employees.length, 1);
let company2 = Company.instantiate();
assert.isArray(company2.employees);
assert.strictEqual(company2.employees.length, 0);
assert.strictEqual(company1.employees.length, 1);
});
test('creation with create()', function() {
let Company = KindaInstantiable.extend('Company', function() {
this.initializer = function() {
this.employees = [];
};
this.creator = function(boss) {
this.boss = boss;
};
});
let company = Company.create('Pierre Durand');
assert.isArray(company.employees);
assert.strictEqual(company.boss, 'Pierre Durand');
});
test('creation with new', function() {
let Company = KindaInstantiable.extend('Company', function() {
this.creator = function(boss) {
this.boss = boss;
};
});
let company = new Company('Pierre Durand');
assert.strictEqual(company.boss, 'Pierre Durand');
});
test('serialization', function() {
let Person = KindaInstantiable.extend('Person', function() {
this.initializer = function() {
this._internalProperty = 'magic value';
};
this.creator = function(name, age, country) {
this.name = name;
this.age = age;
this.country = country || 'Japan'; // default value
};
this.serializer = function() {
return { name: this.name, age: this.age, country: this.country };
};
this.unserializer = function(json) {
this.name = json.name;
this.age = json.age;
this.country = json.country;
};
});
let person1 = Person.create('Jean Dupont', 32);
assert.deepEqual(person1.serialize(), {
name: 'Jean Dupont',
age: 32,
country: 'Japan'
});
assert.deepEqual(person1.toJSON(), { // 'toJSON' is an alias of 'serialize'
name: 'Jean Dupont',
age: 32,
country: 'Japan'
});
let person2 = Person.unserialize({ name: 'Jean Dupont', age: 32 });
assert.strictEqual(person2.name, 'Jean Dupont');
assert.strictEqual(person2.age, 32);
assert.isUndefined(person2.country); // 'creator' is not called in case of unserialization
assert.strictEqual(person2._internalProperty, 'magic value'); // 'unserializer' should be called
});
test('get the class of an instance', function() {
let Class = KindaInstantiable.extend('Class');
let obj = Class.create();
assert.strictEqual(obj.constructor, Class);
assert.strictEqual(obj.class, Class); // 'class' is just an alias of 'constructor'
});
test('test if an instance belongs to a class', function() {
let Class = KindaInstantiable.extend('Class');
let OtherClassWithSameName = KindaInstantiable.extend('Class');
let OtherClassWithDifferentName = KindaInstantiable.extend('OtherClass');
let obj1 = Class.create();
assert.isTrue(Class.isClassOf(obj1));
assert.isTrue(obj1.isInstanceOf(Class));
assert.isTrue(OtherClassWithSameName.isClassOf(obj1));
assert.isFalse(OtherClassWithDifferentName.isClassOf(obj1));
let Mixin = KindaInstantiable.extend('Mixin');
let Subclass = Class.extend('Subclass', function() {
this.include(Mixin);
});
let obj2 = Subclass.create();
assert.isTrue(Subclass.isClassOf(obj2));
assert.isTrue(Class.isClassOf(obj2));
assert.isTrue(Mixin.isClassOf(obj2));
assert.isFalse(OtherClassWithDifferentName.isClassOf(obj2));
});
});