diff options
author | Dan Sinclair <dsinclair@chromium.org> | 2018-02-22 20:01:15 +0000 |
---|---|---|
committer | Chromium commit bot <commit-bot@chromium.org> | 2018-02-22 20:01:15 +0000 |
commit | 26fc813975170de5040e9abe79a80b7034719944 (patch) | |
tree | 3ba21bf39f87fb4b47df8da5ecd31c41f5bb4c9a /xfa/fxfa/fm2js/cxfa_fmparser_unittest.cpp | |
parent | cc48741d63e216b3030fc2e452b6cf07481596ea (diff) | |
download | pdfium-26fc813975170de5040e9abe79a80b7034719944.tar.xz |
[formcalc] Cleanup function handling
This CL creates a CXFA_FMAST root for the AST tree instead of
overloading the CXFA_FMFunctionDefinition. This Removes the m_global
from FunctionDefinition and simpifies the code.
Change-Id: I9347769a291ef1753539701f334cc8dd69b7187e
Reviewed-on: https://pdfium-review.googlesource.com/27590
Commit-Queue: dsinclair <dsinclair@chromium.org>
Reviewed-by: Ryan Harrison <rharrison@chromium.org>
Diffstat (limited to 'xfa/fxfa/fm2js/cxfa_fmparser_unittest.cpp')
-rw-r--r-- | xfa/fxfa/fm2js/cxfa_fmparser_unittest.cpp | 82 |
1 files changed, 69 insertions, 13 deletions
diff --git a/xfa/fxfa/fm2js/cxfa_fmparser_unittest.cpp b/xfa/fxfa/fm2js/cxfa_fmparser_unittest.cpp index e939cd954f..a7f7006fcc 100644 --- a/xfa/fxfa/fm2js/cxfa_fmparser_unittest.cpp +++ b/xfa/fxfa/fm2js/cxfa_fmparser_unittest.cpp @@ -14,20 +14,20 @@ TEST(CXFA_FMParserTest, Empty) { auto parser = pdfium::MakeUnique<CXFA_FMParser>(L""); - std::unique_ptr<CXFA_FMFunctionDefinition> ast = parser->Parse(); + std::unique_ptr<CXFA_FMAST> ast = parser->Parse(); ASSERT(ast != nullptr); EXPECT_FALSE(parser->HasError()); CXFA_FMToJavaScriptDepth::Reset(); CFX_WideTextBuf buf; - EXPECT_TRUE(ast->ToJavaScript(buf, ReturnType::kInfered)); + EXPECT_TRUE(ast->ToJavaScript(buf)); // TODO(dsinclair): This is a little weird ..... EXPECT_EQ(L"// comments only", buf.AsStringView()); } TEST(CXFA_FMParserTest, CommentOnlyIsError) { auto parser = pdfium::MakeUnique<CXFA_FMParser>(L"; Just comment"); - std::unique_ptr<CXFA_FMFunctionDefinition> ast = parser->Parse(); + std::unique_ptr<CXFA_FMAST> ast = parser->Parse(); ASSERT(ast != nullptr); // TODO(dsinclair): This isn't allowed per the spec. EXPECT_FALSE(parser->HasError()); @@ -35,26 +35,26 @@ TEST(CXFA_FMParserTest, CommentOnlyIsError) { CXFA_FMToJavaScriptDepth::Reset(); CFX_WideTextBuf buf; - EXPECT_TRUE(ast->ToJavaScript(buf, ReturnType::kInfered)); + EXPECT_TRUE(ast->ToJavaScript(buf)); EXPECT_EQ(L"// comments only", buf.AsStringView()); } TEST(CXFA_FMParserTest, CommentThenValue) { const wchar_t ret[] = - L"(\nfunction ()\n{\n" + L"(function() {\n" L"var pfm_ret = null;\n" L"pfm_ret = 12;\n" L"return pfm_rt.get_val(pfm_ret);\n" - L"}\n).call(this);\n"; + L"}).call(this);"; auto parser = pdfium::MakeUnique<CXFA_FMParser>(L"; Just comment\n12"); - std::unique_ptr<CXFA_FMFunctionDefinition> ast = parser->Parse(); + std::unique_ptr<CXFA_FMAST> ast = parser->Parse(); ASSERT(ast != nullptr); EXPECT_FALSE(parser->HasError()); CXFA_FMToJavaScriptDepth::Reset(); CFX_WideTextBuf buf; - EXPECT_TRUE(ast->ToJavaScript(buf, ReturnType::kInfered)); + EXPECT_TRUE(ast->ToJavaScript(buf)); EXPECT_EQ(ret, buf.AsStringView()); } @@ -74,7 +74,7 @@ TEST(CXFA_FMParserTest, Parse) { L"$"; const wchar_t ret[] = - L"(\nfunction ()\n{\n" + L"(function() {\n" L"var pfm_ret = null;\n" L"if (pfm_rt.is_obj(this))\n{\n" L"pfm_rt.asgn_val_op(this, pfm_rt.Avg(pfm_rt.neg_op(3), 5, " @@ -114,16 +114,16 @@ TEST(CXFA_FMParserTest, Parse) { L"}\n" L"pfm_ret = this;\n" L"return pfm_rt.get_val(pfm_ret);\n" - L"}\n).call(this);\n"; + L"}).call(this);"; auto parser = pdfium::MakeUnique<CXFA_FMParser>(input); - std::unique_ptr<CXFA_FMFunctionDefinition> ast = parser->Parse(); + std::unique_ptr<CXFA_FMAST> ast = parser->Parse(); ASSERT(ast != nullptr); EXPECT_FALSE(parser->HasError()); CXFA_FMToJavaScriptDepth::Reset(); CFX_WideTextBuf buf; - EXPECT_TRUE(ast->ToJavaScript(buf, ReturnType::kInfered)); + EXPECT_TRUE(ast->ToJavaScript(buf)); EXPECT_EQ(ret, buf.AsStringView()); } @@ -146,7 +146,63 @@ TEST(CFXA_FMParserTest, chromium752201) { TEST(CXFA_FMParserTest, MultipleAssignmentIsNotAllowed) { auto parser = pdfium::MakeUnique<CXFA_FMParser>(L"(a=(b=t))=u"); - std::unique_ptr<CXFA_FMFunctionDefinition> ast = parser->Parse(); + std::unique_ptr<CXFA_FMAST> ast = parser->Parse(); ASSERT(ast == nullptr); EXPECT_TRUE(parser->HasError()); } + +TEST(CXFA_FMParserTest, ParseFuncWithParams) { + const wchar_t input[] = { + L"func MyFunction(param1, param2) do\n" + L" param1 * param2\n" + L"endfunc"}; + + const wchar_t ret[] = { + L"(function() {\n" + L"var pfm_ret = null;\n" + L"function MyFunction(param1, param2) {\n" + L"var pfm_ret = null;\n" + L"pfm_ret = pfm_rt.mul_op(param1, param2);\n" + L"return pfm_ret;\n" + L"}\n" + L"return pfm_rt.get_val(pfm_ret);\n" + L"}).call(this);"}; + + auto parser = pdfium::MakeUnique<CXFA_FMParser>(input); + std::unique_ptr<CXFA_FMAST> ast = parser->Parse(); + ASSERT(ast != nullptr); + EXPECT_FALSE(parser->HasError()); + + CXFA_FMToJavaScriptDepth::Reset(); + CFX_WideTextBuf buf; + EXPECT_TRUE(ast->ToJavaScript(buf)); + EXPECT_EQ(ret, buf.AsStringView()); +} + +TEST(CXFA_FMParserTest, ParseFuncWithoutParams) { + const wchar_t input[] = { + L"func MyFunction() do\n" + L" 42\n" + L"endfunc"}; + + const wchar_t ret[] = { + L"(function() {\n" + L"var pfm_ret = null;\n" + L"function MyFunction() {\n" + L"var pfm_ret = null;\n" + L"pfm_ret = 42;\n" + L"return pfm_ret;\n" + L"}\n" + L"return pfm_rt.get_val(pfm_ret);\n" + L"}).call(this);"}; + + auto parser = pdfium::MakeUnique<CXFA_FMParser>(input); + std::unique_ptr<CXFA_FMAST> ast = parser->Parse(); + ASSERT(ast != nullptr); + EXPECT_FALSE(parser->HasError()); + + CXFA_FMToJavaScriptDepth::Reset(); + CFX_WideTextBuf buf; + EXPECT_TRUE(ast->ToJavaScript(buf)); + EXPECT_EQ(ret, buf.AsStringView()); +} |