OLD | NEW |
1 // Copyright 2011 The Go Authors. All rights reserved. | 1 // Copyright 2011 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package template | 5 package template |
6 | 6 |
7 import ( | 7 import ( |
8 "reflect" | 8 "reflect" |
9 "testing" | 9 "testing" |
10 ) | 10 ) |
(...skipping 17 matching lines...) Expand all Loading... |
28 ) | 28 ) |
29 | 29 |
30 var lexTests = []lexTest{ | 30 var lexTests = []lexTest{ |
31 {"empty", "", []item{tEOF}}, | 31 {"empty", "", []item{tEOF}}, |
32 {"spaces", " \t\n", []item{{itemText, " \t\n"}, tEOF}}, | 32 {"spaces", " \t\n", []item{{itemText, " \t\n"}, tEOF}}, |
33 {"text", `now is the time`, []item{{itemText, "now is the time"}, tEOF}}
, | 33 {"text", `now is the time`, []item{{itemText, "now is the time"}, tEOF}}
, |
34 {"empty action", `{{}}`, []item{tLeft, tRight, tEOF}}, | 34 {"empty action", `{{}}`, []item{tLeft, tRight, tEOF}}, |
35 {"for", `{{for }}`, []item{tLeft, tFor, tRight, tEOF}}, | 35 {"for", `{{for }}`, []item{tLeft, tFor, tRight, tEOF}}, |
36 {"quote", `{{"abc \n\t\" "}}`, []item{tLeft, tQuote, tRight, tEOF}}, | 36 {"quote", `{{"abc \n\t\" "}}`, []item{tLeft, tQuote, tRight, tEOF}}, |
37 {"raw quote", "{{" + raw + "}}", []item{tLeft, tRawQuote, tRight, tEOF}}
, | 37 {"raw quote", "{{" + raw + "}}", []item{tLeft, tRawQuote, tRight, tEOF}}
, |
38 » {"numbers", "{{1 02 0x14 -7.2i 1e3 +1.2e-4}}", []item{ | 38 » {"numbers", "{{1 02 0x14 -7.2i 1e3 +1.2e-4 4.2i 1+2i}}", []item{ |
39 tLeft, | 39 tLeft, |
40 {itemNumber, "1"}, | 40 {itemNumber, "1"}, |
41 {itemNumber, "02"}, | 41 {itemNumber, "02"}, |
42 {itemNumber, "0x14"}, | 42 {itemNumber, "0x14"}, |
43 {itemNumber, "-7.2i"}, | 43 {itemNumber, "-7.2i"}, |
44 {itemNumber, "1e3"}, | 44 {itemNumber, "1e3"}, |
45 {itemNumber, "+1.2e-4"}, | 45 {itemNumber, "+1.2e-4"}, |
| 46 {itemNumber, "4.2i"}, |
| 47 {itemComplex, "1+2i"}, |
46 tRight, | 48 tRight, |
47 tEOF, | 49 tEOF, |
48 }}, | 50 }}, |
49 {"bools", "{{true false}}", []item{ | 51 {"bools", "{{true false}}", []item{ |
50 tLeft, | 52 tLeft, |
51 {itemBool, "true"}, | 53 {itemBool, "true"}, |
52 {itemBool, "false"}, | 54 {itemBool, "false"}, |
53 tRight, | 55 tRight, |
54 tEOF, | 56 tEOF, |
55 }}, | 57 }}, |
56 {"dot", "{{.}}", []item{ | 58 {"dot", "{{.}}", []item{ |
57 tLeft, | 59 tLeft, |
58 {itemDot, "."}, | 60 {itemDot, "."}, |
59 tRight, | 61 tRight, |
60 tEOF, | 62 tEOF, |
61 }}, | 63 }}, |
62 {"dots", "{{.x . .2 .x.y}}", []item{ | 64 {"dots", "{{.x . .2 .x.y}}", []item{ |
63 tLeft, | 65 tLeft, |
64 {itemField, ".x"}, | 66 {itemField, ".x"}, |
65 {itemDot, "."}, | 67 {itemDot, "."}, |
66 {itemNumber, ".2"}, | 68 {itemNumber, ".2"}, |
67 {itemField, ".x.y"}, | 69 {itemField, ".x.y"}, |
68 tRight, | 70 tRight, |
69 tEOF, | 71 tEOF, |
70 }}, | 72 }}, |
71 » {"keywords", "{{range if else end}}", []item{ | 73 » {"keywords", "{{range if else end with}}", []item{ |
72 tLeft, | 74 tLeft, |
73 {itemRange, "range"}, | 75 {itemRange, "range"}, |
74 {itemIf, "if"}, | 76 {itemIf, "if"}, |
75 {itemElse, "else"}, | 77 {itemElse, "else"}, |
76 {itemEnd, "end"}, | 78 {itemEnd, "end"}, |
| 79 {itemWith, "with"}, |
77 tRight, | 80 tRight, |
78 tEOF, | 81 tEOF, |
79 }}, | 82 }}, |
80 {"pipeline", `intro {{echo hi 1.2 |noargs|args 1 "hi"}} outro`, []item{ | 83 {"pipeline", `intro {{echo hi 1.2 |noargs|args 1 "hi"}} outro`, []item{ |
81 {itemText, "intro "}, | 84 {itemText, "intro "}, |
82 tLeft, | 85 tLeft, |
83 {itemIdentifier, "echo"}, | 86 {itemIdentifier, "echo"}, |
84 {itemIdentifier, "hi"}, | 87 {itemIdentifier, "hi"}, |
85 {itemNumber, "1.2"}, | 88 {itemNumber, "1.2"}, |
86 tPipe, | 89 tPipe, |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 } | 135 } |
133 | 136 |
134 func TestLex(t *testing.T) { | 137 func TestLex(t *testing.T) { |
135 for _, test := range lexTests { | 138 for _, test := range lexTests { |
136 items := collect(&test) | 139 items := collect(&test) |
137 if !reflect.DeepEqual(items, test.items) { | 140 if !reflect.DeepEqual(items, test.items) { |
138 t.Errorf("%s: got\n\t%v\nexpected\n\t%v", test.name, ite
ms, test.items) | 141 t.Errorf("%s: got\n\t%v\nexpected\n\t%v", test.name, ite
ms, test.items) |
139 } | 142 } |
140 } | 143 } |
141 } | 144 } |
OLD | NEW |