summaryrefslogtreecommitdiff
path: root/core/fxcrt/css/cfx_cssvaluelistparser_unittest.cpp
blob: 9d19d275f64bc9efddbbd88984e7eb888e644007 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright 2017 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com

#include "core/fxcrt/css/cfx_cssvaluelistparser.h"

#include "core/fxcrt/widestring.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/test_support.h"
#include "third_party/base/ptr_util.h"

TEST(CFX_CSSValueListParserTest, rgb_short) {
  CFX_CSSPrimitiveType type;
  const wchar_t* start;
  int32_t len;

  auto parser = pdfium::MakeUnique<CFX_CSSValueListParser>(L"#abc", 4, L' ');
  EXPECT_TRUE(parser->NextValue(&type, &start, &len));
  EXPECT_EQ(CFX_CSSPrimitiveType::RGB, type);
  EXPECT_EQ(L"#abc", WideString(start, len));
  EXPECT_FALSE(parser->NextValue(&type, &start, &len));

  parser = pdfium::MakeUnique<CFX_CSSValueListParser>(L"#abcdef", 7, L' ');
  EXPECT_TRUE(parser->NextValue(&type, &start, &len));
  EXPECT_EQ(CFX_CSSPrimitiveType::RGB, type);
  EXPECT_EQ(L"#abcdef", WideString(start, len));
  EXPECT_FALSE(parser->NextValue(&type, &start, &len));

  parser =
      pdfium::MakeUnique<CFX_CSSValueListParser>(L"rgb(1, 255, 4)", 14, L' ');
  EXPECT_TRUE(parser->NextValue(&type, &start, &len));
  EXPECT_EQ(CFX_CSSPrimitiveType::RGB, type);
  EXPECT_EQ(L"rgb(1, 255, 4)", WideString(start, len));

  parser = pdfium::MakeUnique<CFX_CSSValueListParser>(L"#abcdefghij", 11, L' ');
  EXPECT_TRUE(parser->NextValue(&type, &start, &len));
  EXPECT_EQ(CFX_CSSPrimitiveType::Unknown, type);
  EXPECT_EQ(L"#abcdefghij", WideString(start, len));
  EXPECT_FALSE(parser->NextValue(&type, &start, &len));
}

TEST(CFX_CSSValueListParserTest, number_parsing) {
  CFX_CSSPrimitiveType type;
  const wchar_t* start;
  int32_t len;

  auto parser = pdfium::MakeUnique<CFX_CSSValueListParser>(L"1234", 4, L' ');
  EXPECT_TRUE(parser->NextValue(&type, &start, &len));
  EXPECT_EQ(CFX_CSSPrimitiveType::Number, type);
  EXPECT_EQ(L"1234", WideString(start, len));

  parser = pdfium::MakeUnique<CFX_CSSValueListParser>(L"-1234", 5, L' ');
  EXPECT_TRUE(parser->NextValue(&type, &start, &len));
  EXPECT_EQ(CFX_CSSPrimitiveType::Number, type);
  EXPECT_EQ(L"-1234", WideString(start, len));

  parser = pdfium::MakeUnique<CFX_CSSValueListParser>(L"+1234", 5, L' ');
  EXPECT_TRUE(parser->NextValue(&type, &start, &len));
  EXPECT_EQ(CFX_CSSPrimitiveType::Number, type);
  EXPECT_EQ(L"+1234", WideString(start, len));

  parser = pdfium::MakeUnique<CFX_CSSValueListParser>(L".1234", 5, L' ');
  EXPECT_TRUE(parser->NextValue(&type, &start, &len));
  EXPECT_EQ(CFX_CSSPrimitiveType::Number, type);
  EXPECT_EQ(L".1234", WideString(start, len));

  parser = pdfium::MakeUnique<CFX_CSSValueListParser>(L"4321.1234", 9, L' ');
  EXPECT_TRUE(parser->NextValue(&type, &start, &len));
  EXPECT_EQ(CFX_CSSPrimitiveType::Number, type);
  EXPECT_EQ(L"4321.1234", WideString(start, len));

  // TODO(dsinclair): These should probably fail but currently don't.
  parser = pdfium::MakeUnique<CFX_CSSValueListParser>(L"4321.12.34", 10, L' ');
  EXPECT_TRUE(parser->NextValue(&type, &start, &len));
  EXPECT_EQ(CFX_CSSPrimitiveType::Number, type);
  EXPECT_EQ(L"4321.12.34", WideString(start, len));

  parser = pdfium::MakeUnique<CFX_CSSValueListParser>(L"43a1.12.34", 10, L' ');
  EXPECT_TRUE(parser->NextValue(&type, &start, &len));
  EXPECT_EQ(CFX_CSSPrimitiveType::Number, type);
  EXPECT_EQ(L"43a1.12.34", WideString(start, len));
}

