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

afxres / binary-cxx / 18196701325

02 Oct 2025 02:50PM UTC coverage: 99.248% (-0.06%) from 99.312%
18196701325

push

github

afxres
Add token type and unit tests

58 of 59 new or added lines in 2 files covered. (98.31%)

924 of 931 relevant lines covered (99.25%)

120.67 hits per line

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

98.18
/code/binary/src/Token.cpp
1
#include "binary/Token.hpp"
2

3
#include <cassert>
4
#include <format>
5
#include <stdexcept>
6

7
#include "binary/ConverterExtensions.hpp"
8

9
namespace binary {
10
Token::Token(const IGenerator& generator, const std::span<const std::byte>& span) noexcept
11✔
11
    : generator(generator), span(span), initialized(span.empty()) {}
11✔
12

13
typename Token::TokenValue Token::DecodeTokens() const noexcept {
10✔
14
    try {
15
        assert(this->initialized == false);
10✔
16
        assert(this->self.lock().get() == this);
10✔
17
        std::unordered_map<std::string, std::shared_ptr<Token>> tokens;
10✔
18
        std::span<const std::byte> copy = this->span;
10✔
19
        const auto& decode = this->decode;
10✔
20
        while (!copy.empty()) {
17✔
21
            auto header = ::binary::DecodeWithLengthPrefix(copy);
14✔
22
            auto buffer = ::binary::DecodeWithLengthPrefix(copy);
9✔
23
            auto source = decode(header);
7✔
24
            auto result = std::shared_ptr<Token>(new Token(this->generator, buffer));
7✔
25
            result->self = result;
7✔
26
            result->parent = this->self;
7✔
27
            result->decode = this->decode;
7✔
28
            tokens.emplace(std::move(source), std::move(result));
7✔
29
        }
7✔
30
        return {tokens, nullptr};
3✔
31
    } catch (...) {
17✔
32
        return {std::unordered_map<std::string, std::shared_ptr<Token>>{}, std::current_exception()};
7✔
33
    }
7✔
34
}
35

36
const typename Token::TokenValue& Token::GetTokens() const noexcept {
23✔
37
    if (this->initialized == false) {
23✔
38
        std::lock_guard locker(this->mutex);
10✔
39
        if (this->initialized == false) {
10✔
40
            this->intent = DecodeTokens();
10✔
41
            this->initialized = true;
10✔
42
        }
43
    }
10✔
44
    assert(this->initialized == true);
23✔
45
    assert(this->self.lock().get() == this);
23✔
46
    return this->intent;
23✔
47
}
48

49
const std::unordered_map<std::string, std::shared_ptr<Token>>& Token::Children() const {
11✔
50
    return std::get<0>(GetTokens());
11✔
51
}
52

53
const std::shared_ptr<Token>& Token::At(const std::string& key) const {
12✔
54
    const auto& values = GetTokens();
12✔
55
    const auto& tokens = std::get<0>(values);
12✔
56
    auto it = tokens.find(key);
12✔
57
    if (it != tokens.end()) {
12✔
58
        return it->second;
14✔
59
    }
60
    const auto& exception = std::get<1>(values);
5✔
61
    std::string message = std::format("key '{}' not found", key);
5✔
62
    if (exception != nullptr) {
5✔
63
        try {
64
            std::rethrow_exception(exception);
8✔
65
        } catch (...) {
4✔
66
            std::throw_with_nested(std::invalid_argument(message));
8✔
67
        }
4✔
68
    }
69
    throw std::invalid_argument(message);
1✔
70
}
5✔
71

72
std::shared_ptr<Token> Token::Create(const IGenerator& generator, const std::span<const std::byte>& span) {
4✔
73
    auto result = std::shared_ptr<Token>(new Token(generator, span));
4✔
74
    result->self = result;
4✔
75
    result->decode = [converter = ::binary::GetConverter<std::string>(generator)](const std::span<const std::byte>& span) { return converter->Decode(span); };
11✔
76
    return result;
4✔
NEW
77
}
×
78
}
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