1 // Copyright 2018 The PDFium Authors
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 "xfa/fxfa/formcalc/cxfa_fmexpression.h"
6
7 #include <utility>
8
9 #include "core/fxcrt/fx_string.h"
10 #include "core/fxcrt/widetext_buffer.h"
11 #include "testing/fxgc_unittest.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "v8/include/cppgc/heap.h"
14 #include "xfa/fxfa/formcalc/cxfa_fmlexer.h"
15 #include "xfa/fxfa/formcalc/cxfa_fmtojavascriptdepth.h"
16
17 class FMExpressionTest : public FXGCUnitTest {};
18 class FMCallExpressionTest : public FXGCUnitTest {};
19 class FMStringExpressionTest : public FXGCUnitTest {};
20
TEST_F(FMCallExpressionTest,more_than_32_arguments)21 TEST_F(FMCallExpressionTest, more_than_32_arguments) {
22 // Use sign as it has 3 object parameters at positions 0, 5, and 6.
23 auto* exp = cppgc::MakeGarbageCollected<CXFA_FMIdentifierExpression>(
24 heap()->GetAllocationHandle(), L"sign");
25
26 std::vector<cppgc::Member<CXFA_FMSimpleExpression>> args;
27 for (size_t i = 0; i < 50; i++) {
28 args.push_back(cppgc::MakeGarbageCollected<CXFA_FMNullExpression>(
29 heap()->GetAllocationHandle()));
30 }
31 CXFA_FMToJavaScriptDepth::Reset();
32 auto* callExp = cppgc::MakeGarbageCollected<CXFA_FMCallExpression>(
33 heap()->GetAllocationHandle(), exp, std::move(args), true);
34
35 WideTextBuffer js;
36 callExp->ToJavaScript(&js, CXFA_FMAssignExpression::ReturnType::kInferred);
37
38 // Generate the result javascript string.
39 WideString result = L"sign(";
40 for (size_t i = 0; i < 50; i++) {
41 if (i > 0)
42 result += L", ";
43
44 result += L"pfm_rt.get_";
45 // Object positions for sign() method.
46 if (i == 0 || i == 5 || i == 6)
47 result += L"jsobj(null)";
48 else
49 result += L"val(null)";
50 }
51 result += L")";
52
53 EXPECT_EQ(result.AsStringView(), js.AsStringView());
54 }
55
TEST_F(FMStringExpressionTest,Empty)56 TEST_F(FMStringExpressionTest, Empty) {
57 CXFA_FMToJavaScriptDepth::Reset();
58 WideTextBuffer accumulator;
59 auto* exp = cppgc::MakeGarbageCollected<CXFA_FMStringExpression>(
60 heap()->GetAllocationHandle(), L"");
61 exp->ToJavaScript(&accumulator,
62 CXFA_FMAssignExpression::ReturnType::kInferred);
63 EXPECT_EQ(L"", accumulator.AsStringView());
64 }
65
TEST_F(FMStringExpressionTest,Short)66 TEST_F(FMStringExpressionTest, Short) {
67 CXFA_FMToJavaScriptDepth::Reset();
68 WideTextBuffer accumulator;
69 auto* exp = cppgc::MakeGarbageCollected<CXFA_FMStringExpression>(
70 heap()->GetAllocationHandle(), L"a");
71 exp->ToJavaScript(&accumulator,
72 CXFA_FMAssignExpression::ReturnType::kInferred);
73 EXPECT_EQ(L"a", accumulator.AsStringView());
74 }
75
TEST_F(FMStringExpressionTest,Medium)76 TEST_F(FMStringExpressionTest, Medium) {
77 CXFA_FMToJavaScriptDepth::Reset();
78 WideTextBuffer accumulator;
79 auto* exp = cppgc::MakeGarbageCollected<CXFA_FMStringExpression>(
80 heap()->GetAllocationHandle(), L".abcd.");
81 exp->ToJavaScript(&accumulator,
82 CXFA_FMAssignExpression::ReturnType::kInferred);
83 EXPECT_EQ(L"\"abcd\"", accumulator.AsStringView());
84 }
85
TEST_F(FMStringExpressionTest,Long)86 TEST_F(FMStringExpressionTest, Long) {
87 CXFA_FMToJavaScriptDepth::Reset();
88 WideTextBuffer accumulator;
89 std::vector<WideStringView::UnsignedType> vec(140000, L'A');
90 auto* exp = cppgc::MakeGarbageCollected<CXFA_FMStringExpression>(
91 heap()->GetAllocationHandle(), WideString(WideStringView(vec)));
92 exp->ToJavaScript(&accumulator,
93 CXFA_FMAssignExpression::ReturnType::kInferred);
94 EXPECT_EQ(140000u, accumulator.GetLength());
95 }
96
TEST_F(FMStringExpressionTest,Quoted)97 TEST_F(FMStringExpressionTest, Quoted) {
98 CXFA_FMToJavaScriptDepth::Reset();
99 WideTextBuffer accumulator;
100 auto* exp = cppgc::MakeGarbageCollected<CXFA_FMStringExpression>(
101 heap()->GetAllocationHandle(), L".Simon says \"\"run\"\".");
102 exp->ToJavaScript(&accumulator,
103 CXFA_FMAssignExpression::ReturnType::kInferred);
104 EXPECT_EQ(L"\"Simon says \\\"run\\\"\"", accumulator.AsStringView());
105 }
106
TEST_F(FMExpressionTest,VarExpressionInitNull)107 TEST_F(FMExpressionTest, VarExpressionInitNull) {
108 CXFA_FMToJavaScriptDepth::Reset();
109 WideTextBuffer accumulator;
110
111 auto* expr = cppgc::MakeGarbageCollected<CXFA_FMVarExpression>(
112 heap()->GetAllocationHandle(), L"s", nullptr);
113 expr->ToJavaScript(&accumulator,
114 CXFA_FMAssignExpression::ReturnType::kInferred);
115 EXPECT_STREQ(
116 LR"***(var s = "";
117 )***",
118 accumulator.MakeString().c_str());
119 }
120
TEST_F(FMExpressionTest,VarExpressionInitBlank)121 TEST_F(FMExpressionTest, VarExpressionInitBlank) {
122 CXFA_FMToJavaScriptDepth::Reset();
123 WideTextBuffer accumulator;
124
125 auto* init = cppgc::MakeGarbageCollected<CXFA_FMStringExpression>(
126 heap()->GetAllocationHandle(), LR"("")");
127 auto* expr = cppgc::MakeGarbageCollected<CXFA_FMVarExpression>(
128 heap()->GetAllocationHandle(), L"s", init);
129 expr->ToJavaScript(&accumulator,
130 CXFA_FMAssignExpression::ReturnType::kInferred);
131 EXPECT_STREQ(
132 LR"***(var s = "";
133 s = pfm_rt.var_filter(s);
134 )***",
135 accumulator.MakeString().c_str());
136 }
137
TEST_F(FMExpressionTest,VarExpressionInitString)138 TEST_F(FMExpressionTest, VarExpressionInitString) {
139 CXFA_FMToJavaScriptDepth::Reset();
140 WideTextBuffer accumulator;
141
142 auto* init = cppgc::MakeGarbageCollected<CXFA_FMStringExpression>(
143 heap()->GetAllocationHandle(), LR"("foo")");
144 auto* expr = cppgc::MakeGarbageCollected<CXFA_FMVarExpression>(
145 heap()->GetAllocationHandle(), L"s", init);
146 expr->ToJavaScript(&accumulator,
147 CXFA_FMAssignExpression::ReturnType::kInferred);
148 EXPECT_STREQ(
149 LR"***(var s = "foo";
150 s = pfm_rt.var_filter(s);
151 )***",
152 accumulator.MakeString().c_str());
153 }
154
TEST_F(FMExpressionTest,VarExpressionInitNumeric)155 TEST_F(FMExpressionTest, VarExpressionInitNumeric) {
156 CXFA_FMToJavaScriptDepth::Reset();
157 WideTextBuffer accumulator;
158
159 auto* init = cppgc::MakeGarbageCollected<CXFA_FMNumberExpression>(
160 heap()->GetAllocationHandle(), L"112");
161 auto* expr = cppgc::MakeGarbageCollected<CXFA_FMVarExpression>(
162 heap()->GetAllocationHandle(), L"s", init);
163 expr->ToJavaScript(&accumulator,
164 CXFA_FMAssignExpression::ReturnType::kInferred);
165 EXPECT_STREQ(
166 LR"***(var s = 112;
167 s = pfm_rt.var_filter(s);
168 )***",
169 accumulator.MakeString().c_str());
170 }
171