OLD | NEW |
---|---|
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 TestCase { |
please use gerrit instead
2016/10/12 23:35:50
Please move the "TestCase" down to be immediately
pals
2016/10/13 09:30:38
Done.
| |
15 TestCase(const char* input, bool expectedValid) | 15 TestCase(const char* input, bool expectedValid) |
16 : input(input), expectedValid(expectedValid) {} | 16 : input(input), expectedValid(expectedValid) {} |
17 ~TestCase() {} | 17 ~TestCase() {} |
18 | 18 |
19 const char* input; | 19 const char* input; |
20 bool expectedValid; | 20 bool expectedValid; |
21 }; | 21 }; |
22 | 22 |
23 std::ostream& operator<<(std::ostream& out, const TestCase& testCase) { | 23 std::ostream& operator<<(std::ostream& out, const TestCase& testCase) { |
24 out << "'" << testCase.input << "' is expected to be " | 24 out << "'" << testCase.input << "' is expected to be " |
25 << (testCase.expectedValid ? "valid" : "invalid"); | 25 << (testCase.expectedValid ? "valid" : "invalid"); |
26 return out; | 26 return out; |
27 } | 27 } |
28 | 28 |
29 class PaymentsCurrencyValidatorTest : public testing::TestWithParam<TestCase> { | 29 struct CurrencyCodeTestCase { |
30 CurrencyCodeTestCase(const char* code, const char* system, bool expectedValid) | |
31 : code(code), system(system), expectedValid(expectedValid) {} | |
32 ~CurrencyCodeTestCase() {} | |
33 | |
34 const char* code; | |
35 const char* system; | |
36 bool expectedValid; | |
30 }; | 37 }; |
31 | 38 |
39 class PaymentsCurrencyValidatorTest | |
40 : public testing::TestWithParam<CurrencyCodeTestCase> {}; | |
41 | |
32 const char* longString2048() { | 42 const char* longString2048() { |
33 static char longString[2049]; | 43 static char longString[2049]; |
34 for (int i = 0; i < 2048; i++) | 44 for (int i = 0; i < 2048; i++) |
35 longString[i] = 'a'; | 45 longString[i] = 'a'; |
36 longString[2048] = '\0'; | 46 longString[2048] = '\0'; |
37 return longString; | 47 return longString; |
38 } | 48 } |
39 | 49 |
40 const char* longString2049() { | 50 const char* longString2049() { |
41 static char longString[2050]; | 51 static char longString[2050]; |
42 for (int i = 0; i < 2049; i++) | 52 for (int i = 0; i < 2049; i++) |
43 longString[i] = 'a'; | 53 longString[i] = 'a'; |
44 longString[2049] = '\0'; | 54 longString[2049] = '\0'; |
45 return longString; | 55 return longString; |
46 } | 56 } |
47 | 57 |
48 TEST_P(PaymentsCurrencyValidatorTest, IsValidCurrencyCodeFormat) { | 58 TEST_P(PaymentsCurrencyValidatorTest, IsValidCurrencyCodeFormat) { |
49 String errorMessage; | 59 String errorMessage; |
50 EXPECT_EQ(GetParam().expectedValid, | 60 EXPECT_EQ(GetParam().expectedValid, |
51 PaymentsValidators::isValidCurrencyCodeFormat(GetParam().input, | 61 PaymentsValidators::isValidCurrencyCodeFormat( |
52 &errorMessage)) | 62 GetParam().code, GetParam().system, &errorMessage)) |
53 << errorMessage; | 63 << errorMessage; |
54 EXPECT_EQ(GetParam().expectedValid, errorMessage.isEmpty()) << errorMessage; | 64 EXPECT_EQ(GetParam().expectedValid, errorMessage.isEmpty()) << errorMessage; |
55 | 65 |
56 EXPECT_EQ( | 66 EXPECT_EQ(GetParam().expectedValid, |
57 GetParam().expectedValid, | 67 PaymentsValidators::isValidCurrencyCodeFormat( |
58 PaymentsValidators::isValidCurrencyCodeFormat(GetParam().input, nullptr)); | 68 GetParam().code, GetParam().system, nullptr)); |
59 } | 69 } |
60 | 70 |
61 INSTANTIATE_TEST_CASE_P( | 71 INSTANTIATE_TEST_CASE_P( |
62 CurrencyCodes, | 72 CurrencyCodes, |
63 PaymentsCurrencyValidatorTest, | 73 PaymentsCurrencyValidatorTest, |
64 testing::Values( | 74 testing::Values( |
65 // Any string of at most 2048 characters can be a valid currency code | 75 // Any string of at most 2048 characters can be a valid currency code |
66 TestCase("USD", true), | 76 CurrencyCodeTestCase("USD", "urn:iso:std:iso:4217", true), |
67 TestCase("US1", true), | 77 CurrencyCodeTestCase("US1", "http://www.example.com", true), |
please use gerrit instead
2016/10/12 23:35:50
For each of the valid example.com currency systems
pals
2016/10/13 09:30:39
Done.
| |
68 TestCase("US", true), | 78 CurrencyCodeTestCase("US", "http://www.example.com", true), |
69 TestCase("USDO", true), | 79 CurrencyCodeTestCase("USDO", "http://www.example.com", true), |
70 TestCase("usd", true), | 80 CurrencyCodeTestCase("usd", "http://www.example.com", true), |
71 TestCase("ANYSTRING", true), | 81 CurrencyCodeTestCase("ANYSTRING", "http://www.example.com", true), |
72 TestCase("", true), | 82 CurrencyCodeTestCase("", "http://www.example.com", true), |
73 TestCase(longString2048(), true), | 83 CurrencyCodeTestCase(longString2048(), "http://www.example.com", true), |
74 TestCase(longString2049(), false))); | 84 CurrencyCodeTestCase(longString2049(), |
85 "http://www.example.com", | |
86 false))); | |
75 | 87 |
76 class PaymentsAmountValidatorTest : public testing::TestWithParam<TestCase> {}; | 88 class PaymentsAmountValidatorTest : public testing::TestWithParam<TestCase> {}; |
77 | 89 |
78 TEST_P(PaymentsAmountValidatorTest, IsValidAmountFormat) { | 90 TEST_P(PaymentsAmountValidatorTest, IsValidAmountFormat) { |
79 String errorMessage; | 91 String errorMessage; |
80 EXPECT_EQ(GetParam().expectedValid, PaymentsValidators::isValidAmountFormat( | 92 EXPECT_EQ(GetParam().expectedValid, PaymentsValidators::isValidAmountFormat( |
81 GetParam().input, &errorMessage)) | 93 GetParam().input, &errorMessage)) |
82 << errorMessage; | 94 << errorMessage; |
83 EXPECT_EQ(GetParam().expectedValid, errorMessage.isEmpty()) << errorMessage; | 95 EXPECT_EQ(GetParam().expectedValid, errorMessage.isEmpty()) << errorMessage; |
84 | 96 |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
241 ShippingAddressTestCase("US", "", "", true), | 253 ShippingAddressTestCase("US", "", "", true), |
242 // Invalid shipping addresses | 254 // Invalid shipping addresses |
243 ShippingAddressTestCase("", "", "", false), | 255 ShippingAddressTestCase("", "", "", false), |
244 ShippingAddressTestCase("InvalidCountryCode", "", "", false), | 256 ShippingAddressTestCase("InvalidCountryCode", "", "", false), |
245 ShippingAddressTestCase("US", "InvalidLanguageCode", "", false), | 257 ShippingAddressTestCase("US", "InvalidLanguageCode", "", false), |
246 ShippingAddressTestCase("US", "en", "InvalidScriptCode", false), | 258 ShippingAddressTestCase("US", "en", "InvalidScriptCode", false), |
247 ShippingAddressTestCase("US", "", "Latn", false))); | 259 ShippingAddressTestCase("US", "", "Latn", false))); |
248 | 260 |
249 } // namespace | 261 } // namespace |
250 } // namespace blink | 262 } // namespace blink |
OLD | NEW |