Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

551
LINES

< > BotCompany Repo | #1033988 // test_leftArrowScript

JavaX fragment (include) [tags: use-pretranspiled]

Uses 679K of libraries. Click here for Pure Java version (36402L/183K).

1  
!include once #1034831 // Gazelle 22 Function Include for Testing
2  
3  
set flag PingV3.
4  
5  
scope test_leftArrowScript
6  
7  
sclass #ClassA {}
8  
sclass #ClassB extends ClassA {}
9  
sclass #ClassC extends ClassB {}
10  
11  
sclass #StaticField {
12  
  sS theField;
13  
}
14  
15  
sclass #InheritanceDispatchTest {
16  
  static S f(ClassA a) { ret "a"; }
17  
  static S f(ClassB a) { ret "b"; }
18  
}
19  
20  
sclass #Tester {
21  
  O value = "123";
22  
  sO myStaticField = "ok";
23  
}
24  
25  
sclass #Sub1 {
26  
  sclass X { static int value = 1; }
27  
}
28  
29  
sclass #Sub2 {
30  
  sclass X { static int value = 2; }
31  
}
32  
33  
svoid test_leftArrowScript() {
34  
  test_leftArrowScript_pinging();
35  
  
36  
  embedded S countLoop(int n) {
37  
    ret [[ i <- 0; while lessThan i ]] + n + [[ { i <- plus i 1 }; i ]];
38  
  }
39  
40  
  S ifThenElseScript = [[
41  
    if lessThan a b { joinWithSpace "It's" "less" }
42  
    else if greaterThan a b { joinWithSpace "It's" "greater" }
43  
    else { joinWithSpace "They're" "equal" }
44  
  ]];
45  
  
46  
  testFunctionValues(script -> leftArrowVerbose((S) script),
47  
    // int primitive
48  
    
49  
    [[ 5 ]], 5,
50  
    [[ -5 ]], -5,
51  
    
52  
    // function definition & call
53  
    [[
54  
      def double x {
55  
        Math multiplyExact x 2
56  
      }
57  
      
58  
      double 10
59  
    ]], 20,
60  
  
61  
    // new object with constructor arguments
62  
    [[ new Pair "hello" "world" ]], pair("hello", "world"),
63  
    
64  
    // double constant
65  
    [[ str 1.5e2 ]], "150.0",
66  
    
67  
    // float constant
68  
    [[ 1.5e2f ]], 150f,
69  
    [[ 1.5e2F ]], 150f,
70  
    
71  
    // long constant
72  
    [[ 0L ]], 0L,
73  
    
74  
    // hex constant
75  
    [[ 0x80 ]], 0x80,
76  
    
77  
    // binary constant
78  
    [[ 0B101 ]], 0B101,
79  
    
80  
    // character constant
81  
    [[ '\t' ]], '\t',
82  
    
83  
    // [not implemented, too complicated]
84  
    // last = result of last statement
85  
    // [[ plus 1 2; Math addExact 5 last ]], 8,
86  
    
87  
    // get static field
88  
    [[ Color black ]], Color.black,
89  
    
90  
    // get instance field,
91  
    [[ tester <- new test_leftArrowScript_Tester; tester value ]], "123",
92  
    
93  
    // while loop
94  
    countLoop(5), 5,
95  
    
96  
    // if statement
97  
    [[ if lessThan 0 1 { "ok" } ]], "ok",
98  
    [[ if lessThan 1 0 { "ok" } ]], null,
99  
    
100  
    // for each
101  
    [[ l <- ll 2 3
102  
       sum <- 0
103  
       for number in l { sum <- plus sum number }
104  
       sum
105  
    ]], 5,
106  
    
107  
    // for each is also map()
108  
    [[ for i in ll 2 3 { plus i 1 }
109  
    ]], ll(3, 4),
110  
    
111  
    // for each as nested expression
112  
    [[ joinWithSpace (for i in ll 2 3 { i }) ]], "2 3",
113  
    
114  
    // for i to n
115  
    [[ for i to 2 { i }
116  
    ]], ll(0, 1),
117  
    
118  
    // for with byte array
119  
    [[ for b in litbytearray (toByte 5) (toByte 7) { b } ]], ll((byte) 5, (byte) 7),
120  
    
121  
    // for with double array
122  
    [[ for b in litdoublearray 5.0 7.0 { b } ]], ll(5.0, 7.0),
123  
    
124  
    // some bug
125  
    [[
126  
      img <- newImage 10 10
127  
      r <- randomRect img 10 10
128  
    ]], rect(0, 0, 10, 10),
129  
130  
    // another bug
131  
    [[
132  
      a <- 1
133  
      b <- a
134  
      a
135  
    ]], 1,
136  
    
137  
    // null bug
138  
    [[
139  
      a <- 1
140  
      a <- null
141  
      a
142  
    ]], null,
143  
    
144  
    // return at the end
145  
    [[
146  
      return 17
147  
    ]], 17,
148  
    
149  
    // two returns (first wins)
150  
    [[
151  
      return 13
152  
      return 22
153  
    ]], 13,
154  
    
155  
    // same with "ret" for "return"
156  
    [[
157  
      ret 13
158  
      ret 22
159  
    ]], 13,
160  
    
161  
    // conditional return
162  
    
163  
    [[
164  
      if true { return "so true" }
165  
      return "oops"
166  
    ]], "so true",
167  
    
168  
    [[
169  
      if false { return "not true" }
170  
      return "but this"
171  
    ]], "but this",
172  
    
173  
    // if-then-else
174  
    
175  
    [[ a <- 1; b <- 2; ]] + ifThenElseScript, "It's less",
176  
    [[ a <- 6; b <- 2; ]] + ifThenElseScript, "It's greater",
177  
    [[ a <- 5.0; b <- 5.0; ]] + ifThenElseScript, "They're equal",
178  
    
179  
    // if-then-else with "then" keyword but without curly braces
180  
    [[ if true then "ok" else "not ok" ]], "ok",
181  
    [[ if false then "ok" else "not ok" ]], "not ok",
182  
    
183  
    // nested expressions
184  
    
185  
    [[ plus (plus 1 2) 4 ]], 7,
186  
    [[ (plus 5 6) toString ]], "11",
187  
    
188  
    // nested expressions over 2 lines (it's ok to split an expression
189  
    // over multiple lines if you at least parenthesis is still open)
190  
    
191  
    [[ (plus 1
192  
         (plus 2 3)) ]], 6,
193  
    
194  
    // another case for nested expressions     
195  
    [[ str (Color black) ]], str(Color.black),
196  
    
197  
    // bug with nested for + new + linebreak + for (phew)
198  
    [[ (for y in ll 1 {
199  
         new Pair 1 2
200  
         for x in null {}
201  
       }); 0 ]], 0,
202  
       
203  
    // param declares a variable
204  
    [[ param x; x ]], null,
205  
    
206  
    // same with type
207  
    [[ param x : int; x ]], null,
208  
    
209  
    // return with assignment
210  
    [[ return x <- 1 ]], 1,
211  
    
212  
    // for pair
213  
    [[ for pair a b in ll (pair 1 2) (pair 3 4) {
214  
      plus a b
215  
    } ]], ll(3, 7),
216  
    
217  
    // for key, value (map)
218  
    [[ for a, b in litorderedmap 1 2 3 4 {
219  
      plus a b
220  
    } ]], ll(3, 7),
221  
    
222  
    // for index, value (list)
223  
    [[ for index i, a in ll "a" "b" {
224  
      concatStrings i a
225  
    } ]], ll("0a", "1b"),
226  
    
227  
    // automatically convert int to long for method call
228  
    [[ longPlus 1 2 ]], 3L,
229  
    
230  
    // same for constructors
231  
    [[ new Ratio 1 ]], Ratio(1),
232  
    
233  
    // call a function from another function
234  
    [[ def f1 { 1 }
235  
       def f2 { plus (f1) 2 }
236  
       f2 ]], 3,
237  
    
238  
     // return out of for loop
239  
     [[ for x in iota 2 { return x } ]], 1,
240  
     
241  
     // continue loop
242  
     [[ for x in iota 2 {
243  
          if eq x 1 { continue }
244  
          return "ok"
245  
        } ]], "ok",
246  
     
247  
     // break loop (TODO)
248  
     /*[[ for x in iota 2 {
249  
          if eq x 1 { break }
250  
          return "what"
251  
        }
252  
        "ok"
253  
      ]], "ok",*/
254  
     
255  
     // new without new
256  
     [[ Pair (neg 1) (neg 2) ]], pair(-1, -2),
257  
     
258  
     // variable doesn't come into scope until it's initialized
259  
     [[ neg <- neg 1; neg ]], -1,
260  
     
261  
     // repeat x { ... } construct
262  
     [[ x <- 0
263  
        repeat plus 1 2 { x <- plus x 1 }
264  
        x ]], 3,
265  
        
266  
     // update outer variable from function
267  
     [[ x <- 0
268  
        def incIt { outer x <- plus x 1 }
269  
        incIt
270  
        x ]], 1,
271  
        
272  
      // return directly followed by }
273  
      [[ if true { return } ]], null,
274  
    
275  
      // return from lambda (exits only the lambda, just like in Java)
276  
      [[ repF 2 (IF0 -> { return 1; null }) ]], ll(1, 1),
277  
      
278  
      // "temp" (like in JavaX)
279  
      [[ list <- new ArrayList
280  
         {
281  
           temp tempAdd list 5
282  
           assertContains list 5
283  
         }
284  
         list
285  
      ]], ll(),
286  
     
287  
      // "temp" test 2 (assure it handles exceptions properly)
288  
      [[ list <- new ArrayList
289  
         pcallF (Runnable -> {
290  
           temp tempAdd list 5
291  
           fail
292  
         })
293  
         list
294  
      ]], ll(),
295  
      
296  
      // "temp" combined with variable declaration
297  
      [[ list <- new ArrayList
298  
         {
299  
           temp x <- tempAdd list 5
300  
           assertInstanceOf x AutoCloseable
301  
           assertContains list 5
302  
         }
303  
         list
304  
      ]], ll(),
305  
     
306  
      // reference to functionContainer class itself
307  
      [[ main ]], mc(),
308  
      
309  
      // referencing class with full package name
310  
      [[ java.util.Arrays sort (newFloatArray 1) ]], null,
311  
      
312  
      [[ new java.util.ArrayList ]], ll(),
313  
      
314  
      // call method on literal
315  
      [[ "" length ]], 0,
316  
      
317  
      // L or List to make a new list
318  
      [[ new List ]], ll(),
319  
      [[ new L ]], ll(),
320  
      
321  
      // Map to make a new HashMap
322  
      [[ new Map ]], new Map,
323  
      
324  
      // new with a variable containing a class
325  
      [[ c <- ArrayList; new c ]], ll(),
326  
      
327  
      [[ new Var<S>, getClass ]], Var.class,
328  
      
329  
      shortName(InheritanceDispatchTest) + " f (new " + shortName(ClassC) + ")", "b",
330  
      
331  
      [[
332  
        sleepSeconds 0
333  
        sleepSeconds 0
334  
      ]], null,
335  
      
336  
      // variable declaration with type
337  
      
338  
      [[ a : S <- "s"; a ]], "s",
339  
      
340  
      // try/finally
341  
      
342  
      [[
343  
        a <- 0
344  
        try {
345  
          try { a <- plus a 1 } finally { a <- plus a 2 }
346  
          try { fail } finally { a <- plus a 3 }
347  
        } catch e {}
348  
        a
349  
      ]], 6,
350  
      
351  
      // function definition with explicit parameter type
352  
      [[
353  
        def f x: int {
354  
          plus x 1
355  
        }
356  
        f 5
357  
      ]], 6,
358  
      
359  
      // same with parameter in parentheses
360  
      [[
361  
        def f (x: int) {
362  
          plus x 1
363  
        }
364  
        f 5
365  
      ]], 6,
366  
      
367  
      // param with nested parameterized type
368  
      [[
369  
        param x : L<Pair<Int, Int>>
370  
      ]], null,
371  
      
372  
      // +id shortcut
373  
      [[ x <- 5; ll +x ]], ll(x := 5),
374  
      
375  
      // unclosed multi-line string
376  
      "splitAtSpace [[a b", ll("a", "b"),
377  
      "splitAtSpace [=[c d", ll("c", "d"),
378  
      
379  
      // list { }
380  
      [[ list {
381  
        plus 1 2
382  
        minus 8 4
383  
      } ]], ll(3, 4),
384  
      
385  
      // same in one line with semicolons
386  
      [[ list { plus 1 2; minus 8 4 } ]], ll(3, 4),
387  
      
388  
      // parsing bug
389  
      [[ for x in ll
390  
           1 { x } ]], ll(1),
391  
           
392  
      // will return
393  
      [[ data <- new Var
394  
         will return data get
395  
         data set 1 ]], 1,
396  
         
397  
      // recursive script-defined function
398  
      [[ def myFactorial n {
399  
           if n lessThan 2
400  
             { 1 }
401  
             else { myFactorial (n minus 1), mul n }
402  
         }
403  
         myFactorial 5
404  
      ]], 120,
405  
         
406  
      // for each closes the iterator if it's closeable
407  
      [[
408  
        iterator <- new TestCloseableIterableIterator
409  
        closed <- new Flag
410  
        iterator onClosing closed
411  
        for i in iterator {}
412  
        closed isUp
413  
      ]], true,
414  
    
415  
      // same test with exception in loop
416  
      [[
417  
        iterator <- new TestCloseableIterableIterator
418  
        closed <- new Flag
419  
        iterator onClosing closed
420  
        try {
421  
          for i in iterator { fail }
422  
        } catch e {}
423  
        closed isUp
424  
      ]], true,
425  
      
426  
      // make sure my own classes are found before Java SDK classes
427  
      [[ new Timestamp 1000, unixDate ]],
428  
      1000L,
429  
      
430  
      // TO FIX
431  
      // TODO [[ max 1000L 100 ]], 1000L,
432  
      
433  
      // couldn't fix this quickly (have ~ binding stronger)
434  
      //[[ map <- litmap "a" 1 "b" 2; plus map~a map~b ]], 3,
435  
      
436  
      // question mark syntax for calling methods on null targets
437  
      [[ a <- null; a intValue? ]], null,
438  
      [[ a <- 5L; a intValue? ]], 5,
439  
      
440  
      // long constants without L
441  
      [[ 1234567812345678 ]], 1234567812345678,
442  
      
443  
      // Duration is a standard import
444  
      [[ d: Duration <- daysToDuration 1 ]],
445  
      daysToDuration(1),
446  
      
447  
    // add simple tests here
448  
  );
449  
  
450  
  // get reference to current var context with _context
451  
  
452  
  new GazelleV_LeftArrowScriptParser parser;
453  
  enableScaffolding(parser);
454  
  parser.allowTheWorld();
455  
  new FlexibleVarContext ctx;
456  
  assertSame(ctx, parser.parse("_context").get(ctx));
457  
  
458  
  test_leftArrowScriptParseErrors();
459  
  
460  
  test_leftArrowScript_lambdas();
461  
  
462  
  test_leftArrowScript_binaryOperators();
463  
  
464  
  test_leftArrowScriptCompileErrors();
465  
466  
  // get static field in functionContainer
467  
  
468  
  parser = new GazelleV_LeftArrowScriptParser;
469  
  parser.allowTheWorld(Tester, mc());
470  
  assertEqualsVerbose("ok", parser.parse("myStaticField")!);
471  
  assertEqualsVerbose("ko", parser.parse("reversed myStaticField")!);
472  
  assertEqualsVerbose(2, parser.parse("myStaticField length")!);
473  
  
474  
  test_leftArrowScript_classDefs();
475  
  
476  
  test_leftArrowScript_forIterator();
477  
478  
  var exception = leftArrowVerbose([[
479  
    try { throw new RuntimeException } catch e { e }
480  
  ]]);
481  
  assertEqualsVerbose(exception.getClass(), RuntimeException.class);
482  
  
483  
  // trying to get non-existant field/method on object
484  
  assertFailVerbose(-> leftArrowVerbose([[ "" lengthX ]]));
485  
  
486  
  // make sure fields local to function are not visible outside
487  
  assertFailVerbose(-> leftArrowParse([[ def x { y <- 1 } y ]]));
488  
  
489  
  test_leftArrowScript_comma();
490  
  test_leftArrowScript_antiComma();
491  
  
492  
  // test priority of class resolution
493  
  parser = new GazelleV_LeftArrowScriptParser;
494  
  enableScaffolding(parser);
495  
  parser.allowTheWorld(Sub1.class, Sub2.class);
496  
  assertEqualsVerbose(1, parser.parse("X value")!);
497  
  parser = new GazelleV_LeftArrowScriptParser;
498  
  enableScaffolding(parser);
499  
  parser.allowTheWorld(Sub2.class, Sub1.class);
500  
  assertEqualsVerbose(2, parser.parse("X value")!);
501  
  
502  
  test_leftArrowScript_returnValueNotNeededOptimization();
503  
  
504  
  // test addClassAlias
505  
  
506  
  parser = new GazelleV_LeftArrowScriptParser;
507  
  parser.allowTheWorld();
508  
  parser.addClassAlias("Paar", "Pair");
509  
  assertEqualsVerbose(pair(1, 2), parser.parse("new Paar 1 2")!);
510  
  
511  
  // test that params are stored in parsed script
512  
  
513  
  var script = leftArrowParseVerbose("param x : int");
514  
  assertEqualsVerbose(1, l(script.params));
515  
  assertEqualsVerbose("x", firstKey(script.params));
516  
  assertEqualsVerbose(int.class, firstValue(script.params).javaClass());
517  
518  
  // +id shortcut doesn't work with a space
519  
  assertFailVerbose(-> leftArrowParseVerbose([[ x <- 5; ll + x ]]));
520  
  
521  
  test_leftArrowScript_magicSwitch();
522  
  
523  
  test_leftArrowScript_tilde();
524  
  
525  
  test_leftArrowScript_then();
526  
  
527  
  assertInstanceOf IF0(leftArrowVerbose([[ abcd <- IF0 {}; abcd ]]));
528  
  
529  
  // Test def ... returning a FunctionDef
530  
  O o = leftArrow([[def bla { null } ]]);
531  
  assertEqualsVerbose("bla", ((GazelleV_LeftArrowScript.FunctionDef) o).name);
532  
533  
  o = leftArrow([[a <- def bla { null }; a]]);
534  
  assertEqualsVerbose("bla", ((GazelleV_LeftArrowScript.FunctionDef) o).name);
535  
  
536  
  test_leftArrowScript_pcall();
537  
  
538  
  test_leftArrowScript_ifdef();
539  
  
540  
  //test_leftArrowScript_var();
541  
  
542  
  // assignment to static field
543  
  leftArrowVerbose(replace(
544  
    [[ StaticField theField <- "hello"
545  
  ]], "StaticField", className(StaticField)));
546  
  assertEqualsVerbose("hello", StaticField.theField);
547  
  
548  
  // add tests here
549  
  
550  
  print("All left-arrow script tests OK!");
551  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 5 computer(s): bhatertpkbcr, ekrmjmnbrukm, elmgxqgtpvxh, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1033988
Snippet name: test_leftArrowScript
Eternal ID of this version: #1033988/177
Text MD5: eb217d930ed83b4dedd80555bdd19363
Transpilation MD5: 068b5e4b01d96594713d2e967561da51
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-11-26 17:40:18
Source code size: 14165 bytes / 551 lines
Pitched / IR pitched: No / No
Views / Downloads: 423 / 2232
Version history: 176 change(s)
Referenced in: [show references]