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