47 lines
1.5 KiB
C++
47 lines
1.5 KiB
C++
#include <msgpack/token.h>
|
|
|
|
#include <boost/ut.hpp>
|
|
|
|
using namespace boost::ut;
|
|
|
|
namespace {
|
|
template <typename... Bytes>
|
|
constexpr std::array<std::byte, sizeof...(Bytes)> make_bytes(Bytes &&...bytes) {
|
|
return {std::byte(std::forward<Bytes>(bytes))...};
|
|
}
|
|
|
|
template <typename First, typename... Others>
|
|
constexpr bool wrong_types(auto const& obj) {
|
|
auto err = tl::make_unexpected(msgpack::error::wrong_type);
|
|
if (obj.template get<First>() != err) return false;
|
|
if constexpr (sizeof...(Others)) {
|
|
return wrong_types<Others...>(obj);
|
|
} else {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
template <typename T, typename E>
|
|
std::ostream &operator<<(std::ostream &os, tl::expected<T, E> const &exp) {
|
|
if (exp.has_value()) {
|
|
os << "Value: '" << *exp << "'";
|
|
} else {
|
|
os << "Error";
|
|
}
|
|
return os;
|
|
}
|
|
}
|
|
|
|
suite reader = [] {
|
|
"construction"_test = [] {
|
|
constexpr auto payload = make_bytes(0xce, 0x01, 0x02, 0x03, 0x09, 0xce);
|
|
msgpack::token_reader reader(payload);
|
|
auto token = reader.read_one();
|
|
expect(token && token->type() == msgpack::format::type::unsigned_int);
|
|
expect(token->get<std::uint8_t>() == tl::make_unexpected(msgpack::error::will_truncate));
|
|
expect(token->get<std::uint32_t>() == 0x01020309);
|
|
token = reader.read_one();
|
|
expect(token == tl::make_unexpected(msgpack::error::incomplete_message));
|
|
};
|
|
};
|