File: error.cpp

package info (click to toggle)
actor-framework 0.18.7-1~exp1
  • links: PTS
  • area: main
  • in suites: experimental
  • size: 8,740 kB
  • sloc: cpp: 85,162; sh: 491; python: 187; makefile: 11
file content (58 lines) | stat: -rw-r--r-- 1,962 bytes parent folder | download
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
// This file is part of CAF, the C++ Actor Framework. See the file LICENSE in
// the main distribution directory for license terms and copyright or visit
// https://github.com/actor-framework/actor-framework/blob/master/LICENSE.

#define CAF_SUITE error

#include "caf/error.hpp"

#include "core-test.hpp"

using namespace caf;

CAF_TEST(default constructed errors evaluate to false) {
  error err;
  CHECK(!err);
}

CAF_TEST(error code zero is not an error) {
  CHECK(!error(sec::none));
  CHECK(!make_error(sec::none));
  CHECK(!error{error_code<sec>(sec::none)});
}

CAF_TEST(error codes that are not zero are errors) {
  CHECK(error(sec::unexpected_message));
  CHECK(make_error(sec::unexpected_message));
  CHECK(error{error_code<sec>(sec::unexpected_message)});
}

CAF_TEST(errors convert enums to their integer value) {
  CHECK_EQ(make_error(sec::unexpected_message).code(), 1u);
  CHECK_EQ(error{error_code<sec>(sec::unexpected_message)}.code(), 1u);
}

SCENARIO("errors provide human-readable to_string output") {
  auto err_str = [](auto... xs) {
    return to_string(make_error(std::move(xs)...));
  };
  GIVEN("an error object") {
    WHEN("converting an error without context to a string") {
      THEN("the output is only the error code") {
        CHECK_EQ(err_str(sec::invalid_argument), "caf::sec::invalid_argument");
      }
    }
    WHEN("converting an error with a context containing one element") {
      THEN("the output is the error code plus the context") {
        CHECK_EQ(err_str(sec::invalid_argument, "foo is not bar"),
                 R"_(caf::sec::invalid_argument("foo is not bar"))_");
      }
    }
    WHEN("converting an error with a context containing two or more elements") {
      THEN("the output is the error code plus all elements in the context") {
        CHECK_EQ(err_str(sec::invalid_argument, "want foo", "got bar"),
                 R"_(caf::sec::invalid_argument("want foo", "got bar"))_");
      }
    }
  }
}