Skip to content
This repository was archived by the owner on Jan 26, 2024. It is now read-only.

Commit d901cf0

Browse files
authored
Ensure that assert is not ignored for tests. (#41)
1 parent f645d31 commit d901cf0

File tree

6 files changed

+43
-26
lines changed

6 files changed

+43
-26
lines changed

test/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ TEST_NAMES = [
77

88
[cc_test(
99
name = test_name,
10-
srcs = [test_name + ".cpp"],
10+
srcs = [test_name + ".cpp", "assert.h"],
1111
deps = ["//:opentracing"],
1212
) for test_name in TEST_NAMES]

test/assert.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
3+
#include <iostream>
4+
#include <cstdlib>
5+
6+
#define ASSERT(EXPR) \
7+
do { \
8+
if (!(EXPR)) { \
9+
std::cerr << "Failed in " << __FILE__ << ":" << __LINE__ << ": \"" \
10+
<< #EXPR "\" evaluted to false\n"; \
11+
std::exit(-1); \
12+
} \
13+
} while (false);

test/string_view_test.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
1+
#include "assert.h"
2+
13
#include <opentracing/string_view.h> // test include guard
24
#include <opentracing/string_view.h>
3-
#include <cassert>
45

56
using namespace opentracing;
67

78
static void test_empty() {
89
string_view ref;
9-
assert(0 == ref.data());
10-
assert(0 == ref.length());
10+
ASSERT(0 == ref.data());
11+
ASSERT(0 == ref.length());
1112
}
1213

1314
static void test_cstring() {
1415
const char* val = "hello world";
1516

1617
string_view ref(val);
1718

18-
assert(val == ref.data());
19-
assert(std::strlen(val) == ref.length());
19+
ASSERT(val == ref.data());
20+
ASSERT(std::strlen(val) == ref.length());
2021
}
2122

2223
static void test_std_string() {
2324
const std::string val = "hello world";
2425

2526
string_view ref(val);
2627

27-
assert(val == ref.data());
28-
assert(val.length() == ref.length());
28+
ASSERT(val == ref.data());
29+
ASSERT(val.length() == ref.length());
2930
}
3031

3132
static void test_copy() {
@@ -34,8 +35,8 @@ static void test_copy() {
3435
string_view ref(val);
3536
string_view cpy(ref);
3637

37-
assert(val == cpy.data());
38-
assert(val.length() == cpy.length());
38+
ASSERT(val == cpy.data());
39+
ASSERT(val.length() == cpy.length());
3940
}
4041

4142
int main() {

test/tracer_test.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1+
#include "assert.h"
2+
13
#include <opentracing/noop.h>
24
#include <opentracing/tracer.h>
3-
#include <cassert>
45
using namespace opentracing;
56

67
static void test_tracer_interface() {
78
auto tracer = MakeNoopTracer();
89

910
auto span1 = tracer->StartSpan("a");
10-
assert(span1);
11-
assert(&span1->tracer() == tracer.get());
11+
ASSERT(span1);
12+
ASSERT(&span1->tracer() == tracer.get());
1213

1314
auto span2 = tracer->StartSpan("b", {ChildOf(&span1->context())});
14-
assert(span2);
15+
ASSERT(span2);
1516
span2->SetOperationName("b1");
1617
span2->SetTag("x", true);
17-
assert(span2->BaggageItem("y").empty());
18+
ASSERT(span2->BaggageItem("y").empty());
1819
span2->Log({{"event", "xyz"}, {"abc", 123}});
1920
span2->Finish();
2021
}
@@ -24,7 +25,7 @@ static void test_start_span_options() {
2425

2526
// A reference to null a SpanContext is ignored.
2627
ChildOf(nullptr).Apply(options);
27-
assert(options.references.size() == 0);
28+
ASSERT(options.references.size() == 0);
2829
}
2930

3031
int main() {

test/util_test.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
#include "assert.h"
2+
13
#include <opentracing/util.h>
2-
#include <cassert>
34
#include <cmath>
45
#include <cstdlib> // Work around to https://stackoverflow.com/a/30084734.
56
using namespace opentracing;
@@ -13,7 +14,7 @@ static void test_convert_time_point() {
1314
auto t3 = convert_time_point<SystemClock>(t2);
1415
auto difference = std::abs(
1516
std::chrono::duration_cast<std::chrono::microseconds>(t3 - t1).count());
16-
assert(difference < 100);
17+
ASSERT(difference < 100);
1718
}
1819

1920
int main() {

test/value_test.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
1+
#include "assert.h"
2+
13
#include <opentracing/value.h>
2-
#include <cassert>
34
using namespace opentracing;
45

56
static void test_implicit_construction() {
67
Value v1(123);
7-
assert(v1.is<int64_t>());
8+
ASSERT(v1.is<int64_t>());
89

910
Value v2(123u);
10-
assert(v2.is<uint64_t>());
11+
ASSERT(v2.is<uint64_t>());
1112

1213
Value v3(true);
13-
assert(v3.is<bool>());
14+
ASSERT(v3.is<bool>());
1415

1516
Value v4(1.0);
16-
assert(v4.is<double>());
17+
ASSERT(v4.is<double>());
1718
Value v5(1.0f);
18-
assert(v5.is<double>());
19+
ASSERT(v5.is<double>());
1920

2021
Value v6(std::string("abc"));
21-
assert(v6.is<std::string>());
22+
ASSERT(v6.is<std::string>());
2223

2324
Value v7("abc");
24-
assert(v7.is<const char*>());
25+
ASSERT(v7.is<const char*>());
2526

2627
Value v8(Values{Value(1), Value(2)});
2728
(void)v8;

0 commit comments

Comments
 (0)