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

Side by Side Diff: Source/core/css/parser/MediaQueryTokenizerTest.cpp

Issue 225293006: Moved MQ parsing block tracking to tokenizer (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Debug build fix Created 6 years, 8 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 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/MediaQueryTokenizer.h" 6 #include "core/css/parser/MediaQueryTokenizer.h"
7 7
8 #include "wtf/PassOwnPtr.h" 8 #include "wtf/PassOwnPtr.h"
9 #include <gtest/gtest.h> 9 #include <gtest/gtest.h>
10 10
11 namespace WebCore { 11 namespace WebCore {
12 12
13 typedef struct { 13 typedef struct {
14 const char* input; 14 const char* input;
15 const char* output; 15 const char* output;
16 } TestCase; 16 } TestCase;
17 17
18 typedef struct {
19 const char* input;
20 const unsigned maxLevel;
21 } BlockTestCase;
22
18 TEST(MediaQueryTokenizerTest, Basic) 23 TEST(MediaQueryTokenizerTest, Basic)
19 { 24 {
20 TestCase testCases[] = { 25 TestCase testCases[] = {
21 { "(max-width: 50px)", "(max-width: 50px)" }, 26 { "(max-width: 50px)", "(max-width: 50px)" },
22 { "(max-width: 50\\70\\78)", "(max-width: 50px)" }, 27 { "(max-width: 50\\70\\78)", "(max-width: 50px)" },
23 { "(max-width: /* comment */50px)", "(max-width: 50px)" }, 28 { "(max-width: /* comment */50px)", "(max-width: 50px)" },
24 { "(max-width: /** *commen*t */60px)", "(max-width: 60px)" }, 29 { "(max-width: /** *commen*t */60px)", "(max-width: 60px)" },
25 { "(max-width: /** *commen*t **/70px)", "(max-width: 70px)" }, 30 { "(max-width: /** *commen*t **/70px)", "(max-width: 70px)" },
26 { "(max-width: /** *commen*t **//**/80px)", "(max-width: 80px)" }, 31 { "(max-width: /** *commen*t **//**/80px)", "(max-width: 80px)" },
27 { "(max-width: /*/ **/90px)", "(max-width: 90px)" }, 32 { "(max-width: /*/ **/90px)", "(max-width: 90px)" },
(...skipping 16 matching lines...) Expand all
44 for (int i = 0; testCases[i].input; ++i) { 49 for (int i = 0; testCases[i].input; ++i) {
45 Vector<MediaQueryToken> tokens; 50 Vector<MediaQueryToken> tokens;
46 MediaQueryTokenizer::tokenize(testCases[i].input, tokens); 51 MediaQueryTokenizer::tokenize(testCases[i].input, tokens);
47 StringBuilder output; 52 StringBuilder output;
48 for (size_t j = 0; j < tokens.size(); ++j) 53 for (size_t j = 0; j < tokens.size(); ++j)
49 output.append(tokens[j].textForUnitTests()); 54 output.append(tokens[j].textForUnitTests());
50 ASSERT_STREQ(testCases[i].output, output.toString().ascii().data()); 55 ASSERT_STREQ(testCases[i].output, output.toString().ascii().data());
51 } 56 }
52 } 57 }
53 58
59 TEST(MediaQueryTokenizerBlockTest, Basic)
60 {
61 BlockTestCase testCases[] = {
62 {"(max-width: 800px()), (max-width: 800px)", 2},
63 {"(max-width: 900px(()), (max-width: 900px)", 3},
64 {"(max-width: 600px(())))), (max-width: 600px)", 3},
65 {"(max-width: 500px(((((((((())))), (max-width: 500px)", 11},
66 {"(max-width: 800px[]), (max-width: 800px)", 2},
67 {"(max-width: 900px[[]), (max-width: 900px)", 3},
68 {"(max-width: 600px[[]]]]), (max-width: 600px)", 3},
69 {"(max-width: 500px[[[[[[[[[[]]]]), (max-width: 500px)", 11},
70 {"(max-width: 800px{}), (max-width: 800px)", 2},
71 {"(max-width: 900px{{}), (max-width: 900px)", 3},
72 {"(max-width: 600px{{}}}}), (max-width: 600px)", 3},
73 {"(max-width: 500px{{{{{{{{{{}}}}), (max-width: 500px)", 11},
74 {"[(), (max-width: 400px)", 2},
75 {"[{}, (max-width: 500px)", 2},
76 {"[{]}], (max-width: 900px)", 2},
77 {"[{[]{}{{{}}}}], (max-width: 900px)", 5},
78 {"[{[}], (max-width: 900px)", 3},
79 {"[({)}], (max-width: 900px)", 3},
80 {"[]((), (max-width: 900px)", 2},
81 {"((), (max-width: 900px)", 2},
82 {"(foo(), (max-width: 900px)", 2},
83 {"[](()), (max-width: 900px)", 2},
84 {"all an[isdfs bla())(i())]icalc(i)(()), (max-width: 400px)", 3},
85 {"all an[isdfs bla())(]icalc(i)(()), (max-width: 500px)", 4},
86 {"all an[isdfs bla())(]icalc(i)(())), (max-width: 600px)", 4},
87 {"all an[isdfs bla())(]icalc(i)(()))], (max-width: 800px)", 4},
88 {0, 0} // Do not remove the terminator line.
89 };
90 for (int i = 0; testCases[i].input; ++i) {
91 Vector<MediaQueryToken> tokens;
92 MediaQueryTokenizer::tokenize(testCases[i].input, tokens);
93
94 unsigned maxLevel = 0;
95 unsigned level = 0;
96 for (size_t j = 0; j < tokens.size(); ++j) {
97 if (tokens[j].blockToken() == MediaQueryToken::BlockStart)
98 ++level;
99 else if (tokens[j].blockToken() == MediaQueryToken::BlockEnd)
100 --level;
101
102 ASSERT_GE(level, (unsigned)0);
103 maxLevel = std::max(level, maxLevel);
104 }
105 ASSERT_EQ(testCases[i].maxLevel, maxLevel);
106 }
107 }
108
54 void testToken(UChar c, MediaQueryTokenType tokenType) 109 void testToken(UChar c, MediaQueryTokenType tokenType)
55 { 110 {
56 Vector<MediaQueryToken> tokens; 111 Vector<MediaQueryToken> tokens;
57 StringBuilder input; 112 StringBuilder input;
58 input.append(c); 113 input.append(c);
59 MediaQueryTokenizer::tokenize(input.toString(), tokens); 114 MediaQueryTokenizer::tokenize(input.toString(), tokens);
60 ASSERT_EQ(tokens[0].type(), tokenType); 115 ASSERT_EQ(tokens[0].type(), tokenType);
61 } 116 }
62 117
63 TEST(MediaQueryTokenizerCodepointsTest, Basic) 118 TEST(MediaQueryTokenizerCodepointsTest, Basic)
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 testToken(c, EOFToken); 152 testToken(c, EOFToken);
98 else if (c > SCHAR_MAX) 153 else if (c > SCHAR_MAX)
99 testToken(c, IdentToken); 154 testToken(c, IdentToken);
100 else 155 else
101 testToken(c, DelimiterToken); 156 testToken(c, DelimiterToken);
102 } 157 }
103 testToken(USHRT_MAX, IdentToken); 158 testToken(USHRT_MAX, IdentToken);
104 } 159 }
105 160
106 } // namespace 161 } // namespace
OLDNEW
« Source/core/css/parser/MediaQueryToken.h ('K') | « Source/core/css/parser/MediaQueryTokenizer.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698