Debian Codegraph @

tests/include/catch_extra.hpp

raw
1 #ifndef CATCH_EXTRA_HPP
2 #define CATCH_EXTRA_HPP
3
4 /*
5 * Licensed to the Apache Software Foundation (ASF) under one
6 * or more contributor license agreements. See the NOTICE file
7 * distributed with this work for additional information
8 * regarding copyright ownership. The ASF licenses this file
9 * to you under the Apache License, Version 2.0 (the
10 * "License"); you may not use this file except in compliance
11 * with the License. You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing,
16 * software distributed under the License is distributed on an
17 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 * KIND, either express or implied. See the License for the
19 * specific language governing permissions and limitations
20 * under the License.
21 */
22
23 // Extensions to the Catch2 framework
24
25 #include <catch.hpp>
26 #include <utility>
27
28 namespace Catch {
29 namespace Matchers {
30
31 // Matcher for NUL terminated C-strings.
32
33 /* Example:
34 const char *x, *y
35
36 CHECK(x == y); // POINTER equality, probably not what you want.
37
38 using Catch::Matchers::Equals;
39 CHECK_THAT(x, Equals(y)); // String equality, like strcmp()
40 */
41
42 namespace CString {
43
44 struct CStringMatcherBase : MatcherBase<const char *> {
45 const std::string m_operation;
46 const char *m_comparator;
47
48 CStringMatcherBase(std::string operation, const char *comparator)
49 : m_operation(std::move(operation)), m_comparator(comparator) {}
50 std::string describe() const override {
51 return m_operation + ": " + Catch::Detail::stringify(m_comparator);
52 }
53 };
54
55 struct EqualsMatcher : CStringMatcherBase {
56 explicit EqualsMatcher(const char *comparator)
57 : CStringMatcherBase("equals", comparator) {}
58 bool match(const char * const &source) const override {
59 // match if both null or both non-null and equal
60 return source == m_comparator ||
61 (source && m_comparator &&
62 std::string(source) == std::string(m_comparator));
63 }
64 };
65
66 struct ContainsMatcher : CStringMatcherBase {
67 explicit ContainsMatcher(const char *comparator)
68 : CStringMatcherBase("contains", comparator) {}
69 virtual bool match(const char * const &source) const override {
70 return source && m_comparator && Catch::contains(source, m_comparator);
71 }
72 };
73
74 struct StartsWithMatcher : CStringMatcherBase {
75 explicit StartsWithMatcher(const char *comparator)
76 : CStringMatcherBase("starts with", comparator) {}
77 bool match(const char * const &source) const override {
78 return source && m_comparator && Catch::startsWith(source, m_comparator);
79 }
80 };
81
82 struct EndsWithMatcher : CStringMatcherBase {
83 explicit EndsWithMatcher(const char *comparator)
84 : CStringMatcherBase("ends with", comparator) {}
85 bool match(const char *const &source) const override {
86 return source && m_comparator && Catch::endsWith(source, m_comparator);
87 }
88 };
89
90 } // namespace CString
91
92 inline CString::EqualsMatcher Equals(const char *str) {
93 return CString::EqualsMatcher(str);
94 }
95 inline CString::ContainsMatcher Contains(const char *str) {
96 return CString::ContainsMatcher(str);
97 }
98 inline CString::StartsWithMatcher StartsWith(const char *str) {
99 return CString::StartsWithMatcher(str);
100 }
101 inline CString::EndsWithMatcher EndsWith(const char *str) {
102 return CString::EndsWithMatcher(str);
103 }
104
105 } // namespace Matchers
106 } // namespace Catch
107
108 #endif // CATCH_EXTRA_HPP