summaryrefslogtreecommitdiff
path: root/vendor/bandit/specs/matchers/equal.cpp
blob: f7f31b0bab5915389716cc1ddc7b1f747cd43426 (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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <specs/specs.h>

using namespace bandit::Matchers;

SPEC_BEGIN(Matchers::Equal)

describe("when the actual value is a built-in type", []{
    int actualValue = 1;

    describe("and the expected value is the same built-in type", [&]{
	int expectedValue;

	describe("and the values are equal", [&]{
	    before_each([&]{
		expectedValue = 1;
	    });

	    it("must accept a positive match", [&]{
		actualValue must equal(expectedValue);
	    });

	    it("must reject a negative match", [&]{
		AssertThrows(std::exception, [&]{ actualValue must_not equal(expectedValue); }());
	    });
	});

	describe("and the values are not equal", [&]{
	    before_each([&]{
		expectedValue = 147;
	    });

	    it("must accept a negative match", [&]{
		actualValue must_not equal(expectedValue);
	    });

	    it("must reject a positive match", [&]{
		AssertThrows(std::exception, [&]{ actualValue must equal(expectedValue); }());
	    });
	});
    });

    describe("and the expected value is a different, but comparable, built-in type", [&]{
	long int expectedValue;

	describe("and the values are equal", [&]{
	    before_each([&]{
		expectedValue = 1;
	    });

	    it("must accept a positive match", [&]{
		actualValue must equal(expectedValue);
	    });

	    it("must reject a negative match", [&]{
		AssertThrows(std::exception, [&]{ actualValue must_not equal(expectedValue); }());
	    });
	});

	describe("and the values are not equal", [&]{
	    before_each([&]{
		expectedValue = 42;
	    });

	    it("must accept a negative match", [&]{
		actualValue must_not equal(expectedValue);
	    });

	    it("must reject a positive match", [&]{
		AssertThrows(std::exception, [&]{ actualValue must equal(expectedValue); }());
	    });
	});
    });
});

describe("when the actual value is declared as a C string", []{
    char* actualValue = (char*)"actualValue";

    describe("and the expected value is declared as a C string", [&]{
	std::unique_ptr<char> expectedValue;

	before_each([&]{
	    expectedValue.reset((char*)calloc(strlen(actualValue) + 1, sizeof(char)));
	});

	describe("and the values are equal", [&]{
	    before_each([&]{
		stpcpy(expectedValue.get(), actualValue);
	    });

	    it("must accept a positive match", [&]{
		actualValue must equal(expectedValue.get());
	    });

	    it("must reject a negative match", [&]{
		AssertThrows(std::exception, [&]{ actualValue must_not equal(expectedValue.get()); }());
	    });
	});

	describe("and the values are not equal", [&]{
	    before_each([&]{
		stpcpy(expectedValue.get(), "expectedVal");
	    });

	    it("must accept a negative match", [&]{
		actualValue must_not equal(expectedValue.get());
	    });

	    it("must reject a positive match", [&]{
		AssertThrows(std::exception, [&]{ actualValue must equal(expectedValue.get()); }());
	    });
	});
    });

    describe("and the expected value is declared as a const C string", [&]{
	const char *expectedValue;

	describe("and the values are equal", [&]{
	    before_each([&]{
		expectedValue = "actualValue";
	    });

	    it("must accept a positive match", [&]{
		actualValue must equal(expectedValue);
	    });

	    it("must reject a negative match", [&]{
		AssertThrows(std::exception, [&]{ actualValue must_not equal(expectedValue); }());
	    });
	});
    });

    describe("when the expected value is a unique_ptr to a C string", [&]{
	std::unique_ptr<char> expectedValue;

	before_each([&]{
	    expectedValue.reset((char*)calloc(strlen(actualValue) + 1, sizeof(char)));
	});

	describe("and the values are equal", [&]{
	    before_each([&]{
		stpcpy(expectedValue.get(), actualValue);
	    });

	    it("must accept a positive match", [&]{
		actualValue must equal(expectedValue);
	    });

	    it("must reject a negative match", [&]{
		AssertThrows(std::exception, [&]{ actualValue must_not equal(expectedValue); }());
	    });
	});
    });
});

describe("when the actual value is a unique_ptr", []{
    std::unique_ptr<char> actualValue;
    auto expectedValue = (char*)"expectedValue";

    before_each([&]{
	actualValue.reset((char*)calloc(strlen(expectedValue) + 1, sizeof(char)));
    });

    describe("when the strings are equal", [&]{
	before_each([&]{
	    stpcpy(actualValue.get(), expectedValue);
	});

	it("must accept a positive match", [&]{
	    actualValue must equal(expectedValue);
	});
    });

    describe("when the strings are not equal", [&]{
	before_each([&]{
	    stpcpy(actualValue.get(), "hello");
	});

	it("must accept a negative match", [&]{
	    actualValue must_not equal(expectedValue);
	});
    });
});

describe("when the actual value is declared as char array", []{
    describe("and the expected value is declared as a C string", []{
	char actualValue[] = "actualValue";

	describe("and the values are equal", [&]{
	    char* expectedValue = (char*)"actualValue";

	    it("must accept a positive match", [&]{
		actualValue must equal(expectedValue);
	    });

	    it("must reject a negative match", [&]{
		AssertThrows(std::exception, [&]{ actualValue must_not equal(expectedValue); }());
	    });
	});

	describe("and the values are not equal", [&]{
	    char* expectedValue = (char*)"expectedValue";

	    it("must reject a positive match", [&]{
		AssertThrows(std::exception, [&]{ actualValue must equal(expectedValue); }());
	    });

	    it("must accept a negative match", [&]{
		actualValue must_not equal(expectedValue);
	    });
	});
    });
});

SPEC_END