userver: userver/dump/test_helpers.hpp Source File
Loading...
Searching...
No Matches
test_helpers.hpp
1#pragma once
2
3#include <utility>
4
5#include <userver/utest/utest.hpp>
6#include <userver/utils/fast_scope_guard.hpp>
7
8#include <userver/dump/operations_mock.hpp>
9
10USERVER_NAMESPACE_BEGIN
11
12namespace dump {
13
14/// Converts to binary using `Write(Writer&, const T&)`
15template <typename T>
16std::string ToBinary(const T& value) {
17 MockWriter writer;
18 writer.Write(value);
19 return std::move(writer).Extract();
20}
21
22/// Converts from binary using `Read(Reader&, To<T>)`
23template <typename T>
24T FromBinary(std::string data) {
25 MockReader reader(std::move(data));
26 T value = reader.Read<T>();
27 reader.Finish();
28 return value;
29}
30
31/// Write a value to a cache dump and immediately read it back. If `Write` and
32/// `Read` are implemented correctly, the result should be equal to the original
33/// value.
34template <typename T>
35void TestWriteReadCycle(const T& original) {
36 MockReader reader(dump::ToBinary(original));
37 T after_cycle = reader.Read<T>();
38 reader.Finish();
39 EXPECT_EQ(after_cycle, original);
40}
41
42} // namespace dump
43
44USERVER_NAMESPACE_END