]> git.stg.codes - stg.git/blob - tests/test_conffiles.cpp
Port to CMake, get rid of os_int.h.
[stg.git] / tests / test_conffiles.cpp
1 #include <unistd.h> // unlink
2
3 #include <string>
4 #include <fstream>
5
6 #include "tut/tut.hpp"
7
8 #include "stg/conffiles.h"
9
10 namespace tut
11 {
12     struct conffile_data {
13     };
14
15     typedef test_group<conffile_data> tg;
16     tg conffile_test_group("CONIGFILE tests group");
17
18     typedef tg::object testobject;
19
20     template<>
21     template<>
22     void testobject::test<1>()
23     {
24         set_test_name("Check read/write");
25
26         {
27             CONFIGFILE cf("/tmp/test.cf", true);
28
29             ensure_equals("Correct construction", cf.Error(), 0);
30
31             cf.WriteString("a", "a-string");
32             cf.WriteInt("b", 0);
33             cf.WriteDouble("e", 2.718281828);
34
35             ensure_equals("Correct data flushing", cf.Flush(), 0);
36         }
37
38         {
39             CONFIGFILE cf("/tmp/test.cf");
40
41             ensure_equals("Correct construction (part 2)", cf.Error(), 0);
42             
43             std::string svalue;
44             ensure_equals("Correct reading 'a' param as string", cf.ReadString("a", &svalue, "a-default"), 0);
45             int ivalue;
46             ensure_equals("Correct reading 'b' param as integer", cf.ReadInt("b", &ivalue, -1), 0);
47             double dvalue = 0;
48             ensure_equals("Correct reading 'e' param as double", cf.ReadDouble("e", &dvalue, 0), 0);
49
50             ensure_equals("Correct 'a' value", svalue, "a-string");
51             ensure_equals("Correct 'b' value", ivalue, 0);
52             ensure("Correct 'e' value", dvalue != 0);
53         }
54
55         ensure_equals("Correct temporary file unlinking", unlink("/tmp/test.cf"), 0);
56     }
57
58     template<>
59     template<>
60     void testobject::test<2>()
61     {
62         set_test_name("Check empty lines and comments");
63
64         {
65             std::ofstream f("/tmp/test.cf");
66
67             ensure("Correct construction (part 3)", static_cast<bool>(f));
68
69             f << "\n"
70               << "a=a-string# a string\n"
71               << "              \n"
72               << "b=0\n"
73               << "#abc\n"
74               << "e=2.718281828\n";
75         }
76
77         {
78             CONFIGFILE cf("/tmp/test.cf");
79
80             ensure_equals("Correct construction (part 4)", cf.Error(), 0);
81             
82             std::string svalue;
83             ensure_equals("Correct reading 'a' param as string", cf.ReadString("a", &svalue, "a-default"), 0);
84             int ivalue;
85             ensure_equals("Correct reading 'b' param as integer", cf.ReadInt("b", &ivalue, -1), 0);
86             double dvalue = 0;
87             ensure_equals("Correct reading 'e' param as double", cf.ReadDouble("e", &dvalue, 0), 0);
88
89             ensure_equals("Correct 'a' value", svalue, "a-string");
90             ensure_equals("Correct 'b' value", ivalue, 0);
91             ensure("Correct 'e' value", dvalue != 0);
92         }
93
94         ensure_equals("Correct temporary file unlinking", unlink("/tmp/test.cf"), 0);
95     }
96 }