RegressionTests__JavaScriptTests.st
changeset 733 0d9c441eba61
parent 732 450ab2d5cf8c
child 878 05ff46227bbf
--- a/RegressionTests__JavaScriptTests.st	Wed Oct 31 20:26:02 2012 +0100
+++ b/RegressionTests__JavaScriptTests.st	Thu Nov 01 12:09:48 2012 +0100
@@ -31,6 +31,16 @@
     "Created: / 12-04-2005 / 11:54:25 / cg"
 ! !
 
+!JavaScriptTests class methodsFor:'queries'!
+
+coveredClassNames
+    ^ #( 
+        JavaScriptParser 
+        JavaScriptScanner 
+        JavaScriptCompiler 
+        )
+! !
+
 !JavaScriptTests methodsFor:'helpers'!
 
 doTestEachFromSpec:spec 
@@ -412,6 +422,76 @@
     "Created: / 21-02-2007 / 13:05:48 / cg"
 !
 
+testAssignmentExpression01
+    self 
+        execute:'assign(a, b) {
+                    var x, y;
+
+                    x = y = 0;
+                    x = y = a;
+                    x = y = b;
+                    return (x + y);
+                 }'
+        for:nil
+        arguments:#(10 20)
+        expect:40
+
+    "
+     self run:#testAssignmentExpression01
+     self new testAssignmentExpression01
+    "
+!
+
+testAssignmentExpression02
+    "yes, in JS you can change an argument variable"
+
+    self 
+        execute:'assign(a, b) {
+                    var x, y;
+
+                    a = x = y = 0;
+                    return (a + b);
+                 }'
+        for:nil
+        arguments:#(10 20)
+        expect:20
+
+    "
+     self run:#testAssignmentExpression02
+     self new testAssignmentExpression02
+    "
+!
+
+testCommaExpression01
+    self 
+        execute:'comma(a, b, c) {
+                    return (a , b , c);
+                 }'
+        for:nil
+        arguments:#(10 20 30)
+        expect:30
+
+    "
+     self run:#testCommaExpression01
+     self new testCommaExpression01
+    "
+!
+
+testCommaExpression02
+    self 
+        execute:'comma(a, b, c) {
+                    return (a+10 , b+10 , c+10);
+                 }'
+        for:nil
+        arguments:#(10 20 30)
+        expect:40
+
+    "
+     self run:#testCommaExpression02
+     self new testCommaExpression02
+    "
+!
+
 testComments01
     self 
         execute:'
@@ -1020,18 +1100,56 @@
 !
 
 testForIn01
+    |output|
+
     JavaScriptParser forInAllowed ifFalse:[^ self].
 
