| 1 |
// Copyright 2020 Google LLC |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
#include "dap/typeof.h" |
| 16 |
#include "dap/types.h" |
| 17 |
#include "json_serializer.h" |
| 18 |
|
| 19 |
#include "gmock/gmock.h" |
| 20 |
#include "gtest/gtest.h" |
| 21 |
|
| 22 |
namespace dap { |
| 23 |
|
| 24 |
struct BaseStruct { |
| 25 |
dap::integer i; |
| 26 |
dap::number n; |
| 27 |
}; |
| 28 |
|
| 29 |
DAP_STRUCT_TYPEINFO(BaseStruct, |
| 30 |
"BaseStruct", |
| 31 |
DAP_FIELD(i, "i"), |
| 32 |
DAP_FIELD(n, "n")); |
| 33 |
|
| 34 |
struct DerivedStruct : public BaseStruct { |
| 35 |
dap::string s; |
| 36 |
dap::boolean b; |
| 37 |
}; |
| 38 |
|
| 39 |
DAP_STRUCT_TYPEINFO_EXT(DerivedStruct, |
| 40 |
BaseStruct, |
| 41 |
"DerivedStruct", |
| 42 |
DAP_FIELD(s, "s"), |
| 43 |
DAP_FIELD(b, "b")); |
| 44 |
|
| 45 |
} // namespace dap |
| 46 |
|
| 47 |
TEST(TypeInfo, Derived) { |
| 48 |
dap::DerivedStruct in; |
| 49 |
in.s = "hello world"; |
| 50 |
in.b = true; |
| 51 |
in.i = 42; |
| 52 |
in.n = 3.14; |
| 53 |
|
| 54 |
dap::json::Serializer s; |
| 55 |
ASSERT_TRUE(s.serialize(in)); |
| 56 |
|
| 57 |
dap::DerivedStruct out; |
| 58 |
dap::json::Deserializer d(s.dump()); |
| 59 |
ASSERT_TRUE(d.deserialize(&out)) << "Failed to deserialize\n" << s.dump(); |
| 60 |
|
| 61 |
ASSERT_EQ(out.s, "hello world"); |
| 62 |
ASSERT_EQ(out.b, true); |
| 63 |
ASSERT_EQ(out.i, 42); |
| 64 |
ASSERT_EQ(out.n, 3.14); |
| 65 |
} |