OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include "config.h" | 5 #include "config.h" |
6 #include "core/css/parser/CSSTokenizer.h" | 6 #include "core/css/parser/CSSTokenizer.h" |
7 | 7 |
| 8 #include "core/css/parser/CSSParserTokenRange.h" |
8 #include "core/css/parser/MediaQueryBlockWatcher.h" | 9 #include "core/css/parser/MediaQueryBlockWatcher.h" |
9 #include <gtest/gtest.h> | 10 #include <gtest/gtest.h> |
10 | 11 |
11 namespace blink { | 12 namespace blink { |
12 | 13 |
13 // This let's us see the line numbers of failing tests | 14 // This let's us see the line numbers of failing tests |
14 #define TEST_TOKENS(string, ...) { \ | 15 #define TEST_TOKENS(string, ...) { \ |
15 String s = string; \ | 16 String s = string; \ |
16 SCOPED_TRACE(s.ascii().data()); \ | 17 SCOPED_TRACE(s.ascii().data()); \ |
17 testTokens(string, __VA_ARGS__); \ | 18 testTokens(string, __VA_ARGS__); \ |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 void testTokens(const String& string, const CSSParserToken& token1, const CSSPar
serToken& token2 = CSSParserToken(EOFToken), const CSSParserToken& token3 = CSSP
arserToken(EOFToken)) | 59 void testTokens(const String& string, const CSSParserToken& token1, const CSSPar
serToken& token2 = CSSParserToken(EOFToken), const CSSParserToken& token3 = CSSP
arserToken(EOFToken)) |
59 { | 60 { |
60 Vector<CSSParserToken> expectedTokens; | 61 Vector<CSSParserToken> expectedTokens; |
61 expectedTokens.append(token1); | 62 expectedTokens.append(token1); |
62 if (token2.type() != EOFToken) { | 63 if (token2.type() != EOFToken) { |
63 expectedTokens.append(token2); | 64 expectedTokens.append(token2); |
64 if (token3.type() != EOFToken) | 65 if (token3.type() != EOFToken) |
65 expectedTokens.append(token3); | 66 expectedTokens.append(token3); |
66 } | 67 } |
67 | 68 |
68 Vector<CSSParserToken> actualTokens; | 69 CSSParserTokenRange expected(expectedTokens); |
69 CSSTokenizer::tokenize(string, actualTokens); | |
70 | 70 |
71 ASSERT_EQ(expectedTokens.size(), actualTokens.size()); | 71 CSSTokenizer::Scope actualScope(string); |
72 for (size_t i = 0; i < expectedTokens.size(); ++i) | 72 CSSParserTokenRange actual = actualScope.tokenRange(); |
73 compareTokens(expectedTokens[i], actualTokens[i]); | 73 |
| 74 while (!expected.atEnd() || !actual.atEnd()) |
| 75 compareTokens(expected.consume(), actual.consume()); |
74 } | 76 } |
75 | 77 |
76 static CSSParserToken ident(const String& string) { return CSSParserToken(IdentT
oken, string); } | 78 static CSSParserToken ident(const String& string) { return CSSParserToken(IdentT
oken, string); } |
77 static CSSParserToken atKeyword(const String& string) { return CSSParserToken(At
KeywordToken, string); } | 79 static CSSParserToken atKeyword(const String& string) { return CSSParserToken(At
KeywordToken, string); } |
78 static CSSParserToken string(const String& string) { return CSSParserToken(Strin
gToken, string); } | 80 static CSSParserToken string(const String& string) { return CSSParserToken(Strin
gToken, string); } |
79 static CSSParserToken function(const String& string) { return CSSParserToken(Fun
ctionToken, string); } | 81 static CSSParserToken function(const String& string) { return CSSParserToken(Fun
ctionToken, string); } |
80 static CSSParserToken url(const String& string) { return CSSParserToken(UrlToken
, string); } | 82 static CSSParserToken url(const String& string) { return CSSParserToken(UrlToken
, string); } |
81 static CSSParserToken hash(const String& string, HashTokenType type) { return CS
SParserToken(type, string); } | 83 static CSSParserToken hash(const String& string, HashTokenType type) { return CS
SParserToken(type, string); } |
82 static CSSParserToken delim(char c) { return CSSParserToken(DelimiterToken, c);
} | 84 static CSSParserToken delim(char c) { return CSSParserToken(DelimiterToken, c);
} |
83 static CSSParserToken unicodeRange(UChar32 start, UChar32 end) { return CSSParse
rToken(UnicodeRangeToken, start, end); } | 85 static CSSParserToken unicodeRange(UChar32 start, UChar32 end) { return CSSParse
rToken(UnicodeRangeToken, start, end); } |
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
448 {"((), (max-width: 900px)", 2, 1}, | 450 {"((), (max-width: 900px)", 2, 1}, |
449 {"(foo(), (max-width: 900px)", 2, 1}, | 451 {"(foo(), (max-width: 900px)", 2, 1}, |
450 {"[](()), (max-width: 900px)", 2, 0}, | 452 {"[](()), (max-width: 900px)", 2, 0}, |
451 {"all an[isdfs bla())(i())]icalc(i)(()), (max-width: 400px)", 3, 0}, | 453 {"all an[isdfs bla())(i())]icalc(i)(()), (max-width: 400px)", 3, 0}, |
452 {"all an[isdfs bla())(]icalc(i)(()), (max-width: 500px)", 4, 2}, | 454 {"all an[isdfs bla())(]icalc(i)(()), (max-width: 500px)", 4, 2}, |
453 {"all an[isdfs bla())(]icalc(i)(())), (max-width: 600px)", 4, 1}, | 455 {"all an[isdfs bla())(]icalc(i)(())), (max-width: 600px)", 4, 1}, |
454 {"all an[isdfs bla())(]icalc(i)(()))], (max-width: 800px)", 4, 0}, | 456 {"all an[isdfs bla())(]icalc(i)(()))], (max-width: 800px)", 4, 0}, |
455 {0, 0, 0} // Do not remove the terminator line. | 457 {0, 0, 0} // Do not remove the terminator line. |
456 }; | 458 }; |
457 for (int i = 0; testCases[i].input; ++i) { | 459 for (int i = 0; testCases[i].input; ++i) { |
458 Vector<CSSParserToken> tokens; | 460 CSSTokenizer::Scope scope(testCases[i].input); |
459 CSSTokenizer::tokenize(testCases[i].input, tokens); | 461 CSSParserTokenRange range = scope.tokenRange(); |
460 MediaQueryBlockWatcher blockWatcher; | 462 MediaQueryBlockWatcher blockWatcher; |
461 | 463 |
462 unsigned maxLevel = 0; | 464 unsigned maxLevel = 0; |
463 unsigned level = 0; | 465 unsigned level = 0; |
464 for (size_t j = 0; j < tokens.size(); ++j) { | 466 while (!range.atEnd()) { |
465 blockWatcher.handleToken(tokens[j]); | 467 blockWatcher.handleToken(range.consume()); |
466 level = blockWatcher.blockLevel(); | 468 level = blockWatcher.blockLevel(); |
467 maxLevel = std::max(level, maxLevel); | 469 maxLevel = std::max(level, maxLevel); |
468 } | 470 } |
469 ASSERT_EQ(testCases[i].maxLevel, maxLevel); | 471 ASSERT_EQ(testCases[i].maxLevel, maxLevel); |
470 ASSERT_EQ(testCases[i].finalLevel, level); | 472 ASSERT_EQ(testCases[i].finalLevel, level); |
471 } | 473 } |
472 } | 474 } |
473 | 475 |
474 } // namespace | 476 } // namespace |
OLD | NEW |