aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorDaniel Luz2012-11-04 11:05:58 -0200
committerMisko Hevery2013-02-14 14:43:56 -0800
commit1ed638582d2f2c7f89384d9712f4cfac52cc5b70 (patch)
tree2e2868b6939f2db28ed0cdb9692a32a3b8b85b8d /test
parent3b14092135ab02a4b08c0ba21d40726684acf83e (diff)
downloadangular.js-1ed638582d2f2c7f89384d9712f4cfac52cc5b70.tar.bz2
feat($parse): added `constant` and `literal` properties
* `literal` is set to true if the expression's top-level is a JavaScript literal (number, string, boolean, null/undefined, array, object), even if it contains non-literals inside. * `constant` is set to true if the expression is known to be made entirely of constant values, i.e., evaluating it will always yield the same result. A consequence is that a JSON expression is guaranteed to be both literal and constant.
Diffstat (limited to 'test')
-rw-r--r--test/ng/parseSpec.js68
1 files changed, 68 insertions, 0 deletions
diff --git a/test/ng/parseSpec.js b/test/ng/parseSpec.js
index a6f43282..020a1d6f 100644
--- a/test/ng/parseSpec.js
+++ b/test/ng/parseSpec.js
@@ -671,6 +671,74 @@ describe('parser', function() {
expect($parse('a.b')({a: {b: 0}}, {a: null})).toEqual(undefined);
}));
});
+
+ describe('literal', function() {
+ it('should mark scalar value expressions as literal', inject(function($parse) {
+ expect($parse('0').literal).toBe(true);
+ expect($parse('"hello"').literal).toBe(true);
+ expect($parse('true').literal).toBe(true);
+ expect($parse('false').literal).toBe(true);
+ expect($parse('null').literal).toBe(true);
+ expect($parse('undefined').literal).toBe(true);
+ }));
+
+ it('should mark array expressions as literal', inject(function($parse) {
+ expect($parse('[]').literal).toBe(true);
+ expect($parse('[1, 2, 3]').literal).toBe(true);
+ expect($parse('[1, identifier]').literal).toBe(true);
+ }));
+
+ it('should mark object expressions as literal', inject(function($parse) {
+ expect($parse('{}').literal).toBe(true);
+ expect($parse('{x: 1}').literal).toBe(true);
+ expect($parse('{foo: bar}').literal).toBe(true);
+ }));
+
+ it('should not mark function calls or operator expressions as literal', inject(function($parse) {
+ expect($parse('1 + 1').literal).toBe(false);
+ expect($parse('call()').literal).toBe(false);
+ expect($parse('[].length').literal).toBe(false);
+ }));
+ });
+
+ describe('constant', function() {
+ it('should mark scalar value expressions as constant', inject(function($parse) {
+ expect($parse('12.3').constant).toBe(true);
+ expect($parse('"string"').constant).toBe(true);
+ expect($parse('true').constant).toBe(true);
+ expect($parse('false').constant).toBe(true);
+ expect($parse('null').constant).toBe(true);
+ expect($parse('undefined').constant).toBe(true);
+ }));
+
+ it('should mark arrays as constant if they only contain constant elements', inject(function($parse) {
+ expect($parse('[]').constant).toBe(true);
+ expect($parse('[1, 2, 3]').constant).toBe(true);
+ expect($parse('["string", null]').constant).toBe(true);
+ expect($parse('[[]]').constant).toBe(true);
+ expect($parse('[1, [2, 3], {4: 5}]').constant).toBe(true);
+ }));
+
+ it('should not mark arrays as constant if they contain any non-constant elements', inject(function($parse) {
+ expect($parse('[foo]').constant).toBe(false);
+ expect($parse('[x + 1]').constant).toBe(false);
+ expect($parse('[bar[0]]').constant).toBe(false);
+ }));
+
+ it('should mark complex expressions involving constant values as constant', inject(function($parse) {
+ expect($parse('!true').constant).toBe(true);
+ expect($parse('1 - 1').constant).toBe(true);
+ expect($parse('"foo" + "bar"').constant).toBe(true);
+ expect($parse('5 != null').constant).toBe(true);
+ expect($parse('{standard: 4/3, wide: 16/9}').constant).toBe(true);
+ }));
+
+ it('should not mark any expression involving variables or function calls as constant', inject(function($parse) {
+ expect($parse('true.toString()').constant).toBe(false);
+ expect($parse('foo(1, 2, 3)').constant).toBe(false);
+ expect($parse('"name" + id').constant).toBe(false);
+ }));
+ });
});
});
});