]> git.stg.codes - stg.git/blob - tests/test_conffiles.cpp
Добавлены юнит-тесты для новой реализации CONFIGFILE
[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 "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             ensure_equals("Correct writing 'a' string", cf.WriteString("a", "a-string"), 0);
32             ensure_equals("Correct writing 'b' integer (0)", cf.WriteInt("b", 0), 0);
33             ensure_equals("Correct writing 'e' double (2.718281828)", cf.WriteDouble("e", 2.718281828), 0);
34         }
35
36         {
37             CONFIGFILE cf("/tmp/test.cf");
38
39             ensure_equals("Correct construction (part 2)", cf.Error(), 0);
40             
41             std::string svalue;
42             ensure_equals("Correct reading 'a' param as string", cf.ReadString("a", &svalue, "a-default"), 0);
43             int ivalue;
44             ensure_equals("Correct reading 'b' param as integer", cf.ReadInt("b", &ivalue, -1), 0);
45             double dvalue = 0;
46             ensure_equals("Correct reading 'e' param as double", cf.ReadDouble("e", &dvalue, 0), 0);
47
48             ensure_equals("Correct 'a' value", svalue, "a-string");
49             ensure_equals("Correct 'b' value", ivalue, 0);
50             ensure("Correct 'e' value", dvalue != 0);
51         }
52
53         ensure_equals("Correct temporary file unlinking", unlink("/tmp/test.cf"), 0);
54     }
55
56     template<>
57     template<>
58     void testobject::test<2>()
59     {
60         set_test_name("Check empty lines and comments");
61
62         {
63             ofstream f("/tmp/test.cf");
64
65             ensure("Correct construction (part 3)", f);
66
67             f << "\n"
68               << "a=a-string# a string\n"
69               << "              \n"
70               << "b=0\n"
71               << "#abc\n"
72               << "e=2.718281828\n";
73         }
74
75         {
76             CONFIGFILE cf("/tmp/test.cf");
77
78             ensure_equals("Correct construction (part 4)", cf.Error(), 0);
79             
80             std::string svalue;
81             ensure_equals("Correct reading 'a' param as string", cf.ReadString("a", &svalue, "a-default"), 0);
82             int ivalue;
83             ensure_equals("Correct reading 'b' param as integer", cf.ReadInt("b", &ivalue, -1), 0);
84             double dvalue = 0;
85             ensure_equals("Correct reading 'e' param as double", cf.ReadDouble("e", &dvalue, 0), 0);
86
87             ensure_equals("Correct 'a' value", svalue, "a-string");
88             ensure_equals("Correct 'b' value", ivalue, 0);
89             ensure("Correct 'e' value", dvalue != 0);
90         }
91
92         ensure_equals("Correct temporary file unlinking", unlink("/tmp/test.cf"), 0);
93     }
94 }