1 #include <msp/datafile/except.h>
2 #include <msp/datafile/loader.h>
3 #include <msp/datafile/objectloader.h>
4 #include <msp/io/memory.h>
5 #include <msp/test/test.h>
10 class LoaderTests: public Test::RegisteredTest<LoaderTests>
15 static const char *get_name() { return "Loader"; }
28 class Loader: public DataFile::ObjectLoader<TestObject>
45 LoaderTests::LoaderTests()
47 add(&LoaderTests::load, "Load");
48 add(&LoaderTests::int_as_float, "Int -> float");
49 add(&LoaderTests::overload, "Overload resolution");
50 add(&LoaderTests::bad_keyword, "Bad keyword").expect_throw<DataFile::data_error>();
51 add(&LoaderTests::bad_signature, "Bad signature").expect_throw<DataFile::data_error>();
54 void LoaderTests::load()
63 IO::Memory mem(input, sizeof(input)-1);
64 DataFile::Parser parser(mem, "-");
65 TestObject::Loader loader(obj);
68 EXPECT_EQUAL(obj.i, 1);
69 EXPECT_EQUAL(obj.f, 2.5);
70 EXPECT_EQUAL(obj.s, "foo");
71 EXPECT_EQUAL(obj.func_called, true);
74 void LoaderTests::int_as_float()
80 IO::Memory mem(input, sizeof(input)-1);
81 DataFile::Parser parser(mem, "-");
82 TestObject::Loader loader(obj);
85 EXPECT_EQUAL(obj.f, 2);
88 void LoaderTests::overload()
95 IO::Memory mem(input, sizeof(input)-1);
96 DataFile::Parser parser(mem, "-");
97 TestObject::Loader loader(obj);
100 EXPECT_EQUAL(obj.f, 1.0f);
101 EXPECT_EQUAL(obj.i, 2);
104 void LoaderTests::bad_keyword()
110 IO::Memory mem(input, sizeof(input)-1);
111 DataFile::Parser parser(mem, "-");
112 TestObject::Loader loader(obj);
116 void LoaderTests::bad_signature()
122 IO::Memory mem(input, sizeof(input)-1);
123 DataFile::Parser parser(mem, "-");
124 TestObject::Loader loader(obj);
128 TestObject::TestObject():
134 TestObject::Loader::Loader(TestObject &o):
135 DataFile::ObjectLoader<TestObject>(o)
137 add("i", &TestObject::i);
138 add("f", &TestObject::f);
139 add("o", &TestObject::i);
140 add("o", &TestObject::f);
141 add("s", &TestObject::s);
142 add("c", &Loader::func);
145 void TestObject::Loader::func()
147 obj.func_called = true;