+    output := self outputToTranscriptOf:[
+        self 
+            execute:'test(arg) {
+                        var arr = [ "one", "two", "three" ];
+
+                        for (var el in arr) {
+                            Transcript.showCR(el);
+                        }
+                     }'
+            for:nil
+            arguments:#(5)
+            expect:nil
+    ].
+
+    self assert:(output asCollectionOfLinesWithReturn asArray = #( 'one' 'two' 'three' ))
+
+    "
+     self run:#testForIn01
+     self new testForIn01  
+    "
+!
+
+testForIn02
+    |output|
+
+    JavaScriptParser forInAllowed ifFalse:[^ self].
+
+    output := '' writeStream.
     self 
         execute:'test(arg) {
-                    var arr = [ "one", "two", "three" ];
-                    for (var el in arr) {
-                        Transcript.showCR(el);
+                    var sum = 0;
+
+                    for (var el in arg) {
+                        out.showCR(el*el);
+                        sum += el;
                     }
                  }'
         for:nil
-        arguments:#(5)
-        expect:nil
+        arguments:( #(1 2 3) )
+        expect:6.
+
+    self assert:(output contents asCollectionOfLinesWithReturn asArray = #( 
+        '1'
+        '4'
+        '9'
+       ))
 
     "
      self run:#testForIn01
@@ -2277,6 +2395,48 @@
     "
 !
 
+testOperators27e_postInc
+    |arr|
+
+    arr := (1 to:15) asArray.
+
+    self 
+        execute:'expr(a) {
+                    return (a[10]++);
+                 }'
+        for:nil
+        arguments:{ arr }
+        expect:10.
+
+    self assert:(arr = #(1 2 3 4 5 6 7 8 9 11 11 12 13 14 15))
+
+    "
+     self run:#testOperators27e_postInc
+     self new testOperators27e_postInc
+    "
+!
+
+testOperators27f_preDec
+    |arr|
+
+    arr := (1 to:15) asArray.
+
+    self 
+        execute:'expr(a) {
+                    return (--a[10]);
+                 }'
+        for:nil
+        arguments:{ arr }
+        expect:9.
+
+    self assert:(arr = #(1 2 3 4 5 6 7 8 9 9 11 12 13 14 15))
+
+    "
+     self run:#testOperators27f_preDec
+     self new testOperators27f_preDec
+    "
+!
+
 testOperators28_not
     self 
         execute:'expr(a) {
@@ -3075,6 +3235,73 @@
     "
 !
 
+testParserErrors01
+    self should:[
+        self 
+            execute:'expr(a, ) {
+                        return (a == b);
+                     }'
+            for:nil
+            arguments:#(10 10)
+            expect:true
+    ] raise:Parser::ParseError.
+
+    self should:[
+        self 
+            execute:'expr(a b) {
+                        return (a == b);
+                     }'
+            for:nil
+            arguments:#(10 10)
+            expect:true
+    ] raise:Parser::ParseError.
+
+    self should:[
+        self 
+            execute:'expr(x) {
+                        return (x == 1 ?);
+                     }'
+            for:nil
+            arguments:#(10 10)
+            expect:true
+    ] raise:Parser::ParseError.
+
+    self should:[
+        self 
+            execute:'expr(x) {
+                        return (x == 1 ? 1 );
+                     }'
+            for:nil
+            arguments:#(10 10)
+            expect:true
+    ] raise:Parser::ParseError.
+
+    self should:[
+        self 
+            execute:'expr(x) {
+                        return (x == 1 ? 1 : );
+                     }'
+            for:nil
+            arguments:#(10 10)
+            expect:true
+    ] raise:Parser::ParseError.
+
+    self should:[
+        self 
+            execute:'expr(x) {
+                        return (x == 1 ? 1 : +);
+                     }'
+            for:nil
+            arguments:#(10 10)
+            expect:true
+    ] raise:Parser::ParseError.
+
+    "
+     self run:#testParserErrors01
+     self new testParserErrors01
+    "
+!
+
 testPrecidences01
     self 
         execute:'expr(a, b) {
@@ -3578,6 +3805,17 @@
          ' <<= '       #'<<='  
          ' === '       #'==='    
          ' !!== '       #'!!=='    
