-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextualTemplates.rb
More file actions
5919 lines (4386 loc) · 110 KB
/
Copy pathTextualTemplates.rb
File metadata and controls
5919 lines (4386 loc) · 110 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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Downloaded from https://repo.progsbase.com - Code Developed Using progsbase.
class LinkedListNodeNodes
attr_accessor :endx
attr_accessor :value
attr_accessor :nextx
end
class LinkedListNodes
attr_accessor :first
attr_accessor :last
end
class Node
attr_accessor :type
attr_accessor :p1
attr_accessor :p2
attr_accessor :block1
attr_accessor :hasElseBlock
attr_accessor :block2
attr_accessor :nodes
end
class BooleanArrayReference
attr_accessor :booleanArray
end
class BooleanReference
attr_accessor :booleanValue
end
class CharacterReference
attr_accessor :characterValue
end
class NumberArrayReference
attr_accessor :numberArray
end
class NumberReference
attr_accessor :numberValue
end
class StringArrayReference
attr_accessor :stringArray
end
class StringReference
attr_accessor :string
end
class ElementArrayReference
attr_accessor :array
end
class LinkedListElements
attr_accessor :first
attr_accessor :last
end
class LinkedListNodeElements
attr_accessor :endx
attr_accessor :value
attr_accessor :nextx
end
class Element
attr_accessor :type
attr_accessor :object
attr_accessor :array
attr_accessor :string
attr_accessor :number
attr_accessor :booleanValue
end
class ElementReference
attr_accessor :element
end
class ElementType
attr_accessor :name
end
class StringElementMap
attr_accessor :stringListRef
attr_accessor :elementListRef
end
class DynamicArrayCharacters
attr_accessor :array
attr_accessor :length
end
class LinkedListNodeStrings
attr_accessor :endx
attr_accessor :value
attr_accessor :nextx
end
class LinkedListStrings
attr_accessor :first
attr_accessor :last
end
class LinkedListNodeNumbers
attr_accessor :nextx
attr_accessor :endx
attr_accessor :value
end
class LinkedListNumbers
attr_accessor :first
attr_accessor :last
end
class LinkedListCharacters
attr_accessor :first
attr_accessor :last
end
class LinkedListNodeCharacters
attr_accessor :endx
attr_accessor :value
attr_accessor :nextx
end
class DynamicArrayNumbers
attr_accessor :array
attr_accessor :length
end
def CreateLinkedListNodes()
ll = LinkedListNodes.new
ll.first = LinkedListNodeNodes.new
ll.last = ll.first
ll.last.endx = true
return ll
end
def LinkedListAddNode(ll, value)
ll.last.endx = false
ll.last.value = value
ll.last.nextx = LinkedListNodeNodes.new
ll.last.nextx.endx = true
ll.last = ll.last.nextx
end
def LinkedListNodesToArray(ll)
node = ll.first
length = LinkedListNodesLength(ll)
array = Array.new(length)
i = 0.0
while(i < length)
array[i] = node.value
node = node.nextx
i = i + 1.0
end
return array
end
def LinkedListNodesLength(ll)
l = 0.0
node = ll.first
while(!node.endx)
node = node.nextx
l = l + 1.0
end
return l
end
def FreeLinkedListNode(ll)
node = ll.first
while(!node.endx)
prev = node
node = node.nextx
delete(prev)
end
delete(node)
end
def IsValidTemplate(template)
tokens = CreateLinkedListString()
errorMessage = StringReference.new
success = GenerateTokensFromTemplate(template, tokens, errorMessage)
if success
root = Node.new
ts = LinkedListStringsToArray(tokens)
success = ParseTemplate(ts, root, errorMessage)
end
if !success
FreeStringReference(errorMessage)
end
return success
end
def GenerateTokensFromTemplate(template, tokens, errorMessage)
success = true
cs = Array.new(4)
da = CreateDynamicArrayCharacters()
pc = "x"
i = 0.0
while(i < template.length && success)
c = template[i]
if c != "{"
DynamicArrayAddCharacter(da, c)
i = i + 1.0
else
FillString(cs, "x")
j = 0.0
while(i + j < template.length && j < cs.length)
cs[j] = template[i + j]
j = j + 1.0
end
if StringsEqual(cs, "{use".split("")) || StringsEqual(cs, "{end".split("")) || StringsEqual(cs, "{pri".split("")) || StringsEqual(cs, "{for".split("")) || StringsEqual(cs, "{if ".split("")) || StringsEqual(cs, "{els".split(""))
if pc != "\\"
# Find end.
found = false
l = 0.0
while(i + l < template.length && !found)
if template[i + l] == "}"
found = true
end
l = l + 1.0
end
if found
if da.length > 0.0
a = DynamicArrayCharactersToArray(da)
LinkedListAddString(tokens, a)
FreeDynamicArrayCharacters(da)
da = CreateDynamicArrayCharacters()
end
j = 0.0
while(j < l)
DynamicArrayAddCharacter(da, template[i + j])
j = j + 1.0
end
a = DynamicArrayCharactersToArray(da)
LinkedListAddString(tokens, a)
FreeDynamicArrayCharacters(da)
da = CreateDynamicArrayCharacters()
i = i + l
else
success = false
errorMessage.string = "Template command found, but not ended properly.".split("")
end
else
DynamicArrayAddCharacter(da, c)
i = i + 1.0
end
else
DynamicArrayAddCharacter(da, c)
i = i + 1.0
end
end
pc = c
end
if da.length > 0.0
a = DynamicArrayCharactersToArray(da)
LinkedListAddString(tokens, a)
end
FreeDynamicArrayCharacters(da)
return success
end
def GenerateDocument(template, json, document, errorMessage)
data = ElementReference.new
errorMessages = StringArrayReference.new
success = ReadJSON(json, data, errorMessages)
if success
success = GenerateDocumentBasedOnElement(template, data.element, document, errorMessage)
else
errorMessage.string = JoinStringsWithSeparator(errorMessages.stringArray, ", ".split(""))
FreeStringArrayReference(errorMessages)
end
return success
end
def GenerateDocumentBasedOnElement(template, data, document, errorMessage)
tokens = CreateLinkedListString()
success = GenerateTokensFromTemplate(template, tokens, errorMessage)
if success
root = Node.new
ts = LinkedListStringsToArray(tokens)
FreeLinkedListString(tokens)
success = ParseTemplate(ts, root, errorMessage)
if success
ll = CreateLinkedListCharacter()
success = GenerateDocumentFromBlock(root, data, ll, errorMessage)
if success
document.string = LinkedListCharactersToArray(ll)
FreeLinkedListCharacter(ll)
end
end
end
return success
end
def GenerateDocumentFromBlock(root, data, ll, errorMessage)
n = root.nodes.first
success = true
while(!n.endx && success)
success = GenerateDocumentFromNode(n.value, data, ll, errorMessage)
n = n.nextx
end
return success
end
def GenerateDocumentFromNode(n, data, ll, errorMessage)
success = true
found = BooleanReference.new
if StringsEqual(n.type, "block".split(""))
success = GenerateDocumentFromBlock(n, data, ll, errorMessage)
elsif StringsEqual(n.type, "use".split(""))
success = GenerateDocumentFromUse(n, data, ll, errorMessage)
elsif StringsEqual(n.type, "if".split(""))
success = GenerateDocumentFromIf(n, data, ll, errorMessage)
elsif StringsEqual(n.type, "foreach".split(""))
success = GenerateDocumentFromForeach(n, data, ll, errorMessage)
elsif StringsEqual(n.type, "text".split(""))
success = true
LinkedListCharactersAddString(ll, n.p1)
elsif StringsEqual(n.type, "print".split(""))
if StringsEqual(data.type, "object".split(""))
value = GetObjectValueWithCheck(data.object, n.p1, found)
if found.booleanValue
if StringsEqual(value.type, "string".split(""))
valueString = value.string
else
valueString = WriteJSON(value)
end
LinkedListCharactersAddString(ll, valueString)
else
success = false
errorMessage.string = "Key for printing not found in JSON object: ".split("")
errorMessage.string = ConcatenateString(errorMessage.string, n.p1)
end
else
success = false
errorMessage.string = "Data structure for print command is not a JSON object.".split("")
end
end
return success
end
def GenerateDocumentFromUse(n, data, ll, errorMessage)
found = BooleanReference.new
if StringsEqual(data.type, "object".split(""))
value = GetObjectValueWithCheck(data.object, n.p1, found)
if found.booleanValue
success = GenerateDocumentFromNode(n.block1, value, ll, errorMessage)
else
success = false
errorMessage.string = "Key for use not found in JSON object.".split("")
end
else
success = false
errorMessage.string = "Data structure for use command is not a JSON object.".split("")
end
return success
end
def GenerateDocumentFromIf(n, data, ll, errorMessage)
success = true
found = BooleanReference.new
if StringsEqual(data.type, "object".split(""))
value = GetObjectValueWithCheck(data.object, n.p1, found)
if found.booleanValue
if StringsEqual(value.type, "boolean".split(""))
if value.booleanValue
success = GenerateDocumentFromBlock(n.block1, data, ll, errorMessage)
end
if n.hasElseBlock
if !value.booleanValue
success = GenerateDocumentFromBlock(n.block2, data, ll, errorMessage)
end
end
else
success = false
errorMessage.string = "Value for if not boolean.".split("")
end
else
success = false
errorMessage.string = "Key for if not found in JSON object: ".split("")
errorMessage.string = ConcatenateString(errorMessage.string, n.p1)
end
else
success = false
errorMessage.string = "Data structure for if command is not a JSON object.".split("")
end
return success
end
def GenerateDocumentFromForeach(n, data, ll, errorMessage)
success = true
found = BooleanReference.new
loopVar = CreateObjectElement(0.0)
PutStringElementMap(loopVar.object, n.p1, Element.new)
if StringsEqual(data.type, "object".split(""))
value = GetObjectValueWithCheck(data.object, n.p2, found)
if found.booleanValue
if StringsEqual(value.type, "array".split(""))
i = 0.0
while(i < value.array.length)
arrayElement = value.array[i]
if StringsEqual(arrayElement.type, "object".split(""))
success = GenerateDocumentFromBlock(n.block1, arrayElement, ll, errorMessage)
else
SetStringElementMap(loopVar.object, 0.0, n.p1, arrayElement)
success = GenerateDocumentFromBlock(n.block1, loopVar, ll, errorMessage)
end
i = i + 1.0
end
else
success = false
errorMessage.string = "Value for foreach is not an array.".split("")
end
else
success = false
errorMessage.string = "Key for foreach not found in JSON object: ".split("")
errorMessage.string = ConcatenateString(errorMessage.string, n.p2)
end
else
success = false
errorMessage.string = "Data structure for foreach command is not a JSON object.".split("")
end
return success
end
def ParseTemplate(tokens, node, errorMessage)
position = CreateNumberReference(0.0)
success = ParseTemplateBlock(tokens, position, node, errorMessage)
if success
if position.numberValue != tokens.length
success = false
errorMessage.string = "Unexpected token at the end of template.".split("")
end
end
delete(position)
return success
end
def ParseTemplateBlock(tokens, position, node, errorMessage)
success = true
done = false
node.type = "block".split("")
node.nodes = CreateLinkedListNodes()
while(position.numberValue < tokens.length && success && !done)
tn = Node.new
success = ParseNodeString(tokens[position.numberValue].string, tn, errorMessage)
if success
if StringsEqual(tn.type, "text".split("")) || StringsEqual(tn.type, "print".split(""))
LinkedListAddNode(node.nodes, tn)
position.numberValue = position.numberValue + 1.0
elsif StringsEqual(tn.type, "use".split(""))
nb = Node.new
success = ParseUseBlock(tokens, position, nb, errorMessage)
LinkedListAddNode(node.nodes, nb)
elsif StringsEqual(tn.type, "if".split(""))
nb = Node.new
success = ParseIfBlock(tokens, position, nb, errorMessage)
LinkedListAddNode(node.nodes, nb)
elsif StringsEqual(tn.type, "foreach".split(""))
nb = Node.new
success = ParseForeachBlock(tokens, position, nb, errorMessage)
LinkedListAddNode(node.nodes, nb)
else
done = true
end
end
end
return success
end
def ParseUseBlock(tokens, position, useBlock, errorMessage)
n = Node.new
ParseNodeString(tokens[position.numberValue].string, n, errorMessage)
useBlock.type = CopyString(n.type)
useBlock.p1 = CopyString(n.p1)
useBlock.block1 = Node.new
position.numberValue = position.numberValue + 1.0
success = ParseTemplateBlock(tokens, position, useBlock.block1, errorMessage)
if success
if position.numberValue < tokens.length
n = Node.new
ParseNodeString(tokens[position.numberValue].string, n, errorMessage)
if StringsEqual(n.type, "end".split(""))
success = true
position.numberValue = position.numberValue + 1.0
else
success = false
errorMessage.string = "End block expected at the end of use block.".split("")
end
else
success = false
errorMessage.string = "End block expected at the end of use block.".split("")
end
end
return success
end
def ParseIfBlock(tokens, position, ifBlock, errorMessage)
n = Node.new
ParseNodeString(tokens[position.numberValue].string, n, errorMessage)
ifBlock.type = CopyString(n.type)
ifBlock.p1 = CopyString(n.p1)
ifBlock.block1 = Node.new
ifBlock.hasElseBlock = false
position.numberValue = position.numberValue + 1.0
success = ParseTemplateBlock(tokens, position, ifBlock.block1, errorMessage)
if success
if position.numberValue < tokens.length
n = Node.new
ParseNodeString(tokens[position.numberValue].string, n, errorMessage)
if StringsEqual(n.type, "end".split(""))
success = true
position.numberValue = position.numberValue + 1.0
elsif StringsEqual(n.type, "else".split(""))
position.numberValue = position.numberValue + 1.0
ifBlock.hasElseBlock = true
ifBlock.block2 = Node.new
success = ParseTemplateBlock(tokens, position, ifBlock.block2, errorMessage)
if success
if position.numberValue < tokens.length
n = Node.new
ParseNodeString(tokens[position.numberValue].string, n, errorMessage)
if StringsEqual(n.type, "end".split(""))
success = true
position.numberValue = position.numberValue + 1.0
else
success = false
errorMessage.string = "End block expected at the end of else block.".split("")
end
else
success = false
errorMessage.string = "End block expected at the end of else block.".split("")
end
end
else
success = false
errorMessage.string = "End or else block expected at the end of if block.".split("")
end
else
success = false
errorMessage.string = "End or else block expected at the end of if block.".split("")
end
end
return success
end
def ParseForeachBlock(tokens, position, foreachBlock, errorMessage)
n = Node.new
ParseNodeString(tokens[position.numberValue].string, n, errorMessage)
foreachBlock.type = CopyString(n.type)
foreachBlock.p1 = CopyString(n.p1)
foreachBlock.p2 = CopyString(n.p2)
foreachBlock.block1 = Node.new
position.numberValue = position.numberValue + 1.0
success = ParseTemplateBlock(tokens, position, foreachBlock.block1, errorMessage)
if success
if position.numberValue < tokens.length
n = Node.new
ParseNodeString(tokens[position.numberValue].string, n, errorMessage)
if StringsEqual(n.type, "end".split(""))
success = true
position.numberValue = position.numberValue + 1.0
else
success = false
errorMessage.string = "End block expected at the end of for each block.".split("")
end
else
success = false
errorMessage.string = "End block expected at the end of foreach block.".split("")
end
end
return success
end
def ParseNodeString(token, node, errorMessage)
success = true
isText = false
if token.length <= 2.0
isText = true
elsif token[0] == "\\" && token[1] == "{"
isText = true
elsif token[0] != "{"
isText = true
else
command = Substring(token, 1.0, token.length - 1.0)
parts = SplitByCharacter(command, " ")
if command.length > 0.0
if StringsEqual(parts[0].string, "use".split(""))
if parts.length == 2.0
node.type = CopyString(parts[0].string)
node.p1 = CopyString(parts[1].string)
else
success = false
errorMessage.string = "The use command takes one parameter.".split("")
end
elsif StringsEqual(parts[0].string, "end".split(""))
if parts.length == 1.0
node.type = CopyString(parts[0].string)
else
success = false
errorMessage.string = "The end command takes no parameters.".split("")
end
elsif StringsEqual(parts[0].string, "print".split(""))
if parts.length == 2.0
node.type = CopyString(parts[0].string)
node.p1 = CopyString(parts[1].string)
else
success = false
errorMessage.string = "The print command takes one parameter.".split("")
end
elsif StringsEqual(parts[0].string, "foreach".split(""))
if parts.length == 4.0
if StringsEqual(parts[2].string, "in".split(""))
node.type = CopyString(parts[0].string)
node.p1 = CopyString(parts[1].string)
node.p2 = CopyString(parts[3].string)
else
success = false
errorMessage.string = "The foreach command must have \"in\" after the first parameter.".split("")
end
else
success = false
errorMessage.string = "The foreach command takes three parameters.".split("")
end
elsif StringsEqual(parts[0].string, "if".split(""))
if parts.length == 2.0
node.type = CopyString(parts[0].string)
node.p1 = CopyString(parts[1].string)
else
success = false
errorMessage.string = "The if command takes one parameter.".split("")
end
elsif StringsEqual(parts[0].string, "else".split(""))
if parts.length == 1.0
node.type = CopyString(parts[0].string)
else
success = false
errorMessage.string = "The else command takes no parameters.".split("")
end
else
isText = true
end
else
isText = true
end
end
if isText
node.type = "text".split("")
node.p1 = ReplaceString(token, "\\{print ".split(""), "{print ".split(""))
node.p1 = ReplaceString(node.p1, "\\{use ".split(""), "{use ".split(""))
node.p1 = ReplaceString(node.p1, "\\{if ".split(""), "{if ".split(""))
node.p1 = ReplaceString(node.p1, "\\{end}".split(""), "{end}".split(""))
node.p1 = ReplaceString(node.p1, "\\{foreach ".split(""), "{foreach ".split(""))
node.p1 = ReplaceString(node.p1, "\\{else}".split(""), "{else}".split(""))
end
return success
end
def test()
failures = CreateNumberReference(0.0)
testTokenGeneration(failures)
testGenerateDocument1(failures)
testGenerateDocument2(failures)
testGenerateDocument3(failures)
testGenerateDocument4(failures)
testGenerateDocument5(failures)
testGenerateDocument6(failures)
testGenerateDocument7(failures)
testGenerateDocument8(failures)
return failures.numberValue
end
def testGenerateDocument8(failures)
document = StringReference.new
errorMessage = StringReference.new
template = "This is a test: {print b} {foreach x in a}{print x}{end}.".split("")
json = "{\"a\": [1, 2, 3], \"b\": 4}".split("")
success = GenerateDocument(template, json, document, errorMessage)
if success
AssertStringEquals("This is a test: 4 123.".split(""), document.string, failures)
end
AssertTrue(success, failures)
end
def testTokenGeneration(failures)
errorMessage = StringReference.new
tokens = CreateLinkedListString()
template = "This is a template, this is a value: {print a}.".split("")
success = GenerateTokensFromTemplate(template, tokens, errorMessage)
AssertTrue(success, failures)
AssertEquals(3.0, LinkedListStringsLength(tokens), failures)
tokens = CreateLinkedListString()
template = "This is a template, this is a value: {print a} {use b}{print a} {print b}{end}.".split("")
success = GenerateTokensFromTemplate(template, tokens, errorMessage)
AssertTrue(success, failures)
AssertEquals(9.0, LinkedListStringsLength(tokens), failures)
tokens = CreateLinkedListString()
template = "This is a template, this is a value: {print a} {use b}{print a} {print b}{use c}{print a} {print b}{end}{print a}{end}{print a}.".split("")
success = GenerateTokensFromTemplate(template, tokens, errorMessage)
AssertTrue(success, failures)
AssertEquals(16.0, LinkedListStringsLength(tokens), failures)
tokens = CreateLinkedListString()
template = "T: {foreach a in b}{print a}{end}.".split("")
success = GenerateTokensFromTemplate(template, tokens, errorMessage)
AssertTrue(success, failures)
AssertEquals(5.0, LinkedListStringsLength(tokens), failures)
end
def testGenerateDocument1(failures)
AssertTemplateResult("This is a template, this is a value: {print a}.".split(""), "{\"c\": 5, \"a\": 6}".split(""), "This is a template, this is a value: 6.".split(""), failures)
end
def testGenerateDocument2(failures)
AssertTemplateResult("This is a template, this is a value: {print a} {use b}{print a} {print b}{end}.".split(""), "{\"b\": {\"a\": 1, \"b\": 2}, \"a\": 6}".split(""), "This is a template, this is a value: 6 1 2.".split(""), failures)
end
def testGenerateDocument3(failures)
AssertTemplateResult("This is a template, this is a value: {print a} {use b}{print a} {print b}{use c}{print a} {print b}{end}{print a}{end}{print a}.".split(""), "{\"b\": {\"a\": 1, \"b\": 2, \"c\": {\"a\": 3, \"b\": 4}}, \"a\": 6}".split(""), "This is a template, this is a value: 6 1 23 416.".split(""), failures)
end
def testGenerateDocument4(failures)
AssertTemplateResult("T: {if a}a{end}.".split(""), "{\"b\": {\"a\": 1, \"b\": 2, \"c\": {\"a\": 3, \"b\": 4}}, \"a\": true}".split(""), "T: a.".split(""), failures)
AssertTemplateResult("T: {if a}a{else}b{end}.".split(""), "{\"b\": {\"a\": 1, \"b\": 2, \"c\": {\"a\": 3, \"b\": 4}}, \"a\": false}".split(""), "T: b.".split(""), failures)
end
def testGenerateDocument5(failures)
AssertTemplateResult("T: {foreach a in b}{print a}{end}.".split(""), "{\"b\": [1, 2, 3, 4]}".split(""), "T: 1234.".split(""), failures)
end
def testGenerateDocument6(failures)
AssertTemplateResult("{test}\\{print a}.".split(""), "{\"c\": 5, \"a\": 6}".split(""), "{test}{print a}.".split(""), failures)
end
def testGenerateDocument7(failures)
AssertTemplateResult("{".split(""), "{}".split(""), "{".split(""), failures)
AssertTemplateResult("{ ".split(""), "{}".split(""), "{ ".split(""), failures)
AssertTemplateResult("{use a}\\{print a}{end}".split(""), "{\"a\": {}}".split(""), "{print a}".split(""), failures)
AssertTemplateResult("\\{print a}{print a}}".split(""), "{\"a\": 3}".split(""), "{print a}3}".split(""), failures)
AssertTemplateResult("\\\\{print a}{print a}}".split(""), "{\"a\": 3}".split(""), "\\{print a}3}".split(""), failures)
AssertTemplateResult("\\\\{print a}{print a}\\\\{print a}}".split(""), "{\"a\": 3}".split(""), "\\{print a}3\\{print a}}".split(""), failures)
AssertTemplateResult("\\{print a}{print a}\\{".split(""), "{\"a\": 3}".split(""), "{print a}3\\{".split(""), failures)
AssertTemplateResult(" <div>Pris</div>\n {foreach p in products}\n <div>{print productCode}</div>\n <div>1</div>\n <div>{print price}</div>\n {end}\n <div>Totalt</div>".split(""), "{\"products\": [{\"productCode\": \"kl\", \"price\": \"1.2\"}, {\"productCode\": \"skl\", \"price\": \"20.0\"}]}".split(""), " <div>Pris</div>\n \n <div>kl</div>\n <div>1</div>\n <div>1.2</div>\n \n <div>skl</div>\n <div>1</div>\n <div>20.0</div>\n \n <div>Totalt</div>".split(""), failures)
AssertTemplateError("{print".split(""), "{}".split(""), "Template command found, but not ended properly.".split(""), failures)
AssertTemplateError("{print a}".split(""), "{}".split(""), "Key for printing not found in JSON object: a".split(""), failures)
AssertTemplateError("This is a template, this is a value: {print a {print a}.".split(""), "{\"a\": 5}".split(""), "The print command takes one parameter.".split(""), failures)
AssertTemplateError("This is a {use a}\\{print a}template, this is a value: {print a}.{end}".split(""), "{\"a\": 5}".split(""), "Data structure for print command is not a JSON object.".split(""), failures)
AssertTemplateError("{use a}".split(""), "{}".split(""), "End block expected at the end of use block.".split(""), failures)
AssertTemplateError("{if a}".split(""), "{\"a\": true}".split(""), "End or else block expected at the end of if block.".split(""), failures)
AssertTemplateError("{if a}{else}".split(""), "{\"a\": true}".split(""), "End block expected at the end of else block.".split(""), failures)
AssertTemplateError("{foreach x in a}".split(""), "{\"a\": [1, 2, 3, 4]}".split(""), "End block expected at the end of foreach block.".split(""), failures)
end
def AssertTemplateResult(template, json, result, failures)
data = ElementReference.new
errorMessages = StringArrayReference.new
document = StringReference.new
errorMessage = StringReference.new
success = ReadJSON(json, data, errorMessages)
AssertTrue(success, failures)
if success
success = GenerateDocumentBasedOnElement(template, data.element, document, errorMessage)
AssertTrue(success, failures)
if success
AssertStringEquals(document.string, result, failures)
end
end
end
def AssertTemplateError(template, json, errorMessage, failures)
data = ElementReference.new
errorMessages = StringArrayReference.new
document = StringReference.new
errorMessageRef = StringReference.new
success = ReadJSON(json, data, errorMessages)
AssertTrue(success, failures)
if success
success = GenerateDocumentBasedOnElement(template, data.element, document, errorMessageRef)
AssertFalse(success, failures)
if !success
AssertStringEquals(errorMessage, errorMessageRef.string, failures)
end
end
end
def CreateBooleanReference(value)
ref = BooleanReference.new
ref.booleanValue = value
return ref
end
def CreateBooleanArrayReference(value)
ref = BooleanArrayReference.new
ref.booleanArray = value
return ref
end
def CreateBooleanArrayReferenceLengthValue(length, value)
ref = BooleanArrayReference.new
ref.booleanArray = Array.new(length)
i = 0.0
while(i < length)
ref.booleanArray[i] = value
i = i + 1.0
end
return ref
end
def FreeBooleanArrayReference(booleanArrayReference)
delete(booleanArrayReference.booleanArray)
delete(booleanArrayReference)
end
def CreateCharacterReference(value)
ref = CharacterReference.new
ref.characterValue = value
return ref
end
def CreateNumberReference(value)
ref = NumberReference.new
ref.numberValue = value
return ref
end
def CreateNumberArrayReference(value)
ref = NumberArrayReference.new