TestNumberParser.cpp (1195B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 #include "gtest/gtest.h" 5 6 #include "mozilla/intl/NumberParser.h" 7 8 namespace mozilla::intl { 9 10 TEST(IntlNumberParser, Basic) 11 { 12 // Try with an English locale 13 UniquePtr<NumberParser> np = NumberParser::TryCreate("en-US", true).unwrap(); 14 auto result = np->ParseDouble(MakeStringSpan(u"1,234.56")); 15 ASSERT_TRUE(result.isOk()); 16 ASSERT_EQ(result.unwrap().first, 1234.56); 17 ASSERT_EQ(result.unwrap().second, 8); 18 19 // Disable grouping, parsing will stop at the first comma 20 np = NumberParser::TryCreate("en-US", false).unwrap(); 21 result = np->ParseDouble(MakeStringSpan(u"1,234.56")); 22 ASSERT_TRUE(result.isOk()); 23 ASSERT_EQ(result.unwrap().first, 1); 24 ASSERT_EQ(result.unwrap().second, 1); 25 26 // Try with a Spanish locale 27 np = NumberParser::TryCreate("es-CR", true).unwrap(); 28 result = np->ParseDouble(MakeStringSpan(u"1234,56")); 29 ASSERT_TRUE(result.isOk()); 30 ASSERT_EQ(result.unwrap().first, 1234.56); 31 ASSERT_EQ(result.unwrap().second, 7); 32 } 33 34 } // namespace mozilla::intl