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