Permalink
Browse files

Add parser test cases, nodeunit submodule

  • Loading branch information...
1 parent aeb7c86 commit ffc161ab771180bed65478eac0f8d5d30ce668db @fadrizul committed Sep 20, 2011
Showing with 126 additions and 2 deletions.
  1. +3 −0 .gitmodules
  2. +1 −0 dev/nodeunit
  3. +2 −2 lib/parser.coffee
  4. +120 −0 tests/parser.test.coffee
View
@@ -4,3 +4,6 @@
[submodule "dev/eyes"]
path = dev/eyes
url = git://github.com/cloudhead/eyes.js.git
+[submodule "dev/nodeunit"]
+ path = dev/nodeunit
+ url = git://github.com/caolan/nodeunit.git
Submodule nodeunit added at 7568b4
View
@@ -40,11 +40,11 @@ class Parser
if x.LBracket.test(part)
filters.push
- name: filter_name[0]
+ name: part.replace(/\(.+\)/, "")
args: part.replace(x.Symbols, "").split(",")
else
filters.push
- name: filter_name[0]
+ name: part
args: []
# Create the parser tree
View
@@ -0,0 +1,120 @@
+###
+TwigJS - Parser Test cases
+###
+
+# Module dependencies
+testCase = require("nodeunit").testCase
+Parser = require "../lib/parser"
+
+# Comments test cases
+exports.Comments = testCase
+ "Ignore empty strings" : (test) ->
+ parser = new Parser("", {})
+ output = parser.parseTokens()
+
+ test.deepEqual([], output)
+ test.done()
+
+ "Ignore line breaks \\n" : (test) ->
+ parser = new Parser("\n", {})
+ output = parser.parseTokens()
+
+ test.deepEqual([], output)
+ test.done()
+
+ "Ignore comment tags {# #}" : (test) ->
+ parser = new Parser("{# foor #}", {})
+ output = parser.parseTokens()
+
+ test.deepEqual([], output)
+ test.done()
+
+exports.Variable = testCase
+ "Basic variable {{ }}" : (test) ->
+ parser = new Parser("{{ bar }}", {})
+ output = parser.parseTokens()
+ equals =
+ type : Parser.TOKEN_TYPES.VAR
+ name : "bar"
+ filters : []
+
+ test.deepEqual([equals], output)
+ test.done()
+
+ "Dot-notation variable" : (test) ->
+ parser = new Parser("{{ foo.bar }}", {})
+ output = parser.parseTokens()
+ equals =
+ type : Parser.TOKEN_TYPES.VAR
+ name : "foo.bar"
+ filters : []
+
+ test.deepEqual([equals], output)
+ test.done()
+
+ "Variable with single filter" : (test) ->
+ parser = new Parser("{{ foo|bar }}", {})
+ output = parser.parseTokens()
+ filters =
+ name : "bar"
+ args : []
+ equals =
+ type : Parser.TOKEN_TYPES.VAR
+ name : "foo"
+ filters : [filters]
+
+ test.deepEqual([equals], output)
+ test.done()
+
+ "Variable with single filter and parameters" : (test) ->
+ parser = new Parser("{{ foo|bar('param',2) }}", {})
+ output = parser.parseTokens()
+ filters =
+ name : "bar"
+ args : ["param",2]
+ equals =
+ type : Parser.TOKEN_TYPES.VAR
+ name : "foo"
+ filters : [filters]
+
+ test.deepEqual([equals], output)
+ test.done()
+
+ "Multiple filters" : (test) ->
+ parser = new Parser("{{ foo|bar(1)|baz|rad('params',2) }}", {})
+ output = parser.parseTokens()
+ filters = [
+ name : "bar"
+ args : [ 1 ],
+ name : "baz"
+ args : [],
+ name : "rad"
+ args : ["params", 2]
+ ]
+ equals =
+ type : Parser.TOKEN_TYPES.VAR
+ name : "foo"
+ filters : filters
+
+ test.deepEqual([equals], output)
+ test.done()
+
+ "Filters do not carry over" : (test) ->
+ parser = new Parser("{{ foo|bar}}{{ baz }}", {})
+ output = parser.parseTokens()
+ filters =
+ name : "bar"
+ args : []
+
+ firstVar =
+ type : Parser.TOKEN_TYPES.VAR
+ name : "foo"
+ filters : [filters]
+
+ secondVar =
+ type : Parser.TOKEN_TYPES.VAR
+ name : "baz"
+ filters : []
+
+ test.deepEqual([firstVar, secondVar],output)
+ test.done()

0 comments on commit ffc161a

Please sign in to comment.