File: write_and_read.hpp

package info (click to toggle)
reflect-cpp 0.18.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 12,524 kB
  • sloc: cpp: 44,484; python: 131; makefile: 30; sh: 3
file content (53 lines) | stat: -rw-r--r-- 1,727 bytes parent folder | download | duplicates (4)
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
#ifndef WRITE_AND_READ_
#define WRITE_AND_READ_

#include <gtest/gtest.h>

#include <iostream>
#include <rfl/json.hpp>
#include <string>
#include <type_traits>

template <class... Ps>
auto write(const auto& _struct, const std::string& _expected) {
  const auto json_string1 = rfl::json::write<Ps...>(_struct);
  EXPECT_EQ(json_string1, _expected)
      << "Test failed on write. Expected:" << std::endl
      << _expected << std::endl
      << "Got: " << std::endl
      << json_string1 << std::endl
      << std::endl;
  return json_string1;
}

template <class... Ps>
void read(const std::string& _json, const auto& _expected) {
  using T = std::remove_cvref_t<decltype(_expected)>;
  const auto res = rfl::json::read<T, Ps...>(_json);
  EXPECT_TRUE(res && true) << "Test failed on read. Error: "
                           << res.error().what();
}

template <class... Ps>
void write_and_read(const auto& _struct, const std::string& _expected) {
  using T = std::remove_cvref_t<decltype(_struct)>;
  const auto json_string1 = rfl::json::write<Ps...>(_struct);
  EXPECT_EQ(json_string1, _expected)
      << "Test failed on write. Expected:" << std::endl
      << _expected << std::endl
      << "Got: " << std::endl
      << json_string1 << std::endl
      << std::endl;
  const auto res = rfl::json::read<T, Ps...>(json_string1);
  EXPECT_TRUE(res && true) << "Test failed on read. Error: "
                           << res.error().what();
  const auto json_string2 = rfl::json::write<Ps...>(res.value());
  EXPECT_EQ(json_string2, _expected)
      << "Test failed on read. Expected:" << std::endl
      << _expected << std::endl
      << "Got: " << std::endl
      << json_string2 << std::endl
      << std::endl;
}

#endif