1 module d_properties;
2 
3 public import d_properties.properties;
4 public import d_properties.writer;
5 public import d_properties.reader;
6 
7 unittest {
8     import std.format;
9     import std.file;
10 
11     // First test all valid cases.
12 
13     void readWriteValidTest(uint testCase) {
14         string filename = format("test_cases/valid/%d.properties", testCase);
15         string outFilename = format("test_cases/valid/%d-out.properties", testCase);
16         auto p1 = Properties(filename);
17         p1.writeToFile(outFilename);
18         auto p2 = Properties(outFilename);
19         remove(outFilename); // Remove the extra file now that we're done.
20         assert(p1 == p2, "Properties are not equal after read/write cycle.");
21     }
22 
23     readWriteValidTest(1);
24     readWriteValidTest(2);
25 
26     // Test some specifics to ensure reading produces the expected values.
27     auto p = Properties("test_cases/valid/2.properties");
28     assert("my.value" in p);
29     assert(p["my.value"] == "Hello world!");
30     assert(p["another.value"] == "\"This is a quoted string\"");
31     assert(p["This is an indented value"] == "12345");
32     assert(p["multiline_2"] == "abc");
33     assert("missing_key" !in p);
34     assert(p.get("missing_key", "none") == "none");
35     p["missing_key"] = "yes";
36     assert("missing_key" in p);
37 
38     // Test property overwriting.
39     p = Properties("test_cases/valid/2.properties", "test_cases/valid/3.properties");
40     assert(p["my.value"] == "Goodbye world!");
41     assert(p["another.value"] == "test");
42 
43     p = Properties("test_cases/valid/3.properties", "test_cases/valid/2.properties");
44     assert(p["my.value"] == "Hello world!");
45     assert(p["another.value"] == "\"This is a quoted string\"");
46 
47     p = Properties("test_cases/valid/2.properties");
48     auto p2 = Properties("test_cases/valid/3.properties");
49     p.addAll(p2);
50     assert(p["my.value"] == "Goodbye world!");
51     p.addAll("test_cases/valid/1.properties");
52     assert("language" in p);
53 
54     // Then test all invalid cases, one-by-one, to check line number and/or message.
55 
56     try {
57         readFromFile("test_cases/invalid/1.json");
58     } catch (PropertiesParseException e) {
59         assert(e.lineNumber == 1);
60     }
61 
62     try {
63         readFromFile("test_cases/invalid/2.properties");
64     } catch (PropertiesParseException e) {
65         assert(e.lineNumber == 2);
66     }
67 
68     try {
69         readFromFile("test_cases/invalid/3.properties");
70     } catch (PropertiesParseException e) {
71         assert(e.lineNumber == 3);
72     }
73 }