1 module config.test; 2 3 version(unittest): 4 5 import config.config; 6 7 immutable testFiles = [ 8 [ import("input_0.cfg"), import("output_0.cfg") ], 9 [ import("input_1.cfg"), import("output_1.cfg") ], 10 [ import("input_2.cfg"), import("output_2.cfg") ], 11 [ import("input_3.cfg"), import("output_3.cfg") ], 12 [ import("input_4.cfg"), import("output_4.cfg") ], 13 [ import("input_5.cfg"), import("output_5.cfg") ], 14 ]; 15 16 void parseAndCompare(string input, string expected) 17 { 18 import std.format : format; 19 20 auto conf = Config.readString(input, ["."]); 21 auto res = conf.toString(); 22 assert(expected == res, format( 23 "parseAndCompare failed.\ninput:\n%s\nresult:\n%s\nexpected:\n%s\n", 24 input, res, expected 25 )); 26 } 27 28 void parseFileAndCompare(string input, string expected) 29 { 30 import std.format : format; 31 import std.file : write, remove; 32 33 scope(exit) remove("input.cfg"); 34 write("input.cfg", input); 35 36 auto conf = Config.readFile("input.cfg", ["."]); 37 auto res = conf.toString(); 38 assert(expected == res, format( 39 "parseAndCompare failed.\ninput:\n%s\nresult:\n%s\nexpected:\n%s\n", 40 input, res, expected 41 )); 42 } 43 44 45 unittest 46 { 47 import std.file : write, remove; 48 49 scope(exit) remove("more.cfg"); 50 write("more.cfg", import("more.cfg")); 51 52 foreach (tf; testFiles) 53 { 54 parseAndCompare(tf[0], tf[1]); 55 } 56 57 parseFileAndCompare(testFiles[2][0], testFiles[2][1]); 58 } 59 60 unittest 61 { 62 import std.exception : assertThrown; 63 64 assertThrown!InvalidConfigInput(Config.readString(import("bad_input_0.cfg"))); 65 assertThrown!InconsistentConfigState(Config.readString(import("bad_input_1.cfg"))); 66 } 67 68 unittest 69 { 70 immutable confStr = "someint = 5"; 71 const conf = Config.readString(confStr); 72 immutable si = conf.lookUpValue!int("someint"); 73 immutable sl = conf.lookUpValue!long("someint"); 74 assert(!si.isNull); 75 assert(si.get == 5); 76 assert(!sl.isNull); 77 assert(sl.get == 5); 78 } 79 80 unittest 81 { 82 import std.exception : assertThrown; 83 import std.format : format; 84 85 immutable val = long(2)^^33; 86 immutable confStr = format("someint = %s", val); 87 const conf = Config.readString(confStr); 88 assertThrown(conf.lookUpValue!int("someint")); 89 immutable sl = conf.lookUpValue!long("someint"); 90 assert(!sl.isNull); 91 assert(sl.get == val); 92 } 93 94 unittest 95 { 96 import std.exception : assertThrown; 97 import std.format : format; 98 99 immutable val = -long(2)^^33; 100 immutable confStr = format("someint = %s", val); 101 const conf = Config.readString(confStr); 102 assertThrown(conf.lookUpValue!int("someint")); 103 immutable sl = conf.lookUpValue!long("someint"); 104 assert(!sl.isNull); 105 assert(sl.get == val); 106 } 107 108 unittest 109 { 110 import std.format : format; 111 112 immutable val = 2^^31 - 1; 113 immutable confStr = format("someint = %s", val); 114 const conf = Config.readString(confStr); 115 immutable si = conf.lookUpValue!int("someint"); 116 immutable sl = conf.lookUpValue!long("someint"); 117 assert(!si.isNull); 118 assert(si.get == val); 119 assert(!sl.isNull); 120 assert(sl.get == val); 121 } 122 123 unittest 124 { 125 import std.exception : assertThrown; 126 import std.format : format; 127 128 immutable val = long(2)^^31; 129 immutable confStr = format("someint = %s", val); 130 const conf = Config.readString(confStr); 131 assertThrown(conf.lookUpValue!int("someint")); 132 immutable sui = conf.lookUpValue!uint("someint"); 133 immutable sl = conf.lookUpValue!long("someint"); 134 assert(!sui.isNull); 135 assert(sui.get == val); 136 assert(!sl.isNull); 137 assert(sl.get == val); 138 } 139 140 unittest 141 { 142 import std.format : format; 143 144 immutable val = 52; 145 immutable confStr = format("someint = %s", val); 146 const conf = Config.readString(confStr); 147 immutable sf = conf.lookUpValue!float("someint"); 148 assert(!sf.isNull); 149 assert(sf.get == val); 150 } 151 152 unittest 153 { 154 auto conf = Config.readString("a:{b:3;c:4;}"); 155 156 assert(conf.lookUp("a")); 157 assert(conf.lookUp("a.b")); 158 assert(conf.lookUp("a.c")); 159 160 assert(conf.remove("a.c")); 161 assert(!conf.remove("a.c")); 162 163 assert(conf.lookUp("a")); 164 assert(conf.lookUp("a.b")); 165 assert(!conf.lookUp("a.c")); 166 }