1717 * under the License.
1818 */
1919
20- #include " iceberg/util/formatter.h"
21-
2220#include < format>
2321#include < map>
2422#include < memory>
2523#include < string>
2624#include < unordered_map>
2725#include < vector>
2826
27+ #include < gmock/gmock-matchers.h>
28+ #include < gmock/gmock.h>
2929#include < gtest/gtest.h>
3030
3131#include " iceberg/statistics_file.h"
32+ #include " iceberg/util/formatter_internal.h"
3233
3334namespace iceberg {
3435
@@ -59,9 +60,9 @@ TEST(FormatterTest, UnorderedMapFormat) {
5960 std::unordered_map<std::string, double > scores = {
6061 {" Alice" , 95.5 }, {" Bob" , 87.0 }, {" Charlie" , 92.3 }};
6162 std::string str = std::format (" {}" , scores);
62- EXPECT_TRUE (str. find (" Alice: 95.5" ) != std::string::npos );
63- EXPECT_TRUE (str. find (" Bob: 87" ) != std::string::npos );
64- EXPECT_TRUE (str. find (" Charlie: 92.3" ) != std::string::npos );
63+ EXPECT_THAT (str, :: testing::HasSubstr (" Alice: 95.5" ));
64+ EXPECT_THAT (str, :: testing::HasSubstr (" Bob: 87" ));
65+ EXPECT_THAT (str, :: testing::HasSubstr (" Charlie: 92.3" ));
6566}
6667
6768TEST (FormatterTest, NestedContainersFormat) {
@@ -73,10 +74,10 @@ TEST(FormatterTest, NestedContainersFormat) {
7374 std::map<std::string, std::vector<int >> nested_map = {
7475 {" primes" , {2 , 3 , 5 , 7 , 11 }}, {" fibonacci" , {1 , 1 , 2 , 3 , 5 , 8 , 13 }}};
7576 std::string result = std::format (" {}" , nested_map);
76- EXPECT_TRUE (result. find ( " primes " ) != std::string::npos );
77- EXPECT_TRUE (result. find ( " fibonacci " ) != std::string::npos );
78- EXPECT_TRUE (result. find (" [2, 3, 5, 7, 11]" ) != std::string::npos );
79- EXPECT_TRUE (result. find (" [1, 1, 2, 3, 5, 8, 13]" ) != std::string::npos );
77+ EXPECT_THAT (result, :: testing::HasSubstr ( " primes " ) );
78+ EXPECT_THAT (result, :: testing::HasSubstr ( " fibonacci " ) );
79+ EXPECT_THAT (result, :: testing::HasSubstr (" [2, 3, 5, 7, 11]" ));
80+ EXPECT_THAT (result, :: testing::HasSubstr (" [1, 1, 2, 3, 5, 8, 13]" ));
8081}
8182
8283TEST (FormatterTest, EdgeCasesFormat) {
@@ -91,42 +92,41 @@ TEST(FormatterTest, EdgeCasesFormat) {
9192}
9293
9394TEST (FormatterTest, SmartPointerFormat) {
94- std::vector<std::shared_ptr<int >> int_ptrs;
95- int_ptrs.push_back (std::make_shared<int >(42 ));
96- int_ptrs.push_back (std::make_shared<int >(123 ));
97- int_ptrs.push_back (nullptr );
95+ std::vector<std::shared_ptr<int >> int_ptrs = {
96+ std::make_shared<int >(42 ),
97+ std::make_shared<int >(123 ),
98+ nullptr ,
99+ };
98100 EXPECT_EQ (" [42, 123, null]" , std::format (" {}" , int_ptrs));
99101
100- std::vector<std::shared_ptr<std::string>> str_ptrs;
101- str_ptrs.push_back (std::make_shared<std::string>(" hello" ));
102- str_ptrs.push_back (std::make_shared<std::string>(" world" ));
103- str_ptrs.push_back (nullptr );
102+ std::vector<std::shared_ptr<std::string>> str_ptrs = {
103+ std::make_shared<std::string>(" hello" ),
104+ std::make_shared<std::string>(" world" ),
105+ nullptr ,
106+ };
104107 EXPECT_EQ (" [hello, world, null]" , std::format (" {}" , str_ptrs));
105108
106- std::map<std::string, std::shared_ptr<int >> map_with_ptr_values;
107- map_with_ptr_values[" one" ] = std::make_shared<int >(1 );
108- map_with_ptr_values[" two" ] = std::make_shared<int >(2 );
109- map_with_ptr_values[" null" ] = nullptr ;
109+ std::map<std::string, std::shared_ptr<int >> map_with_ptr_values = {
110+ {" one" , std::make_shared<int >(1 )},
111+ {" two" , std::make_shared<int >(2 )},
112+ {" null" , nullptr },
113+ };
110114 EXPECT_EQ (" {null: null, one: 1, two: 2}" , std::format (" {}" , map_with_ptr_values));
111115
112- std::unordered_map<std::string, std::shared_ptr<double >> scores;
113- scores[" Alice" ] = std::make_shared<double >(95.5 );
114- scores[" Bob" ] = std::make_shared<double >(87.0 );
115- scores[" Charlie" ] = nullptr ;
116+ std::unordered_map<std::string, std::shared_ptr<double >> scores = {
117+ {" Alice" , std::make_shared<double >(95.5 )},
118+ {" Bob" , std::make_shared<double >(87.0 )},
119+ {" Charlie" , nullptr },
120+ };
116121 std::string str = std::format (" {}" , scores);
117- EXPECT_TRUE (str.find (" Alice: 95.5" ) != std::string::npos);
118- EXPECT_TRUE (str.find (" Bob: 87" ) != std::string::npos);
119- EXPECT_TRUE (str.find (" Charlie: null" ) != std::string::npos);
120-
121- std::vector<std::map<std::string, std::shared_ptr<int >>> nested;
122- std::map<std::string, std::shared_ptr<int >> map1;
123- map1[" a" ] = std::make_shared<int >(1 );
124- map1[" b" ] = std::make_shared<int >(2 );
125- std::map<std::string, std::shared_ptr<int >> map2;
126- map2[" c" ] = std::make_shared<int >(3 );
127- map2[" d" ] = nullptr ;
128- nested.push_back (std::move (map1));
129- nested.push_back (std::move (map2));
122+ EXPECT_THAT (str, ::testing::HasSubstr (" Alice: 95.5" ));
123+ EXPECT_THAT (str, ::testing::HasSubstr (" Bob: 87" ));
124+ EXPECT_THAT (str, ::testing::HasSubstr (" Charlie: null" ));
125+
126+ std::vector<std::map<std::string, std::shared_ptr<int >>> nested = {
127+ {{" a" , std::make_shared<int >(1 )}, {" b" , std::make_shared<int >(2 )}},
128+ {{" c" , std::make_shared<int >(3 )}, {" d" , nullptr }},
129+ };
130130 EXPECT_EQ (" [{a: 1, b: 2}, {c: 3, d: null}]" , std::format (" {}" , nested));
131131}
132132
0 commit comments