+
+         ' 1 '         #Integer    
+         ' 12345 '     #Integer    
+         ' 1.0 '       #Float    
+         ' 1e10 '      #Float    
+         ' 1E10 '      #Float    
+         ' 1E+10 '     #Float    
+         ' 1E-10 '     #Float    
+         ' 1.0d '      #Float    
+         ' 1. '        #Float    
+"/         ' 1.'         #Float    
     ) pairWiseDo:[:src :expectedToken |
         |scannedToken|
 
@@ -3713,17 +3951,22 @@
 !
 
 testSwitch02
-    self 
-        execute:'test(arg) {
-                    switch (arg) {
-                        Transcript.show("hello ");
-                        return 1;
-                    }
-                    return 0;
-                 }'
-        for:nil
-        arguments:#(5)
-        expect:0
+    |output|
+
+    output := self outputToTranscriptOf:[
+        self 
+            execute:'test(arg) {
+                        switch (arg) {
+                            Transcript.show("hello ");
+                            return 1;
+                        }
+                        return 0;
+                     }'
+            for:nil
+            arguments:#(5)
+            expect:0
+    ].
+    self assert:(output asCollectionOfLinesWithReturn asArray = #( ))
 
     "
      self run:#testSwitch02
@@ -3732,19 +3975,24 @@
 !
 
 testSwitch03
-    self 
-        execute:'test(arg) {
-                    switch (arg) {
-                        Transcript.show("hello ");
-                        Transcript.show("world ");
-                        Transcript.cr();
-                        return 1;
-                    }
-                    return 0;
-                 }'
-        for:nil
-        arguments:#(5)
-        expect:0
+    |output|
+
+    output := self outputToTranscriptOf:[
+        self 
+            execute:'test(arg) {
+                        switch (arg) {
+                            Transcript.show("hello ");
+                            Transcript.show("world ");
+                            Transcript.cr();
+                            return 1;
+                        }
+                        return 0;
+                     }'
+            for:nil
+            arguments:#(5)
+            expect:0
+    ].
+    self assert:(output asCollectionOfLinesWithReturn asArray = #()  )
 
     "
      self run:#testSwitch03
@@ -3807,21 +4055,26 @@
 !
 
 testSwitch06a
-    self 
-        execute:'test(arg) {
-                    switch (arg) {
-                    case 1:
-                        Transcript.showCR("one");
-                        return 10;
-                    case 2:
-                        Transcript.showCR("two");
-                        return 20;
-                    }
-                    return 0;
-                 }'
-        for:nil
-        arguments:#(5)
-        expect:0
+    |output|
+
+    output := self outputToTranscriptOf:[
+        self 
+            execute:'test(arg) {
+                        switch (arg) {
+                        case 1:
+                            Transcript.showCR("one");
+                            return 10;
+                        case 2:
+                            Transcript.showCR("two");
+                            return 20;
+                        }
+                        return 0;
+                     }'
+            for:nil
+            arguments:#(5)
+            expect:0
+    ].
+    self assert:(output asCollectionOfLinesWithReturn asArray = #( ))
 
     "
      self run:#testSwitch06a
@@ -3830,21 +4083,26 @@
 !
 
 testSwitch06b
-    self 
-        execute:'test(arg) {
-                    switch (arg) {
-                    case 1:
-                        Transcript.showCR("one");
-                        return 10;
-                    case 2:
-                        Transcript.showCR("two");
-                        return 20;
-                    }
-                    return 0;
-                 }'
-        for:nil
-        arguments:#(1)
-        expect:10
+    |output|
+
+    output := self outputToTranscriptOf:[
+        self 
+            execute:'test(arg) {
+                        switch (arg) {
+                        case 1:
+                            Transcript.showCR("one");
+                            return 10;
+                        case 2:
+                            Transcript.showCR("two");
+                            return 20;
+                        }
+                        return 0;
+                     }'
+            for:nil
+            arguments:#(1)
+            expect:10
+    ].
+    self assert:(output asCollectionOfLinesWithReturn asArray = #( 'one'))
 
     "
      self run:#testSwitch06b
@@ -3853,21 +4111,26 @@
 !
 
 testSwitch06c
-    self 
-        execute:'test(arg) {
-                    switch (arg) {
-                    case 1:
-                        Transcript.showCR("one");
-                        return 10;
-                    case 2:
-                        Transcript.showCR("two");
-                        return 20;
-                    }
-                    return 0;
-                 }'
-        for:nil
-        arguments:#(2)
-        expect:20
+    |output|
+
+    output := self outputToTranscriptOf:[
+        self 
+            execute:'test(arg) {
+                        switch (arg) {
+                        case 1:
+                            Transcript.showCR("one");
+                            return 10;
+                        case 2:
+                            Transcript.showCR("two");
+                            return 20;
+                        }
+                        return 0;
+                     }'
+            for:nil
+            arguments:#(2)
+            expect:20
+    ].
+    self assert:(output asCollectionOfLinesWithReturn asArray = #( 'two'))
 
     "
      self run:#testSwitch06c
@@ -3876,21 +4139,26 @@
 !
 
 testSwitch06d
-    self 
-        execute:'test(arg) {
-                    switch (arg) {
-                    case "hallo":
-                        Transcript.showCR("Hallo");
-                        return 10;
-                    case "hello":
-                        Transcript.showCR("Hello");
-                        return 20;
-                    }
-                    return 0;
-                 }'
-        for:nil
-        arguments:#('hello')
-        expect:20
+    |output|
+
+    output := self outputToTranscriptOf:[
+        self 
+            execute:'test(arg) {
+                        switch (arg) {
+                        case "hallo":
+                            Transcript.showCR("Hallo");
+                            return 10;
+                        case "hello":
+                            Transcript.showCR("Hello");
+                            return 20;
+                        }
+                        return 0;
+                     }'
+            for:nil
+            arguments:#('hello')
+            expect:20
+    ].
+    self assert:(output asCollectionOfLinesWithReturn asArray = #( 'Hello'))
 
     "
      self run:#testSwitch06d
@@ -3993,12 +4261,44 @@
     "
 !
 
-testTryCatch01a
+testThrow01
+    "an exception is thrown"
+
     self 
         execute:'test(arg) {
                     var handlerWasCalled = false;
 
-                    function failingMethod() {  return 10 / arg; };
+                    function failingMethod() {  
+                        throw Error;
+                    };
+
+                    try {
+                        failingMethod();
+                    } catch (Error e) {
+                        handlerWasCalled =  true;
+                    }
+                    return handlerWasCalled;
+                 }'
+        for:nil
+        arguments:#(0)
+        expect:true
+
+    "
+     self run:#testThrow01
+     self new testThrow01
+    "
+!
+
+testTryCatch01a
+    "in this testcase, arg is 5, so no exception should be thrown"
+
+    self 
+        execute:'test(arg) {
+                    var handlerWasCalled = false;
+
+                    function failingMethod() {  
+                        return 10 / arg; 
+                    };
 
                     try {
                         failingMethod();
@@ -4018,6 +4318,8 @@
 !
 
 testTryCatch01b
+    "in this testcase, arg is 0, so an exception should be thrown"
+
     self 
         execute:'test(arg) {
                     var handlerWasCalled = false;
@@ -4260,43 +4562,48 @@
 !
 
 testTryFinally02
-    self 
-        execute:'test(arg) {
-                    var handler1WasCalled = false;
-                    var handler2WasCalled = false;
-                    var finallyActionWasEvaluated = false;
-
-                    println("1");
-                    try {
-
-                        function dummy () {
-                            println("2a");
-                            try {
-                                println("2b");
-                                return 10 / arg;
-                            } finally {
-                                println("2c");
-                                handler1WasCalled = true;
-                            }
+    |output|
+
+    output := self outputToTranscriptOf:[
+        self 
+            execute:'test(arg) {
+                        var handler1WasCalled = false;
+                        var handler2WasCalled = false;
+                        var finallyActionWasEvaluated = false;
+
+                        println("1");
+                        try {
+
+                            function dummy () {
+                                println("2a");
+                                try {
+                                    println("2b");
+                                    return 10 / arg;
+                                } finally {
+                                    println("2c");
+                                    handler1WasCalled = true;
+                                }
+                            };
+
+                            println("2");
+                            dummy();
+                            println("3");
+                        } catch(Error e) {  
+                            println("e");
+                            handler2WasCalled =  true; 
+                        } finally {
+                            println("f");
+                            finallyActionWasEvaluated = true;
                         };
 
-                        println("2");
-                        dummy();
-                        println("3");
-                    } catch(Error e) {  
-                        println("e");
-                        handler2WasCalled =  true; 
-                    } finally {
-                        println("f");
-                        finallyActionWasEvaluated = true;
-                    };
-
-                    println("4");
-                    return handler1WasCalled && handler2WasCalled && finallyActionWasEvaluated;
-                 }'
-        for:JavaScriptEnvironment new
-        arguments:#(0)
-        expect:true
+                        println("4");
+                        return handler1WasCalled && handler2WasCalled && finallyActionWasEvaluated;
+                     }'
+            for:JavaScriptEnvironment new
+            arguments:#(0)
+            expect:true
+    ].
+    self assert:(output asCollectionOfLinesWithReturn asArray = #( '1' '2' '2a' '2b' 'e' '2c' 'f' '4'))
 
     "
      self run:#testTryFinally02
@@ -4305,32 +4612,37 @@
 !
 
 testTryFinally03
-    self 
-        execute:'test(arg) {
-                    var handlerWasCalled = false;
-                    var finallyActionWasEvaluated = false;
-
-                    println("1");
-                    try {
-                        function dummy() {};
-
-                        println("2");
-                        dummy();
-                        println("3");
-                    } catch(Error e) {  
-                        println("e");
-                        handlerWasCalled =  true; 
-                    } finally {
-                        println("f");
-                        finallyActionWasEvaluated = true;
-                    };
-
-                    println("4");
-                    return !!handlerWasCalled && finallyActionWasEvaluated;
-                 }'
-        for:JavaScriptEnvironment new
-        arguments:#(0)
-        expect:true
+    |output|
+
+    output := self outputToTranscriptOf:[
+        self 
+            execute:'test(arg) {
+                        var handlerWasCalled = false;
+                        var finallyActionWasEvaluated = false;
+
+                        println("1");
+                        try {
+                            function dummy() {};
+
+                            println("2");
+                            dummy();
+                            println("3");
+                        } catch(Error e) {  
+                            println("e");
+                            handlerWasCalled =  true; 
+                        } finally {
+                            println("f");
+                            finallyActionWasEvaluated = true;
+                        };
+
+                        println("4");
+                        return !!handlerWasCalled && finallyActionWasEvaluated;
+                     }'
+            for:JavaScriptEnvironment new
+            arguments:#(0)
+            expect:true
+    ].
+    self assert:(output asCollectionOfLinesWithReturn asArray = #( '1' '2' '3' 'f' '4'))
 
     "
      self run:#testTryFinally03
@@ -4338,6 +4650,85 @@
     "
 !
 
+testTypeof01
+    self 
+        execute:'test(arg) {
+                    return typeof(arg);
+                 }'
+        for:nil
+        arguments:#(0)
+        expect:'number'.
+
+    self 
+        execute:'test(arg) {
+                    return typeof(arg);
+                 }'
+        for:nil
+        arguments:#('foo')
+        expect:'string'.
+
+    self 
+        execute:'test(arg) {
+                    return typeof(arg);
+                 }'
+        for:nil
+        arguments:#(true)
+        expect:'boolean'.
+
+    self 
+        execute:'test(arg) {
+                    return typeof(arg);
+                 }'
+        for:nil
+        arguments:#(false)
+        expect:'boolean'.
+
+    self 
+        execute:'test(arg) {
+                    return typeof(arg);
+                 }'
+        for:nil
+        arguments:#(nil)
+        expect:'undefined'.
+
+    self 
+        execute:'test(arg) {
+                    return typeof(arg);
+                 }'
+        for:nil
+        arguments:#( #[1 2 3] )
+        expect:'object'.
+
+    self 
+        execute:'test(arg) {
+                    return typeof(arg);
+                 }'
+        for:nil
+        arguments:#( #(1 2 3) )
+        expect:'object'.
+
+    self 
+        execute:'test(arg) {
+                    return typeof(arg);
+                 }'
+        for:nil
+        arguments:(Array with:(Point new))
+        expect:'object'.
+
+    self 
+        execute:'test(arg) {
+                    return typeof(arg);
+                 }'
+        for:nil
+        arguments:(Array with:Integer)
+        expect:'object'.
+
+    "
+     self run:#testTypeof01
+     self new testTypeof01
+    "
+!
+
 testVarDeclaration01
     self 
         execute:'expr(a, b) {
