1 module toml_foolery.attributes.toml_name_test; 2 3 version(unittest) 4 { 5 import exceeds_expectations; 6 import toml_foolery.attributes.toml_name; 7 import toml_foolery.decode; 8 import toml_foolery.encode; 9 import toml_foolery.encode.util; 10 } 11 12 @("Rename a field when encoding") 13 unittest 14 { 15 struct S 16 { 17 @TomlName("key") 18 string field; 19 } 20 21 expectToEqualNoBlanks(tomlify(S("test")), ` 22 key = "test" 23 ` 24 ); 25 } 26 27 @("Rename a field with spaces when encoding") 28 unittest 29 { 30 struct S 31 { 32 @TomlName("this is not a valid D identifier") 33 string field; 34 } 35 36 expectToEqualNoBlanks(tomlify(S("test")), ` 37 "this is not a valid D identifier" = "test" 38 ` 39 ); 40 } 41 42 @("Rename a table when encoding") 43 unittest 44 { 45 struct S 46 { 47 struct Inner 48 { 49 string name; 50 } 51 52 @TomlName("Contents") 53 Inner i; 54 } 55 56 expectToEqualNoBlanks(tomlify(S(S.Inner("test"))), ` 57 [Contents] 58 name = "test" 59 ` 60 ); 61 } 62 63 @("Complex rename on encoding") 64 unittest 65 { 66 struct S 67 { 68 struct Inner 69 { 70 @TomlName("why") string y; 71 @TomlName("y") float why; 72 } 73 74 @TomlName("eye") int i; 75 @TomlName("i") Inner inner; 76 } 77 78 expectToEqualNoBlanks(tomlify(S(5, S.Inner("hello world", 0.5))), ` 79 eye = 5 80 81 [i] 82 why = "hello world" 83 y = 0.5 84 `); 85 } 86 87 @("Fail on compilation if field names conflict") 88 unittest 89 { 90 struct S 91 { 92 int i; 93 94 @TomlName("i") 95 int i2; 96 } 97 98 static assert ( 99 !__traits(compiles, tomlify(S())), 100 "tomlifying a struct with conflicting keys should fail." 101 ); 102 103 static assert ( 104 !__traits(compiles, parseToml!S(`i = 2`)), 105 "parsing when destination struct has conflicting keys should fail." 106 ); 107 } 108 109 @("Rename a field when decoding") 110 unittest 111 { 112 struct S 113 { 114 @TomlName("key") 115 string field; 116 } 117 118 expect(parseToml!S(` 119 key = "test" 120 ` 121 )).toEqual(S("test")); 122 } 123 124 @("Rename a field with spaces when decoding") 125 unittest 126 { 127 struct S 128 { 129 @TomlName("this is not a valid D identifier") 130 string field; 131 } 132 133 expect(parseToml!S(` 134 "this is not a valid D identifier" = "test" 135 ` 136 )).toEqual(S("test")); 137 } 138 139 @("Rename a table when decoding") 140 unittest 141 { 142 struct S 143 { 144 struct Inner 145 { 146 string name; 147 } 148 149 @TomlName("Contents") 150 Inner i; 151 } 152 153 expect(parseToml!S(` 154 [Contents] 155 name = "test" 156 ` 157 )).toEqual(S(S.Inner("test"))); 158 } 159 160 @("Complex rename on decoding") 161 unittest 162 { 163 struct S 164 { 165 struct Inner 166 { 167 @TomlName("why") string y; 168 @TomlName("y") float why; 169 } 170 171 @TomlName("eye") int i; 172 @TomlName("i") Inner inner; 173 } 174 175 expect(parseToml!S(` 176 eye = 5 177 178 [i] 179 why = "hello world" 180 y = 0.5 181 `)).toEqual( 182 S( 183 5, 184 S.Inner( 185 "hello world", 186 0.5 187 ) 188 ) 189 ); 190 }