File size: 6,746 Bytes
30c32c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
const test = require('tap').test;
const adapter = require('../../src/engine/adapter');
const events = require('../fixtures/events.json');

test('spec', t => {
    t.type(adapter, 'function');
    t.end();
});

test('invalid inputs', t => {
    let nothing = adapter('not an object');
    t.type(nothing, 'undefined');
    nothing = adapter({noxmlproperty: true});
    t.type(nothing, 'undefined');
    t.end();
});

test('create event', t => {
    const result = adapter(events.create);

    t.ok(Array.isArray(result));
    t.equal(result.length, 2);

    // Outer block
    t.type(result[0].id, 'string');
    t.type(result[0].opcode, 'string');
    t.type(result[0].comment, 'undefined');
    t.type(result[0].fields, 'object');
    t.type(result[0].inputs, 'object');
    t.type(result[0].inputs.DURATION, 'object');
    t.type(result[0].topLevel, 'boolean');
    t.equal(result[0].topLevel, true);

    // Enclosed shadow block
    t.type(result[1].id, 'string');
    t.type(result[1].opcode, 'string');
    t.type(result[1].fields, 'object');
    t.type(result[1].inputs, 'object');
    t.type(result[1].fields.NUM, 'object');
    t.type(result[1].fields.NUM.value, '10');
    t.type(result[1].topLevel, 'boolean');
    t.equal(result[1].topLevel, false);

    t.end();
});

test('create with comment', t => {
    const result = adapter(events.createComment);

    // This test should be the same as above except that it also has a comment.

    t.ok(Array.isArray(result));
    t.equal(result.length, 2);

    t.type(result[0].comment, 'string');
    t.equal(result[0].comment, 'aCommentId');

    t.end();
});

test('create with branch', t => {
    const result = adapter(events.createbranch);
    // Outer block
    t.type(result[0].id, 'string');
    t.type(result[0].opcode, 'string');
    t.type(result[0].fields, 'object');
    t.type(result[0].inputs, 'object');
    t.type(result[0].inputs.SUBSTACK, 'object');
    t.type(result[0].topLevel, 'boolean');
    t.equal(result[0].topLevel, true);
    // In branch
    const branchBlockId = result[0].inputs.SUBSTACK.block;
    const branchShadowId = result[0].inputs.SUBSTACK.shadow;
    t.type(branchBlockId, 'string');
    t.equal(branchShadowId, null);
    // Find actual branch block
    let branchBlock = null;
    for (let i = 0; i < result.length; i++) {
        if (result[i].id === branchBlockId) {
            branchBlock = result[i];
        }
    }
    t.type(branchBlock, 'object');
    t.end();
});

test('create with two branches', t => {
    const result = adapter(events.createtwobranches);
    // Outer block
    t.type(result[0].id, 'string');
    t.type(result[0].opcode, 'string');
    t.type(result[0].fields, 'object');
    t.type(result[0].inputs, 'object');
    t.type(result[0].inputs.SUBSTACK, 'object');
    t.type(result[0].inputs.SUBSTACK2, 'object');
    t.type(result[0].topLevel, 'boolean');
    t.equal(result[0].topLevel, true);
    // In branchs
    const firstBranchBlockId = result[0].inputs.SUBSTACK.block;
    const secondBranchBlockId = result[0].inputs.SUBSTACK2.block;
    t.type(firstBranchBlockId, 'string');
    t.type(secondBranchBlockId, 'string');
    const firstBranchShadowBlockId = result[0].inputs.SUBSTACK.shadow;
    const secondBranchShadowBlockId = result[0].inputs.SUBSTACK2.shadow;
    t.equal(firstBranchShadowBlockId, null);
    t.equal(secondBranchShadowBlockId, null);
    // Find actual branch blocks
    let firstBranchBlock = null;
    let secondBranchBlock = null;
    for (let i = 0; i < result.length; i++) {
        if (result[i].id === firstBranchBlockId) {
            firstBranchBlock = result[i];
        }
        if (result[i].id === secondBranchBlockId) {
            secondBranchBlock = result[i];
        }
    }
    t.type(firstBranchBlock, 'object');
    t.type(secondBranchBlock, 'object');
    t.end();
});

test('create with top-level shadow', t => {
    const result = adapter(events.createtoplevelshadow);
    t.ok(Array.isArray(result));
    t.equal(result.length, 1);

    // Outer block
    t.type(result[0].id, 'string');
    t.type(result[0].opcode, 'string');
    t.type(result[0].fields, 'object');
    t.type(result[0].inputs, 'object');
    t.type(result[0].topLevel, 'boolean');
    t.equal(result[0].topLevel, true);
    t.end();
});

test('create with next connection', t => {
    const result = adapter(events.createwithnext);

    t.ok(Array.isArray(result));
    t.equal(result.length, 2);

    // First block
    t.type(result[0].id, 'string');
    t.type(result[0].opcode, 'string');
    t.type(result[0].fields, 'object');
    t.type(result[0].inputs, 'object');
    t.type(result[0].topLevel, 'boolean');
    t.equal(result[0].topLevel, true);
    t.type(result[0].next, 'string');
    t.equal(result[0].next, result[1].id);

    // Second block
    t.type(result[1].id, 'string');
    t.type(result[1].opcode, 'string');
    t.type(result[1].fields, 'object');
    t.type(result[1].inputs, 'object');
    t.type(result[1].topLevel, 'boolean');
    t.equal(result[1].topLevel, false);
    t.equal(result[1].next, null);

    t.end();
});

test('create with obscured shadow', t => {
    const result = adapter(events.createobscuredshadow);
    t.ok(Array.isArray(result));
    t.equal(result.length, 4);
    t.end();
});

test('create variable with entity in name', t => {
    const result = adapter(events.createvariablewithentity);

    t.ok(Array.isArray(result));
    t.equal(result.length, 1);

    t.type(result[0].id, 'string');
    t.type(result[0].opcode, 'string');
    t.type(result[0].fields, 'object');
    t.type(result[0].fields.VARIABLE, 'object');
    t.type(result[0].fields.VARIABLE.value, 'string');
    t.equal(result[0].fields.VARIABLE.value, 'this & that');
    t.type(result[0].inputs, 'object');
    t.type(result[0].topLevel, 'boolean');
    t.equal(result[0].topLevel, true);
    t.end();
});

test('create with invalid block xml', t => {
    // Entirely invalid block XML
    const result = adapter(events.createinvalid);
    t.ok(Array.isArray(result));
    t.equal(result.length, 0);

    // Invalid grandchild tag
    const result2 = adapter(events.createinvalidgrandchild);
    t.ok(Array.isArray(result2));
    t.equal(result2.length, 1);
    t.type(result2[0].id, 'string');
    t.equal(Object.keys(result2[0].inputs).length, 0);
    t.equal(Object.keys(result2[0].fields).length, 0);

    t.end();
});

test('create with invalid xml', t => {
    const result = adapter(events.createbadxml);
    t.ok(Array.isArray(result));
    t.equal(result.length, 0);
    t.end();
});

test('create with empty field', t => {
    const result = adapter(events.createemptyfield);
    t.ok(Array.isArray(result));
    t.equal(result.length, 3);
    t.end();
});