Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(792)

Side by Side Diff: mojom/mojom_parser/lexer/lexer_test.go

Issue 1593543004: Update the mojom lexer to emit comment tokens. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package lexer 5 package lexer
6 6
7 import ( 7 import (
8 "strings" 8 "strings"
9 "testing" 9 "testing"
10 ) 10 )
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 {"10e+5", FloatConst}, 73 {"10e+5", FloatConst},
74 {"10e-5", FloatConst}, 74 {"10e-5", FloatConst},
75 {"3.14E5", FloatConst}, 75 {"3.14E5", FloatConst},
76 {"3.14e5", FloatConst}, 76 {"3.14e5", FloatConst},
77 {"3.14E+55", FloatConst}, 77 {"3.14E+55", FloatConst},
78 {"3.14e+55", FloatConst}, 78 {"3.14e+55", FloatConst},
79 {"3.14E-55", FloatConst}, 79 {"3.14E-55", FloatConst},
80 {"3.14e-55", FloatConst}, 80 {"3.14e-55", FloatConst},
81 {"\"hello world\"", StringLiteral}, 81 {"\"hello world\"", StringLiteral},
82 {"\"hello \\\"real\\\" world\"", StringLiteral}, 82 {"\"hello \\\"real\\\" world\"", StringLiteral},
83 {"// hello comment world", SingleLineComment},
84 {"/* hello \n comment */", MultiLineComment},
83 } 85 }
84 86
85 for i := range testData { 87 for i := range testData {
86 l := lexer{source: testData[i].source, tokens: make(chan Token)} 88 l := lexer{source: testData[i].source, tokens: make(chan Token)}
87 go l.run() 89 go l.run()
88 tokens := pumpTokens(l.tokens) 90 tokens := pumpTokens(l.tokens)
89 91
90 if len(tokens) != 1 { 92 if len(tokens) != 1 {
91 t.Fatalf("Source('%v'): Expected 1 token but got %v inst ead: %v", 93 t.Fatalf("Source('%v'): Expected 1 token but got %v inst ead: %v",
92 testData[i].source, len(tokens), tokens) 94 testData[i].source, len(tokens), tokens)
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 checkEq(t, ErrorUnterminatedStringLiteral, ts.PeekNext().Kind) 181 checkEq(t, ErrorUnterminatedStringLiteral, ts.PeekNext().Kind)
180 } 182 }
181 183
182 // TestUnterminatedStringLiteralEol tests that the correct error is emitted if 184 // TestUnterminatedStringLiteralEol tests that the correct error is emitted if
183 // a quoted string is closed on a subsequent line. 185 // a quoted string is closed on a subsequent line.
184 func TestUnterminatedStringLiteralEol(t *testing.T) { 186 func TestUnterminatedStringLiteralEol(t *testing.T) {
185 ts := Tokenize("\"hello\n world\"") 187 ts := Tokenize("\"hello\n world\"")
186 checkEq(t, ErrorUnterminatedStringLiteral, ts.PeekNext().Kind) 188 checkEq(t, ErrorUnterminatedStringLiteral, ts.PeekNext().Kind)
187 } 189 }
188 190
189 // TestSingleLineComment tests that single line comments are correctly skipped. 191 // TestSingleLineComment tests that single line comments are correctly lexed.
rudominer 2016/01/16 01:08:58 Don't you want to test both that they are skipped
azani 2016/01/20 00:00:11 Done.
190 func TestSingleLineComment(t *testing.T) { 192 func TestSingleLineComment(t *testing.T) {
191 » ts := Tokenize("( // some stuff\n)") 193 » ts := tokenizeUnfiltered("( // some stuff\n)")
192 checkEq(t, LParen, ts.PeekNext().Kind) 194 checkEq(t, LParen, ts.PeekNext().Kind)
193 ts.ConsumeNext() 195 ts.ConsumeNext()
196 checkEq(t, SingleLineComment, ts.PeekNext().Kind)
197 checkEq(t, "// some stuff", ts.PeekNext().Text)
198 ts.ConsumeNext()
194 checkEq(t, RParen, ts.PeekNext().Kind) 199 checkEq(t, RParen, ts.PeekNext().Kind)
195 } 200 }
196 201
197 // TestMultiLineComment tests that multi line comments are correctly skipped. 202 // TestMultiLineComment tests that multi line comments are correctly skipped.
rudominer 2016/01/16 01:08:58 skipped?
azani 2016/01/20 00:00:11 Done.
198 func TestMultiLineComment(t *testing.T) { 203 func TestMultiLineComment(t *testing.T) {
199 » ts := Tokenize("( /* hello world/ * *\n */)") 204 » ts := tokenizeUnfiltered("( /* hello world/ * *\n */)")
200 checkEq(t, LParen, ts.PeekNext().Kind) 205 checkEq(t, LParen, ts.PeekNext().Kind)
201 ts.ConsumeNext() 206 ts.ConsumeNext()
207 checkEq(t, MultiLineComment, ts.PeekNext().Kind)
208 checkEq(t, "/* hello world/ * *\n */", ts.PeekNext().Text)
209 ts.ConsumeNext()
202 checkEq(t, RParen, ts.PeekNext().Kind) 210 checkEq(t, RParen, ts.PeekNext().Kind)
203 } 211 }
204 212
205 // TestUnterminatedMultiLineComment tests that unterminated multiline comments 213 // TestUnterminatedMultiLineComment tests that unterminated multiline comments
206 // emit the correct error. 214 // emit the correct error.
207 func TestUnterminatedMultiLineComment(t *testing.T) { 215 func TestUnterminatedMultiLineComment(t *testing.T) {
208 ts := Tokenize("( /* hello world/ * *\n )") 216 ts := Tokenize("( /* hello world/ * *\n )")
209 checkEq(t, LParen, ts.PeekNext().Kind) 217 checkEq(t, LParen, ts.PeekNext().Kind)
210 ts.ConsumeNext() 218 ts.ConsumeNext()
211 checkEq(t, ErrorUnterminatedComment, ts.PeekNext().Kind) 219 checkEq(t, ErrorUnterminatedComment, ts.PeekNext().Kind)
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
266 274
267 // TestTokenSnippetLongPrelude tests snippet generation in the presence of a 275 // TestTokenSnippetLongPrelude tests snippet generation in the presence of a
268 // long line with many characters preceeding the token. 276 // long line with many characters preceeding the token.
269 func TestTokenSnippetLongPrelude(t *testing.T) { 277 func TestTokenSnippetLongPrelude(t *testing.T) {
270 source := strings.Repeat(" ", 70) + "hello world" 278 source := strings.Repeat(" ", 70) + "hello world"
271 ts := Tokenize(source) 279 ts := Tokenize(source)
272 expected := strings.Repeat(" ", 58) + "hello world\n" 280 expected := strings.Repeat(" ", 58) + "hello world\n"
273 expected += strings.Repeat(" ", 58) + "^^^^^" 281 expected += strings.Repeat(" ", 58) + "^^^^^"
274 checkEq(t, expected, ts.PeekNext().Snippet(source, false)) 282 checkEq(t, expected, ts.PeekNext().Snippet(source, false))
275 } 283 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698