• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 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 "testing/gtest/include/gtest/gtest.h"
6 #include "testing/xfa_js_embedder_test.h"
7 
8 class CFXJSE_FormCalcContextEmbedderTest : public XFAJSEmbedderTest {};
9 
10 // TODO(dsinclair): Comment out tests are broken and need to be fixed.
11 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,TranslateEmpty)12 TEST_F(CFXJSE_FormCalcContextEmbedderTest, TranslateEmpty) {
13   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
14 
15   const char input[] = "";
16   EXPECT_TRUE(Execute(input));
17   // TODO(dsinclair): This should probably throw as a blank formcalc script
18   // is invalid.
19 }
20 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,TranslateNumber)21 TEST_F(CFXJSE_FormCalcContextEmbedderTest, TranslateNumber) {
22   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
23 
24   const char input[] = "123";
25   EXPECT_TRUE(Execute(input));
26 
27   CFXJSE_Value* value = GetValue();
28   EXPECT_TRUE(value->IsInteger());
29   EXPECT_EQ(123, value->ToInteger()) << "Program: " << input;
30 }
31 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,Numeric)32 TEST_F(CFXJSE_FormCalcContextEmbedderTest, Numeric) {
33   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
34 
35   struct {
36     const char* program;
37     int result;
38   } tests[] = {{"123 + 456", 579},
39                {"2 - 3 * 10 / 2 + 7", -6},
40                {"10 * 3 + 5 * 4", 50},
41                {"(5 - \"abc\") * 3", 15},
42                {"\"100\" / 10e1", 1},
43                {"5 + null + 3", 8},
44                // {"if (\"abc\") then\n"
45                //  "  10\n"
46                //  "else\n"
47                //  "  20\n"
48                //  "endif",
49                //  20},
50                // {"3 / 0 + 1", 0},
51                {"-(17)", -17},
52                {"-(-17)", 17},
53                {"+(17)", 17},
54                {"+(-17)", -17},
55                {"if (1 < 2) then\n1\nendif", 1},
56                {"if (\"abc\" > \"def\") then\n"
57                 "  1 and 0\n"
58                 "else\n"
59                 "  0\n"
60                 "endif",
61                 0}};
62 
63   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
64     EXPECT_TRUE(Execute(tests[i].program));
65 
66     CFXJSE_Value* value = GetValue();
67     EXPECT_TRUE(value->IsInteger());
68     EXPECT_EQ(tests[i].result, value->ToInteger())
69         << "Program: " << tests[i].program;
70   }
71 }
72 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,Strings)73 TEST_F(CFXJSE_FormCalcContextEmbedderTest, Strings) {
74   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
75 
76   struct {
77     const char* program;
78     const char* result;
79   } tests[] = {
80       {"\"abc\"", "abc"},
81       {"concat(\"The total is \", 2, \" dollars and \", 57, \" cents.\")",
82        "The total is 2 dollars and 57 cents."}};
83 
84   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
85     EXPECT_TRUE(Execute(tests[i].program));
86 
87     CFXJSE_Value* value = GetValue();
88     EXPECT_TRUE(value->IsString());
89     EXPECT_STREQ(tests[i].result, value->ToString().c_str())
90         << "Program: " << tests[i].program << " Result: '"
91         << value->ToString().c_str() << "'";
92   }
93 }
94 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,Booleans)95 TEST_F(CFXJSE_FormCalcContextEmbedderTest, Booleans) {
96   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
97 
98   struct {
99     const char* program;
100     bool result;
101   } tests[] = {{"0 and 1 or 2 > 1", true},
102                {"2 < 3 not 1 == 1", false},
103                {"\"abc\" | 2", true},
104                {"1 or 0", true},
105                {"0 | 0", false},
106                {"0 or 1 | 0 or 0", true},
107                {"1 and 0", false},
108                // {"0 & 0", true},  // TODO(dsinclair) Confirm with Reader.
109                {"0 and 1 & 0 and 0", false},
110                {"not(\"true\")", true},
111                {"not(1)", false},
112                {"3 == 3", true},
113                {"3 <> 4", true},
114                {"\"abc\" eq \"def\"", false},
115                {"\"def\" ne \"abc\"", true},
116                {"5 + 5 == 10", true},
117                {"5 + 5 <> \"10\"", false},
118                {"3 < 3", false},
119                {"3 > 4", false},
120                {"\"abc\" <= \"def\"", true},
121                {"\"def\" > \"abc\"", true},
122                {"12 >= 12", true},
123                {"\"true\" < \"false\"", false}};
124 
125   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
126     EXPECT_TRUE(Execute(tests[i].program));
127 
128     CFXJSE_Value* value = GetValue();
129     EXPECT_TRUE(value->IsInteger()) << "Program: " << tests[i].program;
130     EXPECT_EQ(tests[i].result, value->ToBoolean())
131         << "Program: " << tests[i].program;
132   }
133 }
134 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,Abs)135 TEST_F(CFXJSE_FormCalcContextEmbedderTest, Abs) {
136   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
137 
138   struct {
139     const char* program;
140     float result;
141   } tests[] = {{"Abs(1.03)", 1.03f}, {"Abs(-1.03)", 1.03f}, {"Abs(0)", 0.0f}};
142 
143   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
144     EXPECT_TRUE(Execute(tests[i].program));
145 
146     CFXJSE_Value* value = GetValue();
147     EXPECT_TRUE(value->IsNumber());
148     EXPECT_FLOAT_EQ(tests[i].result, value->ToFloat())
149         << "Program: " << tests[i].program;
150   }
151 }
152 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,Avg)153 TEST_F(CFXJSE_FormCalcContextEmbedderTest, Avg) {
154   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
155 
156   struct {
157     const char* program;
158     float result;
159   } tests[] = {{"Avg(0, 32, 16)", 16.0f}, {"Avg(2.5, 17, null)", 9.75f}};
160 
161   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
162     EXPECT_TRUE(Execute(tests[i].program));
163 
164     CFXJSE_Value* value = GetValue();
165     EXPECT_TRUE(value->IsNumber());
166     EXPECT_FLOAT_EQ(tests[i].result, value->ToFloat())
167         << "Program: " << tests[i].program;
168   }
169 }
170 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,Ceil)171 TEST_F(CFXJSE_FormCalcContextEmbedderTest, Ceil) {
172   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
173 
174   struct {
175     const char* program;
176     int result;
177   } tests[] = {{"Ceil(2.5875)", 3}, {"Ceil(-5.9)", -5}, {"Ceil(\"abc\")", 0}};
178 
179   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
180     EXPECT_TRUE(Execute(tests[i].program));
181 
182     CFXJSE_Value* value = GetValue();
183     EXPECT_TRUE(value->IsInteger());
184     EXPECT_EQ(tests[i].result, value->ToInteger())
185         << "Program: " << tests[i].program;
186   }
187 }
188 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,Count)189 TEST_F(CFXJSE_FormCalcContextEmbedderTest, Count) {
190   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
191 
192   struct {
193     const char* program;
194     int result;
195   } tests[] = {{"Count(\"Tony\", \"Blue\", 41)", 3}};
196 
197   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
198     EXPECT_TRUE(Execute(tests[i].program));
199 
200     CFXJSE_Value* value = GetValue();
201     EXPECT_TRUE(value->IsInteger());
202     EXPECT_EQ(tests[i].result, value->ToInteger())
203         << "Program: " << tests[i].program;
204   }
205 }
206 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,Floor)207 TEST_F(CFXJSE_FormCalcContextEmbedderTest, Floor) {
208   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
209 
210   struct {
211     const char* program;
212     int result;
213   } tests[] = {{"Floor(21.3409873)", 21},
214                {"Floor(5.999965342)", 5},
215                {"Floor(3.2 * 15)", 48}};
216 
217   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
218     EXPECT_TRUE(Execute(tests[i].program));
219 
220     CFXJSE_Value* value = GetValue();
221     EXPECT_TRUE(value->IsInteger());
222     EXPECT_EQ(tests[i].result, value->ToInteger())
223         << "Program: " << tests[i].program;
224   }
225 }
226 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,Max)227 TEST_F(CFXJSE_FormCalcContextEmbedderTest, Max) {
228   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
229 
230   struct {
231     const char* program;
232     int result;
233   } tests[] = {{"Max(234, 15, 107)", 234},
234                {"Max(\"abc\", 15, \"Tony Blue\")", 15},
235                {"Max(\"abc\")", 0}};
236 
237   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
238     EXPECT_TRUE(Execute(tests[i].program));
239 
240     CFXJSE_Value* value = GetValue();
241     EXPECT_TRUE(value->IsInteger());
242     EXPECT_EQ(tests[i].result, value->ToInteger())
243         << "Program: " << tests[i].program;
244   }
245 }
246 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,Min)247 TEST_F(CFXJSE_FormCalcContextEmbedderTest, Min) {
248   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
249 
250   struct {
251     const char* program;
252     int result;
253   } tests[] = {{"Min(234, 15, 107)", 15},
254                // TODO(dsinclair): Verify with Reader; I believe this should
255                // have a return of 0.
256                // {"Min(\"abc\", 15, \"Tony Blue\")", 15},
257                {"Min(\"abc\")", 0}};
258 
259   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
260     EXPECT_TRUE(Execute(tests[i].program));
261 
262     CFXJSE_Value* value = GetValue();
263     EXPECT_TRUE(value->IsInteger());
264     EXPECT_EQ(tests[i].result, value->ToInteger())
265         << "Program: " << tests[i].program;
266   }
267 }
268 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,Mod)269 TEST_F(CFXJSE_FormCalcContextEmbedderTest, Mod) {
270   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
271 
272   struct {
273     const char* program;
274     int result;
275   } tests[] = {{"Mod(64, -3)", 1}, {"Mod(-13, 3)", -1}, {"Mod(\"abc\", 2)", 0}};
276 
277   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
278     EXPECT_TRUE(Execute(tests[i].program));
279 
280     CFXJSE_Value* value = GetValue();
281     EXPECT_TRUE(value->IsInteger());
282     EXPECT_EQ(tests[i].result, value->ToInteger())
283         << "Program: " << tests[i].program;
284   }
285 }
286 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,Round)287 TEST_F(CFXJSE_FormCalcContextEmbedderTest, Round) {
288   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
289 
290   struct {
291     const char* program;
292     float result;
293   } tests[] = {{"Round(12.389764537, 4)", 12.3898f},
294                {"Round(20/3, 2)", 6.67f},
295                {"Round(8.9897, \"abc\")", 9.0f},
296                {"Round(FV(400, 0.10/12, 30*12), 2)", 904195.17f}};
297 
298   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
299     EXPECT_TRUE(Execute(tests[i].program));
300 
301     CFXJSE_Value* value = GetValue();
302     EXPECT_TRUE(value->IsNumber()) << "Program: " << tests[i].program;
303     EXPECT_FLOAT_EQ(tests[i].result, value->ToFloat())
304         << "Program: " << tests[i].program;
305   }
306 }
307 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,Sum)308 TEST_F(CFXJSE_FormCalcContextEmbedderTest, Sum) {
309   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
310 
311   struct {
312     const char* program;
313     int result;
314   } tests[] = {{"Sum(2, 4, 6, 8)", 20},
315                {"Sum(-2, 4, -6, 8)", 4},
316                {"Sum(4, 16, \"abc\", 19)", 39}};
317 
318   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
319     EXPECT_TRUE(Execute(tests[i].program));
320 
321     CFXJSE_Value* value = GetValue();
322     EXPECT_TRUE(value->IsInteger());
323     EXPECT_EQ(tests[i].result, value->ToInteger())
324         << "Program: " << tests[i].program;
325   }
326 }
327 
328 // TEST_F(CFXJSE_FormCalcContextEmbedderTest, DISABLED_Date) {
329 //   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
330 //
331 //   TODO(dsinclair): Make compatible with windows.
332 //   time_t seconds = time(nullptr);
333 //   int days = seconds / (60 * 60 * 24);
334 
335 //   EXPECT_TRUE(Execute("Date()"));
336 
337 //   CFXJSE_Value* value = GetValue();
338 //   EXPECT_TRUE(value->IsNumber());
339 //   EXPECT_EQ(days, value->ToInteger());
340 // }
341 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,Date2Num)342 TEST_F(CFXJSE_FormCalcContextEmbedderTest, Date2Num) {
343   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
344 
345   struct {
346     const char* program;
347     int result;
348   } tests[] = {
349       // {"Date2Num(\"Mar 15, 1996\")", 35138},
350       {"Date2Num(\"1/1/1900\", \"D/M/YYYY\")", 1},
351       {"Date2Num(\"03/15/96\", \"MM/DD/YY\")", 35138},
352       // {"Date2Num(\"Aug 1, 1996\", \"MMM D, YYYY\")", 35277},
353       {"Date2Num(\"96-08-20\", \"YY-MM-DD\", \"fr_FR\")", 35296},
354       {"Date2Num(\"1/3/00\", \"D/M/YY\") - Date2Num(\"1/2/00\", \"D/M/YY\")",
355        29}};
356 
357   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
358     EXPECT_TRUE(Execute(tests[i].program));
359 
360     CFXJSE_Value* value = GetValue();
361     EXPECT_TRUE(value->IsInteger());
362     EXPECT_EQ(tests[i].result, value->ToInteger())
363         << "Program: " << tests[i].program;
364   }
365 }
366 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,DateFmt)367 TEST_F(CFXJSE_FormCalcContextEmbedderTest, DateFmt) {
368   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
369 
370   struct {
371     const char* program;
372     const char* result;
373   } tests[] = {
374       // {"DateFmt(1)", "M/D/YY"},
375       // {"DateFmt(2, \"fr_CA\")", "YY-MM-DD"},
376       {"DateFmt(3, \"de_DE\")", "D. MMMM YYYY"},
377       // {"DateFmt(4, \"fr_FR\")", "EEE D' MMMM YYYY"}
378   };
379 
380   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
381     EXPECT_TRUE(Execute(tests[i].program));
382 
383     CFXJSE_Value* value = GetValue();
384     EXPECT_TRUE(value->IsString());
385     EXPECT_STREQ(tests[i].result, value->ToString().c_str())
386         << "Program: " << tests[i].program << " Result: '"
387         << value->ToString().c_str() << "'";
388   }
389 }
390 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,IsoDate2Num)391 TEST_F(CFXJSE_FormCalcContextEmbedderTest, IsoDate2Num) {
392   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
393 
394   struct {
395     const char* program;
396     int result;
397   } tests[] = {{"IsoDate2Num(\"1900\")", 1},
398                {"IsoDate2Num(\"1900-01\")", 1},
399                {"IsoDate2Num(\"1900-01-01\")", 1},
400                {"IsoDate2Num(\"19960315T20:20:20\")", 35138},
401                {"IsoDate2Num(\"2000-03-01\") - IsoDate2Num(\"20000201\")", 29}};
402 
403   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
404     EXPECT_TRUE(Execute(tests[i].program));
405 
406     CFXJSE_Value* value = GetValue();
407     EXPECT_TRUE(value->IsInteger());
408     EXPECT_EQ(tests[i].result, value->ToInteger())
409         << "Program: " << tests[i].program;
410   }
411 }
412 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,DISABLED_IsoTime2Num)413 TEST_F(CFXJSE_FormCalcContextEmbedderTest, DISABLED_IsoTime2Num) {
414   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
415 
416   struct {
417     const char* program;
418     int result;
419   } tests[] = {{"IsoTime2Num(\"00:00:00Z\")", 1}};
420 
421   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
422     EXPECT_TRUE(Execute(tests[i].program));
423 
424     CFXJSE_Value* value = GetValue();
425     EXPECT_TRUE(value->IsInteger());
426     EXPECT_EQ(tests[i].result, value->ToInteger())
427         << "Program: " << tests[i].program;
428   }
429 }
430 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,LocalDateFmt)431 TEST_F(CFXJSE_FormCalcContextEmbedderTest, LocalDateFmt) {
432   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
433 
434   struct {
435     const char* program;
436     const char* result;
437   } tests[] = {// {"LocalDateFmt(1, \"de_DE\")", "tt.MM.uu"},
438                // {"LocalDateFmt(2, \"fr_CA\")", "aa-MM-jj"},
439                {"LocalDateFmt(3, \"de_CH\")", "t. MMMM jjjj"},
440                {"LocalDateFmt(4, \"fr_FR\")", "EEEE j MMMM aaaa"}};
441 
442   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
443     EXPECT_TRUE(Execute(tests[i].program));
444 
445     CFXJSE_Value* value = GetValue();
446     EXPECT_TRUE(value->IsString());
447     EXPECT_STREQ(tests[i].result, value->ToString().c_str())
448         << "Program: " << tests[i].program << " Result: '"
449         << value->ToString().c_str() << "'";
450   }
451 }
452 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,DISABLED_LocalTimeFmt)453 TEST_F(CFXJSE_FormCalcContextEmbedderTest, DISABLED_LocalTimeFmt) {
454   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
455 
456   struct {
457     const char* program;
458     const char* result;
459   } tests[] = {{"LocalTimeFmt(1, \"de_DE\")", "HH:mm"},
460                {"LocalTimeFmt(2, \"fr_CA\")", "HH:mm::ss"},
461                {"LocalTimeFmt(3, \"de_CH\")", "HH:mm:ss z"},
462                {"LocalTimeFmt(4, \"fr_FR\")", "HH' h 'mm z"}};
463 
464   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
465     EXPECT_TRUE(Execute(tests[i].program));
466 
467     CFXJSE_Value* value = GetValue();
468     EXPECT_TRUE(value->IsString());
469     EXPECT_STREQ(tests[i].result, value->ToString().c_str())
470         << "Program: " << tests[i].program << " Result: '"
471         << value->ToString().c_str() << "'";
472   }
473 }
474 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,Num2Date)475 TEST_F(CFXJSE_FormCalcContextEmbedderTest, Num2Date) {
476   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
477 
478   struct {
479     const char* program;
480     const char* result;
481   } tests[] = {
482       {"Num2Date(1, \"DD/MM/YYYY\")", "01/01/1900"},
483       {"Num2Date(35139, \"DD-MMM-YYYY\", \"de_DE\")", "16-Mrz-1996"},
484       // {"Num2Date(Date2Num(\"Mar 15, 2000\") - Date2Num(\"98-03-15\", "
485       //  "\"YY-MM-DD\", \"fr_CA\"))",
486       //  "Jan 1, 1902"}
487   };
488 
489   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
490     EXPECT_TRUE(Execute(tests[i].program));
491 
492     CFXJSE_Value* value = GetValue();
493     EXPECT_TRUE(value->IsString()) << "Program: " << tests[i].program;
494     EXPECT_STREQ(tests[i].result, value->ToString().c_str())
495         << "Program: " << tests[i].program << " Result: '"
496         << value->ToString().c_str() << "'";
497   }
498 }
499 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,DISABLED_Num2GMTime)500 TEST_F(CFXJSE_FormCalcContextEmbedderTest, DISABLED_Num2GMTime) {
501   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
502 
503   struct {
504     const char* program;
505     const char* result;
506   } tests[] = {// Broken on Windows only.
507                {"Num2GMTime(1, \"HH:MM:SS\")", "00:00:00"},
508                // Below broken on other platforms.
509                {"Num2GMTime(65593001, \"HH:MM:SS Z\")", "18:13:13 GMT"},
510                {"Num2GMTime(43993001, TimeFmt(4, \"de_DE\"), \"de_DE\")",
511                 "12.13 Uhr GMT"}};
512 
513   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
514     EXPECT_TRUE(Execute(tests[i].program));
515 
516     CFXJSE_Value* value = GetValue();
517     EXPECT_TRUE(value->IsString());
518     EXPECT_STREQ(tests[i].result, value->ToString().c_str())
519         << "Program: " << tests[i].program << " Result: '"
520         << value->ToString().c_str() << "'";
521   }
522 }
523 
524 // TODO(dsinclair): Broken on Mac ...
TEST_F(CFXJSE_FormCalcContextEmbedderTest,DISABLED_Num2Time)525 TEST_F(CFXJSE_FormCalcContextEmbedderTest, DISABLED_Num2Time) {
526   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
527 
528   struct {
529     const char* program;
530     const char* result;
531   } tests[] = {{"Num2Time(1, \"HH:MM:SS\")", "00:00:00"}};
532 
533   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
534     EXPECT_TRUE(Execute(tests[i].program));
535 
536     CFXJSE_Value* value = GetValue();
537     EXPECT_TRUE(value->IsString());
538     EXPECT_STREQ(tests[i].result, value->ToString().c_str())
539         << "Program: " << tests[i].program << " Result: '"
540         << value->ToString().c_str() << "'";
541   }
542 }
543 
544 // TEST_F(CFXJSE_FormCalcContextEmbedderTest, DISABLED_Time) {
545 //   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
546 //   TODO(dsinclair): Make compatible with windows.
547 //   struct timeval tp;
548 //   gettimeofday(&tp, nullptr);
549 
550 //   EXPECT_TRUE(Execute("Time()"));
551 
552 //   CFXJSE_Value* value = GetValue();
553 //   EXPECT_TRUE(value->IsInteger());
554 //   EXPECT_EQ(tp.tv_sec * 1000L + tp.tv_usec / 1000, value->ToInteger())
555 //       << "Program: Time()";
556 // }
557 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,Time2Num)558 TEST_F(CFXJSE_FormCalcContextEmbedderTest, Time2Num) {
559   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
560 
561   struct {
562     const char* program;
563     int result;
564   } tests[] = {
565       // {"Time2Num(\"00:00:00 GMT\", \"HH:MM:SS Z\")", 1},
566       {"Time2Num(\"13:13:13 GMT\", \"HH:MM:SS Z\", \"fr_FR\")", 47593001}};
567 
568   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
569     EXPECT_TRUE(Execute(tests[i].program));
570 
571     CFXJSE_Value* value = GetValue();
572     EXPECT_TRUE(value->IsInteger());
573     EXPECT_EQ(tests[i].result, value->ToInteger())
574         << "Program: " << tests[i].program;
575   }
576 }
577 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,TimeFmt)578 TEST_F(CFXJSE_FormCalcContextEmbedderTest, TimeFmt) {
579   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
580 
581   struct {
582     const char* program;
583     const char* result;
584   } tests[] = {
585       // {"TimeFmt(1)", "h::MM A"},
586       {"TimeFmt(2, \"fr_CA\")", "HH:MM:SS"},
587       {"TimeFmt(3, \"fr_FR\")", "HH:MM:SS Z"},
588       // {"TimeFmt(4, \"de_DE\")", "H.MM' Uhr 'Z"}
589   };
590 
591   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
592     EXPECT_TRUE(Execute(tests[i].program));
593 
594     CFXJSE_Value* value = GetValue();
595     EXPECT_TRUE(value->IsString());
596     EXPECT_STREQ(tests[i].result, value->ToString().c_str())
597         << "Program: " << tests[i].program << " Result: '"
598         << value->ToString().c_str() << "'";
599   }
600 }
601 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,DISABLED_Apr)602 TEST_F(CFXJSE_FormCalcContextEmbedderTest, DISABLED_Apr) {
603   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
604 
605   struct {
606     const char* program;
607     float result;
608   } tests[] = {{"Apr(35000, 269.50, 360)", 0.08515404566f},
609                {"Apr(210000 * 0.75, 850 + 110, 25 * 26)", 0.07161332404f}};
610 
611   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
612     EXPECT_TRUE(Execute(tests[i].program));
613 
614     CFXJSE_Value* value = GetValue();
615     EXPECT_TRUE(value->IsNumber());
616     EXPECT_FLOAT_EQ(tests[i].result, value->ToFloat())
617         << "Program: " << tests[i].program;
618   }
619 }
620 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,CTerm)621 TEST_F(CFXJSE_FormCalcContextEmbedderTest, CTerm) {
622   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
623 
624   struct {
625     const char* program;
626     float result;
627   } tests[] = {
628       // {"CTerm(0.02, 1000, 100)", 116.2767474515f},
629       {"CTerm(0.10, 500000, 12000)", 39.13224648502f},
630       // {"CTerm(0.0275 + 0.0025, 1000000, 55000 * 0.10)", 176.02226044975f}
631   };
632 
633   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
634     EXPECT_TRUE(Execute(tests[i].program));
635 
636     CFXJSE_Value* value = GetValue();
637     EXPECT_TRUE(value->IsNumber());
638     EXPECT_FLOAT_EQ(tests[i].result, value->ToFloat())
639         << "Program: " << tests[i].program;
640   }
641 }
642 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,FV)643 TEST_F(CFXJSE_FormCalcContextEmbedderTest, FV) {
644   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
645 
646   struct {
647     const char* program;
648     float result;
649   } tests[] = {{"FV(400, 0.10 / 12, 30 * 12)", 904195.16991842445f},
650                {"FV(1000, 0.075 / 4, 10 * 4)", 58791.96145535981f}};
651 
652   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
653     EXPECT_TRUE(Execute(tests[i].program));
654 
655     CFXJSE_Value* value = GetValue();
656     EXPECT_TRUE(value->IsNumber());
657     EXPECT_FLOAT_EQ(tests[i].result, value->ToFloat())
658         << "Program: " << tests[i].program;
659   }
660 }
661 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,IPmt)662 TEST_F(CFXJSE_FormCalcContextEmbedderTest, IPmt) {
663   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
664 
665   struct {
666     const char* program;
667     float result;
668   } tests[] = {{"IPmt(30000, 0.085, 295.50, 7, 3)", 624.8839283142f},
669                {"IPmt(160000, 0.0475, 980, 24, 12)", 7103.80833569485f},
670                {"IPmt(15000, 0.065, 65.50, 15, 1)", 0.0f}};
671 
672   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
673     EXPECT_TRUE(Execute(tests[i].program));
674 
675     CFXJSE_Value* value = GetValue();
676     EXPECT_TRUE(value->IsNumber());
677     EXPECT_FLOAT_EQ(tests[i].result, value->ToFloat())
678         << "Program: " << tests[i].program;
679   }
680 }
681 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,DISABLED_NPV)682 TEST_F(CFXJSE_FormCalcContextEmbedderTest, DISABLED_NPV) {
683   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
684 
685   struct {
686     const char* program;
687     float result;
688   } tests[] = {{"NPV(0.065, 5000)", 4694.83568075117f},
689                {"NPV(0.10, 500, 1500, 4000, 10000)", 11529.60863329007f},
690                {"NPV(0.0275 / 12, 50, 60, 40, 100, 25)", 273.14193838457f}};
691 
692   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
693     EXPECT_TRUE(Execute(tests[i].program));
694 
695     CFXJSE_Value* value = GetValue();
696     EXPECT_TRUE(value->IsNumber()) << "Program: " << tests[i].program;
697     EXPECT_FLOAT_EQ(tests[i].result, value->ToFloat())
698         << "Program: " << tests[i].program;
699   }
700 }
701 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,Pmt)702 TEST_F(CFXJSE_FormCalcContextEmbedderTest, Pmt) {
703   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
704 
705   struct {
706     const char* program;
707     float result;
708   } tests[] = {// {"Pmt(150000, 0.0475 / 12, 25 * 12)", 855.17604207164f},
709                {"Pmt(25000, 0.085, 12)", 3403.82145169876f}};
710 
711   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
712     EXPECT_TRUE(Execute(tests[i].program));
713 
714     CFXJSE_Value* value = GetValue();
715     EXPECT_TRUE(value->IsNumber());
716     EXPECT_FLOAT_EQ(tests[i].result, value->ToFloat())
717         << "Program: " << tests[i].program;
718   }
719 }
720 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,PPmt)721 TEST_F(CFXJSE_FormCalcContextEmbedderTest, PPmt) {
722   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
723 
724   struct {
725     const char* program;
726     float result;
727   } tests[] = {
728       {"PPmt(30000, 0.085, 295.50, 7, 3)", 261.6160716858f},
729       {"PPmt(160000, 0.0475, 980, 24, 12)", 4656.19166430515f},
730       // {"PPmt(15000, 0.065, 65.50, 15, 1)", 0.0f}
731   };
732 
733   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
734     EXPECT_TRUE(Execute(tests[i].program));
735 
736     CFXJSE_Value* value = GetValue();
737     EXPECT_TRUE(value->IsNumber());
738     EXPECT_FLOAT_EQ(tests[i].result, value->ToFloat())
739         << "Program: " << tests[i].program;
740   }
741 }
742 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,PV)743 TEST_F(CFXJSE_FormCalcContextEmbedderTest, PV) {
744   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
745 
746   struct {
747     const char* program;
748     float result;
749   } tests[] = {
750       {"PV(400, 0.10 / 12, 30 * 12)", 45580.32799074439f},
751       // {"PV(1000, 0.075 / 4, 10 * 4)", 58791.96145535981f}
752   };
753 
754   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
755     EXPECT_TRUE(Execute(tests[i].program));
756 
757     CFXJSE_Value* value = GetValue();
758     EXPECT_TRUE(value->IsNumber());
759     EXPECT_FLOAT_EQ(tests[i].result, value->ToFloat())
760         << "Program: " << tests[i].program;
761   }
762 }
763 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,DISABLED_Rate)764 TEST_F(CFXJSE_FormCalcContextEmbedderTest, DISABLED_Rate) {
765   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
766 
767   struct {
768     const char* program;
769     float result;
770   } tests[] = {{"Rate(12000, 8000, 5)", 0.0844717712f},
771                {"Rate(10000, 0.25 * 5000, 4 * 12)", 0.04427378243f}};
772 
773   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
774     EXPECT_TRUE(Execute(tests[i].program));
775 
776     CFXJSE_Value* value = GetValue();
777     EXPECT_TRUE(value->IsNumber());
778     EXPECT_FLOAT_EQ(tests[i].result, value->ToFloat())
779         << "Program: " << tests[i].program;
780   }
781 }
782 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,Term)783 TEST_F(CFXJSE_FormCalcContextEmbedderTest, Term) {
784   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
785 
786   struct {
787     const char* program;
788     float result;
789   } tests[] = {// {"Term(475, .05, 1500)", 3.00477517728f},
790                {"Term(2500, 0.0275 + 0.0025, 5000)", 1.97128786369f}};
791 
792   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
793     EXPECT_TRUE(Execute(tests[i].program));
794 
795     CFXJSE_Value* value = GetValue();
796     EXPECT_TRUE(value->IsNumber());
797     EXPECT_FLOAT_EQ(tests[i].result, value->ToFloat())
798         << "Program: " << tests[i].program;
799   }
800 }
801 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,Choose)802 TEST_F(CFXJSE_FormCalcContextEmbedderTest, Choose) {
803   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
804 
805   struct {
806     const char* program;
807     const char* result;
808   } tests[] = {
809       {"Choose(3, \"Taxes\", \"Price\", \"Person\", \"Teller\")", "Person"},
810       {"Choose(2, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)", "9"},
811       {"Choose(20/3, \"A\", \"B\", \"C\", \"D\", \"E\", \"F\", \"G\", \"H\")",
812        "F"}};
813 
814   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
815     EXPECT_TRUE(Execute(tests[i].program));
816 
817     CFXJSE_Value* value = GetValue();
818     EXPECT_TRUE(value->IsString());
819     EXPECT_STREQ(tests[i].result, value->ToString().c_str())
820         << "Program: " << tests[i].program << " Result: '"
821         << value->ToString().c_str() << "'";
822   }
823 }
824 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,Exists)825 TEST_F(CFXJSE_FormCalcContextEmbedderTest, Exists) {
826   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
827 
828   EXPECT_TRUE(Execute("Exists(\"hello world\")"));
829   CFXJSE_Value* value = GetValue();
830   EXPECT_TRUE(value->IsInteger());
831   EXPECT_FALSE(value->ToBoolean());
832 }
833 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,HasValue)834 TEST_F(CFXJSE_FormCalcContextEmbedderTest, HasValue) {
835   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
836 
837   struct {
838     const char* program;
839     bool result;
840   } tests[] = {{"HasValue(2)", true}, {"HasValue(\" \")", false}};
841 
842   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
843     EXPECT_TRUE(Execute(tests[i].program));
844 
845     CFXJSE_Value* value = GetValue();
846     EXPECT_TRUE(value->IsInteger()) << "Program: " << tests[i].program;
847     EXPECT_EQ(tests[i].result, value->ToBoolean())
848         << "Program: " << tests[i].program;
849   }
850 }
851 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,Oneof)852 TEST_F(CFXJSE_FormCalcContextEmbedderTest, Oneof) {
853   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
854 
855   struct {
856     const char* program;
857     bool result;
858   } tests[] = {
859       {"Oneof(3, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)", true},
860       {"Oneof(\"John\", \"Bill\", \"Gary\", \"Joan\", \"John\", \"Lisa\")",
861        true},
862       {"Oneof(3, 1, 25)", false},
863       {"Oneof(3, 3, null)", true},
864       {"Oneof(3, null, null)", false},
865   };
866 
867   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
868     EXPECT_TRUE(Execute(tests[i].program));
869 
870     CFXJSE_Value* value = GetValue();
871     EXPECT_TRUE(value->IsInteger()) << "Program: " << tests[i].program;
872     EXPECT_EQ(tests[i].result, value->ToBoolean())
873         << "Program: " << tests[i].program;
874   }
875 }
876 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,Within)877 TEST_F(CFXJSE_FormCalcContextEmbedderTest, Within) {
878   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
879 
880   struct {
881     const char* program;
882     bool result;
883   } tests[] = {{"Within(\"C\", \"A\", \"D\")", true},
884                {"Within(1.5, 0, 2)", true},
885                {"Within(-1, 0, 2)", false}};
886 
887   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
888     EXPECT_TRUE(Execute(tests[i].program));
889 
890     CFXJSE_Value* value = GetValue();
891     EXPECT_TRUE(value->IsInteger()) << "Program: " << tests[i].program;
892     EXPECT_EQ(tests[i].result, value->ToBoolean())
893         << "Program: " << tests[i].program;
894   }
895 }
896 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,DISABLED_Eval)897 TEST_F(CFXJSE_FormCalcContextEmbedderTest, DISABLED_Eval) {
898   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
899 
900   struct {
901     const char* program;
902     int result;
903   } tests[] = {{"eval(\"10*3+5*4\")", 50}};
904 
905   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
906     EXPECT_TRUE(Execute(tests[i].program));
907 
908     CFXJSE_Value* value = GetValue();
909     EXPECT_TRUE(value->IsInteger());
910     EXPECT_EQ(tests[i].result, value->ToInteger())
911         << "Program: " << tests[i].program;
912   }
913 }
914 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,DISABLED_Null)915 TEST_F(CFXJSE_FormCalcContextEmbedderTest, DISABLED_Null) {
916   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
917 
918   struct {
919     const char* program;
920     const char* result;
921   } tests[] = {{"Null()", "null"},
922                {"Concat(\"ABC\", Null(), \"DEF\")", "ABCDEF"}};
923 
924   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
925     EXPECT_TRUE(Execute(tests[i].program));
926 
927     CFXJSE_Value* value = GetValue();
928     EXPECT_TRUE(value->IsString());
929     EXPECT_STREQ(tests[i].result, value->ToString().c_str())
930         << "Program: " << tests[i].program << " Result: '"
931         << value->ToString().c_str() << "'";
932   }
933 
934   EXPECT_TRUE(Execute("Null() + 5"));
935 
936   CFXJSE_Value* value = GetValue();
937   EXPECT_TRUE(value->IsInteger());
938   EXPECT_EQ(5, value->ToInteger());
939 }
940 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,Ref)941 TEST_F(CFXJSE_FormCalcContextEmbedderTest, Ref) {
942   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
943 
944   struct {
945     const char* program;
946     const char* result;
947   } tests[] = {{"Ref(\"10*3+5*4\")", "10*3+5*4"}, {"Ref(\"hello\")", "hello"}};
948 
949   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
950     EXPECT_TRUE(Execute(tests[i].program));
951 
952     CFXJSE_Value* value = GetValue();
953     EXPECT_TRUE(value->IsString());
954     EXPECT_STREQ(tests[i].result, value->ToString().c_str())
955         << "Program: " << tests[i].program << " Result: '"
956         << value->ToString().c_str() << "'";
957   }
958 }
959 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,UnitType)960 TEST_F(CFXJSE_FormCalcContextEmbedderTest, UnitType) {
961   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
962 
963   struct {
964     const char* program;
965     const char* result;
966   } tests[] = {{"UnitType(\"36 in\")", "in"},
967                {"UnitType(\"2.54centimeters\")", "cm"},
968                {"UnitType(\"picas\")", "pt"},
969                {"UnitType(\"2.cm\")", "cm"},
970                {"UnitType(\"2.zero cm\")", "in"},
971                {"UnitType(\"kilometers\")", "in"}};
972 
973   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
974     EXPECT_TRUE(Execute(tests[i].program));
975 
976     CFXJSE_Value* value = GetValue();
977     EXPECT_TRUE(value->IsString());
978     EXPECT_STREQ(tests[i].result, value->ToString().c_str())
979         << "Program: " << tests[i].program << " Result: '"
980         << value->ToString().c_str() << "'";
981   }
982 }
983 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,UnitValue)984 TEST_F(CFXJSE_FormCalcContextEmbedderTest, UnitValue) {
985   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
986 
987   struct {
988     const char* program;
989     float result;
990   } tests[] = {
991       {"UnitValue(\"2in\")", 2.0f}, {"UnitValue(\"2in\", \"cm\")", 5.08f},
992       // {"UnitValue(\"6\", \"pt\")", 432f},
993       // {"UnitType(\"A\", \"cm\")", 0.0f},
994       // {"UnitType(\"5.08cm\", \"kilograms\")", 2.0f}
995   };
996 
997   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
998     EXPECT_TRUE(Execute(tests[i].program));
999 
1000     CFXJSE_Value* value = GetValue();
1001     EXPECT_TRUE(value->IsNumber());
1002     EXPECT_FLOAT_EQ(tests[i].result, value->ToFloat())
1003         << "Program: " << tests[i].program;
1004   }
1005 }
1006 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,At)1007 TEST_F(CFXJSE_FormCalcContextEmbedderTest, At) {
1008   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
1009 
1010   struct {
1011     const char* program;
1012     int result;
1013   } tests[] = {{"At(\"ABCDEFGH\", \"AB\")", 1},
1014                {"At(\"ABCDEFGH\", \"F\")", 6},
1015                {"At(23412931298471, 29)", 5}};
1016 
1017   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
1018     EXPECT_TRUE(Execute(tests[i].program));
1019 
1020     CFXJSE_Value* value = GetValue();
1021     EXPECT_TRUE(value->IsInteger());
1022     EXPECT_EQ(tests[i].result, value->ToInteger())
1023         << "Program: " << tests[i].program;
1024   }
1025 }
1026 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,Concat)1027 TEST_F(CFXJSE_FormCalcContextEmbedderTest, Concat) {
1028   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
1029 
1030   struct {
1031     const char* program;
1032     const char* result;
1033   } tests[] = {{"Concat(\"ABC\", \"DEF\")", "ABCDEF"},
1034                {"Concat(\"Tony\", Space(1), \"Blue\")", "Tony Blue"},
1035                {"Concat(\"You owe \", WordNum(1154.67, 2), \".\")",
1036                 "You owe One Thousand One Hundred Fifty-four Dollars And "
1037                 "Sixty-seven Cents."}};
1038 
1039   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
1040     EXPECT_TRUE(Execute(tests[i].program));
1041 
1042     CFXJSE_Value* value = GetValue();
1043     EXPECT_TRUE(value->IsString());
1044     EXPECT_STREQ(tests[i].result, value->ToString().c_str())
1045         << "Program: " << tests[i].program << " Result: '"
1046         << value->ToString().c_str() << "'";
1047   }
1048 }
1049 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,Decode)1050 TEST_F(CFXJSE_FormCalcContextEmbedderTest, Decode) {
1051   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
1052 
1053   struct {
1054     const char* program;
1055     const char* result;
1056   } tests[] = {
1057       {"Decode(\"&AElig;&Aacute;&Acirc;&Aacute;&Acirc;\", \"html\")", "ÆÁÂÁÂ"},
1058       // {"Decode(\"~!@#$%%^&amp;*()_+|`{&quot;}[]&lt;&gt;?,./;&apos;:\", "
1059       //  "\"xml\")",
1060       //  "~!@#$%%^&*()_+|`{"
1061       //  "}[]<>?,./;':"}
1062   };
1063 
1064   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
1065     EXPECT_TRUE(Execute(tests[i].program));
1066 
1067     CFXJSE_Value* value = GetValue();
1068     EXPECT_TRUE(value->IsString());
1069     EXPECT_STREQ(tests[i].result, value->ToString().c_str())
1070         << "Program: " << tests[i].program << " Result: '"
1071         << value->ToString().c_str() << "'";
1072   }
1073 }
1074 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,DISABLED_Encode)1075 TEST_F(CFXJSE_FormCalcContextEmbedderTest, DISABLED_Encode) {
1076   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
1077 
1078   struct {
1079     const char* program;
1080     const char* result;
1081   } tests[] = {
1082       {"Encode(\"\"\"hello, world!\"\"\", \"url\")",
1083        "%%22hello,%%20world!%%22"},
1084       {"Encode(\"ÁÂÃÄÅÆ\", \"html\")", "&#xc1;&#Xc2;&#Xc3;&#xc4;&#xc5;&#xc6;"}};
1085 
1086   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
1087     EXPECT_TRUE(Execute(tests[i].program));
1088 
1089     CFXJSE_Value* value = GetValue();
1090     EXPECT_TRUE(value->IsString());
1091     EXPECT_STREQ(tests[i].result, value->ToString().c_str())
1092         << "Program: " << tests[i].program << " Result: '"
1093         << value->ToString().c_str() << "'";
1094   }
1095 }
1096 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,DISABLED_Format)1097 TEST_F(CFXJSE_FormCalcContextEmbedderTest, DISABLED_Format) {
1098   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
1099 
1100   struct {
1101     const char* program;
1102     const char* result;
1103   } tests[] = {{"Format(\"MMM D, YYYY\", \"20020901\")", "Sep 1, 2002"},
1104                {"Format(\"$9,999,999.99\", 1234567.89)", "$1,234,567.89"}};
1105 
1106   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
1107     EXPECT_TRUE(Execute(tests[i].program));
1108 
1109     CFXJSE_Value* value = GetValue();
1110     EXPECT_TRUE(value->IsString());
1111     EXPECT_STREQ(tests[i].result, value->ToString().c_str())
1112         << "Program: " << tests[i].program << " Result: '"
1113         << value->ToString().c_str() << "'";
1114   }
1115 }
1116 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,Left)1117 TEST_F(CFXJSE_FormCalcContextEmbedderTest, Left) {
1118   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
1119 
1120   struct {
1121     const char* program;
1122     const char* result;
1123   } tests[] = {{"Left(\"ABCDEFGH\", 3)", "ABC"},
1124                {"Left(\"Tony Blue\", 5)", "Tony "}};
1125 
1126   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
1127     EXPECT_TRUE(Execute(tests[i].program));
1128 
1129     CFXJSE_Value* value = GetValue();
1130     EXPECT_TRUE(value->IsString());
1131     EXPECT_STREQ(tests[i].result, value->ToString().c_str())
1132         << "Program: " << tests[i].program << " Result: '"
1133         << value->ToString().c_str() << "'";
1134   }
1135 }
1136 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,Len)1137 TEST_F(CFXJSE_FormCalcContextEmbedderTest, Len) {
1138   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
1139 
1140   struct {
1141     const char* program;
1142     int result;
1143   } tests[] = {
1144       {"Len(\"ABCDEFGH\")", 8}, {"Len(4)", 1}, {"Len(Str(4.532, 6, 4))", 6}};
1145 
1146   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
1147     EXPECT_TRUE(Execute(tests[i].program));
1148 
1149     CFXJSE_Value* value = GetValue();
1150     EXPECT_TRUE(value->IsInteger());
1151     EXPECT_EQ(tests[i].result, value->ToInteger())
1152         << "Program: " << tests[i].program;
1153   }
1154 }
1155 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,Lower)1156 TEST_F(CFXJSE_FormCalcContextEmbedderTest, Lower) {
1157   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
1158 
1159   struct {
1160     const char* program;
1161     const char* result;
1162   } tests[] = {{"Lower(\"ABC\")", "abc"},
1163                {"Lower(\"21 Main St.\")", "21 main st."},
1164                {"Lower(15)", "15"}};
1165 
1166   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
1167     EXPECT_TRUE(Execute(tests[i].program));
1168 
1169     CFXJSE_Value* value = GetValue();
1170     EXPECT_TRUE(value->IsString());
1171     EXPECT_STREQ(tests[i].result, value->ToString().c_str())
1172         << "Program: " << tests[i].program << " Result: '"
1173         << value->ToString().c_str() << "'";
1174   }
1175 }
1176 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,Ltrim)1177 TEST_F(CFXJSE_FormCalcContextEmbedderTest, Ltrim) {
1178   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
1179 
1180   struct {
1181     const char* program;
1182     const char* result;
1183   } tests[] = {{"Ltrim(\"   ABCD\")", "ABCD"},
1184                {"Ltrim(Rtrim(\"    Tony Blue    \"))", "Tony Blue"}};
1185 
1186   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
1187     EXPECT_TRUE(Execute(tests[i].program));
1188 
1189     CFXJSE_Value* value = GetValue();
1190     EXPECT_TRUE(value->IsString());
1191     EXPECT_STREQ(tests[i].result, value->ToString().c_str())
1192         << "Program: " << tests[i].program << " Result: '"
1193         << value->ToString().c_str() << "'";
1194   }
1195 }
1196 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,DISABLED_Parse)1197 TEST_F(CFXJSE_FormCalcContextEmbedderTest, DISABLED_Parse) {
1198   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
1199 
1200   struct {
1201     const char* program;
1202     const char* result;
1203   } tests[] = {{"Parse(\"MMM D, YYYY\", \"Sep 1, 2002\")", "2002-09-01"}};
1204 
1205   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
1206     EXPECT_TRUE(Execute(tests[i].program));
1207 
1208     CFXJSE_Value* value = GetValue();
1209     EXPECT_TRUE(value->IsString());
1210     EXPECT_STREQ(tests[i].result, value->ToString().c_str())
1211         << "Program: " << tests[i].program << " Result: '"
1212         << value->ToString().c_str() << "'";
1213   }
1214 
1215   EXPECT_TRUE(Execute("Parse(\"$9,999,999.99\", \"$1,234,567.89\")"));
1216   CFXJSE_Value* value = GetValue();
1217   EXPECT_TRUE(value->IsNumber());
1218   EXPECT_FLOAT_EQ(1234567.89f, value->ToFloat());
1219 }
1220 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,Replace)1221 TEST_F(CFXJSE_FormCalcContextEmbedderTest, Replace) {
1222   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
1223 
1224   struct {
1225     const char* program;
1226     const char* result;
1227   } tests[] = {{"Replace(\"Tony Blue\", \"Tony\", \"Chris\")", "Chris Blue"},
1228                {"Replace(\"ABCDEFGH\", \"D\")", "ABCEFGH"},
1229                {"Replace(\"ABCDEFGH\", \"d\")", "ABCDEFGH"}};
1230 
1231   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
1232     EXPECT_TRUE(Execute(tests[i].program));
1233 
1234     CFXJSE_Value* value = GetValue();
1235     EXPECT_TRUE(value->IsString());
1236     EXPECT_STREQ(tests[i].result, value->ToString().c_str())
1237         << "Program: " << tests[i].program << " Result: '"
1238         << value->ToString().c_str() << "'";
1239   }
1240 }
1241 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,Right)1242 TEST_F(CFXJSE_FormCalcContextEmbedderTest, Right) {
1243   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
1244 
1245   struct {
1246     const char* program;
1247     const char* result;
1248   } tests[] = {{"Right(\"ABCDEFGH\", 3)", "FGH"},
1249                {"Right(\"Tony Blue\", 5)", " Blue"}};
1250 
1251   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
1252     EXPECT_TRUE(Execute(tests[i].program));
1253 
1254     CFXJSE_Value* value = GetValue();
1255     EXPECT_TRUE(value->IsString());
1256     EXPECT_STREQ(tests[i].result, value->ToString().c_str())
1257         << "Program: " << tests[i].program << " Result: '"
1258         << value->ToString().c_str() << "'";
1259   }
1260 }
1261 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,Rtrim)1262 TEST_F(CFXJSE_FormCalcContextEmbedderTest, Rtrim) {
1263   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
1264 
1265   struct {
1266     const char* program;
1267     const char* result;
1268   } tests[] = {{"Rtrim(\"ABCD   \")", "ABCD"},
1269                {"Rtrim(\"Tony Blue      \t\")", "Tony Blue"}};
1270 
1271   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
1272     EXPECT_TRUE(Execute(tests[i].program));
1273 
1274     CFXJSE_Value* value = GetValue();
1275     EXPECT_TRUE(value->IsString());
1276     EXPECT_STREQ(tests[i].result, value->ToString().c_str())
1277         << "Program: " << tests[i].program << " Result: '"
1278         << value->ToString().c_str() << "'";
1279   }
1280 }
1281 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,Space)1282 TEST_F(CFXJSE_FormCalcContextEmbedderTest, Space) {
1283   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
1284 
1285   struct {
1286     const char* program;
1287     const char* result;
1288   } tests[] = {{"Space(5)", "     "},
1289                {"Concat(\"Tony\", Space(1), \"Blue\")", "Tony Blue"}};
1290 
1291   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
1292     EXPECT_TRUE(Execute(tests[i].program));
1293 
1294     CFXJSE_Value* value = GetValue();
1295     EXPECT_TRUE(value->IsString());
1296     EXPECT_STREQ(tests[i].result, value->ToString().c_str())
1297         << "Program: " << tests[i].program << " Result: '"
1298         << value->ToString().c_str() << "'";
1299   }
1300 }
1301 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,Str)1302 TEST_F(CFXJSE_FormCalcContextEmbedderTest, Str) {
1303   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
1304 
1305   struct {
1306     const char* program;
1307     const char* result;
1308   } tests[] = {{"Str(2.456)", "         2"},
1309                {"Str(4.532, 6, 4)", "4.5320"},
1310                {"Str(234.458, 4)", " 234"},
1311                {"Str(31.2345, 4, 2)", "****"}};
1312 
1313   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
1314     EXPECT_TRUE(Execute(tests[i].program));
1315 
1316     CFXJSE_Value* value = GetValue();
1317     EXPECT_TRUE(value->IsString());
1318     EXPECT_STREQ(tests[i].result, value->ToString().c_str())
1319         << "Program: " << tests[i].program << " Result: '"
1320         << value->ToString().c_str() << "'";
1321   }
1322 }
1323 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,Stuff)1324 TEST_F(CFXJSE_FormCalcContextEmbedderTest, Stuff) {
1325   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
1326 
1327   struct {
1328     const char* program;
1329     const char* result;
1330   } tests[] = {{"Stuff(\"TonyBlue\", 5, 0, \" \")", "Tony Blue"},
1331                {"Stuff(\"ABCDEFGH\", 4, 2)", "ABCFGH"},
1332                {"Stuff(\"members-list@myweb.com\", 0, 0, \"cc:\")",
1333                 "cc:members-list@myweb.com"}};
1334 
1335   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
1336     EXPECT_TRUE(Execute(tests[i].program));
1337 
1338     CFXJSE_Value* value = GetValue();
1339     EXPECT_TRUE(value->IsString());
1340     EXPECT_STREQ(tests[i].result, value->ToString().c_str())
1341         << "Program: " << tests[i].program << " Result: '"
1342         << value->ToString().c_str() << "'";
1343   }
1344 }
1345 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,Substr)1346 TEST_F(CFXJSE_FormCalcContextEmbedderTest, Substr) {
1347   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
1348 
1349   struct {
1350     const char* program;
1351     const char* result;
1352   } tests[] = {{"Substr(\"ABCDEFG\", 3, 4)", "CDEF"},
1353                {"Substr(3214, 2, 1)", "2"},
1354                {"Substr(\"ABCDEFG\", 5, 0)", ""},
1355                {"Substr(\"21 Waterloo St.\", 4, 5)", "Water"}};
1356 
1357   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
1358     EXPECT_TRUE(Execute(tests[i].program));
1359 
1360     CFXJSE_Value* value = GetValue();
1361     EXPECT_TRUE(value->IsString());
1362     EXPECT_STREQ(tests[i].result, value->ToString().c_str())
1363         << "Program: " << tests[i].program << " Result: '"
1364         << value->ToString().c_str() << "'";
1365   }
1366 }
1367 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,Uuid)1368 TEST_F(CFXJSE_FormCalcContextEmbedderTest, Uuid) {
1369   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
1370 
1371   EXPECT_TRUE(Execute("Uuid()"));
1372 
1373   CFXJSE_Value* value = GetValue();
1374   EXPECT_TRUE(value->IsString());
1375 }
1376 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,Upper)1377 TEST_F(CFXJSE_FormCalcContextEmbedderTest, Upper) {
1378   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
1379 
1380   struct {
1381     const char* program;
1382     const char* result;
1383   } tests[] = {{"Upper(\"abc\")", "ABC"},
1384                {"Upper(\"21 Main St.\")", "21 MAIN ST."},
1385                {"Upper(15)", "15"}};
1386 
1387   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
1388     EXPECT_TRUE(Execute(tests[i].program));
1389 
1390     CFXJSE_Value* value = GetValue();
1391     EXPECT_TRUE(value->IsString());
1392     EXPECT_STREQ(tests[i].result, value->ToString().c_str())
1393         << "Program: " << tests[i].program << " Result: '"
1394         << value->ToString().c_str() << "'";
1395   }
1396 }
1397 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,WordNum)1398 TEST_F(CFXJSE_FormCalcContextEmbedderTest, WordNum) {
1399   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
1400 
1401   struct {
1402     const char* program;
1403     const char* result;
1404   } tests[] = {
1405       // {"WordNum(123.45)",
1406       //  "One Hundred and Twenty-three"},  // This looks like it's wrong in the
1407       //                                    // Formcalc document.
1408       // {"WordNum(123.45, 1)", "One Hundred and Twenty-three Dollars"},
1409       {"WordNum(1154.67, 2)",
1410        "One Thousand One Hundred Fifty-four Dollars And Sixty-seven Cents"},
1411       {"WordNum(43, 2)", "Forty-three Dollars And Zero Cents"}};
1412 
1413   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
1414     EXPECT_TRUE(Execute(tests[i].program));
1415 
1416     CFXJSE_Value* value = GetValue();
1417     EXPECT_TRUE(value->IsString());
1418     EXPECT_STREQ(tests[i].result, value->ToString().c_str())
1419         << "Program: " << tests[i].program << " Result: '"
1420         << value->ToString().c_str() << "'";
1421   }
1422 }
1423 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,Get)1424 TEST_F(CFXJSE_FormCalcContextEmbedderTest, Get) {
1425   // TODO(dsinclair): Is this supported?
1426 }
1427 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,Post)1428 TEST_F(CFXJSE_FormCalcContextEmbedderTest, Post) {
1429   // TODO(dsinclair): Is this supported?
1430 }
1431 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,Put)1432 TEST_F(CFXJSE_FormCalcContextEmbedderTest, Put) {
1433   // TODO(dsinclair): Is this supported?
1434 }
1435 
TEST_F(CFXJSE_FormCalcContextEmbedderTest,InvalidFunctions)1436 TEST_F(CFXJSE_FormCalcContextEmbedderTest, InvalidFunctions) {
1437   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
1438 
1439   const char* const tests[] = {
1440       "F()", "()", "()()()", "Round(2.0)()",
1441   };
1442 
1443   for (size_t i = 0; i < FX_ArraySize(tests); ++i) {
1444     EXPECT_FALSE(ExecuteSilenceFailure(tests[i]));
1445   }
1446 }
1447