@@ -1399,187 +1399,237 @@ TEST(Value, GetBadMap) {
13991399 EXPECT_THAT (v.get <M>(), Not (IsOk ()));
14001400}
14011401
1402- TEST (Value, OutputStream) {
1403- auto const normal = [](std::ostream& os) -> std::ostream& { return os; };
1404- auto const hex = [](std::ostream& os) -> std::ostream& {
1405- return os << std::hex;
1406- };
1407- auto const boolalpha = [](std::ostream& os) -> std::ostream& {
1402+ struct OsModes {
1403+ static std::ostream& normal (std::ostream& os) { return os; };
1404+ static std::ostream& hex (std::ostream& os) { return os << std::hex; };
1405+ static std::ostream& boolalpha (std::ostream& os) {
14081406 return os << std::boolalpha;
14091407 };
1410- auto const float4 = []( std::ostream& os) -> std::ostream& {
1408+ static std::ostream& float4 ( std::ostream& os) {
14111409 return os << std::showpoint << std::setprecision (4 );
14121410 };
1413- auto const alphahex = []( std::ostream& os) -> std::ostream& {
1411+ static std::ostream& alphahex ( std::ostream& os) {
14141412 return os << std::boolalpha << std::hex;
14151413 };
1414+ };
1415+
1416+ TEST (Value, MapsWithValuesOutputStream) {
1417+ struct TestCase {
1418+ Value value;
1419+ std::vector<std::string> expected;
1420+ std::function<std::ostream&(std::ostream&)> manip;
1421+ };
1422+
1423+ std::vector<TestCase> test_case = {
1424+ {Value (std::unordered_map<std::string, bool >{
1425+ {{" bar" , false }, {" foo" , true }}}),
1426+ {R"( {"foo" : 1})" , R"( {"bar" : 0})" },
1427+ OsModes::normal},
1428+ {Value (std::unordered_map<std::string, bool >{
1429+ {{" bar" , false }, {" foo" , true }}}),
1430+ {R"( {"foo" : true})" , R"( {"bar" : false})" },
1431+ OsModes::boolalpha},
1432+ {Value (std::unordered_map<std::string, std::int64_t >{
1433+ {{" bar" , 12 }, {" foo" , 34 }}}),
1434+ {R"( {"foo" : 34})" , R"( {"bar" : 12})" },
1435+ OsModes::normal},
1436+ {Value (std::unordered_map<std::int64_t , std::int64_t >{
1437+ {{10 , 11 }, {12 , 13 }}}),
1438+ {R"( {c : d})" , R"( {a : b})" },
1439+ OsModes::hex},
1440+ {Value (std::unordered_map<std::string, double >{
1441+ {{" bar" , 12.0 }, {" foo" , 34.0 }}}),
1442+ {R"( {"foo" : 34})" , R"( {"bar" : 12})" },
1443+ OsModes::normal},
1444+ {Value (std::unordered_map<std::string, double >{
1445+ {{" bar" , 2.0 }, {" foo" , 3.0 }}}),
1446+ {R"( {"foo" : 3.000})" , R"( {"bar" : 2.000})" },
1447+ OsModes::float4},
1448+ {Value (std::unordered_map<std::string, float >{
1449+ {{" bar" , 12 .0F }, {" foo" , 34 .0F }}}),
1450+ {R"( {"foo" : 34})" , R"( {"bar" : 12})" },
1451+ OsModes::normal},
1452+ {Value (std::unordered_map<std::string, float >{
1453+ {{" bar" , 2 .0F }, {" foo" , 3 .0F }}}),
1454+ {R"( {"foo" : 3.000})" , R"( {"bar" : 2.000})" },
1455+ OsModes::float4},
1456+ {Value (std::unordered_map<std::string, std::string>{
1457+ {{" bar" , std::string (" a" )}, {" foo" , std::string (" b" )}}}),
1458+ {R"( {"foo" : "b"})" , R"( {"bar" : "a"})" },
1459+ OsModes::normal},
1460+ {Value (std::unordered_map<Bytes, Bytes>{
1461+ {Bytes (std::string (" bar" )), Bytes (std::string (" foo" ))}}),
1462+ {R"( {B"bar" : B"foo"})" },
1463+ OsModes::normal},
1464+ {Value (std::unordered_map<Bytes, absl::CivilDay>{
1465+ {Bytes (std::string (" bar" )), absl::CivilDay ()}}),
1466+ {R"( {B"bar" : 1970-01-01})" },
1467+ OsModes::normal},
1468+ {Value (std::unordered_map<std::string, Timestamp>{{" bar" , Timestamp ()}}),
1469+ {R"( {"bar" : 1970-01-01T00:00:00Z})" },
1470+ OsModes::normal},
1471+
1472+ // Tests maps with null elements
1473+ {Value (std::unordered_map<std::string, absl::optional<double >>{
1474+ {" foo" , 2.0 }, {" bar" , absl::optional<double >()}}),
1475+ {R"( {"bar" : NULL})" , R"( {"foo" : 2})" },
1476+ OsModes::normal},
1477+
1478+ };
14161479
1480+ for (auto const & tc : test_case) {
1481+ std::stringstream ss;
1482+ tc.manip (ss) << tc.value ;
1483+ // 2 outer brackets and 2(n - 1) commas and spaces.
1484+ size_t expected_length = 2 + (2 * (tc.expected .size () - 1 ));
1485+ for (auto const & expected_kv : tc.expected ) {
1486+ EXPECT_TRUE (ss.str ().find (expected_kv) != std::string::npos);
1487+ expected_length += expected_kv.size ();
1488+ }
1489+ EXPECT_EQ (ss.str ().size (), expected_length);
1490+ }
1491+ }
1492+
1493+ TEST (Value, OutputStream) {
14171494 struct TestCase {
14181495 Value value;
14191496 std::string expected;
14201497 std::function<std::ostream&(std::ostream&)> manip;
14211498 };
14221499
14231500 std::vector<TestCase> test_case = {
1424- {Value (false ), " 0" , normal},
1425- {Value (true ), " 1" , normal},
1426- {Value (false ), " false" , boolalpha},
1427- {Value (true ), " true" , boolalpha},
1428- {Value (42 ), " 42" , normal},
1429- {Value (42 ), " 2a" , hex},
1430- {Value (42.0 ), " 42" , normal},
1431- {Value (42.0 ), " 42.00" , float4},
1432- {Value (42 .0F ), " 42" , normal},
1433- {Value (42 .0F ), " 42.00" , float4},
1434- {Value (" " ), " " , normal},
1435- {Value (" foo" ), " foo" , normal},
1436- {Value (" NULL" ), " NULL" , normal},
1437- {Value (Bytes (std::string (" DEADBEEF" ))), R"( B"DEADBEEF")" , normal},
1438- {Value (Timestamp ()), " 1970-01-01T00:00:00Z" , normal},
1439- {Value (absl::CivilDay ()), " 1970-01-01" , normal},
1501+ {Value (false ), " 0" , OsModes::normal},
1502+ {Value (true ), " 1" , OsModes::normal},
1503+ {Value (false ), " false" , OsModes::boolalpha},
1504+ {Value (true ), " true" , OsModes::boolalpha},
1505+ {Value (42 ), " 42" , OsModes::normal},
1506+ {Value (42 ), " 2a" , OsModes::hex},
1507+ {Value (42.0 ), " 42" , OsModes::normal},
1508+ {Value (42.0 ), " 42.00" , OsModes::float4},
1509+ {Value (42 .0F ), " 42" , OsModes::normal},
1510+ {Value (42 .0F ), " 42.00" , OsModes::float4},
1511+ {Value (" " ), " " , OsModes::normal},
1512+ {Value (" foo" ), " foo" , OsModes::normal},
1513+ {Value (" NULL" ), " NULL" , OsModes::normal},
1514+ {Value (Bytes (std::string (" DEADBEEF" ))), R"( B"DEADBEEF")" ,
1515+ OsModes::normal},
1516+ {Value (Timestamp ()), " 1970-01-01T00:00:00Z" , OsModes::normal},
1517+ {Value (absl::CivilDay ()), " 1970-01-01" , OsModes::normal},
14401518
14411519 // Tests string quoting: No quotes for scalars; quotes within aggregates
1442- {Value (" " ), " " , normal},
1443- {Value (" foo" ), " foo" , normal},
1444- {Value (std::vector<std::string>{" a" , " b" }), R"( ["a", "b"])" , normal},
1520+ {Value (" " ), " " , OsModes::normal},
1521+ {Value (" foo" ), " foo" , OsModes::normal},
1522+ {Value (std::vector<std::string>{" a" , " b" }), R"( ["a", "b"])" ,
1523+ OsModes::normal},
14451524 {Value (std::vector<std::string>{" \" a\" " , " \" b\" " }),
1446- R"( ["\"a\"", "\"b\""])" , normal},
1525+ R"( ["\"a\"", "\"b\""])" , OsModes:: normal},
14471526 {Value (std::unordered_map<std::string, std::string>{{" \" a\" " , " \" b\" " }}),
1448- R"( {{"\"a\"" : "\"b\""}})" , normal},
1527+ R"( {{"\"a\"" : "\"b\""}})" , OsModes:: normal},
14491528
14501529 // Tests null values
1451- {MakeNullValue<bool >(), " NULL" , normal},
1452- {MakeNullValue<std::int64_t >(), " NULL" , normal},
1453- {MakeNullValue<double >(), " NULL" , normal},
1454- {MakeNullValue<float >(), " NULL" , normal},
1455- {MakeNullValue<std::string>(), " NULL" , normal},
1456- {MakeNullValue<Bytes>(), " NULL" , normal},
1457- {MakeNullValue<Timestamp>(), " NULL" , normal},
1458- {MakeNullValue<absl::CivilDay>(), " NULL" , normal},
1530+ {MakeNullValue<bool >(), " NULL" , OsModes:: normal},
1531+ {MakeNullValue<std::int64_t >(), " NULL" , OsModes:: normal},
1532+ {MakeNullValue<double >(), " NULL" , OsModes:: normal},
1533+ {MakeNullValue<float >(), " NULL" , OsModes:: normal},
1534+ {MakeNullValue<std::string>(), " NULL" , OsModes:: normal},
1535+ {MakeNullValue<Bytes>(), " NULL" , OsModes:: normal},
1536+ {MakeNullValue<Timestamp>(), " NULL" , OsModes:: normal},
1537+ {MakeNullValue<absl::CivilDay>(), " NULL" , OsModes:: normal},
14591538
14601539 // Tests arrays
1461- {Value (std::vector<bool >{false , true }), " [0, 1]" , normal},
1462- {Value (std::vector<bool >{false , true }), " [false, true]" , boolalpha},
1463- {Value (std::vector<std::int64_t >{10 , 11 }), " [10, 11]" , normal},
1464- {Value (std::vector<std::int64_t >{10 , 11 }), " [a, b]" , hex},
1465- {Value (std::vector<double >{1.0 , 2.0 }), " [1, 2]" , normal},
1466- {Value (std::vector<double >{1.0 , 2.0 }), " [1.000, 2.000]" , float4},
1467- {Value (std::vector<float >{1 .0F , 2 .0F }), " [1, 2]" , normal},
1468- {Value (std::vector<float >{1 .0F , 2 .0F }), " [1.000, 2.000]" , float4},
1469- {Value (std::vector<std::string>{" a" , " b" }), R"( ["a", "b"])" , normal},
1470- {Value (std::vector<Bytes>{2 }), R"( [B"", B""])" , normal},
1540+ {Value (std::vector<bool >{false , true }), " [0, 1]" , OsModes::normal},
1541+ {Value (std::vector<bool >{false , true }), " [false, true]" ,
1542+ OsModes::boolalpha},
1543+ {Value (std::vector<std::int64_t >{10 , 11 }), " [10, 11]" , OsModes::normal},
1544+ {Value (std::vector<std::int64_t >{10 , 11 }), " [a, b]" , OsModes::hex},
1545+ {Value (std::vector<double >{1.0 , 2.0 }), " [1, 2]" , OsModes::normal},
1546+ {Value (std::vector<double >{1.0 , 2.0 }), " [1.000, 2.000]" , OsModes::float4},
1547+ {Value (std::vector<float >{1 .0F , 2 .0F }), " [1, 2]" , OsModes::normal},
1548+ {Value (std::vector<float >{1 .0F , 2 .0F }), " [1.000, 2.000]" ,
1549+ OsModes::float4},
1550+ {Value (std::vector<std::string>{" a" , " b" }), R"( ["a", "b"])" ,
1551+ OsModes::normal},
1552+ {Value (std::vector<Bytes>{2 }), R"( [B"", B""])" , OsModes::normal},
14711553 {Value (std::vector<absl::CivilDay>{2 }), " [1970-01-01, 1970-01-01]" ,
1472- normal},
1473- {Value (std::vector<Timestamp>{1 }), " [1970-01-01T00:00:00Z]" , normal},
1554+ OsModes::normal},
1555+ {Value (std::vector<Timestamp>{1 }), " [1970-01-01T00:00:00Z]" ,
1556+ OsModes::normal},
14741557
14751558 // Tests arrays with null elements
14761559 {Value (std::vector<absl::optional<double >>{1 , {}, 2 }), " [1, NULL, 2]" ,
1477- normal},
1560+ OsModes:: normal},
14781561
14791562 // Tests null arrays
1480- {MakeNullValue<std::vector<bool >>(), " NULL" , normal},
1481- {MakeNullValue<std::vector<std::int64_t >>(), " NULL" , normal},
1482- {MakeNullValue<std::vector<double >>(), " NULL" , normal},
1483- {MakeNullValue<std::vector<float >>(), " NULL" , normal},
1484- {MakeNullValue<std::vector<std::string>>(), " NULL" , normal},
1485- {MakeNullValue<std::vector<Bytes>>(), " NULL" , normal},
1486- {MakeNullValue<std::vector<absl::CivilDay>>(), " NULL" , normal},
1487- {MakeNullValue<std::vector<Timestamp>>(), " NULL" , normal},
1563+ {MakeNullValue<std::vector<bool >>(), " NULL" , OsModes:: normal},
1564+ {MakeNullValue<std::vector<std::int64_t >>(), " NULL" , OsModes:: normal},
1565+ {MakeNullValue<std::vector<double >>(), " NULL" , OsModes:: normal},
1566+ {MakeNullValue<std::vector<float >>(), " NULL" , OsModes:: normal},
1567+ {MakeNullValue<std::vector<std::string>>(), " NULL" , OsModes:: normal},
1568+ {MakeNullValue<std::vector<Bytes>>(), " NULL" , OsModes:: normal},
1569+ {MakeNullValue<std::vector<absl::CivilDay>>(), " NULL" , OsModes:: normal},
1570+ {MakeNullValue<std::vector<Timestamp>>(), " NULL" , OsModes:: normal},
14881571
14891572 // Tests structs
1490- {Value (std::make_tuple (true , 123 )), " (1, 123)" , normal},
1491- {Value (std::make_tuple (true , 123 )), " (true, 7b)" , alphahex},
1573+ {Value (std::make_tuple (true , 123 )), " (1, 123)" , OsModes:: normal},
1574+ {Value (std::make_tuple (true , 123 )), " (true, 7b)" , OsModes:: alphahex},
14921575 {Value (std::make_tuple (std::make_pair (" A" , true ),
14931576 std::make_pair (" B" , 123 ))),
1494- R"( ("A": 1, "B": 123))" , normal},
1577+ R"( ("A": 1, "B": 123))" , OsModes:: normal},
14951578 {Value (std::make_tuple (std::make_pair (" A" , true ),
14961579 std::make_pair (" B" , 123 ))),
1497- R"( ("A": true, "B": 7b))" , alphahex},
1580+ R"( ("A": true, "B": 7b))" , OsModes:: alphahex},
14981581 {Value (std::make_tuple (
14991582 std::vector<std::int64_t >{10 , 11 , 12 },
15001583 std::make_pair (" B" , std::vector<std::int64_t >{13 , 14 , 15 }))),
1501- R"( ([10, 11, 12], "B": [13, 14, 15]))" , normal},
1584+ R"( ([10, 11, 12], "B": [13, 14, 15]))" , OsModes:: normal},
15021585 {Value (std::make_tuple (
15031586 std::vector<std::int64_t >{10 , 11 , 12 },
15041587 std::make_pair (" B" , std::vector<std::int64_t >{13 , 14 , 15 }))),
1505- R"( ([a, b, c], "B": [d, e, f]))" , hex},
1588+ R"( ([a, b, c], "B": [d, e, f]))" , OsModes:: hex},
15061589 {Value (std::make_tuple (std::make_tuple (
15071590 std::make_tuple (std::vector<std::int64_t >{10 , 11 , 12 })))),
1508- " ((([10, 11, 12])))" , normal},
1591+ " ((([10, 11, 12])))" , OsModes:: normal},
15091592 {Value (std::make_tuple (std::make_tuple (
15101593 std::make_tuple (std::vector<std::int64_t >{10 , 11 , 12 })))),
1511- " ((([a, b, c])))" , hex},
1594+ " ((([a, b, c])))" , OsModes:: hex},
15121595
15131596 // Tests struct with null members
1514- {Value (std::make_tuple (absl::optional<bool >{})), " (NULL)" , normal},
1597+ {Value (std::make_tuple (absl::optional<bool >{})), " (NULL)" ,
1598+ OsModes::normal},
15151599 {Value (std::make_tuple (absl::optional<bool >{}, 123 )), " (NULL, 123)" ,
1516- normal},
1517- {Value (std::make_tuple (absl::optional<bool >{}, 123 )), " (NULL, 7b)" , hex},
1600+ OsModes::normal},
1601+ {Value (std::make_tuple (absl::optional<bool >{}, 123 )), " (NULL, 7b)" ,
1602+ OsModes::hex},
15181603 {Value (std::make_tuple (absl::optional<bool >{},
15191604 absl::optional<std::int64_t >{})),
1520- " (NULL, NULL)" , normal},
1605+ " (NULL, NULL)" , OsModes:: normal},
15211606
15221607 // Tests null structs
1523- {MakeNullValue<std::tuple<bool >>(), " NULL" , normal},
1524- {MakeNullValue<std::tuple<bool , std::int64_t >>(), " NULL" , normal},
1525- {MakeNullValue<std::tuple<float , std::string>>(), " NULL" , normal},
1526- {MakeNullValue<std::tuple<double , Bytes, Timestamp>>(), " NULL" , normal},
1527-
1528- // Tests maps
1529- {Value (std::unordered_map<std::string, bool >{
1530- {{" bar" , false }, {" foo" , true }}}),
1531- R"( {{"foo" : 1}, {"bar" : 0}})" , normal},
1532- {Value (std::unordered_map<std::string, bool >{
1533- {{" bar" , false }, {" foo" , true }}}),
1534- R"( {{"foo" : true}, {"bar" : false}})" , boolalpha},
1535- {Value (std::unordered_map<std::string, std::int64_t >{
1536- {{" bar" , 12 }, {" foo" , 34 }}}),
1537- R"( {{"foo" : 34}, {"bar" : 12}})" , normal},
1538- {Value (std::unordered_map<std::int64_t , std::int64_t >{
1539- {{10 , 11 }, {12 , 13 }}}),
1540- R"( {{c : d}, {a : b}})" , hex},
1541- {Value (std::unordered_map<std::string, double >{
1542- {{" bar" , 12.0 }, {" foo" , 34.0 }}}),
1543- R"( {{"foo" : 34}, {"bar" : 12}})" , normal},
1544- {Value (std::unordered_map<std::string, double >{
1545- {{" bar" , 2.0 }, {" foo" , 3.0 }}}),
1546- R"( {{"foo" : 3.000}, {"bar" : 2.000}})" , float4},
1547- {Value (std::unordered_map<std::string, float >{
1548- {{" bar" , 12 .0F }, {" foo" , 34 .0F }}}),
1549- R"( {{"foo" : 34}, {"bar" : 12}})" , normal},
1550- {Value (std::unordered_map<std::string, float >{
1551- {{" bar" , 2 .0F }, {" foo" , 3 .0F }}}),
1552- R"( {{"foo" : 3.000}, {"bar" : 2.000}})" , float4},
1553- {Value (std::unordered_map<std::string, std::string>{
1554- {{" bar" , std::string (" a" )}, {" foo" , std::string (" b" )}}}),
1555- R"( {{"foo" : "b"}, {"bar" : "a"}})" , normal},
1556- {Value (std::unordered_map<Bytes, Bytes>{
1557- {Bytes (std::string (" bar" )), Bytes (std::string (" foo" ))}}),
1558- R"( {{B"bar" : B"foo"}})" , normal},
1559- {Value (std::unordered_map<Bytes, absl::CivilDay>{
1560- {Bytes (std::string (" bar" )), absl::CivilDay ()}}),
1561- R"( {{B"bar" : 1970-01-01}})" , normal},
1562- {Value (std::unordered_map<std::string, Timestamp>{{" bar" , Timestamp ()}}),
1563- R"( {{"bar" : 1970-01-01T00:00:00Z}})" , normal},
1564-
1565- // Tests maps with null elements
1566- {Value (std::unordered_map<std::string, absl::optional<double >>{
1567- {" foo" , 2.0 }, {" bar" , absl::optional<double >()}}),
1568- R"( {{"bar" : NULL}, {"foo" : 2}})" , normal},
1569-
1570- // Tests null arrays
1571- {MakeNullValue<std::unordered_map<std::int64_t , bool >>(), " NULL" , normal},
1608+ {MakeNullValue<std::tuple<bool >>(), " NULL" , OsModes::normal},
1609+ {MakeNullValue<std::tuple<bool , std::int64_t >>(), " NULL" ,
1610+ OsModes::normal},
1611+ {MakeNullValue<std::tuple<float , std::string>>(), " NULL" ,
1612+ OsModes::normal},
1613+ {MakeNullValue<std::tuple<double , Bytes, Timestamp>>(), " NULL" ,
1614+ OsModes::normal},
1615+
1616+ // Tests null maps
1617+ {MakeNullValue<std::unordered_map<std::int64_t , bool >>(), " NULL" ,
1618+ OsModes::normal},
15721619 {MakeNullValue<std::unordered_map<std::int64_t , std::int64_t >>(), " NULL" ,
1573- normal},
1620+ OsModes:: normal},
15741621 {MakeNullValue<std::unordered_map<std::int64_t , double >>(), " NULL" ,
1575- normal},
1622+ OsModes:: normal},
15761623 {MakeNullValue<std::unordered_map<std::int64_t , float >>(), " NULL" ,
1577- normal},
1578- {MakeNullValue<std::unordered_map<Bytes, std::string>>(), " NULL" , normal},
1579- {MakeNullValue<std::unordered_map<Bytes, Bytes>>(), " NULL" , normal},
1624+ OsModes::normal},
1625+ {MakeNullValue<std::unordered_map<Bytes, std::string>>(), " NULL" ,
1626+ OsModes::normal},
1627+ {MakeNullValue<std::unordered_map<Bytes, Bytes>>(), " NULL" ,
1628+ OsModes::normal},
15801629 {MakeNullValue<std::unordered_map<Bytes, absl::CivilDay>>(), " NULL" ,
1581- normal},
1582- {MakeNullValue<std::unordered_map<Bytes, Timestamp>>(), " NULL" , normal}};
1630+ OsModes::normal},
1631+ {MakeNullValue<std::unordered_map<Bytes, Timestamp>>(), " NULL" ,
1632+ OsModes::normal}};
15831633
15841634 for (auto const & tc : test_case) {
15851635 std::stringstream ss;
0 commit comments