Skip to content

Commit e2f440c

Browse files
ryanmeiera-maurice
authored andcommitted
Enable Storage OSS Tests (Desktop)
Moved test files to tests/ folder and added CMakeLists.txt file for tests. Also added json_util.h/cc to testing/cppsdk to replace the EqualsJson functionality that was being used from the internal version of jsoncpp (in third_party) to avoid having that as a dependency for open source. PiperOrigin-RevId: 258970591
1 parent 6104dc6 commit e2f440c

File tree

4 files changed

+198
-1
lines changed

4 files changed

+198
-1
lines changed

storage/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,8 @@ if(IOS)
149149
# Add a dependency to downloading the headers onto admob.
150150
add_dependencies(firebase_storage ${pod_target_name})
151151
endif()
152+
153+
if(FIREBASE_CPP_BUILD_TESTS)
154+
# Add the tests subdirectory
155+
add_subdirectory(tests)
156+
endif()

testing/CMakeLists.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,17 @@ else()
115115
set(util_SRCS "")
116116
endif()
117117

118+
set(json_util_SRCS
119+
json_util.h
120+
json_util.cc)
121+
118122
add_library(firebase_testing STATIC
119123
${config_SRCS}
124+
${json_util_SRCS}
120125
${reporter_SRCS}
121126
${ticker_SRCS}
122-
${util_SRCS})
127+
${util_SRCS}
128+
)
123129

