aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/tests/issue/issue_test.cpp
blob: 4799e032da2c2fbab05b818146ca3789a9b88e9b (plain) (blame)
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vespalib/util/issue.h>
#include <vespa/vespalib/gtest/gtest.h>

using namespace vespalib;

struct MyHandler : Issue::Handler {
    std::vector<vespalib::string> list;
    void handle(const Issue &issue) override {
        list.push_back(issue.message());
    }
};

struct MyException : std::exception {
    vespalib::string my_what;
    MyException(vespalib::string what_in) : my_what(what_in) {}
    const char *what() const noexcept override { return my_what.c_str(); }
};

std::vector<vespalib::string> make_list(std::vector<vespalib::string> list) {
    return list;
}

TEST(IssueTest, log_issue_not_captured) {
    Issue::report(Issue("this should be logged"));
}

TEST(IssueTest, capture_an_issue) {
    MyHandler my_handler;
    {
        Issue::report(Issue("this should be logged"));
        Issue::Binding my_binding = Issue::listen(my_handler);
        Issue::report(Issue("this should be captured"));
    }
    Issue::report(Issue("this should also be logged"));
    EXPECT_EQ(my_handler.list, make_list({"this should be captured"}));
}

TEST(IssueTest, capture_issues_with_nested_bindings) {
    MyHandler my_handler1;
    MyHandler my_handler2;
    {
        Issue::report(Issue("this should be logged"));
        auto my_binding1 = Issue::listen(my_handler1);
        Issue::report(Issue("issue1"));
        {
            auto my_binding2 = Issue::listen(my_handler2);
            Issue::report(Issue("issue2"));
        }
        Issue::report(Issue("issue3"));
    }
    Issue::report(Issue("this should also be logged"));
    EXPECT_EQ(my_handler1.list, make_list({"issue1", "issue3"}));
    EXPECT_EQ(my_handler2.list, make_list({"issue2"}));
}

TEST(IssueTest, handler_can_be_bound_multiple_times) {
    MyHandler my_handler;
    {
        Issue::report(Issue("this should be logged"));
        auto my_binding1 = Issue::listen(my_handler);
        Issue::report(Issue("issue1"));
        {
            auto my_binding2 = Issue::listen(my_handler);
            Issue::report(Issue("issue2"));
        }
        Issue::report(Issue("issue3"));
    }
    Issue::report(Issue("this should also be logged"));
    EXPECT_EQ(my_handler.list, make_list({"issue1", "issue2", "issue3"}));
}

TEST(IssueTest, alternative_report_functions) {
    MyHandler my_handler;
    auto capture = Issue::listen(my_handler);
    Issue::report(vespalib::string("str"));
    Issue::report("fmt_%s_%d", "msg", 7);
    try {
        throw MyException("exception");
    } catch (const std::exception &e) {
        Issue::report(e);
    }
    EXPECT_EQ(my_handler.list, make_list({"str", "fmt_msg_7", "exception"}));
}

GTEST_MAIN_RUN_ALL_TESTS()