1 /*
2 * Copyright 2019 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/result_matchers.h"
18
19 #include "gtest/gtest.h"
20 #include "fcp/base/error.h"
21 #include "fcp/base/result.h"
22 #include "fcp/base/tracing_schema.h"
23 #include "fcp/testing/testing.h"
24 #include "fcp/tracing/test_tracing_recorder.h"
25
26 namespace fcp {
27 using testing::Eq;
28 using testing::Not;
29
TEST(ExpectTest,HasValueDirect)30 TEST(ExpectTest, HasValueDirect) {
31 EXPECT_THAT(Result<int>(42), HasValue(42));
32 EXPECT_THAT(Result<int>(42), Not(HasValue(24)));
33 EXPECT_THAT(Result<std::string>("foo"), HasValue("foo"));
34 EXPECT_THAT(Result<std::string>("foo"), Not(HasValue("bar")));
35 }
36
TEST(ExpectTest,HasValueEq)37 TEST(ExpectTest, HasValueEq) {
38 EXPECT_THAT(Result<int>(42), HasValue(Eq(42)));
39 EXPECT_THAT(Result<int>(42), Not(HasValue(Eq(24))));
40 EXPECT_THAT(Result<int>(42), HasValue(Not(Eq(24))));
41 EXPECT_THAT(Result<std::string>("foo"), HasValue(Eq("foo")));
42 EXPECT_THAT(Result<std::string>("foo"), Not(HasValue(Eq("bar"))));
43 EXPECT_THAT(Result<std::string>("foo"), HasValue(Not(Eq("bar"))));
44 }
45
TEST(ExpectTest,ExpectIsError)46 TEST(ExpectTest, ExpectIsError) {
47 EXPECT_THAT(Result<int>(TraceTestError()), IsError());
48 EXPECT_THAT(Result<int>(42), Not(IsError()));
49 }
50
TEST(ExpectTest,ExpectStatus)51 TEST(ExpectTest, ExpectStatus) {
52 TestTracingRecorder recorder;
53 EXPECT_THAT(Result<Status>(FCP_STATUS(INVALID_ARGUMENT))
54 .Then(ExpectStatus<INVALID_ARGUMENT>()),
55 HasValue(Unit{}));
56 }
57
TEST(ExpectTest,ExpectStatusReturnsError)58 TEST(ExpectTest, ExpectStatusReturnsError) {
59 TestTracingRecorder recorder;
60 recorder.ExpectError<ResultExpectStatusError>();
61 EXPECT_THAT(
62 Result<Status>(FCP_STATUS(OK)).Then(ExpectStatus<INVALID_ARGUMENT>()),
63 IsError());
64 }
65
66 } // namespace fcp
67