124130
target_include_directories(firebase_testing
125131
PUBLIC

testing/json_util.cc

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
#include "testing/json_util.h"
2+
3+
#include <ostream>
4+
#include <string>
5+
6+
#include "flatbuffers/flatbuffers.h"
7+
#include "flatbuffers/flexbuffers.h"
8+
#include "flatbuffers/idl.h"
9+
#include "flatbuffers/util.h"
10+
11+
namespace firebase {
12+
namespace testing {
13+
namespace cppsdk {
14+
namespace internal {
15+
16+
EqualsJsonMatcher::EqualsJsonMatcher(const std::string& expected_json)
17+
: expected_json_(expected_json) {}
18+
19+
bool EqualsJsonMatcher::MatchAndExplain(
20+
const std::string& actual, ::testing::MatchResultListener* listener) const {
21+
flatbuffers::Parser parser_actual;
22+
flatbuffers::Parser parser_expected;
23+
24+
flexbuffers::Builder builder_actual;
25+
flexbuffers::Builder builder_expected;
26+
27+
if (actual.empty() || !parser_actual.ParseFlexBuffer(actual.c_str(), nullptr,
28+
&builder_actual)) {
29+
*listener << "Unable to parse actual value (" << parser_actual.error_
30+
<< ").\n";
31+
return false;
32+
}
33+
if (expected_json_.empty() ||
34+
!parser_expected.ParseFlexBuffer(expected_json_.c_str(), nullptr,
35+
&builder_expected)) {
36+
*listener << "Unable to parse expected value (" << parser_expected.error_
37+
<< ").\n";
38+
return false;
39+
}
40+
41+
std::vector<uint8_t> buffer_actual = builder_actual.GetBuffer();
42+
std::vector<uint8_t> buffer_expected = builder_expected.GetBuffer();
43+
44+
auto root_actual = flexbuffers::GetRoot(buffer_actual);
45+
auto root_expected = flexbuffers::GetRoot(buffer_expected);
46+
return CompareFlexbufferReference(root_actual, root_expected, "root",
47+
listener);
48+
}
49+
50+
void EqualsJsonMatcher::DescribeTo(std::ostream* os) const {
51+
*os << "equals JSON: \n" << expected_json_;
52+
}
53+
54+
void EqualsJsonMatcher::DescribeNegationTo(std::ostream* os) const {
55+
*os << "doesn't equal JSON: \n" << expected_json_;
56+
}
57+
58+
bool EqualsJsonMatcher::CompareFlexbufferReference(
59+
flexbuffers::Reference reference_actual,
60+
flexbuffers::Reference reference_expected, std::string key_name,
61+
::testing::MatchResultListener* listener) const {
62+
63+
auto type_actual = reference_actual.GetType();
64+
auto type_expected = reference_expected.GetType();
65+
if (type_actual != type_expected) {
66+
*listener << "Type Mismatch (" << key_name << ")\n";
67+
return false;
68+
} else if (type_actual == flexbuffers::FBT_MAP) {
69+
flexbuffers::Map map_actual = reference_actual.AsMap();
70+
flexbuffers::Map map_expected = reference_expected.AsMap();
71+
72+
auto keys_actual = map_actual.Keys();
73+
auto keys_expected = map_expected.Keys();
74+
if (keys_actual.size() != keys_expected.size()) {
75+
*listener << "Size of " << key_name
76+
<< " does not match. Expected: " << keys_expected.size()
77+
<< " Actual: " << keys_actual.size() << "\n";
78+
return false;
79+
}
80+
81+
bool map_matches = true;
82+
for (int i = 0; i < keys_actual.size(); i++) {
83+
auto key_actual = keys_actual[i];
84+
auto key_expected = keys_expected[i];
85+
86+
if (key_actual.ToString() != key_expected.ToString()) {
87+
*listener << "Key mismatch in " << key_name
88+
<< " Expected: " << key_expected.ToString()
89+
<< " Actual: " << key_actual.ToString() << "\n";
90+
map_matches = false;
91+
continue;
92+
}
93+
94+
auto val_actual = map_actual.Values()[i];
95+
auto val_expected = map_expected.Values()[i];
96+
if (!CompareFlexbufferReference(
97+
val_actual, val_expected,
98+
key_name + "[" + key_actual.ToString() + "]", listener)) {
99+
map_matches = false;
100+
}
101+
}
102+
return map_matches;
103+
} else if (type_actual == flexbuffers::FBT_VECTOR) {
104+
flexbuffers::Vector vec_actual = reference_actual.AsVector();
105+
flexbuffers::Vector vec_expected = reference_expected.AsVector();
106+
107+
if (vec_actual.size() != vec_expected.size()) {
108+
*listener << "Size of " << key_name
109+
<< " does not match. Expected: " << vec_expected.size()
110+
<< " Actual: " << vec_actual.size() << "\n";
111+
return false;
112+
}
113+
114+
bool vectors_match = true;
115+
for (int i = 0; i < vec_actual.size(); i++) {
116+
auto vec_element_actual = vec_actual[i];
117+
auto vec_element_expected = vec_expected[i];
118+
if (!CompareFlexbufferReference(vec_element_actual, vec_element_expected,
119+
key_name + "[" + std::to_string(i) + "]",
120+
listener)) {
121+
vectors_match = false;
122+
}
123+
}
124+
return vectors_match;
125+
} else {
126+
auto str_actual = reference_actual.ToString();
127+
auto str_expected = reference_expected.ToString();
128+
129+
if (str_actual != str_expected) {
130+
*listener << "Values for " << key_name << " do not match.\n"
131+
<< "Expected: " << str_expected << "\n"
132+
<< "Actual: " << str_actual << "\n";
133+
return false;
134+
}
135+
return true;
136+
}
137+
}
138+
139+
} // namespace internal
140+
141+
} // namespace cppsdk
142+
} // namespace testing
143+
} // namespace firebase

testing/json_util.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#ifndef FIREBASE_TESTING_CPPSDK_JSON_UTIL_H_
2+
#define FIREBASE_TESTING_CPPSDK_JSON_UTIL_H_
3+
4+
#include "gtest/gtest.h"
5+
#include "gmock/gmock.h"
6+
#include "flatbuffers/flexbuffers.h"
7+
8+
namespace firebase {
9+
namespace testing {
10+
namespace cppsdk {
11+
12+
namespace internal {
13+
14+
class EqualsJsonMatcher {
15+
public:
16+
explicit EqualsJsonMatcher(const std::string& expected_json);
17+
bool MatchAndExplain(const std::string& actual,
18+
::testing::MatchResultListener* listener) const;
19+
void DescribeTo(std::ostream* os) const;
20+
void DescribeNegationTo(std::ostream* os) const;
21+
22+
private:
23+
std::string expected_json_;
24+
25+
bool CompareFlexbufferReference(
26+
flexbuffers::Reference reference_actual,
27+
flexbuffers::Reference reference_expected, std::string key_name,
28+
::testing::MatchResultListener* listener) const;
29+
};
30+
31+
} // namespace internal
32+
33+
inline ::testing::PolymorphicMatcher<internal::EqualsJsonMatcher> EqualsJson(
34+
const std::string& expected_json) {
35+
return ::testing::MakePolymorphicMatcher(
36+
internal::EqualsJsonMatcher(expected_json));
37+
}
38+
39+
} // namespace cppsdk
40+
} // namespace testing
41+
} // namespace firebase
42+
43+
#endif // FIREBASE_TESTING_CPPSDK_JSON_UTIL_H_

0 commit comments

Comments
 (0)