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 "third_party/base/ptr_util.h"
14 #include "xfa/fxfa/fm2js/cxfa_fmlexer.h"
15 #include "xfa/fxfa/fm2js/cxfa_fmtojavascriptdepth.h"
16 
TEST(FMCallExpressionTest,more_than_32_arguments)17 TEST(FMCallExpressionTest, more_than_32_arguments) {
18   // Use sign as it has 3 object parameters at positions 0, 5, and 6.
19   auto exp = pdfium::MakeUnique<CXFA_FMIdentifierExpression>(L"sign");
20 
21   std::vector<std::unique_ptr<CXFA_FMSimpleExpression>> args;
22   for (size_t i = 0; i < 50; i++)
23     args.push_back(pdfium::MakeUnique<CXFA_FMNullExpression>());
24 
25   CXFA_FMToJavaScriptDepth::Reset();
26   CXFA_FMCallExpression callExp(std::move(exp), std::move(args), true);
27 
28   CFX_WideTextBuf js;
29   callExp.ToJavaScript(&js, ReturnType::kInfered);
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(null)";
41     else
42       result += L"val(null)";
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(L"").ToJavaScript(&accumulator, ReturnType::kInfered);
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(L"a").ToJavaScript(&accumulator,
60                                              ReturnType::kInfered);
61   EXPECT_EQ(L"a", accumulator.AsStringView());
62 }
63 
TEST(FMStringExpressionTest,Medium)64 TEST(FMStringExpressionTest, Medium) {
65   CXFA_FMToJavaScriptDepth::Reset();
66   CFX_WideTextBuf accumulator;
67   CXFA_FMStringExpression(L".abcd.").ToJavaScript(&accumulator,
68                                                   ReturnType::kInfered);
69   EXPECT_EQ(L"\"abcd\"", accumulator.AsStringView());
70 }
71 
TEST(FMStringExpressionTest,Long)72 TEST(FMStringExpressionTest, Long) {
73   CXFA_FMToJavaScriptDepth::Reset();
74   CFX_WideTextBuf accumulator;
75   std::vector<WideStringView::UnsignedType> vec(140000, L'A');
76   CXFA_FMStringExpression(WideStringView(vec))
77       .ToJavaScript(&accumulator, ReturnType::kInfered);
78   EXPECT_EQ(140000u, accumulator.GetLength());
79 }
80 
TEST(FMStringExpressionTest,Quoted)81 TEST(FMStringExpressionTest, Quoted) {
82   CXFA_FMToJavaScriptDepth::Reset();
83   CFX_WideTextBuf accumulator;
84   CXFA_FMStringExpression(L".Simon says \"\"run\"\".")
85       .ToJavaScript(&accumulator, ReturnType::kInfered);
86   EXPECT_EQ(L"\"Simon says \\\"run\\\"\"", accumulator.AsStringView());
87 }
88