1 // Copyright 2016 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 "xfa/fxfa/fm2js/cxfa_fmsimpleexpression.h"
6
7 #include <memory>
8 #include <utility>
9
10 #include "core/fxcrt/cfx_widetextbuf.h"
11 #include "core/fxcrt/fx_string.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "testing/test_support.h"
14 #include "third_party/base/ptr_util.h"
15 #include "xfa/fxfa/fm2js/cxfa_fmlexer.h"
16 #include "xfa/fxfa/fm2js/cxfa_fmtojavascriptdepth.h"
17
TEST(FMCallExpressionTest,more_than_32_arguments)18 TEST(FMCallExpressionTest, more_than_32_arguments) {
19 // Use sign as it has 3 object parameters at positions 0, 5, and 6.
20 auto exp = pdfium::MakeUnique<CXFA_FMIdentifierExpression>(0, L"sign");
21
22 std::vector<std::unique_ptr<CXFA_FMSimpleExpression>> args;
23 for (size_t i = 0; i < 50; i++)
24 args.push_back(pdfium::MakeUnique<CXFA_FMSimpleExpression>(0, TOKnan));
25
26 CXFA_FMToJavaScriptDepth::Reset();
27 CXFA_FMCallExpression callExp(0, std::move(exp), std::move(args), true);
28 CFX_WideTextBuf js;
29 callExp.ToJavaScript(js);
30
31 // Generate the result javascript string.
32 WideString result = L"sign(";
33 for (size_t i = 0; i < 50; i++) {
34 if (i > 0)
35 result += L", ";
36
37 result += L"pfm_rt.get_";
38 // Object positions for sign() method.
39 if (i == 0 || i == 5 || i == 6)
40 result += L"jsobj()";
41 else
42 result += L"val()";
43 }
44 result += L")";
45
46 EXPECT_EQ(result.AsStringView(), js.AsStringView());
47 }
48
TEST(FMStringExpressionTest,Empty)49 TEST(FMStringExpressionTest, Empty) {
50 CXFA_FMToJavaScriptDepth::Reset();
51 CFX_WideTextBuf accumulator;
52 CXFA_FMStringExpression(1, WideStringView()).ToJavaScript(accumulator);
53 EXPECT_EQ(L"", accumulator.AsStringView());
54 }
55
TEST(FMStringExpressionTest,Short)56 TEST(FMStringExpressionTest, Short) {
57 CXFA_FMToJavaScriptDepth::Reset();
58 CFX_WideTextBuf accumulator;
59 CXFA_FMStringExpression(1, L"a").ToJavaScript(accumulator);
60 EXPECT_EQ(L"a", accumulator.AsStringView());
61 }
62
TEST(FMStringExpressionTest,Medium)63 TEST(FMStringExpressionTest, Medium) {
64 CXFA_FMToJavaScriptDepth::Reset();
65 CFX_WideTextBuf accumulator;
66 CXFA_FMStringExpression(1, L".abcd.").ToJavaScript(accumulator);
67 EXPECT_EQ(L"\"abcd\"", accumulator.AsStringView());
68 }
69
TEST(FMStringExpressionTest,Long)70 TEST(FMStringExpressionTest, Long) {
71 CXFA_FMToJavaScriptDepth::Reset();
72 CFX_WideTextBuf accumulator;
73 std::vector<WideStringView::UnsignedType> vec(140000, L'A');
74 CXFA_FMStringExpression(1, WideStringView(vec)).ToJavaScript(accumulator);
75 EXPECT_EQ(140000u, accumulator.GetLength());
76 }
77
TEST(FMStringExpressionTest,Quoted)78 TEST(FMStringExpressionTest, Quoted) {
79 CXFA_FMToJavaScriptDepth::Reset();
80 CFX_WideTextBuf accumulator;
81 CXFA_FMStringExpression(1, L".Simon says \"\"run\"\".")
82 .ToJavaScript(accumulator);
83 EXPECT_EQ(L"\"Simon says \\\"run\\\"\"", accumulator.AsStringView());
84 }
85