Home | History | Annotate | Download | only in fxjs
      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 "fxjs/cjs_util.h"
      6 
      7 #include "testing/gtest/include/gtest/gtest.h"
      8 #include "testing/test_support.h"
      9 
     10 TEST(CJS_Util, ParseDataType) {
     11   struct ParseDataTypeCase {
     12     const wchar_t* const input_string;
     13     const int expected;
     14   };
     15 
     16   // Commented out tests follow the spec but are not passing.
     17   const ParseDataTypeCase cases[] = {
     18       // Not conversions
     19       {L"", -1},
     20       {L"d", -1},
     21 
     22       // Simple cases
     23       {L"%d", UTIL_INT},
     24       {L"%x", UTIL_INT},
     25       {L"%f", UTIL_DOUBLE},
     26       {L"%s", UTIL_STRING},
     27 
     28       // nDecSep Not implemented
     29       // {L"%,0d", UTIL_INT},
     30       // {L"%,1d", UTIL_INT},
     31       // {L"%,2d", UTIL_INT},
     32       // {L"%,3d", UTIL_INT},
     33       // {L"%,4d", -1},
     34       // {L"%,d", -1},
     35 
     36       // cFlags("+ 0#"") are only valid for numeric conversions.
     37       {L"%+d", UTIL_INT},
     38       {L"%+x", UTIL_INT},
     39       {L"%+f", UTIL_DOUBLE},
     40       // {L"%+s", -1},
     41       {L"% d", UTIL_INT},
     42       {L"% x", UTIL_INT},
     43       {L"% f", UTIL_DOUBLE},
     44       // {L"% s", -1},
     45       {L"%0d", UTIL_INT},
     46       {L"%0x", UTIL_INT},
     47       {L"%0f", UTIL_DOUBLE},
     48       // {L"%0s", -1},
     49       {L"%#d", UTIL_INT},
     50       {L"%#x", UTIL_INT},
     51       {L"%#f", UTIL_DOUBLE},
     52       // {L"%#s", -1},
     53 
     54       // nWidth should work. for all conversions, can be combined with cFlags=0
     55       // for numbers.
     56       {L"%5d", UTIL_INT},
     57       {L"%05d", UTIL_INT},
     58       {L"%5x", UTIL_INT},
     59       {L"%05x", UTIL_INT},
     60       {L"%5f", UTIL_DOUBLE},
     61       {L"%05f", UTIL_DOUBLE},
     62       {L"%5s", UTIL_STRING},
     63       // {L"%05s", -1},
     64 
     65       // nPrecision should only work for float
     66       // {L"%.5d", -1},
     67       // {L"%.5x", -1},
     68       {L"%.5f", UTIL_DOUBLE},
     69       // {L"%.5s", -1},
     70       // {L"%.14d", -1},
     71       // {L"%.14x", -1},
     72       {L"%.14f", UTIL_DOUBLE},
     73       // {L"%.14s", -1},
     74       // {L"%.f", -1},
     75 
     76       // See https://crbug.com/740166
     77       // nPrecision too large (> 260) causes crashes in Windows.
     78       // Avoid this by limiting to two digits
     79       {L"%.1d", UTIL_INT},
     80       {L"%.10d", UTIL_INT},
     81       {L"%.100d", -1},
     82 
     83       // Unexpected characters
     84       {L"%ad", -1},
     85       {L"%bx", -1},
     86       // {L"%cf", -1},
     87       // {L"%es", -1},
     88       // {L"%gd", -1},
     89       {L"%hx", -1},
     90       // {L"%if", -1},
     91       {L"%js", -1},
     92       {L"%@d", -1},
     93       {L"%~x", -1},
     94       {L"%[f", -1},
     95       {L"%\0s", -1},
     96       {L"%\nd", -1},
     97       {L"%\rx", -1},
     98       // {L"%%f", -1},
     99       // {L"%  s", -1},
    100 
    101       // Combine multiple valid components
    102       {L"%+6d", UTIL_INT},
    103       {L"% 7x", UTIL_INT},
    104       {L"%#9.3f", UTIL_DOUBLE},
    105       {L"%10s", UTIL_STRING},
    106   };
    107 
    108   for (size_t i = 0; i < FX_ArraySize(cases); i++) {
    109     std::wstring input(cases[i].input_string);
    110     EXPECT_EQ(cases[i].expected, util::ParseDataType(&input))
    111         << cases[i].input_string;
    112   }
    113 }
    114