forked from EngineHub/CommandHelper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethodScriptCompilerTest.java
More file actions
1425 lines (1286 loc) · 50.4 KB
/
MethodScriptCompilerTest.java
File metadata and controls
1425 lines (1286 loc) · 50.4 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
package com.laytonsmith.core;
import com.laytonsmith.PureUtilities.Common.FileUtil;
import com.laytonsmith.abstraction.MCPlayer;
import com.laytonsmith.abstraction.MCServer;
import com.laytonsmith.core.compiler.analysis.Declaration;
import com.laytonsmith.core.compiler.analysis.Namespace;
import com.laytonsmith.core.compiler.analysis.ProcDeclaration;
import com.laytonsmith.core.compiler.analysis.ProcRootDeclaration;
import com.laytonsmith.core.compiler.analysis.Scope;
import com.laytonsmith.core.compiler.analysis.StaticAnalysis;
import com.laytonsmith.core.constructs.CFunction;
import com.laytonsmith.core.constructs.CInt;
import com.laytonsmith.core.constructs.CPrimitive;
import com.laytonsmith.core.constructs.CString;
import com.laytonsmith.core.constructs.CVoid;
import com.laytonsmith.core.constructs.Target;
import com.laytonsmith.core.constructs.Token;
import com.laytonsmith.core.constructs.Variable;
import com.laytonsmith.core.environments.CommandHelperEnvironment;
import com.laytonsmith.core.environments.Environment;
import com.laytonsmith.core.exceptions.AbstractCompileException;
import com.laytonsmith.core.exceptions.ConfigCompileException;
import com.laytonsmith.core.exceptions.ConfigCompileGroupException;
import com.laytonsmith.core.exceptions.ConfigRuntimeException;
import com.laytonsmith.testing.AbstractIntegrationTest;
import com.laytonsmith.testing.StaticTest;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import java.io.File;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import static com.laytonsmith.testing.StaticTest.RunCommand;
import static com.laytonsmith.testing.StaticTest.SRun;
import java.util.Collections;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
//import org.powermock.api.mockito.PowerMockito;
//import org.powermock.core.classloader.annotations.PowerMockIgnore;
//import org.powermock.core.classloader.annotations.PrepareForTest;
//import org.powermock.modules.junit4.PowerMockRunner;
/**
*
*
*/
//@RunWith(PowerMockRunner.class)
//@PrepareForTest(CommandHelperPlugin.class)
//@PowerMockIgnore({"javax.xml.parsers.*", "com.sun.org.apache.xerces.internal.jaxp.*"})
public class MethodScriptCompilerTest extends AbstractIntegrationTest {
MCServer fakeServer;
MCPlayer fakePlayer;
com.laytonsmith.core.environments.Environment env;
static Set<Class<? extends Environment.EnvironmentImpl>> envs = Environment.getDefaultEnvClasses();
public MethodScriptCompilerTest() {
//StaticTest.InstallFakeServerFrontend();
}
@AfterClass
public static void tearDownClass() throws Exception {
new File("profiler.config").deleteOnExit();
}
@Before
public void setUp() throws Exception {
fakePlayer = StaticTest.GetOnlinePlayer();
fakeServer = StaticTest.GetFakeServer();
env = Static.GenerateStandaloneEnvironment();
env = env.cloneAndAdd(new CommandHelperEnvironment());
env.getEnv(CommandHelperEnvironment.class).SetPlayer(fakePlayer);
}
@After
public void tearDown() {
}
/**
* Test of lex method, of class MethodScriptCompiler.
*/
@Test
public void testLex() throws Exception {
String config = "/cmd = msg('string')";
List<Token> e = new ArrayList<>();
//This is the decomposed version of the above config
e.add(new Token(Token.TType.COMMAND, "/cmd", Target.UNKNOWN));
e.add(new Token(Token.TType.WHITESPACE, " ", Target.UNKNOWN));
e.add(new Token(Token.TType.ALIAS_END, "=", Target.UNKNOWN));
e.add(new Token(Token.TType.WHITESPACE, " ", Target.UNKNOWN));
e.add(new Token(Token.TType.FUNC_NAME, "msg", Target.UNKNOWN));
e.add(new Token(Token.TType.FUNC_START, "(", Target.UNKNOWN));
e.add(new Token(Token.TType.STRING, "string", Target.UNKNOWN));
e.add(new Token(Token.TType.FUNC_END, ")", Target.UNKNOWN));
e.add(new Token(Token.TType.NEWLINE, "\n", Target.UNKNOWN));
List<Token> result = MethodScriptCompiler.lex(config, null, null, false);
assertEquals(e, result);
String[] badConfigs = {
"'\\q'", //Bad escape sequences
"'\\m'"};
for(String c : badConfigs) {
try {
MethodScriptCompiler.lex(c, null, null, false);
//Shouldn't get here
fail(c + " should not have lexed, but did.");
} catch (ConfigCompileException ex) {
//Success!
}
}
}
@Test
public void testCompile() throws Exception {
MethodScriptCompiler.preprocess(
MethodScriptCompiler.lex("/cmd = msg('this is a string', if(true, 'and', 'another') 'function')", null, null, false), envs).get(0).compileRight(env);
try {
//extra parameter
MethodScriptCompiler.preprocess(MethodScriptCompiler.lex("/cmd = msg('this is a string', if(true, 'and', 'another', 'oops') 'function')", null, null, false), envs).get(0).compileRight(env);
fail("Did not expect test to pass");
} catch (ConfigCompileException e) {
//passed
}
try {
//missing parenthesis
MethodScriptCompiler.preprocess(MethodScriptCompiler.lex("/cmd = msg('this is a string', if(true, 'and', 'another') 'function'", null, null, false), envs).get(0).compileRight(env);
fail("Did not expect test to pass");
} catch (ConfigCompileException e) {
//passed
}
try {
//extra parenthesis
MethodScriptCompiler.preprocess(MethodScriptCompiler.lex("/cmd = msg('this is a string', if(true, 'and', 'another') 'function')))))", null, null, false), envs).get(0).compileRight(env);
fail("Did not expect test to pass");
} catch (ConfigCompileException e) {
//passed
}
try {
//extra multiline end construct
MethodScriptCompiler.preprocess(MethodScriptCompiler.lex("/cmd = msg('this is a string', if(true, 'and', 'another') 'function') <<<", null, null, false), envs).get(0).compileRight(env);
fail("Did not expect test to pass");
} catch (ConfigCompileException e) {
//passed
}
try {
//no multiline end construct
MethodScriptCompiler.preprocess(MethodScriptCompiler.lex("/cmd = >>>\nmsg('hi')\n", null, null, false), envs).get(0).compileRight(env);
fail("Did not expect no multiline end construct to pass");
} catch (ConfigCompileException e) {
//passed
}
MethodScriptCompiler.compile(MethodScriptCompiler.lex("if(1, msg('') msg(''))", null, null, true), null, envs);
}
@Test
public void testLabel() throws Exception {
assertEquals(Static.GLOBAL_PERMISSION, MethodScriptCompiler.preprocess(MethodScriptCompiler.lex("*:/cmd = die()", null, null, false), envs).get(0).compile(env).getLabel());
assertEquals(Static.GLOBAL_PERMISSION, MethodScriptCompiler.preprocess(MethodScriptCompiler.lex("* : /cmd = die()", null, null, false), envs).get(0).compile(env).getLabel());
assertEquals("~lol/fun", MethodScriptCompiler.preprocess(MethodScriptCompiler.lex("~lol/fun: /cmd = die()", null, null, false), envs).get(0).compile(env).getLabel());
}
@Test
public void testCompile13() throws Exception {
MethodScriptCompiler.compile(MethodScriptCompiler.lex("msg ('hi')", null, null, true), null, envs);
}
@Test
public void testCompile14() throws Exception {
MethodScriptCompiler.compile(MethodScriptCompiler.lex("msg(('hi'))", null, null, true), null, envs);
}
@Test
public void testCompile15() throws Exception {
try {
SRun("\n\nmsg(/, 'test')\n\n", fakePlayer);
} catch (ConfigCompileException e) {
assertEquals("3", e.getLineNum());
}
}
@Test
public void testExecute1() throws Exception {
String script = "proc('_hello', @hello0,"
+ " msg(@hello0)"
+ " )"
+ " assign(@hello1, 'hello')"
+ " _hello(@hello1)";
MethodScriptCompiler.execute(MethodScriptCompiler.compile(MethodScriptCompiler.lex(script, null, null, true), null, envs), env, null, null);
verify(fakePlayer).sendMessage("hello");
}
@Test
public void testExecute2() throws Exception {
String script
= "proc('_hello',\n"
+ " assign(@hello, 'hello')\n"
+ " return(@hello)\n"
+ ")\n"
+ "assign(@blah, 'blah')\n"
+ "assign(@blah, _hello())\n"
+ "msg(@blah)\n";
MethodScriptCompiler.execute(MethodScriptCompiler.compile(MethodScriptCompiler.lex(script, null, null, true), null, envs), env, null, null);
verify(fakePlayer).sendMessage("hello");
}
/**
* Here we are testing for mismatched (or empty) square brackets. Essentially, it should throw an exception.
*/
@Test
public void testExecute3() {
try {
String script
= "[";
MethodScriptCompiler.execute(MethodScriptCompiler.compile(MethodScriptCompiler.lex(script, null, null, true), null, envs), env, null, null);
fail("Test passed, but wasn't supposed to");
} catch (AbstractCompileException ex) {
//Passed
}
try {
String script
= "]";
MethodScriptCompiler.execute(MethodScriptCompiler.compile(MethodScriptCompiler.lex(script, null, null, true), null, envs), env, null, null);
fail("Test passed, but wasn't supposed to");
} catch (AbstractCompileException ex) {
//Passed
}
}
@Test
public void testExecute4() throws Exception {
String script
= "proc('_hello',"
+ " return('hello')"
+ ")"
+ "msg(_hello())";
MethodScriptCompiler.execute(MethodScriptCompiler.compile(MethodScriptCompiler.lex(script, null, null, true), null, envs), env, null, null);
verify(fakePlayer).sendMessage("hello");
}
@Test
public void testExecute6() throws Exception {
String script
= "#This is a comment invalid()'\"'' function\n"
+ "msg('hello')";
MethodScriptCompiler.execute(MethodScriptCompiler.compile(MethodScriptCompiler.lex(script, null, null, true), null, envs), env, null, null);
verify(fakePlayer).sendMessage("hello");
}
@Test
public void testExecute7() throws Exception {
String script
= "msg('hello') #This is a comment too invalid()'\"'' function\n";
MethodScriptCompiler.execute(MethodScriptCompiler.compile(MethodScriptCompiler.lex(script, null, null, true), null, envs), env, null, null);
verify(fakePlayer).sendMessage("hello");
}
@Test
public void testExecute9() throws Exception {
String script
= "msg('hello') /* This is a comment too invalid()'\"'' function\n"
+ "yup, still a comment. yay() */";
MethodScriptCompiler.execute(MethodScriptCompiler.compile(MethodScriptCompiler.lex(script, null, null, true), null, envs), env, null, null);
verify(fakePlayer).sendMessage("hello");
}
@Test(expected = AbstractCompileException.class)
public void testExecute10() throws Exception {
String script
= "msg('hello') /* This is a comment too invalid()'\"'' function\n"
+ "yup, still a comment. yay() This will fail though, because the comment is unended.";
MethodScriptCompiler.execute(MethodScriptCompiler.compile(MethodScriptCompiler.lex(script, null, null, true), null, envs), env, null, null);
}
@Test(expected = AbstractCompileException.class)
public void testExecute11() throws Exception {
String script
= "msg('hello') 'unended string";
MethodScriptCompiler.execute(MethodScriptCompiler.compile(MethodScriptCompiler.lex(script, null, null, true), null, envs), env, null, null);
}
@Test
public void testExecute12() throws Exception {
String script
= "msg('hello') /* This is a comment too invalid()'\"'' function\n"
+ "yup, still a comment. yay() */";
MethodScriptCompiler.execute(MethodScriptCompiler.compile(MethodScriptCompiler.lex(script, null, null, true), null, envs), env, null, null);
verify(fakePlayer).sendMessage("hello");
}
@Test
public void testExecute13() throws Exception {
String script
= "assign(@a, array(0, 1, 2))"
+ "msg(@a[0])";
MethodScriptCompiler.execute(MethodScriptCompiler.compile(MethodScriptCompiler.lex(script, null, null, true), null, envs), env, null, null);
verify(fakePlayer).sendMessage("0");
}
@Test
public void testExecute14() throws Exception {
String script
= "proc('_hello', assign(@i, 'world'),"
+ " return(@i)"
+ ")"
+ "msg(_hello('hello'))"
+ "msg(_hello())";
MethodScriptCompiler.execute(MethodScriptCompiler.compile(MethodScriptCompiler.lex(script, null, null, true), null, envs), env, null, null);
verify(fakePlayer).sendMessage("hello");
verify(fakePlayer).sendMessage("world");
}
@Test
public void testExecute15() throws Exception {
String script
= "assign(@i, 0)\n"
+ "msg('@i is currently' @i)\n"
+ "proc('_out', @i,\n"
+ " msg(trim('@i is currently' @i 'and @j is' @j))\n"
+ ")\n"
+ "_out('hello')\n"
+ "assign(@j, 'goodbye')\n"
+ "_out('world')\n";
MethodScriptCompiler.execute(MethodScriptCompiler.compile(MethodScriptCompiler.lex(script, null, null, true), null, envs), env, null, null);
verify(fakePlayer).sendMessage("@i is currently 0");
verify(fakePlayer).sendMessage("@i is currently hello and @j is null");
verify(fakePlayer).sendMessage("@i is currently world and @j is null");
}
@Test
public void testExecute16() throws Exception {
String script
= "proc('_myProc', @i, @j, @k, msg(trim(\"@i @j @k\")))\n"
+ "_myProc()\n"
+ "_myProc(1)\n"
+ "_myProc(1, 2)\n"
+ "_myProc(1, 2, 3)\n"
+ "_myProc(1, 2, 3, 4)\n";
MethodScriptCompiler.execute(MethodScriptCompiler.compile(MethodScriptCompiler.lex(script, null, null, true), null, envs), env, null, null);
verify(fakePlayer).sendMessage("1");
verify(fakePlayer).sendMessage("1 2");
verify(fakePlayer, times(2)).sendMessage("1 2 3");
}
@Test
public void testExecute17() throws Exception {
String script
= "proc('_addition', @i, @j, msg(add(@i, @j)))\n"
+ "_addition(1, 1)\n"
+ "_addition(2, 2)";
MethodScriptCompiler.execute(MethodScriptCompiler.compile(MethodScriptCompiler.lex(script, null, null, true), null, envs), env, null, null);
//verify(fakePlayer).sendMessage("null null null");
verify(fakePlayer).sendMessage("2");
verify(fakePlayer).sendMessage("4");
}
@Test
public void testExecute18() throws Exception {
String script
= "proc('_myProc', msg(@arguments))\n"
+ "_myProc()\n"
+ "_myProc(1)\n"
+ "_myProc(1, 2)";
MethodScriptCompiler.execute(MethodScriptCompiler.compile(MethodScriptCompiler.lex(script, null, null, true), null, envs), env, null, null);
//verify(fakePlayer).sendMessage("null null null");
verify(fakePlayer).sendMessage("{}");
verify(fakePlayer).sendMessage("{1}");
verify(fakePlayer).sendMessage("{1, 2}");
}
/**
* Variables are locked in when the procedure is defined
*
* @throws Exception
*/
@Test
public void testExecute19() throws Exception {
String script
= "assign(@j, 'world')\n"
+ "proc('_hello', assign(@i, @j),"
+ " return(@i)"
+ ")\n"
+ "assign(@j, 'goodbye')\n"
+ "msg(_hello('hello'))"
+ "msg(_hello())";
MethodScriptCompiler.execute(MethodScriptCompiler.compile(MethodScriptCompiler.lex(script, null, null, true), null, envs), env, null, null);
verify(fakePlayer).sendMessage("hello");
verify(fakePlayer).sendMessage("world");
}
@Test
public void testExecute20() throws Exception {
final AtomicBoolean bool = new AtomicBoolean(false);
String script
= "msg('hello') 'world'";
MethodScriptCompiler.execute(MethodScriptCompiler.compile(MethodScriptCompiler.lex(script, null, null, true), null, envs), env, new MethodScriptComplete() {
@Override
public void done(String output) {
assertEquals("world", output.trim());
bool.set(true);
}
}, null);
verify(fakePlayer).sendMessage("hello");
assertTrue(bool.get());
}
@Test
public void testExecute21() throws Exception {
String script = "#*\n"
+ "msg('Not a comment')\n"
+ "#*#";
SRun(script, fakePlayer);
verify(fakePlayer).sendMessage("Not a comment");
}
@Test
public void testExecute22() throws Exception {
String script = "/* Empty script */";
SRun(script, fakePlayer);
}
@Test
public void testCompile1() {
try {
String config = "/cmd [$p] $q = msg('')";
MethodScriptCompiler.preprocess(MethodScriptCompiler.lex(config, null, null, false), envs).get(0).compile(env);
fail("Test passed, but wasn't supposed to");
} catch (AbstractCompileException ex) {
//Passed
}
}
// @Test
// public void testCompileWithDoubleSlashCommand() throws Exception{
// AliasCore ac = mock(AliasCore.class);
// ac.autoIncludes = new ArrayList<File>();
// PowerMockito.mockStatic(CommandHelperPlugin.class);
// when(CommandHelperPlugin.getCore()).thenReturn(ac);
// assertEquals(ac, CommandHelperPlugin.getCore());
// String config = "//cmd blah = msg('success')";
// Script s = MethodScriptCompiler.preprocess(MethodScriptCompiler.lex(config, null, false)).get(0);
// s.compile(env);
// env.getEnv(CommandHelperEnvironment.class).SetPlayer(fakePlayer);
// s.run(Arrays.asList(new Variable("$var", "hello", Target.UNKNOWN)), env, null);
// verify(fakePlayer).sendMessage("success");
// }
//
// @Test
// public void testCompileTwoAliases() throws Exception{
// AliasCore ac = mock(AliasCore.class);
// ac.autoIncludes = new ArrayList<File>();
// PowerMockito.mockStatic(CommandHelperPlugin.class);
// when(CommandHelperPlugin.getCore()).thenReturn(ac);
// assertEquals(ac, CommandHelperPlugin.getCore());
// String config = "/cmd1 = msg('success')\n"
// + " \n" //Spaces and tabs are here
// + "/cmd2 = msg('success')";
// env.getEnv(CommandHelperEnvironment.class).SetPlayer(fakePlayer);
// Script s1 = MethodScriptCompiler.preprocess(MethodScriptCompiler.lex(config, null, false)).get(0);
// s1.compile(env);
// s1.run(Arrays.asList(new Variable("$var", "hello", Target.UNKNOWN)), env, null);
// Script s2 = MethodScriptCompiler.preprocess(MethodScriptCompiler.lex(config, null, false)).get(1);
// s2.compile(env);
// s2.run(Arrays.asList(new Variable("$var", "hello", Target.UNKNOWN)), env, null);
// verify(fakePlayer, times(2)).sendMessage("success");
// }
@Test
public void testCompile2() {
try {
String config = "/cmd [$p=player()] = msg('')";
MethodScriptCompiler.preprocess(MethodScriptCompiler.lex(config, null, null, false), envs).get(0).compile(env);
fail("Test passed, but wasn't supposed to");
} catch (AbstractCompileException ex) {
//Passed
}
}
@Test
public void testCompile4() throws Exception {
String config = "/cmd = >>>\n"
+ "msg('hello') #This is a comment too invalid()'\"'' function\n"
+ "<<<";
MethodScriptCompiler.preprocess(MethodScriptCompiler.lex(config, null, null, false), envs);
}
@Test
public void testCompile5() throws Exception {
String config = "label:/cmd = >>>\n"
+ "msg('hello') #This is a comment too invalid()'\"'' function\n"
+ "<<<";
Script s = MethodScriptCompiler.preprocess(MethodScriptCompiler.lex(config, null, null, false), envs).get(0);
s.compile(env);
assertEquals("label", s.getLabel());
}
@Test
public void testCompile6() throws Exception {
String config = "/cmd = >>>\n"
+ "msg(trim('hello' 'world \\\\ \\' \\n'))"
+ "<<<";
RunCommand(config, fakePlayer, "/cmd");
verify(fakePlayer).sendMessage("hello world \\ '");
}
@Test
public void testCompile7() throws Exception {
String config = "/cmd = >>>\n"
+ "msg('hello') \\ msg('world')"
+ "<<<";
RunCommand(config, fakePlayer, "/cmd");
verify(fakePlayer).sendMessage("hello");
verify(fakePlayer).sendMessage("world");
}
//TODO: Make these tests possible
// @Test
// public void testCompile8() throws Exception {
// String config = "/cmd $one $ = >>>\n"
// + "msg($one) \\ msg($)"
// + "<<<";
//
// RunVars(Arrays.asList(new Variable[]{new Variable("$one", "first", false, false, Target.UNKNOWN),
// new Variable("$", "several variables", false, true, Target.UNKNOWN)}), config, fakePlayer);
// Script s = MethodScriptCompiler.preprocess(MethodScriptCompiler.lex(config, null), env).get(0);
// s.compile(env);
// s.run(Arrays.asList(new Variable[]{new Variable("$one", "first", false, false, Target.UNKNOWN),
// new Variable("$", "several variables", false, true, Target.UNKNOWN)}), env, null);
// verify(fakePlayer).sendMessage("first");
// verify(fakePlayer).sendMessage("several variables");
// }
//
// @Test
// public void testCompile9() throws Exception {
// String config = "/test [$var=1] = >>>\n"
// + "msg($var)"
// + "<<<";
// Script s = MethodScriptCompiler.preprocess(MethodScriptCompiler.lex(config, null), env).get(0);
// s.compile(env);
// assertTrue(s.match("/test 2"));
// s.run(Arrays.asList(new Variable[]{new Variable("$var", "2", true, false, Target.UNKNOWN)}), env, null);
// verify(fakePlayer).sendMessage("2");
// assertTrue(s.match("/test"));
// s.run(new ArrayList<Variable>(), env, null);
// verify(fakePlayer).sendMessage("1");
// }
@Test
public void testCompile10() throws Exception {
String config = "/test $var = >>>\n"
+ "msg($var)"
+ "<<<";
Script s = MethodScriptCompiler.preprocess(MethodScriptCompiler.lex(config, null, null, false), envs).get(0);
s.compile(env);
assertTrue(s.match("/test 2"));
assertFalse(s.match("/test"));
s.run(Arrays.asList(new Variable[]{new Variable("$var", "2", true, false, Target.UNKNOWN)}), env, null);
}
//TODO: Make this test possible
// @Test public void testCompile11() throws Exception{
//
// CommandHelperPlugin.perms = mock(PermissionsResolverManager.class);
// when(CommandHelperPlugin.perms.hasPermission(fakePlayer.getVariableName(), "ch.alias.safe")).thenReturn(true);
// CommandHelperPlugin.myServer = fakeServer;
// when(fakeServer.getOnlinePlayers()).thenReturn(new MCPlayer[]{fakePlayer});
// String config = "safe:/test $var = >>>\n"
// + "all_players()\n"
// + "msg($var)\n"
// + "<<<";
// Script s = MethodScriptCompiler.preprocess(MethodScriptCompiler.lex(config, null), env).get(0);
// s.compile(env);
// assertEquals("safe", s.getLabel());
// assertTrue(s.match("/test 2"));
// s.run(Arrays.asList(new Variable[]{new Variable("$var", "2", true, false, Target.UNKNOWN)}), env, null);
// verify(fakePlayer).sendMessage("2");
// verify(CommandHelperPlugin.perms).hasPermission(fakePlayer.getVariableName(), "ch.alias.safe");
// }
@Test
public void testCompile12() throws Exception {
String config = "/*/one = bad()*/\n"
+ "/two = msg('Good')\n";
Script s = MethodScriptCompiler.preprocess(MethodScriptCompiler.lex(config, null, null, false), envs).get(0);
s.compile(env);
assertFalse(s.match("/one"));
assertTrue(s.match("/two"));
}
@Test
public void testSmartCommentBeforeCommand() throws Exception {
String config = "/** sc */\n/cmd = msg('');";
Script s = MethodScriptCompiler.preprocess(MethodScriptCompiler.lex(config, null, null, false), envs).get(0);
s.compile(env);
assertTrue(s.match("/cmd"));
}
@Test
public void testUnicode() throws Exception {
SRun("msg('\\u0037 is win!')", fakePlayer);
verify(fakePlayer).sendMessage("7 is win!");
SRun("msg('\\u20ac')", fakePlayer);
verify(fakePlayer).sendMessage("€");
}
@Test
public void testInfixMath1() throws Exception {
assertEquals("4", SRun("2 + 2", fakePlayer));
assertEquals("4", SRun("8 - 4", fakePlayer));
assertEquals("4", SRun("2 * 2", fakePlayer));
assertEquals("4", SRun("16/4", fakePlayer));
assertEquals("4.0", SRun("-0.5 + 4.5", fakePlayer));
}
@Test
public void testInfixMath2() throws Exception {
assertEquals("2", SRun("-2 + 2 + 2", fakePlayer));
assertEquals("18", SRun("(2 + 4) * 3", fakePlayer));
assertEquals("14", SRun("2 + 4 * 3", fakePlayer));
}
@Test
public void testInfixMath3() throws Exception {
assertEquals("8.0", SRun("2 ** 3", fakePlayer));
}
@Test
public void testUnary() throws Exception {
assertEquals("1", SRun("2 + - 1", fakePlayer));
}
@Test
public void testSymbolicConcat() throws Exception {
SRun("@hello = 'hello'\n"
+ "@world = 'world'\n"
+ "msg(@hello.@world)", fakePlayer);
verify(fakePlayer).sendMessage("helloworld");
}
@Test
public void testSymbolicLogic1() throws Exception {
assertEquals("true", SRun("2 <= 5", fakePlayer));
assertEquals("false", SRun("2 === '2'", fakePlayer));
assertEquals("true", SRun("g(assign(@var, false)) !@var", fakePlayer));
}
@Test
public void testSymbolicLogic2() throws Exception {
assertEquals("true", SRun("true || true", fakePlayer));
assertEquals("false", SRun("true && false", fakePlayer));
}
@Test
public void testComplexSymbolicLogic() throws Exception {
assertEquals("true", SRun("2 == 2 && true", fakePlayer));
}
@Test
public void testSymbolCompileError() throws Exception {
try {
SRun("(+ * 2)", fakePlayer);
fail("Did not expect test to pass");
} catch (ConfigCompileException | ConfigRuntimeException e) {
//pass
}
//pass?
}
@Test
public void testComplexSymbols() throws Exception {
SRun("assign(@var, 2) if((@var + 2) == 4, msg('Success!'))", fakePlayer);
verify(fakePlayer).sendMessage("Success!");
}
@Test
public void testPostfix() throws Exception {
SRun("assign(@var, 2) msg(@var++) msg(@var)", fakePlayer);
verify(fakePlayer).sendMessage("2");
verify(fakePlayer).sendMessage("3");
}
@Test
public void testPrefix() throws Exception {
SRun("assign(@var, 2) msg(++@var) msg(@var)", fakePlayer);
verify(fakePlayer, times(2)).sendMessage("3");
}
@Test
public void testModulo() throws Exception {
assertEquals(Integer.toString(2 % 3), SRun("2 % 3", fakePlayer));
}
@Test
public void TestOperationsWithFunction() throws Exception {
SRun("if(!and(false, false), msg('yes'))", fakePlayer);
verify(fakePlayer).sendMessage("yes");
}
@Test
public void testArrayBooleanType() throws Exception {
assertEquals("true", SRun("boolean(array(1))", null));
assertEquals("false", SRun("boolean(array())", null));
}
@Test(expected = AbstractCompileException.class)
public void testCompileErrorOfStaticConstructOptimization() throws Exception {
MethodScriptCompiler.compile(MethodScriptCompiler.lex("2 / 0", null, null, true), null, envs);
}
// If people complain that the old format is broken, I can see about re-adding this. For now,
// this is covered in the other exception handler test
// @Test public void testLineNumberCorrectInException1() throws Exception{
// String script =
// "try(\n" //Line 1
// + "assign(@a, array(1, 2))\n" //Line 2
// + "\n" //Line 3
// + "assign(@d, @a[2])\n" //Line 4
// + "\n" //Line 5
// + ", @e, msg(@e))\n"; //Line 6
// SRun(script, fakePlayer);
// verify(fakePlayer).sendMessage("4");
// }
@Test
public void testLineNumberCorrectInException2() throws Exception {
String script
= "assign(@a, array(1, 2))\n" //Line 1
+ "@b = null\n" //Line 2
+ "assign(@d, @a[@b])\n"; //Line 3
try {
SRun(script, fakePlayer);
} catch (ConfigRuntimeException e) {
assertEquals(3, e.getTarget().line());
}
}
@Test
public void testLineNumberCorrectInException3() throws Exception {
String script
= "\n"
+ "assign(@a, array('aaa', 'bbb'))\n"
+ "assign(@b, 'test')\n"
+ "msg('test1')\n"
+ "assign(@d, @a[@b])\n"
+ "msg('test2')\n";
try {
SRun(script, fakePlayer);
} catch (ConfigRuntimeException e) {
assertEquals(5, e.getTarget().line());
}
}
@Test
public void testExtraParenthesis() throws Exception {
try {
SRun("\n"
+ "\n"
+ "player())\n", fakePlayer);
} catch (ConfigCompileException e) {
assertEquals("3", e.getLineNum());
}
}
@Test(expected = AbstractCompileException.class)
public void testSpuriousSymbols() throws Exception {
SRun("2 +", fakePlayer);
}
@Test
public void testBraceIf() throws Exception {
SRun("if(true)\n\n"
+ "{\n"
+ "msg('success!')\n"
+ "}", fakePlayer);
verify(fakePlayer).sendMessage("success!");
}
@Test
public void testBraceElseIfElse() throws Exception {
SRun("if(false){"
+ "msg('fail')"
+ "} else if(true == true){"
+ "msg('success!')"
+ "} else {\n"
+ "msg('fail')"
+ "}", fakePlayer);
verify(fakePlayer).sendMessage("success!");
}
@Test
public void testBraceElseIfElseWithElseCondTrue() throws Exception {
SRun("if(false){"
+ "msg('fail')"
+ "} else if(false){"
+ "msg('fail')"
+ "} else {"
+ "msg('success!')"
+ "}", fakePlayer);
verify(fakePlayer).sendMessage("success!");
}
@Test(expected = AbstractCompileException.class)
public void testFailureOfBraces() throws Exception {
SRun("and(1){ 1 }", fakePlayer);
}
@Test(expected = AbstractCompileException.class)
public void testInnerElseInElseIf() throws Exception {
SRun("if(true){"
+ "msg('fail')"
+ "} else {"
+ "msg('fail')"
+ "} else if(true){"
+ "msg('fail')"
+ "}", fakePlayer);
}
@Test
public void testBracketsOnFor() throws Exception {
SRun("for(assign(@i, 0), @i < 5, @i++){\n"
+ "msg('worked')\n"
+ "}", fakePlayer);
verify(fakePlayer, times(5)).sendMessage("worked");
}
@Test
public void testBracketsOnForeach() throws Exception {
SRun("foreach(range(2), @i){\n"
+ "msg(@i)\n"
+ "}", fakePlayer);
verify(fakePlayer).sendMessage("0");
verify(fakePlayer).sendMessage("1");
}
@Test
public void testWhitespaceInBetweenFunctionAndParen() throws Exception {
SRun("msg ('hi')", fakePlayer);
verify(fakePlayer).sendMessage("hi");
}
@Test
public void testMultipleFunctionsWithBraces() throws Exception {
SRun("if(dyn(false)){\n"
+ "msg('nope')\n"
+ "} else {\n"
+ " msg('hi')\n"
+ " msg('hi')\n"
+ "}", fakePlayer);
verify(fakePlayer, times(2)).sendMessage("hi");
}
@Test
public void testArrayGetCatchesInvalidParameter() throws Exception {
try {
try {
SRun("1[4]", null);
fail("Did not expect test to pass");
} catch (ConfigCompileException e) {
//Pass
}
try {
SRun("'string'['index']", null);
fail("Did not expect test to pass");
} catch (ConfigCompileException e) {
//Pass
}
} catch (ConfigRuntimeException e) {
fail("Expecting a compile error here, not a runtime exception");
}
}
@Test
public void testBlockComments1() throws Exception {
SRun("/*\n"
+ " * Herp\n"
+ " * Derp\n"
+ " */\n"
+ "msg('hi');", fakePlayer);
}
@Test
public void testCommentsInStrings() throws Exception {
SRun("'#'", fakePlayer);
}
@Test
public void testCommentsInStrings2() throws Exception {
SRun("'/*'", fakePlayer);
}
@Test
public void testDoubleQuotesInSingleQuotes() throws Exception {
SRun("'This \"should work\" correctly, and not throw an exception'", null);
}
@Test
public void testClosureToString1() throws Exception {
SRun("msg(closure(msg('')))", fakePlayer);
verify(fakePlayer).sendMessage("msg('')");
}
@Test
public void testClosureToString2() throws Exception {
SRun("msg(closure(msg('\\n')))", fakePlayer);
verify(fakePlayer).sendMessage("msg('\\n')");
}
@Test
public void testClosureToString3() throws Exception {
SRun("msg(closure(msg('\\\\')))", fakePlayer);
verify(fakePlayer).sendMessage("msg('\\\\')");
}
@Test
public void testClosureToString4() throws Exception {
SRun("msg(closure(msg('\\'')))", fakePlayer);
verify(fakePlayer).sendMessage("msg('\\'')");
}
@Test
public void testCSlices() throws Exception {
assertEquals("-1", SRun("p(-1..-3[0])", null));
assertEquals("-2", SRun("p(-1..-3[1])", null));
assertEquals("-3", SRun("p(-1..-3[2])", null));
assertEquals("0", SRun("p(0..2[0])", null));
assertEquals("1", SRun("p(0..2[1])", null));
assertEquals("2", SRun("p(0..2[2])", null));
assertEquals("5", SRun("p(5..3[0])", null));
assertEquals("4", SRun("p(5..3[1])", null));
assertEquals("3", SRun("p(5..3[2])", null));
assertEquals("1", SRun("p(1..3[0])", null));
assertEquals("2", SRun("p(1..3[1])", null));
assertEquals("3", SRun("p(1..3[2])", null));
try {
SRun("1..2[10]", null);
fail("Expected an exception");
} catch (ConfigRuntimeException e) {
//pass
}
assertEquals("2", SRun("array_size(1..2)", null));
assertEquals("true", SRun("array_contains(1..2, 1)", null));
assertEquals("false", SRun("array_contains(1..2, 3)", null));
assertEquals("true", SRun("array_contains(-1..-2, -2)", null));
assertEquals("false", SRun("array_contains(-1..-2, -3)", null));
assertEquals("true", SRun("array_index_exists(1..2, 0)", null));
assertEquals("false", SRun("array_index_exists(1..2, 2)", null));
}
@Test
public void testWhitespaceAroundSymbol1() throws Exception {
assertEquals("true", SRun("false == false", null));
}
//This is accounted for in the new compiler branch, and will not be fixed in this branch.
// @Test public void testWhitespaceAroundSymbol2() throws Exception{
// assertEquals("true", SRun("false==false", null));
// }
//TODO: Once the lexer is rewritten, this should work
// @Test
// public void testAssignmentWithEquals1() throws Exception{
// SRun("@var=yep nope msg(@var)", fakePlayer);
// verify(fakePlayer).sendMessage("yep");
// }
//
// @Test
// public void testAssignmentWithEquals2() throws Exception{
// SRun("@var = yep nope msg(@var)", fakePlayer);
// verify(fakePlayer).sendMessage("yep");
// }
//
// @Test
// public void testAssignmentWithEquals3() throws Exception{
// SRun("@var = 'yep yep' msg(@var)", fakePlayer);
// verify(fakePlayer).sendMessage("yep yep");