-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.cpp
More file actions
109 lines (84 loc) · 2.85 KB
/
demo.cpp
File metadata and controls
109 lines (84 loc) · 2.85 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "optional_ext.h"
#include "catch.hpp"
#include <string>
#include <iostream>
using std::string;
using knatten::optional;
struct tweet {
string content;
};
struct author {
author(string name) : name(name) {}
string name;
};
optional<tweet> find_first(const string& search_string);
author lookup_author(const tweet& t);
TEST_CASE("Demo of transform") {
SECTION("old style") {
auto foo = find_first("foo");
auto foo_author(foo.has_value() ? lookup_author(*foo) : optional<author>());
REQUIRE(foo_author->name == "@knatten");
}
SECTION("new style") {
auto foo_author = find_first("foo").transform(lookup_author);
REQUIRE(foo_author->name == "@knatten");
}
SECTION("such oneliner") {
REQUIRE(find_first("foo").transform(lookup_author)->name == "@knatten");
}
}
optional<tweet> tweet_replied_to(const tweet& t);
TEST_CASE("Demo of transform_optional") {
SECTION("old style") {
auto foo = find_first("foo");
auto foo_replied_to(foo.has_value() ? tweet_replied_to(*foo) : optional<tweet>());
REQUIRE(foo_replied_to.has_value() == true);
}
SECTION("new style") {
auto foo_replied_to = find_first("foo").transform_optional(tweet_replied_to);
REQUIRE(foo_replied_to.has_value() == true);
}
SECTION("such oneliner") {
REQUIRE(find_first("foo").transform_optional(tweet_replied_to).has_value() == true);
}
}
TEST_CASE("Demo of call") {
SECTION("old style") {
auto foo = find_first("foo");
if (foo.has_value())
std::cout << "Found a tweet about 'foo'!";
}
SECTION("new style") {
auto foo = find_first("foo");
foo.call([](const tweet& t) { std::cout << "Found a tweet about 'foo': " << t.content; });
}
SECTION("such oneliner") {
find_first("foo").call([](const tweet& t) { std::cout << "Found a tweet about 'foo': " << t.content; });
}
}
TEST_CASE("Combined demo") {
SECTION("old style") {
auto foo = find_first("foo");
auto foo_replied_to(foo.has_value() ? tweet_replied_to(*foo) : optional<tweet>());
auto orig_author(foo_replied_to.has_value() ? lookup_author(*foo_replied_to) : optional<author>());
REQUIRE(orig_author->name == "@knatten");
}
SECTION("new style") {
auto orig_author = find_first("foo")
.transform_optional(tweet_replied_to)
.transform(lookup_author);
REQUIRE(orig_author->name == "@knatten");
}
SECTION("such oneliner") {
REQUIRE(find_first("foo").transform_optional(tweet_replied_to).transform(lookup_author)->name == "@knatten");
}
}
optional<tweet> find_first(const string&) {
return tweet();
}
author lookup_author(const tweet&) {
return author("@knatten");
}
optional<tweet> tweet_replied_to(const tweet&) {
return tweet();
}