1 // Copyright 2017 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "fxjs/cjs_util.h"
6
7 #include "testing/gtest/include/gtest/gtest.h"
8
TEST(CJS_Util,ParseDataType)9 TEST(CJS_Util, ParseDataType) {
10 struct ParseDataTypeCase {
11 const wchar_t* const input_string;
12 const int expected;
13 };
14
15 // Commented out tests follow the spec but are not passing.
16 const ParseDataTypeCase cases[] = {
17 // Not conversions
18 {L"", -1},
19 {L"d", -1},
20
21 // Simple cases
22 {L"%d", UTIL_INT},
23 {L"%x", UTIL_INT},
24 {L"%f", UTIL_DOUBLE},
25 {L"%s", UTIL_STRING},
26
27 // nDecSep Not implemented
28 // {L"%,0d", UTIL_INT},
29 // {L"%,1d", UTIL_INT},
30 // {L"%,2d", UTIL_INT},
31 // {L"%,3d", UTIL_INT},
32 // {L"%,4d", -1},
33 // {L"%,d", -1},
34
35 // cFlags("+ 0#"") are only valid for numeric conversions.
36 {L"%+d", UTIL_INT},
37 {L"%+x", UTIL_INT},
38 {L"%+f", UTIL_DOUBLE},
39 // {L"%+s", -1},
40 {L"% d", UTIL_INT},
41 {L"% x", UTIL_INT},
42 {L"% f", UTIL_DOUBLE},
43 // {L"% s", -1},
44 {L"%0d", UTIL_INT},
45 {L"%0x", UTIL_INT},
46 {L"%0f", UTIL_DOUBLE},
47 // {L"%0s", -1},
48 {L"%#d", UTIL_INT},
49 {L"%#x", UTIL_INT},
50 {L"%#f", UTIL_DOUBLE},
51 // {L"%#s", -1},
52
53 // nWidth should work. for all conversions, can be combined with cFlags=0
54 // for numbers.
55 {L"%5d", UTIL_INT},
56 {L"%05d", UTIL_INT},
57 {L"%5x", UTIL_INT},
58 {L"%05x", UTIL_INT},
59 {L"%5f", UTIL_DOUBLE},
60 {L"%05f", UTIL_DOUBLE},
61 {L"%5s", UTIL_STRING},
62 // {L"%05s", -1},
63
64 // nPrecision should only work for float
65 // {L"%.5d", -1},
66 // {L"%.5x", -1},
67 {L"%.5f", UTIL_DOUBLE},
68 // {L"%.5s", -1},
69 // {L"%.14d", -1},
70 // {L"%.14x", -1},
71 {L"%.14f", UTIL_DOUBLE},
72 // {L"%.14s", -1},
73 // {L"%.f", -1},
74
75 // See https://crbug.com/740166
76 // nPrecision too large (> 260) causes crashes in Windows.
77 // Avoid this by limiting to two digits
78 {L"%.1d", UTIL_INT},
79 {L"%.10d", UTIL_INT},
80 {L"%.100d", -1},
81
82 // Unexpected characters
83 {L"%ad", -1},
84 {L"%bx", -1},
85 // {L"%cf", -1},
86 // {L"%es", -1},
87 // {L"%gd", -1},
88 {L"%hx", -1},
89 // {L"%if", -1},
90 {L"%js", -1},
91 {L"%@d", -1},
92 {L"%~x", -1},
93 {L"%[f", -1},
94 {L"%\0s", -1},
95 {L"%\nd", -1},
96 {L"%\rx", -1},
97 // {L"%%f", -1},
98 // {L"% s", -1},
99
100 // Combine multiple valid components
101 {L"%+6d", UTIL_INT},
102 {L"% 7x", UTIL_INT},
103 {L"%#9.3f", UTIL_DOUBLE},
104 {L"%10s", UTIL_STRING},
105 };
106
107 for (size_t i = 0; i < FX_ArraySize(cases); i++) {
108 WideString input(cases[i].input_string);
109 EXPECT_EQ(cases[i].expected, CJS_Util::ParseDataType(&input))
110 << cases[i].input_string;
111 }
112 }
113