1 // Copyright 2019 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 // Tests of generated code for text format.
16 #include <stdint.h>
17
18 #include <type_traits>
19 #include <utility>
20 #include <vector>
21
22 #include "gtest/gtest.h"
23 #include "testdata/text_format.emb.h"
24
25 namespace emboss {
26 namespace test {
27 namespace {
28
TEST(TextFormat,VanillaOutput)29 TEST(TextFormat, VanillaOutput) {
30 ::std::array<char, 2> values = {1, 2};
31 const auto view = MakeVanillaView(&values);
32 EXPECT_EQ("{ a: 1, b: 2 }", ::emboss::WriteToString(view));
33 EXPECT_EQ(
34 "{\n"
35 " a: 1 # 0x1\n"
36 " b: 2 # 0x2\n"
37 "}",
38 ::emboss::WriteToString(view, ::emboss::MultilineText()));
39 }
40
TEST(TextFormat,SkippedFieldOutput)41 TEST(TextFormat, SkippedFieldOutput) {
42 ::std::array<char, 3> values = {1, 2, 3};
43 const auto view = MakeStructWithSkippedFieldsView(&values);
44 EXPECT_EQ("{ a: 1, c: 3 }", ::emboss::WriteToString(view));
45 EXPECT_EQ(
46 "{\n"
47 " a: 1 # 0x1\n"
48 " c: 3 # 0x3\n"
49 "}",
50 ::emboss::WriteToString(view, ::emboss::MultilineText()));
51 }
52
TEST(TextFormat,SkippedStructureFieldOutput)53 TEST(TextFormat, SkippedStructureFieldOutput) {
54 ::std::array<char, 6> values = {1, 2, 3, 4, 5, 6};
55 const auto view = MakeStructWithSkippedStructureFieldsView(&values);
56 EXPECT_EQ("{ a: { a: 1, b: 2 }, c: { a: 5, b: 6 } }",
57 ::emboss::WriteToString(view));
58 EXPECT_EQ(
59 "{\n"
60 " a: {\n"
61 " a: 1 # 0x1\n"
62 " b: 2 # 0x2\n"
63 " }\n"
64 " c: {\n"
65 " a: 5 # 0x5\n"
66 " b: 6 # 0x6\n"
67 " }\n"
68 "}",
69 ::emboss::WriteToString(view, ::emboss::MultilineText()));
70 EXPECT_EQ("{ a: 3, b: 4 }", ::emboss::WriteToString(view.b()));
71 EXPECT_EQ(
72 "{\n"
73 " a: 3 # 0x3\n"
74 " b: 4 # 0x4\n"
75 "}",
76 ::emboss::WriteToString(view.b(), ::emboss::MultilineText()));
77 }
78
TEST(TextFormat,UpdateFromText)79 TEST(TextFormat, UpdateFromText) {
80 ::std::array<char, 2> values{};
81 const auto view = MakeVanillaView(&values);
82
83 ::emboss::UpdateFromText(view, "{ a: 1, b: 2 }");
84 EXPECT_EQ(view.a().Read(), 1);
85 EXPECT_EQ(view.b().Read(), 2);
86
87 ::emboss::UpdateFromText(view, "{ a: 3 }");
88 EXPECT_EQ(view.a().Read(), 3);
89 EXPECT_EQ(view.b().Read(), 2);
90
91 ::emboss::UpdateFromText(view, "{ b: 4 }");
92 EXPECT_EQ(view.a().Read(), 3);
93 EXPECT_EQ(view.b().Read(), 4);
94 }
95
96 } // namespace
97 } // namespace test
98 } // namespace emboss
99