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
|
// 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 "xfa/fxfa/fm2js/cxfa_fmparse.h"
#include <vector>
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/test_support.h"
#include "third_party/base/ptr_util.h"
TEST(CXFA_FMParseTest, Empty) {
CXFA_FMErrorInfo errorInfo;
auto parser = pdfium::MakeUnique<CXFA_FMParse>(L"", &errorInfo);
std::unique_ptr<CXFA_FMFunctionDefinition> ast = parser->Parse();
ASSERT(ast != nullptr);
EXPECT_TRUE(errorInfo.message.IsEmpty());
CFX_WideTextBuf buf;
EXPECT_TRUE(ast->ToJavaScript(buf));
// TODO(dsinclair): This is a little weird .....
EXPECT_EQ(L"// comments only", buf.AsStringC());
}
TEST(CXFA_FMParseTest, CommentOnlyIsError) {
CXFA_FMErrorInfo errorInfo;
auto parser = pdfium::MakeUnique<CXFA_FMParse>(L"; Just comment", &errorInfo);
std::unique_ptr<CXFA_FMFunctionDefinition> ast = parser->Parse();
ASSERT(ast != nullptr);
// TODO(dsinclair): This isn't allowed per the spec.
EXPECT_TRUE(errorInfo.message.IsEmpty());
// EXPECT_FALSE(errorInfo.message.IsEmpty());
CFX_WideTextBuf buf;
EXPECT_TRUE(ast->ToJavaScript(buf));
EXPECT_EQ(L"// comments only", buf.AsStringC());
}
TEST(CXFA_FMParseTest, CommentThenValue) {
CXFA_FMErrorInfo errorInfo;
auto parser =
pdfium::MakeUnique<CXFA_FMParse>(L"; Just comment\n12", &errorInfo);
std::unique_ptr<CXFA_FMFunctionDefinition> ast = parser->Parse();
ASSERT(ast != nullptr);
EXPECT_TRUE(errorInfo.message.IsEmpty());
CFX_WideTextBuf buf;
EXPECT_TRUE(ast->ToJavaScript(buf));
EXPECT_EQ(L"(\n"
L"function ()\n"
L"{\n"
L"var foxit_xfa_formcalc_runtime_func_return_value = null;\n"
L"foxit_xfa_formcalc_runtime_func_return_value = 12;\n"
L"return foxit_xfa_formcalc_runtime.get_fm_value("
L"foxit_xfa_formcalc_runtime_func_return_value);\n"
L"}\n"
L").call(this);\n", buf.AsStringC());
}
TEST(CXFA_FMParseTest, Parse) {
const wchar_t input[] = L"$ = Avg (-3, 5, -6, 12, -13);\n"
L"$ = Avg (Table2..Row[*].Cell1);\n"
L"\n"
L"if ($ ne -1)then\n"
L" border.fill.color.value = \"255,64,64\";\n"
L"else\n"
L" border.fill.color.value = \"20,170,13\";\n"
L"endif\n"
L"\n"
L"$";
CXFA_FMErrorInfo errorInfo;
auto parser = pdfium::MakeUnique<CXFA_FMParse>(input, &errorInfo);
std::unique_ptr<CXFA_FMFunctionDefinition> ast = parser->Parse();
ASSERT(ast != nullptr);
EXPECT_TRUE(errorInfo.message.IsEmpty());
CFX_WideTextBuf buf;
EXPECT_TRUE(ast->ToJavaScript(buf));
EXPECT_EQ(
L"(\nfunction ()\n{\n"
L"var foxit_xfa_formcalc_runtime_func_return_value = null;\n"
L"if (foxit_xfa_formcalc_runtime.is_fm_object(this))\n{\n"
L"foxit_xfa_formcalc_runtime.assign_value_operator(this, "
L"foxit_xfa_formcalc_runtime.Avg("
L"foxit_xfa_formcalc_runtime.negative_operator(3), 5, "
L"foxit_xfa_formcalc_runtime.negative_operator(6), 12, "
L"foxit_xfa_formcalc_runtime.negative_operator(13)));\n"
L"}\n"
L"if (foxit_xfa_formcalc_runtime.is_fm_object(this))\n{\n"
L"foxit_xfa_formcalc_runtime.assign_value_operator(this, "
L"foxit_xfa_formcalc_runtime.Avg("
L"foxit_xfa_formcalc_runtime.dot_accessor("
L"foxit_xfa_formcalc_runtime.dotdot_accessor(Table2, \"Table2\", "
L"\"Row\", 1), \"\", \"Cell1\", 0, 0)));\n"
L"}\n"
L"if (foxit_xfa_formcalc_runtime.get_fm_value("
L"foxit_xfa_formcalc_runtime.notequality_operator(this, "
L"foxit_xfa_formcalc_runtime.negative_operator(1))))\n{\n"
L"if (foxit_xfa_formcalc_runtime.is_fm_object("
L"foxit_xfa_formcalc_runtime.dot_accessor("
L"foxit_xfa_formcalc_runtime.dot_accessor("
L"foxit_xfa_formcalc_runtime.dot_accessor(border, "
L"\"border\", \"fill\", 0, 0), \"\", \"color\", 0, 0), "
L"\"\", \"value\", 0, 0)))\n{\n"
L"foxit_xfa_formcalc_runtime.assign_value_operator("
L"foxit_xfa_formcalc_runtime.dot_accessor("
L"foxit_xfa_formcalc_runtime.dot_accessor("
L"foxit_xfa_formcalc_runtime.dot_accessor("
L"border, \"border\", \"fill\", 0, 0), \"\", "
L"\"color\", 0, 0), \"\", \"value\", 0, 0), "
L"\"255,64,64\");\n"
L"}\n"
L"}\nelse\n{\n"
L"if (foxit_xfa_formcalc_runtime.is_fm_object("
L"foxit_xfa_formcalc_runtime.dot_accessor("
L"foxit_xfa_formcalc_runtime.dot_accessor("
L"foxit_xfa_formcalc_runtime.dot_accessor("
L"border, \"border\", \"fill\", 0, 0), \"\", \"color\", "
L"0, 0), \"\", \"value\", 0, 0)))\n{\n"
L"foxit_xfa_formcalc_runtime.assign_value_operator("
L"foxit_xfa_formcalc_runtime.dot_accessor("
L"foxit_xfa_formcalc_runtime.dot_accessor("
L"foxit_xfa_formcalc_runtime.dot_accessor("
L"border, \"border\", \"fill\", 0, 0), \"\", "
L"\"color\", 0, 0), \"\", \"value\", 0, 0), "
L"\"20,170,13\");\n"
L"}\n"
L"}\n"
L"foxit_xfa_formcalc_runtime_func_return_value = this;\n"
L"return foxit_xfa_formcalc_runtime.get_fm_value("
L"foxit_xfa_formcalc_runtime_func_return_value);\n"
L"}\n).call(this);\n", buf.AsStringC());
}
|