Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(709)

Side by Side Diff: third_party/WebKit/Source/modules/payments/PaymentsValidatorsTest.cpp

Issue 2410143003: Add currencySystem field to PaymentCurrencyAmount (Closed)
Patch Set: Add currencySystem field to PaymentCurrencyAmount Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "modules/payments/PaymentsValidators.h" 5 #include "modules/payments/PaymentsValidators.h"
6 6
7 #include "testing/gtest/include/gtest/gtest.h" 7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "wtf/text/WTFString.h" 8 #include "wtf/text/WTFString.h"
9 #include <ostream> // NOLINT 9 #include <ostream> // NOLINT
10 10
11 namespace blink { 11 namespace blink {
12 namespace { 12 namespace {
13 13
14 struct TestCase { 14 struct CurrencyCodeTestCase {
15 TestCase(const char* input, bool expectedValid) 15 CurrencyCodeTestCase(const char* code, const char* system, bool expectedValid)
16 : input(input), expectedValid(expectedValid) {} 16 : code(code), system(system), expectedValid(expectedValid) {}
17 ~TestCase() {} 17 ~CurrencyCodeTestCase() {}
18 18
19 const char* input; 19 const char* code;
20 const char* system;
20 bool expectedValid; 21 bool expectedValid;
21 }; 22 };
22 23
23 std::ostream& operator<<(std::ostream& out, const TestCase& testCase) { 24 class PaymentsCurrencyValidatorTest
24 out << "'" << testCase.input << "' is expected to be " 25 : public testing::TestWithParam<CurrencyCodeTestCase> {};
25 << (testCase.expectedValid ? "valid" : "invalid");
26 return out;
27 }
28
29 class PaymentsCurrencyValidatorTest : public testing::TestWithParam<TestCase> {
30 };
31 26
32 const char* longString2048() { 27 const char* longString2048() {
33 static char longString[2049]; 28 static char longString[2049];
34 for (int i = 0; i < 2048; i++) 29 for (int i = 0; i < 2048; i++)
35 longString[i] = 'a'; 30 longString[i] = 'a';
36 longString[2048] = '\0'; 31 longString[2048] = '\0';
37 return longString; 32 return longString;
38 } 33 }
39 34
40 const char* longString2049() { 35 const char* longString2049() {
41 static char longString[2050]; 36 static char longString[2050];
42 for (int i = 0; i < 2049; i++) 37 for (int i = 0; i < 2049; i++)
43 longString[i] = 'a'; 38 longString[i] = 'a';
44 longString[2049] = '\0'; 39 longString[2049] = '\0';
45 return longString; 40 return longString;
46 } 41 }
47 42
48 TEST_P(PaymentsCurrencyValidatorTest, IsValidCurrencyCodeFormat) { 43 TEST_P(PaymentsCurrencyValidatorTest, IsValidCurrencyCodeFormat) {
49 String errorMessage; 44 String errorMessage;
50 EXPECT_EQ(GetParam().expectedValid, 45 EXPECT_EQ(GetParam().expectedValid,
51 PaymentsValidators::isValidCurrencyCodeFormat(GetParam().input, 46 PaymentsValidators::isValidCurrencyCodeFormat(
52 &errorMessage)) 47 GetParam().code, GetParam().system, &errorMessage))
53 << errorMessage; 48 << errorMessage;
54 EXPECT_EQ(GetParam().expectedValid, errorMessage.isEmpty()) << errorMessage; 49 EXPECT_EQ(GetParam().expectedValid, errorMessage.isEmpty()) << errorMessage;
55 50
56 EXPECT_EQ( 51 EXPECT_EQ(GetParam().expectedValid,
57 GetParam().expectedValid, 52 PaymentsValidators::isValidCurrencyCodeFormat(
58 PaymentsValidators::isValidCurrencyCodeFormat(GetParam().input, nullptr)); 53 GetParam().code, GetParam().system, nullptr));
59 } 54 }
60 55
61 INSTANTIATE_TEST_CASE_P( 56 INSTANTIATE_TEST_CASE_P(
62 CurrencyCodes, 57 CurrencyCodes,
63 PaymentsCurrencyValidatorTest, 58 PaymentsCurrencyValidatorTest,
64 testing::Values( 59 testing::Values(
65 // Any string of at most 2048 characters can be a valid currency code 60 // The most common identifiers are three-letter alphabetic codes as defi ned by [ISO4217] (for example, "USD" for US Dollars).
66 TestCase("USD", true), 61 // |system| is a URL that indicates the currency system that the currenc y identifier belongs to. By default,
67 TestCase("US1", true), 62 // the value is urn:iso:std:iso:4217 indicating that currency is defined by [[ISO4217]], however any string of at most 2048
68 TestCase("US", true), 63 // characters is considered valid in other currencySystem. Returns false if currency |code| is too long (greater than 2048).
69 TestCase("USDO", true), 64 CurrencyCodeTestCase("USD", "urn:iso:std:iso:4217", true),
70 TestCase("usd", true), 65 CurrencyCodeTestCase("US1", "http://www.example.com", true),
71 TestCase("ANYSTRING", true), 66 CurrencyCodeTestCase("US1", "urn:iso:std:iso:4217", false),
72 TestCase("", true), 67 CurrencyCodeTestCase("US", "http://www.example.com", true),
73 TestCase(longString2048(), true), 68 CurrencyCodeTestCase("US", "urn:iso:std:iso:4217", false),
74 TestCase(longString2049(), false))); 69 CurrencyCodeTestCase("USDO", "http://www.example.com", true),
70 CurrencyCodeTestCase("USDO", "urn:iso:std:iso:4217", false),
71 CurrencyCodeTestCase("usd", "http://www.example.com", true),
72 CurrencyCodeTestCase("usd", "urn:iso:std:iso:4217", false),
73 CurrencyCodeTestCase("ANYSTRING", "http://www.example.com", true),
74 CurrencyCodeTestCase("ANYSTRING", "urn:iso:std:iso:4217", false),
75 CurrencyCodeTestCase("", "http://www.example.com", true),
76 CurrencyCodeTestCase("", "urn:iso:std:iso:4217", false),
77 CurrencyCodeTestCase(longString2048(), "http://www.example.com", true),
78 CurrencyCodeTestCase(longString2048(), "urn:iso:std:iso:4217", false),
79 CurrencyCodeTestCase(longString2049(),
80 "http://www.example.com",
81 false)));
82
83 struct TestCase {
84 TestCase(const char* input, bool expectedValid)
85 : input(input), expectedValid(expectedValid) {}
86 ~TestCase() {}
87
88 const char* input;
89 bool expectedValid;
90 };
91
92 std::ostream& operator<<(std::ostream& out, const TestCase& testCase) {
93 out << "'" << testCase.input << "' is expected to be "
94 << (testCase.expectedValid ? "valid" : "invalid");
95 return out;
96 }
75 97
76 class PaymentsAmountValidatorTest : public testing::TestWithParam<TestCase> {}; 98 class PaymentsAmountValidatorTest : public testing::TestWithParam<TestCase> {};
77 99
78 TEST_P(PaymentsAmountValidatorTest, IsValidAmountFormat) { 100 TEST_P(PaymentsAmountValidatorTest, IsValidAmountFormat) {
79 String errorMessage; 101 String errorMessage;
80 EXPECT_EQ(GetParam().expectedValid, PaymentsValidators::isValidAmountFormat( 102 EXPECT_EQ(GetParam().expectedValid, PaymentsValidators::isValidAmountFormat(
81 GetParam().input, &errorMessage)) 103 GetParam().input, &errorMessage))
82 << errorMessage; 104 << errorMessage;
83 EXPECT_EQ(GetParam().expectedValid, errorMessage.isEmpty()) << errorMessage; 105 EXPECT_EQ(GetParam().expectedValid, errorMessage.isEmpty()) << errorMessage;
84 106
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 ShippingAddressTestCase("US", "", "", true), 263 ShippingAddressTestCase("US", "", "", true),
242 // Invalid shipping addresses 264 // Invalid shipping addresses
243 ShippingAddressTestCase("", "", "", false), 265 ShippingAddressTestCase("", "", "", false),
244 ShippingAddressTestCase("InvalidCountryCode", "", "", false), 266 ShippingAddressTestCase("InvalidCountryCode", "", "", false),
245 ShippingAddressTestCase("US", "InvalidLanguageCode", "", false), 267 ShippingAddressTestCase("US", "InvalidLanguageCode", "", false),
246 ShippingAddressTestCase("US", "en", "InvalidScriptCode", false), 268 ShippingAddressTestCase("US", "en", "InvalidScriptCode", false),
247 ShippingAddressTestCase("US", "", "Latn", false))); 269 ShippingAddressTestCase("US", "", "Latn", false)));
248 270
249 } // namespace 271 } // namespace
250 } // namespace blink 272 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698