2011-07-17 08:05:43 +00:00
'use strict' ;
2010-11-08 22:29:41 +00:00
describe ( 'parser' , function ( ) {
describe ( 'lexer' , function ( ) {
it ( 'should tokenize a string' , function ( ) {
2010-10-15 20:44:53 +00:00
var tokens = lex ( "a.bc[22]+1.3|f:'a\\\'c':\"d\\\"e\"" ) ;
var i = 0 ;
2010-11-08 22:29:41 +00:00
expect ( tokens [ i ] . index ) . toEqual ( 0 ) ;
expect ( tokens [ i ] . text ) . toEqual ( 'a.bc' ) ;
2010-10-15 20:44:53 +00:00
i ++ ;
2010-11-08 22:29:41 +00:00
expect ( tokens [ i ] . index ) . toEqual ( 4 ) ;
expect ( tokens [ i ] . text ) . toEqual ( '[' ) ;
2010-10-15 20:44:53 +00:00
i ++ ;
2010-11-08 22:29:41 +00:00
expect ( tokens [ i ] . index ) . toEqual ( 5 ) ;
expect ( tokens [ i ] . text ) . toEqual ( 22 ) ;
2010-10-15 20:44:53 +00:00
i ++ ;
2010-11-08 22:29:41 +00:00
expect ( tokens [ i ] . index ) . toEqual ( 7 ) ;
expect ( tokens [ i ] . text ) . toEqual ( ']' ) ;
2010-10-15 20:44:53 +00:00
i ++ ;
2010-11-08 22:29:41 +00:00
expect ( tokens [ i ] . index ) . toEqual ( 8 ) ;
expect ( tokens [ i ] . text ) . toEqual ( '+' ) ;
2010-10-15 20:44:53 +00:00
i ++ ;
2010-11-08 22:29:41 +00:00
expect ( tokens [ i ] . index ) . toEqual ( 9 ) ;
expect ( tokens [ i ] . text ) . toEqual ( 1.3 ) ;
2010-10-15 20:44:53 +00:00
i ++ ;
2010-11-08 22:29:41 +00:00
expect ( tokens [ i ] . index ) . toEqual ( 12 ) ;
expect ( tokens [ i ] . text ) . toEqual ( '|' ) ;
2010-10-15 20:44:53 +00:00
i ++ ;
2010-11-08 22:29:41 +00:00
expect ( tokens [ i ] . index ) . toEqual ( 13 ) ;
expect ( tokens [ i ] . text ) . toEqual ( 'f' ) ;
2010-10-15 20:44:53 +00:00
i ++ ;
2010-11-08 22:29:41 +00:00
expect ( tokens [ i ] . index ) . toEqual ( 14 ) ;
expect ( tokens [ i ] . text ) . toEqual ( ':' ) ;
2010-10-15 20:44:53 +00:00
i ++ ;
2010-11-08 22:29:41 +00:00
expect ( tokens [ i ] . index ) . toEqual ( 15 ) ;
expect ( tokens [ i ] . string ) . toEqual ( "a'c" ) ;
2010-10-15 20:44:53 +00:00
i ++ ;
2010-11-08 22:29:41 +00:00
expect ( tokens [ i ] . index ) . toEqual ( 21 ) ;
expect ( tokens [ i ] . text ) . toEqual ( ':' ) ;
2010-10-15 20:44:53 +00:00
i ++ ;
2010-11-08 22:29:41 +00:00
expect ( tokens [ i ] . index ) . toEqual ( 22 ) ;
expect ( tokens [ i ] . string ) . toEqual ( 'd"e' ) ;
2010-10-15 20:44:53 +00:00
} ) ;
2010-11-08 22:29:41 +00:00
it ( 'should tokenize undefined' , function ( ) {
2010-10-15 20:44:53 +00:00
var tokens = lex ( "undefined" ) ;
var i = 0 ;
2010-11-08 22:29:41 +00:00
expect ( tokens [ i ] . index ) . toEqual ( 0 ) ;
expect ( tokens [ i ] . text ) . toEqual ( 'undefined' ) ;
expect ( undefined ) . toEqual ( tokens [ i ] . fn ( ) ) ;
2010-10-15 20:44:53 +00:00
} ) ;
2011-01-19 23:42:11 +00:00
2010-11-08 22:29:41 +00:00
it ( 'should tokenize quoted string' , function ( ) {
2010-10-15 20:44:53 +00:00
var str = "['\\'', \"\\\"\"]" ;
var tokens = lex ( str ) ;
2010-11-08 22:29:41 +00:00
expect ( tokens [ 1 ] . index ) . toEqual ( 1 ) ;
expect ( tokens [ 1 ] . string ) . toEqual ( "'" ) ;
2010-10-15 20:44:53 +00:00
2010-11-08 22:29:41 +00:00
expect ( tokens [ 3 ] . index ) . toEqual ( 7 ) ;
expect ( tokens [ 3 ] . string ) . toEqual ( '"' ) ;
2010-10-15 20:44:53 +00:00
} ) ;
2010-11-08 22:29:41 +00:00
it ( 'should tokenize escaped quoted string' , function ( ) {
2010-10-15 20:44:53 +00:00
var str = '"\\"\\n\\f\\r\\t\\v\\u00A0"' ;
var tokens = lex ( str ) ;
2010-11-08 22:29:41 +00:00
expect ( tokens [ 0 ] . string ) . toEqual ( '"\n\f\r\t\v\u00A0' ) ;
2010-10-15 20:44:53 +00:00
} ) ;
2010-11-08 22:29:41 +00:00
it ( 'should tokenize unicode' , function ( ) {
2010-10-15 20:44:53 +00:00
var tokens = lex ( '"\\u00A0"' ) ;
2010-11-08 22:29:41 +00:00
expect ( tokens . length ) . toEqual ( 1 ) ;
expect ( tokens [ 0 ] . string ) . toEqual ( '\u00a0' ) ;
2010-10-15 20:44:53 +00:00
} ) ;
2010-11-08 22:29:41 +00:00
it ( 'should ignore whitespace' , function ( ) {
2010-12-22 01:38:04 +00:00
var tokens = lex ( "a \t \n \r b" ) ;
2010-11-08 22:29:41 +00:00
expect ( tokens [ 0 ] . text ) . toEqual ( 'a' ) ;
expect ( tokens [ 1 ] . text ) . toEqual ( 'b' ) ;
2010-10-15 20:44:53 +00:00
} ) ;
2010-11-08 22:29:41 +00:00
it ( 'should tokenize relation' , function ( ) {
2010-10-15 20:44:53 +00:00
var tokens = lex ( "! == != < > <= >=" ) ;
2010-11-08 22:29:41 +00:00
expect ( tokens [ 0 ] . text ) . toEqual ( '!' ) ;
expect ( tokens [ 1 ] . text ) . toEqual ( '==' ) ;
expect ( tokens [ 2 ] . text ) . toEqual ( '!=' ) ;
expect ( tokens [ 3 ] . text ) . toEqual ( '<' ) ;
expect ( tokens [ 4 ] . text ) . toEqual ( '>' ) ;
expect ( tokens [ 5 ] . text ) . toEqual ( '<=' ) ;
expect ( tokens [ 6 ] . text ) . toEqual ( '>=' ) ;
2010-10-15 20:44:53 +00:00
} ) ;
2010-11-08 22:29:41 +00:00
it ( 'should tokenize statements' , function ( ) {
2010-10-15 20:44:53 +00:00
var tokens = lex ( "a;b;" ) ;
2010-11-08 22:29:41 +00:00
expect ( tokens [ 0 ] . text ) . toEqual ( 'a' ) ;
expect ( tokens [ 1 ] . text ) . toEqual ( ';' ) ;
expect ( tokens [ 2 ] . text ) . toEqual ( 'b' ) ;
expect ( tokens [ 3 ] . text ) . toEqual ( ';' ) ;
2010-10-15 20:44:53 +00:00
} ) ;
2010-11-08 22:29:41 +00:00
it ( 'should tokenize number' , function ( ) {
2010-10-15 20:44:53 +00:00
var tokens = lex ( "0.5" ) ;
expect ( tokens [ 0 ] . text ) . toEqual ( 0.5 ) ;
} ) ;
2010-11-08 22:29:41 +00:00
it ( 'should tokenize negative number' , function ( ) {
2010-10-15 20:44:53 +00:00
var value = createScope ( ) . $eval ( "-0.5" ) ;
expect ( value ) . toEqual ( - 0.5 ) ;
value = createScope ( ) . $eval ( "{a:-0.5}" ) ;
expect ( value ) . toEqual ( { a : - 0.5 } ) ;
} ) ;
2010-11-08 22:29:41 +00:00
it ( 'should tokenize number with exponent' , function ( ) {
2010-10-15 20:44:53 +00:00
var tokens = lex ( "0.5E-10" ) ;
expect ( tokens [ 0 ] . text ) . toEqual ( 0.5 E - 10 ) ;
expect ( createScope ( ) . $eval ( "0.5E-10" ) ) . toEqual ( 0.5 E - 10 ) ;
tokens = lex ( "0.5E+10" ) ;
expect ( tokens [ 0 ] . text ) . toEqual ( 0.5 E + 10 ) ;
} ) ;
2010-12-22 01:38:04 +00:00
it ( 'should throws exception for invalid exponent' , function ( ) {
expect ( function ( ) {
lex ( "0.5E-" ) ;
} ) . toThrow ( new Error ( 'Lexer Error: Invalid exponent at column 4 in expression [0.5E-].' ) ) ;
2011-01-19 23:42:11 +00:00
2010-12-22 01:38:04 +00:00
expect ( function ( ) {
lex ( "0.5E-A" ) ;
} ) . toThrow ( new Error ( 'Lexer Error: Invalid exponent at column 4 in expression [0.5E-A].' ) ) ;
} ) ;
2010-11-08 22:29:41 +00:00
it ( 'should tokenize number starting with a dot' , function ( ) {
2010-10-15 20:44:53 +00:00
var tokens = lex ( ".5" ) ;
expect ( tokens [ 0 ] . text ) . toEqual ( 0.5 ) ;
} ) ;
2010-10-15 21:06:30 +00:00
2010-11-08 22:29:41 +00:00
it ( 'should throw error on invalid unicode' , function ( ) {
expect ( function ( ) {
2010-12-22 01:38:04 +00:00
lex ( "'\\u1''bla'" ) ;
} ) . toThrow ( new Error ( "Lexer Error: Invalid unicode escape [\\u1''b] at column 2 in expression ['\\u1''bla']." ) ) ;
2010-10-15 21:06:30 +00:00
} ) ;
2010-10-15 20:44:53 +00:00
} ) ;
2011-01-19 23:42:11 +00:00
2010-11-08 22:29:41 +00:00
var scope ;
beforeEach ( function ( ) {
scope = createScope ( ) ;
2010-10-15 20:44:53 +00:00
} ) ;
2010-11-08 22:29:41 +00:00
it ( 'should parse expressions' , function ( ) {
expect ( scope . $eval ( "-1" ) ) . toEqual ( - 1 ) ;
expect ( scope . $eval ( "1 + 2.5" ) ) . toEqual ( 3.5 ) ;
expect ( scope . $eval ( "1 + -2.5" ) ) . toEqual ( - 1.5 ) ;
expect ( scope . $eval ( "1+2*3/4" ) ) . toEqual ( 1 + 2 * 3 / 4 ) ;
expect ( scope . $eval ( "0--1+1.5" ) ) . toEqual ( 0 - - 1 + 1.5 ) ;
expect ( scope . $eval ( "-0--1++2*-3/-4" ) ) . toEqual ( - 0 - - 1 + + 2 * - 3 / - 4 ) ;
expect ( scope . $eval ( "1/2*3" ) ) . toEqual ( 1 / 2 * 3 ) ;
} ) ;
2010-10-15 20:44:53 +00:00
2010-11-08 22:29:41 +00:00
it ( 'should parse comparison' , function ( ) {
expect ( scope . $eval ( "false" ) ) . toBeFalsy ( ) ;
expect ( scope . $eval ( "!true" ) ) . toBeFalsy ( ) ;
expect ( scope . $eval ( "1==1" ) ) . toBeTruthy ( ) ;
expect ( scope . $eval ( "1!=2" ) ) . toBeTruthy ( ) ;
expect ( scope . $eval ( "1<2" ) ) . toBeTruthy ( ) ;
expect ( scope . $eval ( "1<=1" ) ) . toBeTruthy ( ) ;
expect ( scope . $eval ( "1>2" ) ) . toEqual ( 1 > 2 ) ;
expect ( scope . $eval ( "2>=1" ) ) . toEqual ( 2 >= 1 ) ;
expect ( scope . $eval ( "true==2<3" ) ) . toEqual ( true === 2 < 3 ) ;
2010-10-15 20:44:53 +00:00
} ) ;
2010-11-08 22:29:41 +00:00
it ( 'should parse logical' , function ( ) {
expect ( scope . $eval ( "0&&2" ) ) . toEqual ( 0 && 2 ) ;
expect ( scope . $eval ( "0||2" ) ) . toEqual ( 0 || 2 ) ;
expect ( scope . $eval ( "0||1&&2" ) ) . toEqual ( 0 || 1 && 2 ) ;
2010-10-15 20:44:53 +00:00
} ) ;
2010-11-08 22:29:41 +00:00
it ( 'should parse string' , function ( ) {
expect ( scope . $eval ( "'a' + 'b c'" ) ) . toEqual ( "ab c" ) ;
2010-10-15 20:44:53 +00:00
} ) ;
2011-10-07 18:27:49 +00:00
it ( 'should parse filters' , function ( ) {
2010-10-15 20:44:53 +00:00
angular . filter . substring = function ( input , start , end ) {
return input . substring ( start , end ) ;
} ;
2010-11-08 22:29:41 +00:00
angular . filter . upper = { _case : function ( input ) {
2010-10-15 20:44:53 +00:00
return input . toUpperCase ( ) ;
} } ;
2010-11-08 22:29:41 +00:00
expect ( function ( ) {
2010-10-15 20:44:53 +00:00
scope . $eval ( "1|nonExistant" ) ;
2011-04-05 18:00:26 +00:00
} ) . toThrow ( new Error ( "Syntax Error: Token 'nonExistant' should be a function at column 3 of the expression [1|nonExistant] starting at [nonExistant]." ) ) ;
2011-01-19 23:42:11 +00:00
2011-03-23 16:33:29 +00:00
scope . offset = 3 ;
2010-11-08 22:29:41 +00:00
expect ( scope . $eval ( "'abcd'|upper._case" ) ) . toEqual ( "ABCD" ) ;
expect ( scope . $eval ( "'abcd'|substring:1:offset" ) ) . toEqual ( "bc" ) ;
expect ( scope . $eval ( "'abcd'|substring:1:3|upper._case" ) ) . toEqual ( "BC" ) ;
2010-10-15 20:44:53 +00:00
} ) ;
2010-11-08 22:29:41 +00:00
it ( 'should access scope' , function ( ) {
2011-03-23 16:33:29 +00:00
scope . a = 123 ;
scope . b = { c : 456 } ;
2010-11-08 22:29:41 +00:00
expect ( scope . $eval ( "a" , scope ) ) . toEqual ( 123 ) ;
expect ( scope . $eval ( "b.c" , scope ) ) . toEqual ( 456 ) ;
expect ( scope . $eval ( "x.y.z" , scope ) ) . not . toBeDefined ( ) ;
2010-10-15 20:44:53 +00:00
} ) ;
2010-11-08 22:29:41 +00:00
it ( 'should evaluate grouped expressions' , function ( ) {
expect ( scope . $eval ( "(1+2)*3" ) ) . toEqual ( ( 1 + 2 ) * 3 ) ;
2010-10-15 20:44:53 +00:00
} ) ;
2010-11-08 22:29:41 +00:00
it ( 'should evaluate assignments' , function ( ) {
expect ( scope . $eval ( "a=12" ) ) . toEqual ( 12 ) ;
2011-03-23 16:33:29 +00:00
expect ( scope . a ) . toEqual ( 12 ) ;
2010-10-15 20:44:53 +00:00
scope = createScope ( ) ;
2010-11-08 22:29:41 +00:00
expect ( scope . $eval ( "x.y.z=123;" ) ) . toEqual ( 123 ) ;
2011-03-23 16:33:29 +00:00
expect ( scope . x . y . z ) . toEqual ( 123 ) ;
2010-10-15 20:44:53 +00:00
2010-11-08 22:29:41 +00:00
expect ( scope . $eval ( "a=123; b=234" ) ) . toEqual ( 234 ) ;
2011-03-23 16:33:29 +00:00
expect ( scope . a ) . toEqual ( 123 ) ;
expect ( scope . b ) . toEqual ( 234 ) ;
2010-10-15 20:44:53 +00:00
} ) ;
2010-11-08 22:29:41 +00:00
it ( 'should evaluate function call without arguments' , function ( ) {
2011-03-23 16:33:29 +00:00
scope [ 'const' ] = function ( a , b ) { return 123 ; } ;
2010-11-08 22:29:41 +00:00
expect ( scope . $eval ( "const()" ) ) . toEqual ( 123 ) ;
2010-10-15 20:44:53 +00:00
} ) ;
2010-11-08 22:29:41 +00:00
it ( 'should evaluate function call with arguments' , function ( ) {
2011-03-23 16:33:29 +00:00
scope . add = function ( a , b ) {
2010-10-15 20:44:53 +00:00
return a + b ;
2011-03-23 16:33:29 +00:00
} ;
2010-11-08 22:29:41 +00:00
expect ( scope . $eval ( "add(1,2)" ) ) . toEqual ( 3 ) ;
2010-10-15 20:44:53 +00:00
} ) ;
2010-11-08 22:29:41 +00:00
it ( 'should evaluate multiplication and division' , function ( ) {
2011-03-23 16:33:29 +00:00
scope . taxRate = 8 ;
scope . subTotal = 100 ;
2010-11-08 22:29:41 +00:00
expect ( scope . $eval ( "taxRate / 100 * subTotal" ) ) . toEqual ( 8 ) ;
expect ( scope . $eval ( "subTotal * taxRate / 100" ) ) . toEqual ( 8 ) ;
2010-10-15 20:44:53 +00:00
} ) ;
2010-11-08 22:29:41 +00:00
it ( 'should evaluate array' , function ( ) {
expect ( scope . $eval ( "[]" ) . length ) . toEqual ( 0 ) ;
expect ( scope . $eval ( "[1, 2]" ) . length ) . toEqual ( 2 ) ;
expect ( scope . $eval ( "[1, 2]" ) [ 0 ] ) . toEqual ( 1 ) ;
expect ( scope . $eval ( "[1, 2]" ) [ 1 ] ) . toEqual ( 2 ) ;
2010-10-15 20:44:53 +00:00
} ) ;
2010-11-08 22:29:41 +00:00
it ( 'should evaluate array access' , function ( ) {
expect ( scope . $eval ( "[1][0]" ) ) . toEqual ( 1 ) ;
expect ( scope . $eval ( "[[1]][0][0]" ) ) . toEqual ( 1 ) ;
expect ( scope . $eval ( "[].length" ) ) . toEqual ( 0 ) ;
expect ( scope . $eval ( "[1, 2].length" ) ) . toEqual ( 2 ) ;
2010-10-15 20:44:53 +00:00
} ) ;
2010-11-08 22:29:41 +00:00
it ( 'should evaluate object' , function ( ) {
expect ( toJson ( scope . $eval ( "{}" ) ) ) . toEqual ( "{}" ) ;
expect ( toJson ( scope . $eval ( "{a:'b'}" ) ) ) . toEqual ( '{"a":"b"}' ) ;
expect ( toJson ( scope . $eval ( "{'a':'b'}" ) ) ) . toEqual ( '{"a":"b"}' ) ;
expect ( toJson ( scope . $eval ( "{\"a\":'b'}" ) ) ) . toEqual ( '{"a":"b"}' ) ;
2010-10-15 20:44:53 +00:00
} ) ;
2010-11-08 22:29:41 +00:00
it ( 'should evaluate object access' , function ( ) {
expect ( scope . $eval ( "{false:'WC', true:'CC'}[false]" ) ) . toEqual ( "WC" ) ;
2010-10-15 20:44:53 +00:00
} ) ;
2010-11-08 22:29:41 +00:00
it ( 'should evaluate JSON' , function ( ) {
expect ( toJson ( scope . $eval ( "[{}]" ) ) ) . toEqual ( "[{}]" ) ;
expect ( toJson ( scope . $eval ( "[{a:[]}, {b:1}]" ) ) ) . toEqual ( '[{"a":[]},{"b":1}]' ) ;
2010-10-15 20:44:53 +00:00
} ) ;
2010-11-08 22:29:41 +00:00
it ( 'should evaluate multipple statements' , function ( ) {
expect ( scope . $eval ( "a=1;b=3;a+b" ) ) . toEqual ( 4 ) ;
expect ( scope . $eval ( ";;1;;" ) ) . toEqual ( 1 ) ;
2010-10-15 20:44:53 +00:00
} ) ;
2010-11-08 22:29:41 +00:00
it ( 'should evaluate object methods in correct context (this)' , function ( ) {
var C = function ( ) {
this . a = 123 ;
2010-10-15 20:44:53 +00:00
} ;
2010-11-08 22:29:41 +00:00
C . prototype . getA = function ( ) {
2010-10-15 20:44:53 +00:00
return this . a ;
} ;
2011-03-23 16:33:29 +00:00
scope . obj = new C ( ) ;
2010-11-08 22:29:41 +00:00
expect ( scope . $eval ( "obj.getA()" ) ) . toEqual ( 123 ) ;
2010-10-15 20:44:53 +00:00
} ) ;
2011-01-19 23:42:11 +00:00
2010-11-08 22:29:41 +00:00
it ( 'should evaluate methods in correct context (this) in argument' , function ( ) {
var C = function ( ) {
this . a = 123 ;
2010-10-15 20:44:53 +00:00
} ;
2010-11-08 22:29:41 +00:00
C . prototype . sum = function ( value ) {
2010-10-15 20:44:53 +00:00
return this . a + value ;
} ;
2010-11-08 22:29:41 +00:00
C . prototype . getA = function ( ) {
2010-10-15 20:44:53 +00:00
return this . a ;
} ;
2011-03-23 16:33:29 +00:00
scope . obj = new C ( ) ;
2010-11-08 22:29:41 +00:00
expect ( scope . $eval ( "obj.sum(obj.getA())" ) ) . toEqual ( 246 ) ;
2010-10-15 20:44:53 +00:00
} ) ;
2010-11-08 22:29:41 +00:00
it ( 'should evaluate objects on scope context' , function ( ) {
2011-03-23 16:33:29 +00:00
scope . a = "abc" ;
2010-11-08 22:29:41 +00:00
expect ( scope . $eval ( "{a:a}" ) . a ) . toEqual ( "abc" ) ;
2010-10-15 20:44:53 +00:00
} ) ;
2010-11-08 22:29:41 +00:00
it ( 'should evaluate field access on function call result' , function ( ) {
2011-03-23 16:33:29 +00:00
scope . a = function ( ) {
2010-11-08 22:29:41 +00:00
return { name : 'misko' } ;
2011-03-23 16:33:29 +00:00
} ;
2010-11-08 22:29:41 +00:00
expect ( scope . $eval ( "a().name" ) ) . toEqual ( "misko" ) ;
2010-10-15 20:44:53 +00:00
} ) ;
2010-11-08 22:29:41 +00:00
it ( 'should evaluate field access after array access' , function ( ) {
2011-03-23 16:33:29 +00:00
scope . items = [ { } , { name : 'misko' } ] ;
2010-11-08 22:29:41 +00:00
expect ( scope . $eval ( 'items[1].name' ) ) . toEqual ( "misko" ) ;
2010-10-15 20:44:53 +00:00
} ) ;
2010-11-08 22:29:41 +00:00
it ( 'should evaluate array assignment' , function ( ) {
2011-03-23 16:33:29 +00:00
scope . items = [ ] ;
2010-10-15 20:44:53 +00:00
2010-11-08 22:29:41 +00:00
expect ( scope . $eval ( 'items[1] = "abc"' ) ) . toEqual ( "abc" ) ;
expect ( scope . $eval ( 'items[1]' ) ) . toEqual ( "abc" ) ;
2010-10-15 20:44:53 +00:00
// Dont know how to make this work....
2010-11-08 22:29:41 +00:00
// expect(scope.$eval('books[1] = "moby"')).toEqual("moby");
// expect(scope.$eval('books[1]')).toEqual("moby");
2010-10-15 20:44:53 +00:00
} ) ;
2010-11-08 22:29:41 +00:00
it ( 'should evaluate grouped filters' , function ( ) {
scope . name = 'MISKO' ;
expect ( scope . $eval ( 'n = (name|lowercase)' ) ) . toEqual ( 'misko' ) ;
expect ( scope . $eval ( 'n' ) ) . toEqual ( 'misko' ) ;
2010-10-15 20:44:53 +00:00
} ) ;
2010-11-08 22:29:41 +00:00
it ( 'should evaluate remainder' , function ( ) {
expect ( scope . $eval ( '1%2' ) ) . toEqual ( 1 ) ;
2010-10-15 20:44:53 +00:00
} ) ;
2010-11-08 22:29:41 +00:00
it ( 'should evaluate sum with undefined' , function ( ) {
expect ( scope . $eval ( '1+undefined' ) ) . toEqual ( 1 ) ;
expect ( scope . $eval ( 'undefined+1' ) ) . toEqual ( 1 ) ;
2010-10-15 20:44:53 +00:00
} ) ;
2010-11-08 22:29:41 +00:00
it ( 'should throw exception on non-closed bracket' , function ( ) {
expect ( function ( ) {
2010-10-15 20:44:53 +00:00
scope . $eval ( '[].count(' ) ;
2010-11-08 22:29:41 +00:00
} ) . toThrow ( 'Unexpected end of expression: [].count(' ) ;
2010-10-15 20:44:53 +00:00
} ) ;
2010-11-08 22:29:41 +00:00
it ( 'should evaluate double negation' , function ( ) {
expect ( scope . $eval ( 'true' ) ) . toBeTruthy ( ) ;
expect ( scope . $eval ( '!true' ) ) . toBeFalsy ( ) ;
expect ( scope . $eval ( '!!true' ) ) . toBeTruthy ( ) ;
expect ( scope . $eval ( '{true:"a", false:"b"}[!!true]' ) ) . toEqual ( 'a' ) ;
2010-10-15 20:44:53 +00:00
} ) ;
2010-11-08 22:29:41 +00:00
it ( 'should evaluate negation' , function ( ) {
expect ( scope . $eval ( "!false || true" ) ) . toEqual ( ! false || true ) ;
expect ( scope . $eval ( "!11 == 10" ) ) . toEqual ( ! 11 == 10 ) ;
expect ( scope . $eval ( "12/6/2" ) ) . toEqual ( 12 / 6 / 2 ) ;
2010-10-15 20:44:53 +00:00
} ) ;
2010-11-08 22:29:41 +00:00
it ( 'should evaluate exclamation mark' , function ( ) {
expect ( scope . $eval ( 'suffix = "!"' ) ) . toEqual ( '!' ) ;
2010-10-15 20:44:53 +00:00
} ) ;
2010-11-08 22:29:41 +00:00
it ( 'should evaluate minus' , function ( ) {
expect ( scope . $eval ( "{a:'-'}" ) ) . toEqual ( { a : "-" } ) ;
2010-10-15 20:44:53 +00:00
} ) ;
2010-11-08 22:29:41 +00:00
it ( 'should evaluate undefined' , function ( ) {
expect ( scope . $eval ( "undefined" ) ) . not . toBeDefined ( ) ;
expect ( scope . $eval ( "a=undefined" ) ) . not . toBeDefined ( ) ;
2011-03-23 16:33:29 +00:00
expect ( scope . a ) . not . toBeDefined ( ) ;
2010-10-15 20:44:53 +00:00
} ) ;
2011-01-19 23:42:11 +00:00
2011-09-30 23:55:01 +00:00
it ( 'should allow assignment after array dereference' , function ( ) {
2010-12-07 19:39:59 +00:00
scope = angular . scope ( ) ;
scope . obj = [ { } ] ;
scope . $eval ( 'obj[0].name=1' ) ;
expect ( scope . obj . name ) . toBeUndefined ( ) ;
expect ( scope . obj [ 0 ] . name ) . toEqual ( 1 ) ;
} ) ;
2011-01-19 23:42:11 +00:00
2011-09-30 23:55:01 +00:00
it ( 'should short-circuit AND operator' , function ( ) {
var scope = angular . scope ( ) ;
scope . run = function ( ) {
throw "IT SHOULD NOT HAVE RUN" ;
} ;
expect ( scope . $eval ( 'false && run()' ) ) . toBe ( false ) ;
} ) ;
it ( 'should short-circuit OR operator' , function ( ) {
var scope = angular . scope ( ) ;
scope . run = function ( ) {
throw "IT SHOULD NOT HAVE RUN" ;
} ;
expect ( scope . $eval ( 'true || run()' ) ) . toBe ( true ) ;
} ) ;
2011-01-19 23:42:11 +00:00
2011-10-07 18:27:49 +00:00
describe ( 'assignable' , function ( ) {
it ( 'should expose assignment function' , function ( ) {
2011-01-13 18:35:26 +00:00
var fn = parser ( 'a' ) . assignable ( ) ;
expect ( fn . assign ) . toBeTruthy ( ) ;
var scope = { } ;
fn . assign ( scope , 123 ) ;
expect ( scope ) . toEqual ( { a : 123 } ) ;
} ) ;
} ) ;
2010-10-15 20:44:53 +00:00
} ) ;