1 /*
2 * Copyright 2017 Google LLC
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "fcp/testing/testing.h"
18
19 #include "gtest/gtest.h"
20 #include "absl/flags/flag.h"
21 #include "fcp/base/monitoring.h"
22 #include "fcp/testing/test_messages.pb.h"
23
24 ABSL_FLAG(std::string, baseline_path, "", "Path to baseline");
25
26 namespace fcp {
27
28 namespace {
29
30 using ::testing::Not;
31
TEST(TestingTest,TestName)32 TEST(TestingTest, TestName) { ASSERT_EQ(TestName(), "TestName"); }
33
TEST(TestingTest,TestDataPath)34 TEST(TestingTest, TestDataPath) {
35 auto path = GetTestDataPath(absl::GetFlag(FLAGS_baseline_path));
36 ASSERT_TRUE(FileExists(path));
37 }
38
TEST(TestingTest,TemporaryTestFile)39 TEST(TestingTest, TemporaryTestFile) {
40 auto path = TemporaryTestFile(".dat");
41 ASSERT_EQ(WriteStringToFile(path, "test").code(), OK);
42 ASSERT_EQ(ReadFileToString(path).value(), "test");
43 }
44
TEST(TestingTest,VerifyAgainstBaseline)45 TEST(TestingTest, VerifyAgainstBaseline) {
46 auto status_or_diff = VerifyAgainstBaseline(
47 absl::GetFlag(FLAGS_baseline_path), "Dies ist ein Test.");
48 ASSERT_TRUE(status_or_diff.ok()) << status_or_diff.status();
49 if (!status_or_diff.value().empty()) {
50 FAIL() << status_or_diff.value();
51 }
52 }
53
TEST(TestingTest,VerifyAgainstBaselineFailure)54 TEST(TestingTest, VerifyAgainstBaselineFailure) {
55 auto status_or_diff = VerifyAgainstBaseline(
56 absl::GetFlag(FLAGS_baseline_path), "Dies ist kein Test.");
57 ASSERT_TRUE(status_or_diff.ok()) << status_or_diff.status();
58 // The actual output of the diff is much dependent on which mode we run
59 // in and on which platform. Hence only test whether *some* thing is reported.
60 ASSERT_FALSE(status_or_diff.value().empty());
61 }
62
TEST(TestingTest,EqualsProtoMessage)63 TEST(TestingTest, EqualsProtoMessage) {
64 testing::Foo foo1;
65 foo1.set_foo("foo");
66 testing::Foo foo2;
67 foo2.set_foo("foo");
68 ASSERT_THAT(foo1, EqualsProto(foo2));
69 }
70
TEST(TestingTest,NotEqualsProtoMessage)71 TEST(TestingTest, NotEqualsProtoMessage) {
72 testing::Foo foo1;
73 foo1.set_foo("foo-1");
74 testing::Foo foo2;
75 foo2.set_foo("foo-2");
76 ASSERT_THAT(foo1, Not(EqualsProto(foo2)));
77 }
78
TEST(TestingTest,NotEqualsProtoMessageType)79 TEST(TestingTest, NotEqualsProtoMessageType) {
80 testing::Foo foo;
81 foo.set_foo("foo");
82 testing::Bar bar;
83 bar.set_bar(1);
84 ASSERT_THAT(foo, Not(EqualsProto(bar)));
85 }
86
TEST(TestingTest,EqualsProtoMessageText)87 TEST(TestingTest, EqualsProtoMessageText) {
88 testing::Bar bar;
89 bar.set_bar(1);
90 ASSERT_THAT(bar, EqualsProto("bar: 1"));
91 }
92
TEST(TestingTest,NotEqualsProtoMessageText)93 TEST(TestingTest, NotEqualsProtoMessageText) {
94 testing::Bar bar;
95 bar.set_bar(1);
96 ASSERT_THAT(bar, Not(EqualsProto("bar: 2")));
97 }
98
99 } // namespace
100
101 } // namespace fcp
102