• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

realm / realm-core / thomas.goyne_442

02 Jul 2024 07:51PM UTC coverage: 90.995% (+0.02%) from 90.974%
thomas.goyne_442

push

Evergreen

web-flow
[RCORE-2146] CAPI Remove `is_fatal` flag flip (#7751)

102372 of 180620 branches covered (56.68%)

0 of 1 new or added line in 1 file covered. (0.0%)

625 existing lines in 26 files now uncovered.

215592 of 236928 relevant lines covered (90.99%)

5608163.57 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.69
/test/object-store/util/test_utils.hpp
1
////////////////////////////////////////////////////////////////////////////
2
//
3
// Copyright 2016 Realm Inc.
4
//
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing, software
12
// distributed under the License is distributed on an "AS IS" BASIS,
13
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
// See the License for the specific language governing permissions and
15
// limitations under the License.
16
//
17
////////////////////////////////////////////////////////////////////////////
18

19
#ifndef REALM_TEST_UTILS_HPP
20
#define REALM_TEST_UTILS_HPP
21

22
#include <catch2/catch_all.hpp>
23
#include <catch2/matchers/catch_matchers_all.hpp>
24
#include <realm/util/file.hpp>
25
#include <realm/util/optional.hpp>
26

27
#include <functional>
28
#include <filesystem>
29
#include <mutex>
30
#include <condition_variable>
31
namespace fs = std::filesystem;
32

33
namespace realm {
34
template <typename E>
35
class TestingStateMachine {
36
public:
37
    explicit TestingStateMachine(E initial_state)
38
        : m_cur_state(initial_state)
13✔
39
    {
26✔
40
    }
26✔
41

42
    E get()
43
    {
240✔
44
        std::lock_guard lock{m_mutex};
240✔
45
        return m_cur_state;
240✔
46
    }
240✔
47

48
    void transition_to(E new_state)
49
    {
40✔
50
        {
40✔
51
            std::lock_guard lock{m_mutex};
40✔
52
            m_cur_state = new_state;
40✔
53
        }
40✔
54
        m_cv.notify_one();
40✔
55
    }
40✔
56

57
    template <typename Func>
58
    void transition_with(Func&& func)
59
    {
586✔
60
        {
586✔
61
            std::lock_guard lock{m_mutex};
586✔
62
            std::optional<E> new_state = func(m_cur_state);
586✔
63
            if (!new_state) {
586✔
64
                return;
441✔
65
            }
441✔
66
            m_cur_state = *new_state;
145✔
67
        }
145✔
68
        m_cv.notify_one();
×
69
    }
145✔
70

71
    bool wait_for(E target, std::chrono::milliseconds period = std::chrono::seconds(15))
72
    {
73
        std::unique_lock lock{m_mutex};
74
        return m_cv.wait_for(lock, period, [&] {
50✔
75
            return m_cur_state == target;
50✔
76
        });
100✔
77
    }
100✔
78

100✔
79
private:
50✔
80
    std::mutex m_mutex;
81
    std::condition_variable m_cv;
82
    E m_cur_state;
83
};
38✔
84

38✔
85
template <typename MessageMatcher>
88✔
86
class ExceptionMatcher final : public Catch::Matchers::MatcherBase<Exception> {
88✔
87
public:
88✔
88
    ExceptionMatcher(ErrorCodes::Error code, MessageMatcher&& matcher)
38✔
89
        : m_code(code)
38✔
90
        , m_matcher(std::move(matcher))
91
    {
92
    }
93

94
    bool match(Exception const& ex) const override
95
    {
96
        return ex.code() == m_code && m_matcher.match(ex.what());
97
    }
98

99
    std::string describe() const override
100
    {
101
        return util::format("Exception(%1, \"%2\")", ErrorCodes::error_string(m_code), m_matcher.describe());
94✔
102
    }
94✔
103

188✔
104
private:
188✔
105
    ErrorCodes::Error m_code;
106
    MessageMatcher m_matcher;
107
};
188✔
108

188✔
109
template <>
188✔
110
class ExceptionMatcher<void> final : public Catch::Matchers::MatcherBase<Exception> {
111
public:
UNCOV
112
    ExceptionMatcher(ErrorCodes::Error code, std::string_view msg)
×
UNCOV
113
        : m_code(code)
×
UNCOV
114
        , m_message(msg)
×
115
    {
116
    }
117

118
    bool match(Exception const& ex) const override;
119
    std::string describe() const override;
120

121
private:
122
    ErrorCodes::Error m_code;
123
    std::string m_message;
124
};
125

1,304✔
126
class OutOfBoundsMatcher final : public Catch::Matchers::MatcherBase<OutOfBounds> {
1,304✔
127
public:
2,613✔
128
    OutOfBoundsMatcher(size_t index, size_t size, std::string_view msg)
2,613✔
129
        : m_index(index)
130
        , m_size(size)
131
        , m_message(msg)
132
    {
133
    }
134

135
    bool match(OutOfBounds const& ex) const override;
136
    std::string describe() const override;
137

138
private:
139
    size_t m_index, m_size;
140
    std::string m_message;
141
};
105✔
142

105✔
143
class LogicErrorMatcher final : public Catch::Matchers::MatcherBase<LogicError> {
105✔
144
public:
210✔
145
    LogicErrorMatcher(ErrorCodes::Error code)
210✔
146
        : m_code(code)
147
    {
148
    }
149

150
    bool match(LogicError const& ex) const override;
151
    std::string describe() const override;
152

153
private:
154
    ErrorCodes::Error m_code;
155
};
156

157
namespace _impl {
158
template <typename T>
7✔
159
ExceptionMatcher<T> make_exception_matcher(ErrorCodes::Error code, T&& matcher)
14✔
160
{
14✔
161
    return ExceptionMatcher<T>(code, std::move(matcher));
162
}
163
inline ExceptionMatcher<void> make_exception_matcher(ErrorCodes::Error code, const char* msg)
164
{
165
    return ExceptionMatcher<void>(code, msg);
166
}
167
inline ExceptionMatcher<void> make_exception_matcher(ErrorCodes::Error code, std::string_view msg)
168
{
169
    return ExceptionMatcher<void>(code, msg);
170
}
171
inline ExceptionMatcher<void> make_exception_matcher(ErrorCodes::Error code, const std::string& msg)
172
{
188✔
173
    return ExceptionMatcher<void>(code, msg);
188✔
174
}
188✔
175
inline ExceptionMatcher<void> make_exception_matcher(ErrorCodes::Error code, std::string&& msg)
176
{
1,203✔
177
    return ExceptionMatcher<void>(code, msg);
1,203✔
178
}
1,203✔
179
} // namespace _impl
UNCOV
180

×
UNCOV
181
std::ostream& operator<<(std::ostream&, const Exception&);
×
UNCOV
182

×
183
class Realm;
UNCOV
184
/// Open a Realm at a given path, creating its files.
×
UNCOV
185
bool create_dummy_realm(std::string path, std::shared_ptr<Realm>* out = nullptr);
×
UNCOV
186
std::vector<char> make_test_encryption_key(const char start = 0);
×
187
void catch2_ensure_section_run_workaround(bool did_run_a_section, std::string section_name,
188
                                          util::FunctionRef<void()> func);
1,410✔
189

1,410✔
190
std::string encode_fake_jwt(const std::string& in, util::Optional<int64_t> exp = {},
1,410✔
191
                            util::Optional<int64_t> iat = {});
192

193
std::string random_string(std::string::size_type length);
194
int64_t random_int();
195

196
bool chmod_supported(const std::string& path);
197
int get_permissions(const std::string& path);
198
void chmod(const std::string& path, int permissions);
199

200
} // namespace realm
201

202
#define REQUIRE_DIR_EXISTS(macro_path)                                                                               \
203
    do {                                                                                                             \
204
        CHECK(util::File::is_dir(macro_path) == true);                                                               \
205
    } while (0)
206

207
#define REQUIRE_DIR_PATH_EXISTS(macro_path)                                                                          \
208
    do {                                                                                                             \
209
        REQUIRE(util::File::is_dir((macro_path).string()));                                                          \
210
    } while (0)
211

212
#define REQUIRE_DIR_DOES_NOT_EXIST(macro_path)                                                                       \
213
    do {                                                                                                             \
214
        CHECK(util::File::exists(macro_path) == false);                                                              \
215
    } while (0)
14✔
216

14✔
217
#define REQUIRE_DIR_PATH_DOES_NOT_EXIST(macro_path)                                                                  \
14✔
218
    do {                                                                                                             \
219
        REQUIRE_FALSE(util::File::exists((macro_path).string()));                                                    \
220
    } while (0)
28✔
221

28✔
222
#define REQUIRE_REALM_EXISTS(macro_path)                                                                             \
28✔
223
    do {                                                                                                             \
224
        REQUIRE(util::File::exists(macro_path));                                                                     \
225
        REQUIRE(util::File::exists((macro_path) + ".lock"));                                                         \
2✔
226
        REQUIRE_DIR_EXISTS((macro_path) + ".management");                                                            \
2✔
227
    } while (0)
2✔
228

229
#define REQUIRE_REALM_DOES_NOT_EXIST(macro_path)                                                                     \
230
    do {                                                                                                             \
26✔
231
        REQUIRE(!util::File::exists(macro_path));                                                                    \
26✔
232
        REQUIRE(!util::File::exists((macro_path) + ".lock"));                                                        \
26✔
233
        REQUIRE_DIR_DOES_NOT_EXIST((macro_path) + ".management");                                                    \
234
    } while (0)
235

12✔
236
#define REQUIRE_THROWS_CONTAINING(expr, msg) REQUIRE_THROWS_WITH(expr, Catch::Matchers::ContainsSubstring(msg))
12✔
237

12✔
238
#define REQUIRE_EXCEPTION(expr, c, msg)                                                                              \
12✔
239
    REQUIRE_THROWS_MATCHES(expr, realm::Exception, _impl::make_exception_matcher(realm::ErrorCodes::c, msg))
12✔
240
#define REQUIRE_THROWS_OUT_OF_BOUNDS(expr, index, size, msg)                                                         \
241
    REQUIRE_THROWS_MATCHES(expr, OutOfBounds, OutOfBoundsMatcher(index, size, msg));
242
#define REQUIRE_THROW_LOGIC_ERROR_WITH_CODE(expr, err)                                                               \
243
    REQUIRE_THROWS_MATCHES(expr, LogicError, LogicErrorMatcher(err))
244

245
#define ENCODE_FAKE_JWT(in) realm::encode_fake_jwt(in)
246

247
#endif // REALM_TEST_UTILS_HPP
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc