Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/projects/SelfTest/MatchersTests.cpp
blob: 52a49230a3c149b950eed5e248386c78d75d2665 (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
 *  Created by Phil on 21/02/2017.
 *  Copyright 2017 Two Blue Cubes Ltd. All rights reserved.
 *
 *  Distributed under the Boost Software License, Version 1.0. (See accompanying
 *  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
 */

#include "catch.hpp"

#ifndef CATCH_CONFIG_DISABLE_MATCHERS

inline const char* testStringForMatching()
{
    return "this string contains 'abc' as a substring";
}
inline const char* testStringForMatching2()
{
    return "some completely different text that contains one common word";
}

using namespace Catch::Matchers;

TEST_CASE("String matchers", "[matchers]" )
{
    REQUIRE_THAT( testStringForMatching(), Contains( "string" ) );
    CHECK_THAT( testStringForMatching(), Contains( "abc" ) );

    CHECK_THAT( testStringForMatching(), StartsWith( "this" ) );
    CHECK_THAT( testStringForMatching(), EndsWith( "substring" ) );
}

TEST_CASE("Contains string matcher", "[.][failing][matchers]")
{
    CHECK_THAT( testStringForMatching(), Contains( "not there" ) );
}

TEST_CASE("StartsWith string matcher", "[.][failing][matchers]")
{
    CHECK_THAT( testStringForMatching(), StartsWith( "string" ) );
}

TEST_CASE("EndsWith string matcher", "[.][failing][matchers]")
{
    CHECK_THAT( testStringForMatching(), EndsWith( "this" ) );
}

TEST_CASE("Equals string matcher", "[.][failing][matchers]")
{
    CHECK_THAT( testStringForMatching(), Equals( "something else" ) );
}

TEST_CASE("AllOf matcher", "[matchers]")
{
    CHECK_THAT( testStringForMatching(), AllOf( Catch::Contains( "string" ), Catch::Contains( "abc" ) ) );
}
TEST_CASE("AnyOf matcher", "[matchers]")
{
    CHECK_THAT( testStringForMatching(), AnyOf( Catch::Contains( "string" ), Catch::Contains( "not there" ) ) );
    CHECK_THAT( testStringForMatching(), AnyOf( Catch::Contains( "not there" ), Catch::Contains( "string" ) ) );
}

TEST_CASE("Equals", "[matchers]")
{
    CHECK_THAT( testStringForMatching(), Equals( "this string contains 'abc' as a substring" ) );
}

TEST_CASE("Matchers can be (AllOf) composed with the && operator", "[matchers][operators][operator&&]")
{
    CHECK_THAT( testStringForMatching(),
                Contains( "string" ) &&
                Contains( "abc" ) &&
                Contains( "substring" ) &&
                Contains( "contains" ) );
}

TEST_CASE("Matchers can be (AnyOf) composed with the || operator", "[matchers][operators][operator||]")
{
    CHECK_THAT( testStringForMatching(), Contains( "string" ) || Contains( "different" ) || Contains( "random" ) );
    CHECK_THAT( testStringForMatching2(), Contains( "string" ) || Contains( "different" ) || Contains( "random" ) );
}

TEST_CASE("Matchers can be composed with both && and ||", "[matchers][operators][operator||][operator&&]")
{
    CHECK_THAT( testStringForMatching(), ( Contains( "string" ) || Contains( "different" ) ) && Contains( "substring" ) );
}

TEST_CASE("Matchers can be composed with both && and || - failing", "[matchers][operators][operator||][operator&&][.failing]")
{
    CHECK_THAT( testStringForMatching(), ( Contains( "string" ) || Contains( "different" ) ) && Contains( "random" ) );
}

TEST_CASE("Matchers can be negated (Not) with the ! operator", "[matchers][operators][not]")
{
    CHECK_THAT( testStringForMatching(), !Contains( "different" ) );
}

TEST_CASE("Matchers can be negated (Not) with the ! operator - failing", "[matchers][operators][not][.failing]")
{
    CHECK_THAT( testStringForMatching(), !Contains( "substring" ) );
}

TEST_CASE( "Vector matchers", "[matchers][vector]" ) {
    std::vector<int> v;
    v.push_back( 1 );
    v.push_back( 2 );
    v.push_back( 3 );

    std::vector<int> v2;
    v2.push_back( 1 );
    v2.push_back( 2 );

    std::vector<int> empty;

    SECTION( "Contains (element)" ) {
        CHECK_THAT( v, VectorContains( 1 ) );
        CHECK_THAT( v, VectorContains( 2 ) );
    }
    SECTION( "Contains (vector)" ) {
        CHECK_THAT( v, Contains( v2 ) );
        v2.push_back( 3 ); // now exactly matches
        CHECK_THAT( v, Contains( v2 ) );

        CHECK_THAT( v, Contains( empty) );
        CHECK_THAT( empty, Contains( empty) );
    }

    SECTION( "Equals" ) {

        // Same vector
        CHECK_THAT( v, Equals( v ) );

        CHECK_THAT( empty, Equals( empty ) );

        // Different vector with same elements
        v2.push_back( 3 );
        CHECK_THAT( v, Equals( v2 ) );
    }
}

TEST_CASE( "Vector matchers that fail", "[matchers][vector][.][failing]" ) {
    std::vector<int> v;
    v.push_back( 1 );
    v.push_back( 2 );
    v.push_back( 3 );

    std::vector<int> v2;
    v2.push_back( 1 );
    v2.push_back( 2 );

    std::vector<int> empty;

    SECTION( "Contains (element)" ) {
        CHECK_THAT( v, VectorContains( -1 ) );
        CHECK_THAT( empty, VectorContains( 1 ) );
    }
    SECTION( "Contains (vector)" ) {
        CHECK_THAT( empty, Contains( v) );
        v2.push_back( 4 );
        CHECK_THAT( v, Contains( v2 ) );
    }

    SECTION( "Equals" ) {

        CHECK_THAT( v, Equals( v2 ) );
        CHECK_THAT( v2, Equals( v ) );
        CHECK_THAT( empty, Equals( v ) );
        CHECK_THAT( v, Equals( empty ) );
    }
}

#ifdef _MSC_VER
#pragma warning(disable:4702) // Unreachable code -- MSVC 19 (VS 2015) sees right through the indirection
#endif

#include <exception>

struct SpecialException : std::exception {
    SpecialException(int i):i(i) {}
    int i;
};

void doesNotThrow() {}

void throws(int i) {
    throw SpecialException{ i };
}

void throwsAsInt(int i) {
    throw i;
}

class ExceptionMatcher : public Catch::MatcherBase<SpecialException> {
    int m_expected;
public:
    ExceptionMatcher(int i):m_expected(i) {}
    bool match(SpecialException const& se) const override {
        return se.i == m_expected;
    }

    std::string describe() const override {
        std::ostringstream ss;
        ss << "special exception has value of " << m_expected;
        return ss.str();
    }
};


TEST_CASE( "Exception matchers that succeed", "[matchers][exceptions][!throws]" ) {
    CHECK_THROWS_MATCHES(throws(1), SpecialException, ExceptionMatcher{ 1 });
    REQUIRE_THROWS_MATCHES(throws(2), SpecialException, ExceptionMatcher{ 2 });
}

TEST_CASE("Exception matchers that fail", "[matchers][exceptions][!throws][.failing]") {
    SECTION("No exception") {
        CHECK_THROWS_MATCHES(doesNotThrow(), SpecialException, ExceptionMatcher{ 1 });
        REQUIRE_THROWS_MATCHES(doesNotThrow(), SpecialException, ExceptionMatcher{ 1 });
    }
    SECTION("Type mismatch") {
        CHECK_THROWS_MATCHES(throwsAsInt(1), SpecialException, ExceptionMatcher{ 1 });
        REQUIRE_THROWS_MATCHES(throwsAsInt(1), SpecialException, ExceptionMatcher{ 1 });
    }
    SECTION("Contents are wrong") {
        CHECK_THROWS_MATCHES(throws(3), SpecialException, ExceptionMatcher{ 1 });
        REQUIRE_THROWS_MATCHES(throws(4), SpecialException, ExceptionMatcher{ 1 });
    }
}

#endif // CATCH_CONFIG_DISABLE_MATCHERS