TerrainGen coverage


terraingen/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
26 / 26
100.0%
Branches:
38 / 46
82.6%
Functions:
6 / 6
100.0%

config/fields/checks.h
Line Branch Exec Source
1 /// @file
2 /// @brief JSON paths and value checks that report configuration errors at a path.
3 #pragma once
4
5 #include "terraingen/config/json.h"
6 #include "terraingen/util/checks.h"
7
8 #include <cmath>
9 #include <cstddef>
10 #include <stdexcept>
11 #include <string>
12 #include <string_view>
13
14 namespace terraingen::config {
15
16 /// The JSON path `path.key` of an object's member, or just `key` when `path` is empty.
17 28064 inline std::string Child(const std::string& path, std::string_view key) {
18
14/22
✓ Branch 1 taken 1105 times.
✓ Branch 2 taken 26959 times.
✓ Branch 4 taken 1105 times.
✓ Branch 7 taken 26959 times.
✓ Branch 10 taken 26959 times.
✓ Branch 13 taken 26959 times.
✓ Branch 15 taken 26959 times.
✓ Branch 16 taken 1105 times.
✓ Branch 18 taken 26959 times.
✓ Branch 19 taken 1105 times.
✓ Branch 21 taken 26959 times.
✓ Branch 22 taken 1105 times.
✓ Branch 23 taken 1105 times.
✓ Branch 24 taken 26959 times.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
✗ Branch 31 not taken.
✗ Branch 32 not taken.
✗ Branch 33 not taken.
✗ Branch 34 not taken.
84192 return path.empty() ? std::string(key) : path + "." + std::string(key);
19 }
20
21 /// The JSON path `path[index]` of an array's element.
22 3795 inline std::string Element(const std::string& path, std::size_t index) {
23
4/4
✓ Branch 1 taken 3795 times.
✓ Branch 4 taken 3795 times.
✓ Branch 7 taken 3795 times.
✓ Branch 10 taken 3795 times.
3795 return path + "[" + std::to_string(index) + "]";
24 }
25
26 /// Checks that a configuration value is positive and finite.
27 /// @throws ConfigError "must be positive" at `path` otherwise.
28 731 inline void RequirePositive(double value, const std::string& path) {
29
2/2
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 720 times.
731 if (!IsPositive(value)) {
30
3/3
✓ Branch 2 taken 11 times.
✓ Branch 5 taken 11 times.
✓ Branch 8 taken 11 times.
33 throw ConfigError(path, "must be positive");
31 }
32 720 }
33
34 /// Checks that a configuration value is finite and not negative.
35 /// @throws ConfigError "must not be negative" at `path` otherwise.
36 103 inline void RequireNonNegative(double value, const std::string& path) {
37
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 100 times.
103 if (!IsNonNegative(value)) {
38
3/3
✓ Branch 2 taken 3 times.
✓ Branch 5 taken 3 times.
✓ Branch 8 taken 3 times.
9 throw ConfigError(path, "must not be negative");
39 }
40 100 }
41
42 /// Checks that a configuration value is finite.
43 /// @throws ConfigError "must be finite" at `path` for an infinite or NaN `value`.
44 6 inline void RequireFinite(double value, const std::string& path) {
45
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 3 times.
6 if (!std::isfinite(value)) {
46
3/3
✓ Branch 2 taken 3 times.
✓ Branch 5 taken 3 times.
✓ Branch 8 taken 3 times.
9 throw ConfigError(path, "must be finite");
47 }
48 3 }
49
50 /// Checks that a configuration string, such as a file path, is not empty.
51 /// @throws ConfigError "must not be empty" at `path` otherwise.
52 522 inline void RequireNonEmpty(const std::string& value, const std::string& path) {
53
2/2
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 513 times.
522 if (value.empty()) {
54
3/3
✓ Branch 2 taken 9 times.
✓ Branch 5 taken 9 times.
✓ Branch 8 taken 9 times.
27 throw ConfigError(path, "must not be empty");
55 }
56 513 }
57
58 /// Runs a library check or build step `check()` and returns its result, reporting its errors at
59 /// the configuration path `path`.
60 /// @throws ConfigError at `path` with the message of a std::invalid_argument or std::domain_error
61 /// that `check` throws.
62 3510 template <typename Check> auto At(const std::string& path, Check check) {
63 try {
64 5137 return check();
65 107 } catch (const std::invalid_argument& error) {
66 150 throw ConfigError(path, error.what());
67 14 } catch (const std::domain_error& error) {
68 21 throw ConfigError(path, error.what());
69 }
70 }
71
72 } // namespace terraingen::config
73