TEST(CFX_CSSValueListParserTest, string_parsing) {
  CFX_CSSPrimitiveType type;
  const wchar_t* start;
  int32_t len;

  auto parser =
      pdfium::MakeUnique<CFX_CSSValueListParser>(L"'string'", 8, L' ');
  EXPECT_TRUE(parser->NextValue(&type, &start, &len));
  EXPECT_EQ(CFX_CSSPrimitiveType::String, type);
  EXPECT_EQ(L"string", WideString(start, len));

  parser = pdfium::MakeUnique<CFX_CSSValueListParser>(L"\"another string\"", 16,
                                                      L' ');
  EXPECT_TRUE(parser->NextValue(&type, &start, &len));
  EXPECT_EQ(CFX_CSSPrimitiveType::String, type);
  EXPECT_EQ(L"another string", WideString(start, len));

  parser = pdfium::MakeUnique<CFX_CSSValueListParser>(L"standalone", 10, L' ');
  EXPECT_TRUE(parser->NextValue(&type, &start, &len));
  EXPECT_EQ(CFX_CSSPrimitiveType::String, type);
  EXPECT_EQ(L"standalone", WideString(start, len));
}

TEST(CFX_CSSValueListParserTest, multiparsing) {
  CFX_CSSPrimitiveType type;
  const wchar_t* start;
  int32_t len;

  auto parser = pdfium::MakeUnique<CFX_CSSValueListParser>(L"1, 2, 3", 7, L',');
  EXPECT_TRUE(parser->NextValue(&type, &start, &len));
  EXPECT_EQ(CFX_CSSPrimitiveType::Number, type);
  EXPECT_EQ(L"1", WideString(start, len));

  EXPECT_TRUE(parser->NextValue(&type, &start, &len));
  EXPECT_EQ(CFX_CSSPrimitiveType::Number, type);
  EXPECT_EQ(L"2", WideString(start, len));

  EXPECT_TRUE(parser->NextValue(&type, &start, &len));
  EXPECT_EQ(CFX_CSSPrimitiveType::Number, type);
  EXPECT_EQ(L"3", WideString(start, len));

  EXPECT_FALSE(parser->NextValue(&type, &start, &len));

  parser = pdfium::MakeUnique<CFX_CSSValueListParser>(L"'str', rgb(1, 2, 3), 4",
                                                      22, L',');
  EXPECT_TRUE(parser->NextValue(&type, &start, &len));
  EXPECT_EQ(CFX_CSSPrimitiveType::String, type);
  EXPECT_EQ(L"str", WideString(start, len));

  EXPECT_TRUE(parser->NextValue(&type, &start, &len));
  EXPECT_EQ(CFX_CSSPrimitiveType::RGB, type);
  EXPECT_EQ(L"rgb(1, 2, 3)", WideString(start, len));

  EXPECT_TRUE(parser->NextValue(&type, &start, &len));
  EXPECT_EQ(CFX_CSSPrimitiveType::Number, type);
  EXPECT_EQ(L"4", WideString(start, len));
}