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 #include "integration/googletest/emboss_test_util.h"
16
17 #include "gmock/gmock.h"
18 #include "gtest/gtest.h"
19 #include "testdata/complex_structure.emb.h"
20
21 namespace emboss {
22 namespace test {
23 namespace {
24
25 class EmbossTestUtilTest : public ::testing::Test {
26 protected:
EmbossTestUtilTest()27 EmbossTestUtilTest() { b_.s().Write(1); }
28 ::std::array</**/ ::std::uint8_t, 64> buf_a_{};
29 ::emboss_test::ComplexWriter a_{&buf_a_};
30 ::std::array<char, 64> buf_b_{};
31 ::emboss_test::ComplexWriter b_{&buf_b_};
32 };
33
TEST_F(EmbossTestUtilTest,EqualsEmb)34 TEST_F(EmbossTestUtilTest, EqualsEmb) {
35 EXPECT_THAT(a_, EqualsEmb(a_));
36 EXPECT_THAT(b_, EqualsEmb(b_));
37
38 EXPECT_THAT(a_, ::testing::Not(EqualsEmb(b_)));
39 EXPECT_THAT(b_, ::testing::Not(EqualsEmb(a_)));
40 }
41
TEST_F(EmbossTestUtilTest,NotOkView)42 TEST_F(EmbossTestUtilTest, NotOkView) {
43 auto null_view = ::emboss_test::ComplexView(nullptr);
44 EXPECT_THAT(a_, ::testing::Not(EqualsEmb(null_view)));
45 EXPECT_THAT(b_, ::testing::Not(EqualsEmb(null_view)));
46 EXPECT_THAT(null_view, ::testing::Not(EqualsEmb(null_view)));
47 EXPECT_THAT(null_view, ::testing::Not(EqualsEmb(a_)));
48 EXPECT_THAT(null_view, ::testing::Not(EqualsEmb(b_)));
49 }
50
TEST_F(EmbossTestUtilTest,NotOkViewMatcherDescribe)51 TEST_F(EmbossTestUtilTest, NotOkViewMatcherDescribe) {
52 auto null_view = ::emboss_test::ComplexView(nullptr);
53
54 ::testing::StringMatchResultListener listener;
55 EqualsEmb(a_).impl().MatchAndExplain(null_view, &listener);
56 EXPECT_EQ(listener.str(), "View for comparison from is not OK.");
57
58 listener.Clear();
59 EqualsEmb(null_view).impl().MatchAndExplain(a_, &listener);
60 EXPECT_EQ(listener.str(), "View for comparison to is not OK.");
61 }
62
TEST_F(EmbossTestUtilTest,MatcherDescribeEquivalent)63 TEST_F(EmbossTestUtilTest, MatcherDescribeEquivalent) {
64 ::std::stringstream ss;
65 EqualsEmb(a_).impl().DescribeTo(&ss);
66 EXPECT_EQ(ss.str(), "are equal");
67 }
68
TEST_F(EmbossTestUtilTest,MatcherDescribeNotEquivalent)69 TEST_F(EmbossTestUtilTest, MatcherDescribeNotEquivalent) {
70 ::std::stringstream ss;
71 EqualsEmb(a_).impl().DescribeNegationTo(&ss);
72 EXPECT_EQ(ss.str(), "are NOT equal");
73 }
74
TEST_F(EmbossTestUtilTest,MatcherExplainEquivalent)75 TEST_F(EmbossTestUtilTest, MatcherExplainEquivalent) {
76 ::testing::StringMatchResultListener listener;
77
78 EqualsEmb(a_).impl().MatchAndExplain(a_, &listener);
79 EXPECT_EQ(listener.str(), "");
80
81 EqualsEmb(b_).impl().MatchAndExplain(b_, &listener);
82 EXPECT_EQ(listener.str(), "");
83 }
84
TEST_F(EmbossTestUtilTest,MatcherExplainNotEquivalent)85 TEST_F(EmbossTestUtilTest, MatcherExplainNotEquivalent) {
86 ::testing::StringMatchResultListener listener;
87 EqualsEmb(a_).impl().MatchAndExplain(b_, &listener);
88 EXPECT_EQ(listener.str(), R"(
89 @@ -1,3 +1,3 @@
90 - s: 0 # 0x0
91 + s: 1 # 0x1
92 u: 0 # 0x0
93 i: 0 # 0x0
94 @@ +4,34 @@
95 b: 0 # 0x0
96 a: {
97 + [0]: {
98 + [0]: {
99 + a: {
100 + x: 0 # 0x0
101 + l: 0 # 0x0
102 + h: 0 # 0x0
103 + }
104 + }
105 + [1]: {
106 + a: {
107 + x: 0 # 0x0
108 + l: 0 # 0x0
109 + h: 0 # 0x0
110 + }
111 + }
112 + [2]: {
113 + a: {
114 + x: 0 # 0x0
115 + l: 0 # 0x0
116 + h: 0 # 0x0
117 + }
118 + }
119 + [3]: {
120 + a: {
121 + x: 0 # 0x0
122 + l: 0 # 0x0
123 + h: 0 # 0x0
124 + }
125 + }
126 + }
127 }
128 a0: 0 # 0x0
129 )");
130 }
131
132 } // namespace
133 } // namespace test
134 } // namespace emboss
135