summaryrefslogtreecommitdiff
path: root/fpdfsdk/javascript/util_unittest.cpp
blob: eaebc9c7f27266a3b7ea984cd1f7a1e748b0b741 (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
// 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.

#include "fpdfsdk/javascript/util.h"

#include "testing/gtest/include/gtest/gtest.h"
#include "testing/test_support.h"

// Should mirror the defines in util.cpp.
#define UTIL_INT 0
#define UTIL_DOUBLE 1
#define UTIL_STRING 2

TEST(CJS_Util, ParseDataType) {
  struct ParseDataTypeCase {
    const wchar_t* const input_string;
    const int expected;
  };

  // Commented out tests follow the spec but are not passing.
  const ParseDataTypeCase cases[] = {
      // Not conversions
      {L"", -1},
      {L"d", -1},

      // Simple cases
      {L"%d", UTIL_INT},
      {L"%x", UTIL_INT},
      {L"%f", UTIL_DOUBLE},
      {L"%s", UTIL_STRING},

      // nDecSep Not implemented
      // {L"%,0d", UTIL_INT},
      // {L"%,1d", UTIL_INT},
      // {L"%,2d", UTIL_INT},
      // {L"%,3d", UTIL_INT},
      // {L"%,4d", -1},
      // {L"%,d", -1},

      // cFlags("+ 0#"") are only valid for numeric conversions.
      {L"%+d", UTIL_INT},
      {L"%+x", UTIL_INT},
      {L"%+f", UTIL_DOUBLE},
      // {L"%+s", -1},
      {L"% d", UTIL_INT},
      {L"% x", UTIL_INT},
      {L"% f", UTIL_DOUBLE},
      // {L"% s", -1},
      {L"%0d", UTIL_INT},
      {L"%0x", UTIL_INT},
      {L"%0f", UTIL_DOUBLE},
      // {L"%0s", -1},
      {L"%#d", UTIL_INT},
      {L"%#x", UTIL_INT},
      {L"%#f", UTIL_DOUBLE},
      // {L"%#s", -1},

      // nWidth should work. for all conversions, can be combined with cFlags=0
      // for numbers.
      {L"%5d", UTIL_INT},
      {L"%05d", UTIL_INT},
      {L"%5x", UTIL_INT},
      {L"%05x", UTIL_INT},
      {L"%5f", UTIL_DOUBLE},
      {L"%05f", UTIL_DOUBLE},
      {L"%5s", UTIL_STRING},
      // {L"%05s", -1},

      // nPrecision should only work for float
      // {L"%.5d", -1},
      // {L"%.5x", -1},
      {L"%.5f", UTIL_DOUBLE},
      // {L"%.5s", -1},
      // {L"%.14d", -1},
      // {L"%.14x", -1},
      {L"%.14f", UTIL_DOUBLE},
      // {L"%.14s", -1},
      // {L"%.f", -1},

      // nPrecision too large (> 260) causes crashes in Windows.
      // TODO(tsepez): Reenable when fix is out.
      // {L"%.261d", -1},
      // {L"%.261x", -1},
      // {L"%.261f", -1},
      // {L"%.261s", -1},

      // Unexpected characters
      {L"%ad", -1},
      {L"%bx", -1},
      // {L"%cf", -1},
      // {L"%es", -1},
      // {L"%gd", -1},
      {L"%hx", -1},
      // {L"%if", -1},
      {L"%js", -1},
      {L"%@d", -1},
      {L"%~x", -1},
      {L"%[f", -1},
      {L"%\0s", -1},
      {L"%\nd", -1},
      {L"%\rx", -1},
      // {L"%%f", -1},
      // {L"%  s", -1},

      // Combine multiple valid components
      {L"%+6d", UTIL_INT},
      {L"% 7x", UTIL_INT},
      {L"%#9.3f", UTIL_DOUBLE},
      {L"%10s", UTIL_STRING},
  };

  for (size_t i = 0; i < FX_ArraySize(cases); i++) {
    std::wstring input(cases[i].input_string);
    EXPECT_EQ(cases[i].expected, util::ParseDataType(&input))
        << cases[i].input_string;
  }
}