/
060_encoder.t.js
159 lines (100 loc) · 7.63 KB
/
060_encoder.t.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
StartTest(function(t) {
var async0 = t.beginAsync()
use([ 'KiokuJS.Test.Person' ], function () {
//======================================================================================================================================================================================================================================================
t.diag('Sanity')
t.ok(KiokuJS, "'KiokuJS' is here")
t.ok(KiokuJS.Collapser, "'KiokuJS.Collapser' is here")
t.ok(KiokuJS.Test.Person, "'KiokuJS.Test.Person' is here")
//======================================================================================================================================================================================================================================================
t.diag('Graph setup')
var Homer = new KiokuJS.Test.Person({
name : 'Homer Simpson'
})
var Marge = new KiokuJS.Test.Person({
name : 'Marge Simpson'
})
var Bart = new KiokuJS.Test.Person({
name : 'Bart Simpson'
})
var Lisa = new KiokuJS.Test.Person({
name : 'Lisa Simpson'
})
Homer.spouse(Marge)
Marge.spouse(Homer)
Bart.father = Lisa.father = Homer
Bart.mother = Lisa.mother = Marge
var kids = [ Bart, Lisa ]
Homer.children = Marge.children = kids
//======================================================================================================================================================================================================================================================
t.diag('Collapser setup')
var backend = new KiokuJS.Backend.Hash()
var scope = backend.newScope()
var collapser = new KiokuJS.Collapser({
scope : scope
})
t.ok(collapser, "KiokuJS collapser was instantiated")
//======================================================================================================================================================================================================================================================
t.diag('Extracting first-class nodes from graph and inserting them in the scope')
var nodes = collapser.collapse({}, [ Homer ])
t.ok(nodes.length == 5, 'Correct number of nodes is returned (`kids` array is shared, thus has its own node)')
Joose.A.each(nodes, scope.pinNode, scope)
//======================================================================================================================================================================================================================================================
t.diag('Setting up nodes & entries')
var homerNode = scope.objectToNode(Homer)
var homerEntry = backend.encodeNode(homerNode)
var homerData = homerEntry.data
var margeNode = scope.objectToNode(Marge)
var margeEntry = backend.encodeNode(margeNode)
var margeData = margeEntry.data
var bartNode = scope.objectToNode(Bart)
var bartEntry = backend.encodeNode(bartNode)
var bartData = bartEntry.data
var lisaNode = scope.objectToNode(Lisa)
var lisaEntry = backend.encodeNode(lisaNode)
var lisaData = lisaEntry.data
var childrenNode = scope.objectToNode(kids)
var childrenEntry = backend.encodeNode(childrenNode)
var childrenData = childrenEntry.data
//======================================================================================================================================================================================================================================================
t.diag('Checking entries')
t.ok(homerEntry.$entry, "Homer's entry is marked with $entry")
t.ok(homerEntry.ID == homerNode.ID, "Homer's entry has correct ID")
t.ok(homerEntry.className == 'KiokuJS.Test.Person', "Homer's entry has correct `className`")
t.ok(homerData.name == 'Homer Simpson', "Homer's entry has correct name")
t.ok(homerData.self.$ref == homerEntry.ID, "Homer's entry has correct self-reference")
t.ok(homerData.spouse.$ref == margeNode.ID, "Homer's entry has correct `spouse` ref")
t.ok(homerData.children.$ref == childrenNode.ID, "Homer's entry has correct `children` ref")
t.ok(!homerData.hasOwnProperty('age'), "No property for 'age' attribute (entry only contain attributes with values)")
t.ok(!homerData.hasOwnProperty('task'), "No property for 'task' attribute (entry only contain attributes with values)")
t.ok(margeEntry.$entry, "Marge's entry is marked with $entry")
t.ok(margeEntry.ID == margeNode.ID, "Marge's entry has correct ID")
t.ok(margeEntry.className == 'KiokuJS.Test.Person', "Marge's entry has correct `className`")
t.ok(margeData.name == 'Marge Simpson', "Marge's entry has correct name")
t.ok(margeData.self.$ref == margeEntry.ID, "Marge's entry has correct self-reference")
t.ok(margeData.spouse.$ref == homerNode.ID, "Marge's entry has correct `spouse` ref")
t.ok(margeData.children.$ref == childrenNode.ID, "Marge's entry has correct `children` ref")
t.ok(!margeData.hasOwnProperty('age'), "No property for 'age' attribute (entry only contain attributes with values)")
t.ok(childrenEntry.$entry, "Children's entry is marked with $entry")
t.ok(childrenEntry.ID == childrenNode.ID, "Children's entry has correct ID")
t.ok(childrenEntry.className == 'Array', "Children's entry has correct `className`")
t.ok(childrenData[0].$ref == bartNode.ID, "Children's entry has correct first element")
t.ok(childrenData[1].$ref == lisaNode.ID, "Children's entry has correct second element")
t.ok(bartEntry.$entry, "Bart's entry is marked with $entry")
t.ok(bartEntry.ID == bartNode.ID, "Bart's entry has correct ID")
t.ok(bartEntry.className == 'KiokuJS.Test.Person', "Bart's entry has correct `className`")
t.ok(bartData.name == 'Bart Simpson', "Bart's entry has correct name")
t.ok(bartData.self.$ref == bartEntry.ID, "Bart's entry has correct self-reference")
t.ok(bartData.father.$ref == homerNode.ID, "Bart's entry has correct `father` ref")
t.ok(bartData.mother.$ref == margeNode.ID, "Bart's entry has correct `mother` ref")
t.ok(lisaEntry.$entry, "Lisa's entry is marked with $entry")
t.ok(lisaEntry.ID == lisaNode.ID, "Lisa's entry has correct ID")
t.ok(lisaEntry.className == 'KiokuJS.Test.Person', "Lisa's entry has correct `className`")
t.ok(lisaData.name == 'Lisa Simpson', "Lisa's entry has correct name")
t.ok(lisaData.self.$ref == lisaEntry.ID, "Lisa's entry has correct self-reference")
t.ok(lisaData.father.$ref == homerNode.ID, "Lisa's entry has correct `father` ref")
t.ok(lisaData.mother.$ref == margeNode.ID, "Lisa's entry has correct `mother` ref")
t.done()
t.endAsync(async0)
})
})