Add CFXJSE_FormCalcContext::GenerateSomExpression() unittests.
Give this static method full test coverage.
Bug: chromium:1301044
Change-Id: Ib7246b7f43a620ba04fdfb78e9a8200f40a089a7
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/91053
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/fxjs/BUILD.gn b/fxjs/BUILD.gn
index 73f22ab..fa20e38 100644
--- a/fxjs/BUILD.gn
+++ b/fxjs/BUILD.gn
@@ -259,6 +259,7 @@
"gc/gced_tree_node_unittest.cpp",
"gc/heap_unittest.cpp",
"gc/move_unittest.cpp",
+ "xfa/cfxjse_formcalc_context_unittest.cpp",
"xfa/cfxjse_mapmodule_unittest.cpp",
]
deps += [
diff --git a/fxjs/xfa/cfxjse_formcalc_context.cpp b/fxjs/xfa/cfxjse_formcalc_context.cpp
index af90fef..4f7a546 100644
--- a/fxjs/xfa/cfxjse_formcalc_context.cpp
+++ b/fxjs/xfa/cfxjse_formcalc_context.cpp
@@ -28,7 +28,7 @@
#include "fxjs/xfa/cfxjse_value.h"
#include "fxjs/xfa/cjx_object.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
-#include "third_party/base/check.h"
+#include "third_party/base/check_op.h"
#include "third_party/base/cxx17_backports.h"
#include "third_party/base/numerics/safe_conversions.h"
#include "v8/include/v8-container.h"
@@ -5253,6 +5253,8 @@
if (bIsStar)
return ByteString(bsName, "[*]");
+ // `iIndexFlags` values are the same as enum class
+ // `CXFA_FMIndexExpression::AccessorIndex` values.
if (iIndexFlags == 0)
return ByteString(bsName);
@@ -5263,10 +5265,12 @@
const bool bNegative = iIndexValue < 0;
ByteString bsSomExp(bsName);
- if (iIndexFlags == 2)
+ if (iIndexFlags == 2) {
bsSomExp += bNegative ? "[-" : "[+";
- else
+ } else {
+ DCHECK_EQ(iIndexFlags, 3);
bsSomExp += bNegative ? "[" : "[-";
+ }
iIndexValue = bNegative ? 0 - iIndexValue : iIndexValue;
bsSomExp += ByteString::FormatInteger(iIndexValue);
bsSomExp += "]";
diff --git a/fxjs/xfa/cfxjse_formcalc_context_unittest.cpp b/fxjs/xfa/cfxjse_formcalc_context_unittest.cpp
new file mode 100644
index 0000000..1a6abb5
--- /dev/null
+++ b/fxjs/xfa/cfxjse_formcalc_context_unittest.cpp
@@ -0,0 +1,80 @@
+// Copyright 2022 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 "fxjs/xfa/cfxjse_formcalc_context.h"
+
+#include "core/fxcrt/bytestring.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+TEST(FormCalcContextTest, GenerateSomExpression) {
+ ByteString result =
+ CFXJSE_FormCalcContext::GenerateSomExpression("", 0, 0, /*bIsStar=*/true);
+ EXPECT_EQ(result, "[*]");
+
+ result = CFXJSE_FormCalcContext::GenerateSomExpression("foo", 0, 0,
+ /*bIsStar=*/true);
+ EXPECT_EQ(result, "foo[*]");
+
+ result = CFXJSE_FormCalcContext::GenerateSomExpression("foo", 0, 0,
+ /*bIsStar=*/false);
+ EXPECT_EQ(result, "foo");
+
+ result = CFXJSE_FormCalcContext::GenerateSomExpression("fu", 1, 0,
+ /*bIsStar=*/false);
+ EXPECT_EQ(result, "fu[0]");
+
+ result = CFXJSE_FormCalcContext::GenerateSomExpression("food", 1, 99,
+ /*bIsStar=*/false);
+ EXPECT_EQ(result, "food[99]");
+
+ result = CFXJSE_FormCalcContext::GenerateSomExpression("foot", 1, -65,
+ /*bIsStar=*/false);
+ EXPECT_EQ(result, "foot[-65]");
+
+ result = CFXJSE_FormCalcContext::GenerateSomExpression("football", 2, 0,
+ /*bIsStar=*/false);
+ EXPECT_EQ(result, "football[0]");
+
+ result = CFXJSE_FormCalcContext::GenerateSomExpression("foosball", 2, 123,
+ /*bIsStar=*/false);
+ EXPECT_EQ(result, "foosball[+123]");
+
+ result = CFXJSE_FormCalcContext::GenerateSomExpression("bar", 2, -654,
+ /*bIsStar=*/false);
+ EXPECT_EQ(result, "bar[-654]");
+
+ result = CFXJSE_FormCalcContext::GenerateSomExpression("barb", 2, 2147483647,
+ /*bIsStar=*/false);
+ EXPECT_EQ(result, "barb[+2147483647]");
+
+#if 0
+ // TODO(crbug.com/1301044): Fix and enable.
+ result = CFXJSE_FormCalcContext::GenerateSomExpression(
+ "bart", 2, -2147483648, /*bIsStar=*/false);
+ EXPECT_EQ(result, "bart[--2147483648]");
+#endif
+
+ result = CFXJSE_FormCalcContext::GenerateSomExpression("bark", 3, 0,
+ /*bIsStar=*/false);
+ EXPECT_EQ(result, "bark[0]");
+
+ result = CFXJSE_FormCalcContext::GenerateSomExpression("bard", 3, 357,
+ /*bIsStar=*/false);
+ EXPECT_EQ(result, "bard[-357]");
+
+ result = CFXJSE_FormCalcContext::GenerateSomExpression("bars", 3, -9876,
+ /*bIsStar=*/false);
+ EXPECT_EQ(result, "bars[9876]");
+
+ result = CFXJSE_FormCalcContext::GenerateSomExpression("cars", 3, 2147483647,
+ /*bIsStar=*/false);
+ EXPECT_EQ(result, "cars[-2147483647]");
+
+#if 0
+ // TODO(crbug.com/1301044): Fix and enable.
+ result = CFXJSE_FormCalcContext::GenerateSomExpression(
+ "mars", 3, -2147483648, /*bIsStar=*/false);
+ EXPECT_EQ(result, "mars[-2147483648]");
+#endif
+}