def test_try():
    input = """
    try {
        foo;
    }
    catch (clause) {
        bar;
    }

    try {
        foo;
    }
    catch (clause) {
        bar;
    }
    finally {
        foobar;
    }

    try {
        foo;
    }
    finally {
        foobar;
    }    
    """
    parser = Parser()
    program = parser.parse(input)        
def test_if():
    input = """
        if (true)
            var p = 100;
        
        if (true) {
            var p = 100;
            var z = 200;
        }
        
        if (true)
            if (true)
                var p = 100;
            else
                var p = 200;
        
        if (true) {
            var p = 1000;
        }
        else {
            var z = 200;
        }

    """
    parser = Parser()
    program = parser.parse(input)
def test_function():
    input = """

    function foo() {
        
    }

    function foo(bar) {
        
    }

    function foo(bar, foo) {
        
    }
    
    var p = function() {
        
    }
    
    var p = function(bar) {
        
    }
    
    var p = function(foo, bar) {
        
    }
    
    (function(foo){
        
    }())
    
    """
    parser = Parser()
    program = parser.parse(input)
def test_switch():
    input = """
    switch(100) {
        case 99:
        case 98: {
            false;
            break;
        }
        default: {
            break;
        }
        case 100: {
            var found=true;
            break;
        }
        case 101: {
            found=true;
            /* fall-through */
        }
        case 102: {
            found=false;
            break;
        }
    }
    """
    parser = Parser()
    program = parser.parse(input)

    assert len(program.statements[0].cases) == 5
    assert isinstance(program.statements[0].default, ast.DefaultCase)
def test_binop():
    input = """
        100 - 100;
        100 + 100;
        100 / 100;
        100 * 100;
        100 % 100;
        
        p -= 100;
        p += 100;
        p *= 100;
        p /= 100;
        p %= 100;
        p >>= 1;
        p >>>= 2;
        p <<= 3;
        
        p == 100;
        p === 100;
        p != 100;
        p !== 100;
        
        100 == 100 === 100
        100 != 100 !== 100
        
        100 & 1;
        100 | 1;
        100 ^ 1;
        100 &= 100;
        100 |= 100;
        100 ^= 100;
        
    """
    parser = Parser()
    program = parser.parse(input)
def test_switch():
    input = """
    switch(100) {
        case 99:
        case 98: {
            false;
            break;
        }
        default: {
            break;
        }
        case 100: {
            var found=true;
            break;
        }
        case 101: {
            found=true;
            /* fall-through */
        }
        case 102: {
            found=false;
            break;
        }
    }
    """
    parser = Parser()
    program = parser.parse(input)
def test_empty():
    input = """
    ;
    ;
    """
    parser = Parser()
    program = parser.parse(input)
def test_iteration():
    input = """
    for(var i=0; i < 20; i++) {
        p = 100 * i;
    }
    
    for(i = 100; i > 0; i++) {
        p = 1000-i;
    }
    
    for (var i=0, p=0; i < 10 || p < 9; i++, p++) {
        a = i*p;
    }
    
    for (var p in object) {
        p = 100;
    }
    
    for (p in object) {
        p = 200;
    }
    
    do {
        p = 100;
    } while(false);
    
    while(false) {
        p = 200;
    }

    """
    parser = Parser()
    program = parser.parse(input)
Beispiel #9
0
def test_number_hex():
    input = r"""
        0xabcdef
        0XAbEfC
    """
    parser = Parser()
    program = parser.parse(input)
Beispiel #10
0
def test_null():
    input = """
        var p = null;
        null;
    """
    parser = Parser()
    program = parser.parse(input)
def test_with():
    input = """
    with(verylong.longlong.foobar) {
        var foo = bar;
    }
    """
    parser = Parser()
    program = parser.parse(input)
def test_block() :
    input = """
    {
        foo;
    }
    """
    parser = Parser()
    program = parser.parse(input)
def test_labelled_statement():
    input = """
    label: {
        foo;
    }
    """
    parser = Parser()
    program = parser.parse(input)        
def test_break():
    input = """
    while(true) {
        break;
        notreached = true;
    }
    """
    parser = Parser()
    program = parser.parse(input)
Beispiel #15
0
def test_regex():
    input = """
    /foo/ai;
    foo = /bar/;
    /foo/
    {true?(pf(el.match(/op=([^)]*)/)[1])/100)+'':"";}np=np.repl(/-([a-z])/ig)
    """
    parser = Parser()
    program = parser.parse(input)
Beispiel #16
0
def test_comments():
    input = r"""
    // line comment
    /* foo /* foo
     *
     */
    """
    parser = Parser()
    program = parser.parse(input)
def test_new():
    input = """
    var p = new Foo.Bar();
    new Foo.Bar();
    Foo.Bar()[100];
    new Foo.Bar().bar;
    """
    parser = Parser()
    program = parser.parse(input)    
def test_expression_statement():
    input = """
    100/100;
    foo++;
    --foo;
    
    """
    parser = Parser()
    program = parser.parse(input)
def test_continue():
    input = """
    while(true) {
        continue;
        notreached = true;
    }
    """
    parser = Parser()
    program = parser.parse(input)
Beispiel #20
0
def test_object():
    input = r"""
    var obj = {
        foo: 10,
        bar: 20
    };
    obj.foo;
    """
    parser = Parser()
    program = parser.parse(input)
Beispiel #21
0
def test_array():
    input = r"""
    var p = [1, 2, 3, 4]
    p[1]
    
    // elision
    var p = [,]
    """
    parser = Parser()
    program = parser.parse(input)
Beispiel #22
0
def test_number_simple():
    input = r"""
        1;
        1000;
        1000.0020;
        -100200.20;
        +1002010.2
    """
    parser = Parser()
    program = parser.parse(input)
Beispiel #23
0
def test_number_complex():
    input = r"""
        .100e1;
        .2e00;
        .2023e1;
        2.e10;
        1000.23e0;
        100e10;
    """
    parser = Parser()
    program = parser.parse(input)
def test_var_declaration():
    input = """
    var foo, bar = 100;
    var foo=100, bar;
    var foo, bar;
    var bar
    var foo
    
    """
    parser = Parser()
    program = parser.parse(input)   
Beispiel #25
0
def test_string():
    input = r"""
    "foo ";
    "foo\"zar";
    'xar';
    'foo\'b ar';
    'foo\' \'\' b"ar\'\'"\'"\'"';
    "<" + ("div");
    """
    parser = Parser()
    program = parser.parse(input)
def test_function_in_function():
    
    input = """
    // Officialy not allowed, but browsers do support it.
    // jQuery uses it also once.
    
    function foo() {
       function bar() {
       }
    }
    """
    parser = Parser()
    program = parser.parse(input) 
def test_switch_last_default():
    input = """
    switch(100) {
        case 100:
        default: {
            break;
        }
    }
    """
    parser = Parser()
    program = parser.parse(input)

    assert isinstance(program.statements[0].default, ast.DefaultCase)
def test_return():
    input = """
    function foo(bar) {
        return 100;
    }
    function foo(bar) {
        return
        100 * 2 // separate statement..
    }

    """
    parser = Parser()
    program = parser.parse(input)        
def test_debugger():
    input = """
    debugger;
    """
    parser = Parser()
    program = parser.parse(input)        
def test_function_call():
    input = """
    foo();
    """
    parser = Parser()
    program = parser.parse(input)