aboutsummaryrefslogtreecommitdiff
path: root/src/test/OptionalRefTest.cpp
blob: 219cccae5189756a349d558dc520075eacc5d6bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <gtest/gtest.h>

#include <crepe/util/OptionalRef.h>

using namespace std;
using namespace crepe;
using namespace testing;

TEST(OptionalRefTest, Explicit) {
	string value = "foo";
	OptionalRef<string> ref;
	EXPECT_FALSE(ref);
	ASSERT_THROW(ref.get(), runtime_error);

	ref.set(value);
	EXPECT_TRUE(ref);
	ASSERT_NO_THROW(ref.get());

	ref.clear();
	EXPECT_FALSE(ref);
	ASSERT_THROW(ref.get(), runtime_error);
}

TEST(OptionalRefTest, Implicit) {
	string value = "foo";
	OptionalRef<string> ref = value;
	EXPECT_TRUE(ref);
	ASSERT_NO_THROW(ref.get());

	ref.clear();
	EXPECT_FALSE(ref);
	ASSERT_THROW(ref.get(), runtime_error);

	ref = value;
	EXPECT_TRUE(ref);
	ASSERT_NO_THROW(ref.get());
}