 Chromium Code Reviews
 Chromium Code Reviews Issue 252743004:
  A thread safe CSS calc parser for sizes  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/blink.git@master
    
  
    Issue 252743004:
  A thread safe CSS calc parser for sizes  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/blink.git@master| Index: Source/core/css/parser/SizesCalcParserTest.cpp | 
| diff --git a/Source/core/css/parser/SizesCalcParserTest.cpp b/Source/core/css/parser/SizesCalcParserTest.cpp | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..7ca6bc133e975671f2bb9fe7804e8ca8e52ca7f1 | 
| --- /dev/null | 
| +++ b/Source/core/css/parser/SizesCalcParserTest.cpp | 
| @@ -0,0 +1,86 @@ | 
| +// Copyright 2014 The Chromium Authors. All rights reserved. | 
| +// Use of this source code is governed by a BSD-style license that can be | 
| +// found in the LICENSE file. | 
| + | 
| +#include "config.h" | 
| +#include "core/css/parser/SizesCalcParser.h" | 
| + | 
| +#include "core/css/MediaValuesCached.h" | 
| +#include "core/css/parser/MediaQueryTokenizer.h" | 
| + | 
| +#include <gtest/gtest.h> | 
| + | 
| +namespace WebCore { | 
| + | 
| +typedef struct { | 
| + const char* input; | 
| + const unsigned output; | 
| + const bool valid; | 
| +} TestCase; | 
| 
alancutter (OOO until 2018)
2014/04/30 04:02:17
struct TestCase {}; notation would be preferred.
 | 
| + | 
| +TEST(SizesCalcParserTest, Basic) | 
| +{ | 
| + // The first string represents the input string. | 
| + // The second string represents the output string, if present. | 
| + // Otherwise, the output string is identical to the first string. | 
| 
alancutter (OOO until 2018)
2014/04/30 04:02:17
This comment doesn't seem to match the struct. The
 | 
| + TestCase testCases[] = { | 
| + {"calc(500px + 10em)", 660, true}, | 
| + {"calc(500px + 2 * 10em)", 820, true}, | 
| + {"calc(500px + 2*10em)", 820, true}, | 
| + {"calc(500px + 0.5*10em)", 580, true}, | 
| + {"calc(500px + (0.5*10em + 13px))", 593, true}, | 
| + {"calc(100vw + (0.5*10em + 13px))", 593, true}, | 
| + {"calc(100vh + (0.5*10em + 13px))", 736, true}, | 
| + {"calc(100vh + calc(0.5*10em + 13px))", 736, true}, | 
| + {"calc(100vh + (50%*10em + 13px))", 0, false}, | 
| + {"calc(50em+13px)", 0, false}, | 
| + {"calc(50em-13px)", 0, false}, | 
| + {"calc(500px + 10)", 0, false}, | 
| + {"calc(500 + 10)", 0, false}, | 
| + {"calc(500px + 10s)", 0, false}, | 
| + {"calc(500px + 1cm)", 537, true}, | 
| + {"calc(500px - 10s)", 0, false}, | 
| + {"calc(500px - 1cm)", 463, true}, | 
| + {"calc(50px*10)", 500, true}, | 
| + {"calc(50px*10px)", 0, false}, | 
| + {"calc(50px/10px)", 0, false}, | 
| + {"calc(500px/10)", 50, true}, | 
| + {"calc(500/10)", 0, false}, | 
| + {"calc(500px/0.5)", 1000, true}, | 
| + {"calc(500px/.5)", 1000, true}, | 
| + {"calc(500/0)", 0, false}, | 
| + {"calc(500px/0)", 0, false}, | 
| + {"calc(-500px/10)", 0, true}, | 
| + {0, 0, true} // Do not remove the terminator line. | 
| 
alancutter (OOO until 2018)
2014/04/30 04:02:17
It would be good to see more invalid inputs tested
 | 
| + }; | 
| + | 
| + | 
| + MediaValuesCached::MediaValuesCachedData data; | 
| + data.viewportWidth = 500; | 
| + data.viewportHeight = 643; | 
| + data.deviceWidth = 500; | 
| + data.deviceHeight = 643; | 
| + data.devicePixelRatio = 2.0; | 
| + data.colorBitsPerComponent = 24; | 
| + data.monochromeBitsPerComponent = 0; | 
| + data.pointer = MediaValues::MousePointer; | 
| + data.defaultFontSize = 16; | 
| + data.threeDEnabled = true; | 
| + data.scanMediaType = false; | 
| + data.screenMediaType = true; | 
| + data.printMediaType = false; | 
| + data.strictMode = true; | 
| + RefPtr<MediaValues> mediaValues = MediaValuesCached::create(data); | 
| + | 
| + for (unsigned i = 0; testCases[i].input; ++i) { | 
| + Vector<MediaQueryToken> tokens; | 
| + MediaQueryTokenizer::tokenize(testCases[i].input, tokens); | 
| + unsigned output; | 
| + bool valid = SizesCalcParser::parse(tokens.begin(), tokens.end(), mediaValues, output); | 
| + ASSERT_EQ(testCases[i].valid, valid); | 
| + if (valid) | 
| + ASSERT_EQ(testCases[i].output, output); | 
| + } | 
| +} | 
| + | 
| +} // namespace |