Hash :
b3a8033d
Author :
Date :
2021-03-31T09:34:00
JsonSerializer: use stubs when building without rapidjson Bug: angleproject:5805 Change-Id: Ibf51b8b75c3feb6efdef969effb3f50e2474c6b3 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2795772 Commit-Queue: Gert Wollny <gert.wollny@collabora.com> Reviewed-by: Cody Northrop <cnorthrop@google.com> Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: Jamie Madill <jmadill@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
//
// Copyright 2021 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// JsonSerializer_unittests-cpp: Unit tests for the JSON based serializer
//
#if defined(ANGLE_HAVE_RAPIDJSON)
# include "JsonSerializer.h"
# include <gtest/gtest.h>
class JsonSerializerTest : public ::testing::Test
{
protected:
void SetUp() override;
void check(const std::string &expect);
angle::JsonSerializer js;
};
// Test writing one integer value
TEST_F(JsonSerializerTest, NamedIntValue1)
{
js.addScalar("test1", 1);
const std::string expect =
R"({
"context": {
"test1": 1
}
})";
check(expect);
}
// Test writing one long value
TEST_F(JsonSerializerTest, NamedLongValue)
{
long v = -12;
js.addScalar("test1", v);
const std::string expect =
R"({
"context": {
"test1": -12
}
})";
check(expect);
}
// Test writing one unsigned long value
TEST_F(JsonSerializerTest, NamedULongValue)
{
unsigned long v = 12;
js.addScalar("test1", v);
const std::string expect =
R"({
"context": {
"test1": 12
}
})";
check(expect);
}
// Test writing another integer value
TEST_F(JsonSerializerTest, NamedIntValue2)
{
js.addScalar("test2", 2);
const std::string expect =
R"({
"context": {
"test2": 2
}
})";
check(expect);
}
// Test writing one string value
TEST_F(JsonSerializerTest, NamedStringValue)
{
js.addCString("test2", "value");
const std::string expect =
R"({
"context": {
"test2": "value"
}
})";
check(expect);
}
// Test writing one byte array
// Since he serialiter is only used for testing we don't store
// the whole byte array, but only it's SHA1 checksum
TEST_F(JsonSerializerTest, ByteArrayValue)
{
const uint8_t value[5] = {10, 0, 0xcc, 0xff, 0xaa};
js.addBlob("test2", value, 5);
const std::string expect =
R"({
"context": {
"test2": "SHA1:4315724B1AB1EB2C0128E8E9DAD6D76254BA711D"
}
})";
check(expect);
}
// Test writing one vector of integer values
TEST_F(JsonSerializerTest, IntVectorValue)
{
std::vector<int> v = {0, 1, -1};
js.addVector("test2", v);
const std::string expect =
R"({
"context": {
"test2": [
0,
1,
-1
]
}
})";
check(expect);
}
// Test writing boolean values
TEST_F(JsonSerializerTest, NamedBoolValues)
{
js.addScalar("test_false", false);
js.addScalar("test_true", true);
const std::string expect =
R"({
"context": {
"test_false": false,
"test_true": true
}
})";
check(expect);
}
// test writing two values in a sub-group
TEST_F(JsonSerializerTest, GroupedIntValue)
{
js.startGroup("group");
js.addScalar("test1", 1);
js.addScalar("test2", 2);
js.endGroup();
const std::string expect =
R"({
"context": {
"group": {
"test1": 1,
"test2": 2
}
}
})";
check(expect);
}
void JsonSerializerTest::SetUp()
{
js.startDocument("context");
}
void JsonSerializerTest::check(const std::string &expect)
{
js.endDocument();
EXPECT_EQ(js.data(), expect);
EXPECT_EQ(js.length(), expect.length());
std::vector<uint8_t> expect_as_ubyte(expect.begin(), expect.end());
EXPECT_EQ(js.getData(), expect_as_ubyte);
}
#endif