summaryrefslogtreecommitdiff
path: root/modules/tools/tests/c_iface.cpp
blob: 01f1106a5cb6a150f180bfdb3695582322d937e1 (plain)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <forstio/test/suite.hpp>
#include <forstio/codec/json/json.hpp>

#include "../c++/c_gen_iface.hpp"

#include <iostream>

namespace {
namespace schema {
using namespace saw::schema;

using TestStruct = Struct<
	Member<Int32, "a">,
	Member<Float64, "b">
>;

using TestEmptyInterface = Interface<>;

using TestOneFunctionInterface = Interface<
	Member<Function<Int32, Int32>, "one">
>;

using TestStructFunctionInterface = Interface<
	Member<Function<TestStruct, Int32>, "two">
>;
}

template<typename Schema>
void test_generate(std::string& res){
	using namespace saw;
	
	data<schema::CIface> c_iface;
	{
		auto eov = generate_c_interface<Schema>(c_iface);
		SAW_EXPECT(eov.is_value(), "Couldn't generate interface info");
	}

	data<schema::CIface, encode::Json> j_data;
	codec_config<encode::Json> j_config;
	j_config.pretty = true;
	codec<schema::CIface, encode::Json> j_codec{j_config};

	{
		auto eov = j_codec.encode(c_iface, j_data);
		SAW_EXPECT(eov.is_value(), "Couldn't encode data");
	}

	res = convert_to_string(j_data.get_buffer());
}

SAW_TEST("CIface Empty Interface"){
	using namespace saw;

	std::string res;
	test_generate<schema::TestEmptyInterface>(res);

	std::cout<<"\n"<<res<<"\n"<<std::endl;
}

SAW_TEST("CIface One Function Interface"){
	using namespace saw;

	std::string res;
	test_generate<schema::TestOneFunctionInterface>(res);
	std::cout<<"\n"<<res<<"\n"<<std::endl;
}

SAW_TEST("CIface Struct Function Interface"){
	using namespace saw;

	std::string res;
	test_generate<schema::TestStructFunctionInterface>(res);
	std::cout<<"\n"<<res<<"\n"<<std::endl;
}
}