1 #include "bej_common_test.hpp"
2 #include "bej_decoder_json.hpp"
3
4 #include <memory>
5 #include <string_view>
6
7 #include <gmock/gmock-matchers.h>
8 #include <gmock/gmock.h>
9 #include <gtest/gtest.h>
10
11 namespace libbej
12 {
13
14 struct BejDecoderTestParams
15 {
16 const std::string testName;
17 const BejTestInputFiles inputFiles;
18 };
19
PrintTo(const BejDecoderTestParams & params,std::ostream * os)20 void PrintTo(const BejDecoderTestParams& params, std::ostream* os)
21 {
22 *os << params.testName;
23 }
24
25 using BejDecoderTest = testing::TestWithParam<BejDecoderTestParams>;
26
27 const BejTestInputFiles driveOemTestFiles = {
28 .jsonFile = "../test/json/drive_oem.json",
29 .schemaDictionaryFile = "../test/dictionaries/drive_oem_dict.bin",
30 .annotationDictionaryFile = "../test/dictionaries/annotation_dict.bin",
31 .errorDictionaryFile = "",
32 .encodedStreamFile = "../test/encoded/drive_oem_enc.bin",
33 };
34
35 const BejTestInputFiles circuitTestFiles = {
36 .jsonFile = "../test/json/circuit.json",
37 .schemaDictionaryFile = "../test/dictionaries/circuit_dict.bin",
38 .annotationDictionaryFile = "../test/dictionaries/annotation_dict.bin",
39 .errorDictionaryFile = "",
40 .encodedStreamFile = "../test/encoded/circuit_enc.bin",
41 };
42
43 const BejTestInputFiles storageTestFiles = {
44 .jsonFile = "../test/json/storage.json",
45 .schemaDictionaryFile = "../test/dictionaries/storage_dict.bin",
46 .annotationDictionaryFile = "../test/dictionaries/annotation_dict.bin",
47 .errorDictionaryFile = "",
48 .encodedStreamFile = "../test/encoded/storage_enc.bin",
49 };
50
51 const BejTestInputFiles dummySimpleTestFiles = {
52 .jsonFile = "../test/json/dummysimple.json",
53 .schemaDictionaryFile = "../test/dictionaries/dummy_simple_dict.bin",
54 .annotationDictionaryFile = "../test/dictionaries/annotation_dict.bin",
55 .errorDictionaryFile = "",
56 .encodedStreamFile = "../test/encoded/dummy_simple_enc.bin",
57 };
58
TEST_P(BejDecoderTest,Decode)59 TEST_P(BejDecoderTest, Decode)
60 {
61 const BejDecoderTestParams& test_case = GetParam();
62 auto inputsOrErr = loadInputs(test_case.inputFiles);
63 EXPECT_TRUE(inputsOrErr);
64
65 BejDictionaries dictionaries = {
66 .schemaDictionary = inputsOrErr->schemaDictionary,
67 .annotationDictionary = inputsOrErr->annotationDictionary,
68 .errorDictionary = inputsOrErr->errorDictionary,
69 };
70
71 BejDecoderJson decoder;
72 EXPECT_THAT(decoder.decode(dictionaries, inputsOrErr->encodedStream), 0);
73 std::string decoded = decoder.getOutput();
74 nlohmann::json jsonDecoded = nlohmann::json::parse(decoded);
75
76 // Just comparing nlohmann::json types could lead to errors. It compares the
77 // byte values. So int64 and unit64 comparisons might be incorrect. Eg:
78 // bytes values for -5 and 18446744073709551611 are the same. So compare the
79 // string values.
80 EXPECT_TRUE(jsonDecoded.dump() == inputsOrErr->expectedJson.dump());
81 }
82
83 /**
84 * TODO: Add more test cases.
85 * - Test Enums inside array elemets
86 * - Array inside an array: is this a valid case?
87 * - Real numbers with exponent part
88 * - Every type inside an array.
89 */
90 INSTANTIATE_TEST_SUITE_P(
91 , BejDecoderTest,
92 testing::ValuesIn<BejDecoderTestParams>({
93 {"DriveOEM", driveOemTestFiles},
94 {"Circuit", circuitTestFiles},
95 {"Storage", storageTestFiles},
96 {"DummySimple", dummySimpleTestFiles},
97 }),
__anon72dd28ce0102(const testing::TestParamInfo<BejDecoderTest::ParamType>& info) 98 [](const testing::TestParamInfo<BejDecoderTest::ParamType>& info) {
99 return info.param.testName;
100 });
101
102 } // namespace libbej
103