| 
									
										
										
										
											2013-08-30 00:50:57 +08:00
										 |  |  | describe('UNIT: Expression Parser', function () { | 
					
						
							| 
									
										
										
										
											2013-11-13 02:45:54 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     var ExpParser = require('seed/src/exp-parser') | 
					
						
							| 
									
										
										
										
											2013-08-30 00:50:57 +08:00
										 |  |  |      | 
					
						
							|  |  |  |     var testCases = [ | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             // string concat
 | 
					
						
							|  |  |  |             exp: 'a + b', | 
					
						
							|  |  |  |             vm: { | 
					
						
							|  |  |  |                 a: 'hello', | 
					
						
							|  |  |  |                 b: 'world' | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             expectedValue: 'helloworld' | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             // math
 | 
					
						
							|  |  |  |             exp: 'a - b * 2 + 45', | 
					
						
							|  |  |  |             vm: { | 
					
						
							|  |  |  |                 a: 100, | 
					
						
							|  |  |  |                 b: 23 | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             expectedValue: 100 - 23 * 2 + 45 | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             // boolean logic
 | 
					
						
							|  |  |  |             exp: '(a && b) ? c : d || e', | 
					
						
							|  |  |  |             vm: { | 
					
						
							|  |  |  |                 a: true, | 
					
						
							|  |  |  |                 b: false, | 
					
						
							|  |  |  |                 c: null, | 
					
						
							|  |  |  |                 d: false, | 
					
						
							|  |  |  |                 e: 'worked' | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             expectedValue: 'worked' | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             // inline string
 | 
					
						
							|  |  |  |             exp: "a + 'hello'", | 
					
						
							|  |  |  |             vm: { | 
					
						
							|  |  |  |                 a: 'inline ' | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             expectedValue: 'inline hello' | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             // complex with nested values
 | 
					
						
							|  |  |  |             exp: "todo.title + ' : ' + (todo.done ? 'yep' : 'nope')", | 
					
						
							| 
									
										
										
										
											2013-10-22 02:17:20 +08:00
										 |  |  |             paths: ['todo.title', 'todo.done'], | 
					
						
							| 
									
										
										
										
											2013-08-30 00:50:57 +08:00
										 |  |  |             vm: { | 
					
						
							|  |  |  |                 todo: { | 
					
						
							|  |  |  |                     title: 'write tests', | 
					
						
							|  |  |  |                     done: false | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             expectedValue: 'write tests : nope' | 
					
						
							| 
									
										
										
										
											2013-11-16 00:46:24 +08:00
										 |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             // expression with no scope variables
 | 
					
						
							|  |  |  |             exp: "'a' + 'b'", | 
					
						
							|  |  |  |             vm: {}, | 
					
						
							|  |  |  |             expectedValue: 'ab' | 
					
						
							| 
									
										
										
										
											2013-08-30 00:50:57 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |     ] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     testCases.forEach(describeCase) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function describeCase (testCase) { | 
					
						
							|  |  |  |         describe(testCase.exp, function () { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             var result = ExpParser.parse(testCase.exp), | 
					
						
							|  |  |  |                 vm     = testCase.vm, | 
					
						
							| 
									
										
										
										
											2013-10-22 02:17:20 +08:00
										 |  |  |                 vars   = testCase.paths || Object.keys(vm) | 
					
						
							| 
									
										
										
										
											2013-08-30 00:50:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // mock the $get().
 | 
					
						
							|  |  |  |             // the real $get() will be tested in integration tests.
 | 
					
						
							|  |  |  |             vm.$get = function (key) { return this[key] } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             it('should get correct args', function () { | 
					
						
							| 
									
										
										
										
											2013-11-16 00:46:24 +08:00
										 |  |  |                 if (!vars.length) return | 
					
						
							| 
									
										
										
										
											2013-10-22 02:17:20 +08:00
										 |  |  |                 assert.strictEqual(result.paths.length, vars.length) | 
					
						
							| 
									
										
										
										
											2013-08-30 00:50:57 +08:00
										 |  |  |                 for (var i = 0; i < vars.length; i++) { | 
					
						
							| 
									
										
										
										
											2013-10-22 02:17:20 +08:00
										 |  |  |                     assert.strictEqual(vars[i], result.paths[i]) | 
					
						
							| 
									
										
										
										
											2013-08-30 00:50:57 +08:00
										 |  |  |                 } | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             it('should generate correct getter function', function () { | 
					
						
							|  |  |  |                 var value = result.getter.call(vm) | 
					
						
							|  |  |  |                 assert.strictEqual(value, testCase.expectedValue) | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-16 00:46:24 +08:00
										 |  |  |     // extra case for invalid expressions
 | 
					
						
							|  |  |  |     describe('invalid expression', function () { | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         it('should capture the error and warn', function () { | 
					
						
							|  |  |  |             var utils = require('seed/src/utils'), | 
					
						
							|  |  |  |                 oldWarn = utils.warn, | 
					
						
							|  |  |  |                 warned = false | 
					
						
							|  |  |  |             utils.warn = function () { | 
					
						
							|  |  |  |                 warned = true | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             ExpParser.parse('a + "fsef') | 
					
						
							|  |  |  |             assert.ok(warned) | 
					
						
							|  |  |  |             utils.warn = oldWarn | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-30 00:50:57 +08:00
										 |  |  | }) |