@@ -4490,19 +4881,24 @@
 !
 
 testWhile01
-    self 
-        execute:'test(arg) {
-                    var n;
-
-                    n = arg;
-                    while (n > 0) {
-                        n--;
-                        Transcript.showCR("hello");
-                    }
-                 }'
-        for:nil
-        arguments:#(5)
-        expect:nil
+    |output|
+
+    output := self outputToTranscriptOf:[
+        self 
+            execute:'test(arg) {
+                        var n;
+
+                        n = arg;
+                        while (n > 0) {
+                            n--;
+                            Transcript.showCR("hello");
+                        }
+                     }'
+            for:nil
+            arguments:#(5)
+            expect:nil
+    ].
+    self assert:(output asCollectionOfLinesWithReturn asArray = #( 'hello' 'hello' 'hello' 'hello' 'hello' ))
 
     "
      self run:#testWhile01
@@ -4511,19 +4907,24 @@
 !
 
 testWhile02
-    self 
-        execute:'test(arg) {
-                    var n;
-
-                    n = 1;
-                    while (n <= arg) {
-                        n++;
-                        Transcript.showCR("hello");
-                    }
-                 }'
-        for:nil
-        arguments:#(5)
-        expect:nil
+    |output|
+
+    output := self outputToTranscriptOf:[
+        self 
+            execute:'test(arg) {
+                        var n;
+
+                        n = 1;
+                        while (n <= arg) {
+                            n++;
+                            Transcript.showCR("hello");
+                        }
+                     }'
+            for:nil
+            arguments:#(5)
+            expect:nil
+    ].
+    self assert:(output asCollectionOfLinesWithReturn asArray = #( 'hello' 'hello' 'hello' 'hello' 'hello' ))
 
     "
      self run:#testWhile02
@@ -4532,18 +4933,23 @@
 !
 
 testWhile03
-    self 
-        execute:'test(arg) {
-                    var n;
-
-                    n = 1;
-                    while (n <= arg) {
-                        Transcript.showCR(n++);
-                    }
-                 }'
-        for:nil
-        arguments:#(5)
-        expect:nil
+    |output|
+
+    output := self outputToTranscriptOf:[
+        self 
+            execute:'test(arg) {
+                        var n;
+
+                        n = 1;
+                        while (n <= arg) {
+                            Transcript.showCR(n++);
+                        }
+                     }'
+            for:nil
+            arguments:#(5)
+            expect:nil
+    ].
+    self assert:(output asCollectionOfLinesWithReturn asArray = #( '1' '2' '3' '4' '5' ))
 
     "
      self run:#testWhile03
@@ -4552,18 +4958,23 @@
 !
 
 testWhile04
-    self 
-        execute:'test(arg) {
-                    var n;
-
-                    n = 1;
-                    while (n++ <= arg) {
-                        Transcript.showCR(n);
-                    }
-                 }'
-        for:nil
-        arguments:#(5)
-        expect:nil
+    |output|
+
+    output := self outputToTranscriptOf:[
+        self 
+            execute:'test(arg) {
+                        var n;
+
+                        n = 1;
+                        while (n++ <= arg) {
+                            Transcript.showCR(n);
+                        }
+                     }'
+            for:nil
+            arguments:#(5)
+            expect:nil
+    ].
+    self assert:(output asCollectionOfLinesWithReturn asArray = #( '2' '3' '4' '5' '6'))
 
     "
      self run:#testWhile04
@@ -4572,18 +4983,23 @@
 !
 
 testWhile04b
-    self 
-        execute:'test(arg) {
-                    var n;
-
-                    n = 1;
-                    while (n++ <= arg) {
-                        Transcript.showCR(n);
-                    }
-                 }'
-        for:nil
-        arguments:#(5)
-        expect:nil
+    |output|
+
+    output := self outputToTranscriptOf:[
+        self 
+            execute:'test(arg) {
+                        var n;
+
+                        n = 1;
+                        while (n++ <= arg) {
+                            Transcript.showCR(n);
+                        }
+                     }'
+            for:nil
+            arguments:#(5)
+            expect:nil
+    ].
+    self assert:(output asCollectionOfLinesWithReturn asArray = #( '2' '3' '4' '5' '6'))
 
     "
      self run:#testWhile04b
@@ -4592,19 +5008,24 @@
 !
 
 testWhile05
-    self 
-        execute:'test(arg) {
-                    var n;
-
-                    n = 1;
-                    while (n++ <= arg) {
-                        if (n == 3) continue;
-                        Transcript.showCR(n);
-                    }
-                 }'
-        for:nil
-        arguments:#(5)
-        expect:nil
+    |output|
+
+    output := self outputToTranscriptOf:[
+        self 
+            execute:'test(arg) {
+                        var n;
+
+                        n = 1;
+                        while (n++ <= arg) {
+                            if (n == 3) continue;
+                            Transcript.showCR(n);
+                        }
+                     }'
+            for:nil
+            arguments:#(5)
+            expect:nil
+    ].
+    self assert:(output asCollectionOfLinesWithReturn asArray = #( '2' '4' '5' '6'))
 
     "
      self run:#testWhile05