|
| 1 | +using Xunit; |
| 2 | +using sly.parser; |
| 3 | +using sly.parser.generator; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Text.RegularExpressions; |
| 6 | +using jsonparser; |
| 7 | +using jsonparser.JsonModel; |
| 8 | + |
| 9 | +namespace ParserTests |
| 10 | +{ |
| 11 | + |
| 12 | + public class JsonGenericTests |
| 13 | + { |
| 14 | + |
| 15 | + private static Parser<JsonTokenGeneric,JSon> Parser; |
| 16 | + |
| 17 | + |
| 18 | + public JsonGenericTests() |
| 19 | + { |
| 20 | + EbnfJsonGenericParser jsonParser = new EbnfJsonGenericParser(); |
| 21 | + ParserBuilder<JsonTokenGeneric, JSon> builder = new ParserBuilder<JsonTokenGeneric, JSon>(); |
| 22 | + Parser = builder.BuildParser(jsonParser, ParserType.EBNF_LL_RECURSIVE_DESCENT, "root").Result; |
| 23 | + } |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | +#region VALUES |
| 29 | + [Fact] |
| 30 | + public void TestIntValue() |
| 31 | + { |
| 32 | + ParseResult<JsonTokenGeneric, JSon> r = Parser.Parse("1"); |
| 33 | + Assert.False(r.IsError); |
| 34 | + Assert.False(r.IsError); |
| 35 | + Assert.NotNull(r.Result); |
| 36 | + Assert.True(r.Result.IsValue); |
| 37 | + Assert.True(((JValue)r.Result).IsInt); |
| 38 | + Assert.Equal(1, ((JValue)r.Result).GetValue<int>()); |
| 39 | + } |
| 40 | + |
| 41 | + [Fact] |
| 42 | + public void TestDoubleValue() |
| 43 | + { |
| 44 | + ParseResult<JsonTokenGeneric, JSon> r = Parser.Parse("0.1"); |
| 45 | + Assert.False(r.IsError); |
| 46 | + Assert.NotNull(r.Result); |
| 47 | + Assert.True(r.Result.IsValue); |
| 48 | + Assert.True(((JValue)r.Result).IsDouble); |
| 49 | + Assert.Equal(0.1d, ((JValue)r.Result).GetValue<double>()); |
| 50 | + } |
| 51 | + |
| 52 | + [Fact] |
| 53 | + public void TestStringValue() |
| 54 | + { |
| 55 | + string val = "hello"; |
| 56 | + ParseResult<JsonTokenGeneric, JSon> r = Parser.Parse("\"" + val + "\""); |
| 57 | + Assert.False(r.IsError); |
| 58 | + Assert.NotNull(r.Result); |
| 59 | + Assert.True(r.Result.IsValue); |
| 60 | + Assert.True(((JValue)r.Result).IsString); |
| 61 | + Assert.Equal(val, ((JValue)r.Result).GetValue<string>()); |
| 62 | + } |
| 63 | + |
| 64 | + [Fact] |
| 65 | + public void TestTrueBooleanValue() |
| 66 | + { |
| 67 | + ParseResult<JsonTokenGeneric, JSon> r = Parser.Parse("true"); |
| 68 | + Assert.False(r.IsError); |
| 69 | + Assert.NotNull(r.Result); |
| 70 | + Assert.True(r.Result.IsValue); |
| 71 | + JValue val = ((JValue)r.Result); |
| 72 | + Assert.True(val.IsBool); |
| 73 | + Assert.True(val.IsBool); |
| 74 | + Assert.Equal(true, val.GetValue<bool>()); |
| 75 | + } |
| 76 | + |
| 77 | + [Fact] |
| 78 | + public void TestFalseBooleanValue() |
| 79 | + { |
| 80 | + ParseResult<JsonTokenGeneric, JSon> r = Parser.Parse("false"); |
| 81 | + Assert.False(r.IsError); |
| 82 | + Assert.NotNull(r.Result); |
| 83 | + Assert.True(r.Result.IsValue); |
| 84 | + JValue val = ((JValue)r.Result); |
| 85 | + Assert.True(val.IsBool); |
| 86 | + Assert.Equal(false, val.GetValue<bool>()); |
| 87 | + } |
| 88 | + |
| 89 | + [Fact] |
| 90 | + public void TestNullValue() |
| 91 | + { |
| 92 | + ParseResult<JsonTokenGeneric, JSon> r = Parser.Parse("null"); |
| 93 | + Assert.False(r.IsError); |
| 94 | + Assert.True(r.Result.IsNull); |
| 95 | + } |
| 96 | + |
| 97 | + #endregion |
| 98 | + |
| 99 | + #region OBJECT |
| 100 | + |
| 101 | + [Fact] |
| 102 | + public void TestEmptyObjectValue() |
| 103 | + { |
| 104 | + ParseResult<JsonTokenGeneric, JSon> r = Parser.Parse("{}"); |
| 105 | + Assert.False(r.IsError); |
| 106 | + Assert.NotNull(r.Result); |
| 107 | + Assert.True(r.Result.IsObject); |
| 108 | + Assert.Equal(0, ((JObject)r.Result).Count); |
| 109 | + } |
| 110 | + |
| 111 | + |
| 112 | + private void AssertString(JObject obj, string key, string value) |
| 113 | + { |
| 114 | + Assert.True(obj.ContainsKey(key)); |
| 115 | + Assert.True(obj[key].IsValue); |
| 116 | + JValue val = (JValue) obj[key]; |
| 117 | + Assert.True(val.IsString); |
| 118 | + Assert.Equal(value, val.GetValue<string>() ); |
| 119 | + } |
| 120 | + |
| 121 | + private void AssertInt(JObject obj, string key, int value) |
| 122 | + { |
| 123 | + Assert.True(obj.ContainsKey(key)); |
| 124 | + Assert.True(obj[key].IsValue); |
| 125 | + JValue val = (JValue) obj[key]; |
| 126 | + Assert.True(val.IsInt); |
| 127 | + Assert.Equal(value, val.GetValue<int>() ); |
| 128 | + } |
| 129 | + |
| 130 | + |
| 131 | + private void AssertDouble(JObject obj, string key, double value) |
| 132 | + { |
| 133 | + Assert.True(obj.ContainsKey(key)); |
| 134 | + Assert.True(obj[key].IsValue); |
| 135 | + JValue val = (JValue) obj[key]; |
| 136 | + Assert.True(val.IsDouble); |
| 137 | + Assert.Equal(value, val.GetValue<double>() ); |
| 138 | + } |
| 139 | + |
| 140 | + |
| 141 | + private void AssertString(JList list, int index, string value) |
| 142 | + { |
| 143 | + Assert.True(list[index].IsValue); |
| 144 | + JValue val = (JValue) list[index]; |
| 145 | + Assert.True(val.IsString); |
| 146 | + Assert.Equal(value, val.GetValue<string>() ); |
| 147 | + } |
| 148 | + |
| 149 | + private void AssertInt(JList list, int index, int value) |
| 150 | + { |
| 151 | + Assert.True(list[index].IsValue); |
| 152 | + JValue val = (JValue) list[index]; |
| 153 | + Assert.True(val.IsInt); |
| 154 | + Assert.Equal(value, val.GetValue<int>() ); |
| 155 | + } |
| 156 | + |
| 157 | + |
| 158 | + private void AssertDouble(JList list, int index, double value) |
| 159 | + { |
| 160 | + Assert.True(list[index].IsValue); |
| 161 | + JValue val = (JValue) list[index]; |
| 162 | + Assert.True(val.IsDouble); |
| 163 | + Assert.Equal(value, val.GetValue<double>() ); |
| 164 | + } |
| 165 | + |
| 166 | + |
| 167 | + private void AssertBool(JList list, int index, bool value) |
| 168 | + { |
| 169 | + Assert.True(list[index].IsValue); |
| 170 | + JValue val = (JValue) list[index]; |
| 171 | + Assert.True(val.IsBool); |
| 172 | + Assert.Equal(value, val.GetValue<bool>() ); |
| 173 | + } |
| 174 | + |
| 175 | + |
| 176 | + private void AssertObject(JList list, int index, int count) |
| 177 | + { |
| 178 | + Assert.True(list[index].IsObject); |
| 179 | + JObject val = (JObject) list[index]; |
| 180 | + Assert.Equal(count, val.Count); |
| 181 | + } |
| 182 | + |
| 183 | + |
| 184 | + [Fact] |
| 185 | + public void TestSinglePropertyObjectValue() |
| 186 | + { |
| 187 | + ParseResult<JsonTokenGeneric, JSon> r = Parser.Parse("{\"prop\":\"value\"}"); |
| 188 | + Assert.False(r.IsError); |
| 189 | + Assert.NotNull(r.Result); |
| 190 | + Assert.True(r.Result.IsObject); |
| 191 | + JObject values = (JObject)r.Result; |
| 192 | + Assert.Equal(1,values.Count); |
| 193 | + AssertString(values,"prop","value"); |
| 194 | + } |
| 195 | + |
| 196 | + [Fact] |
| 197 | + public void TestManyPropertyObjectValue() |
| 198 | + { |
| 199 | + string json = "{\"p1\":\"v1\",\"p2\":\"v2\"}"; |
| 200 | + json = "{\"p1\":\"v1\" , \"p2\":\"v2\" }"; |
| 201 | + ParseResult<JsonTokenGeneric,JSon> r = Parser.Parse(json); |
| 202 | + Assert.False(r.IsError); |
| 203 | + Assert.NotNull(r.Result); |
| 204 | + Assert.True(r.Result.IsObject); |
| 205 | + JObject values = (JObject)r.Result; |
| 206 | + Assert.Equal(2,values.Count); |
| 207 | + AssertString(values,"p1","v1"); |
| 208 | + AssertString(values,"p2","v2"); |
| 209 | + } |
| 210 | + |
| 211 | + [Fact] |
| 212 | + public void TestManyNestedPropertyObjectValue() |
| 213 | + { |
| 214 | + string json = "{\"p1\":\"v1\",\"p2\":\"v2\",\"p3\":{\"inner1\":1}}"; |
| 215 | + |
| 216 | + ParseResult<JsonTokenGeneric,JSon> r = Parser.Parse(json); |
| 217 | + Assert.False(r.IsError); |
| 218 | + Assert.NotNull(r); |
| 219 | + Assert.True(r.Result.IsObject); |
| 220 | + JObject values = (JObject)r.Result; |
| 221 | + Assert.Equal(3,values.Count); |
| 222 | + AssertString(values,"p1","v1"); |
| 223 | + AssertString(values,"p2","v2"); |
| 224 | + |
| 225 | + Assert.True(values.ContainsKey("p3")); |
| 226 | + JSon inner = values["p3"]; |
| 227 | + Assert.True(inner.IsObject); |
| 228 | + JObject innerObj = (JObject) inner; |
| 229 | + |
| 230 | + Assert.Equal(1, innerObj.Count); |
| 231 | + AssertInt(innerObj,"inner1",1); |
| 232 | + } |
| 233 | + |
| 234 | + #endregion |
| 235 | + |
| 236 | + |
| 237 | + #region LIST |
| 238 | + |
| 239 | + |
| 240 | + [Fact] |
| 241 | + public void TestEmptyListValue() |
| 242 | + { |
| 243 | + ParseResult<JsonTokenGeneric,JSon> r = Parser.Parse("[]"); |
| 244 | + Assert.False(r.IsError); |
| 245 | + Assert.NotNull(r.Result); |
| 246 | + Assert.True(r.Result.IsList); |
| 247 | + Assert.Equal(0, ((JList)r.Result).Count); |
| 248 | + } |
| 249 | + |
| 250 | + [Fact] |
| 251 | + public void TestSingleListValue() |
| 252 | + { |
| 253 | + ParseResult<JsonTokenGeneric, JSon> r = Parser.Parse("[1]"); |
| 254 | + Assert.False(r.IsError); |
| 255 | + Assert.NotNull(r.Result); |
| 256 | + Assert.True(r.Result.IsList); |
| 257 | + JList list = (JList) r.Result; |
| 258 | + Assert.Equal(1, list.Count); |
| 259 | + AssertInt(list, 0, 1); |
| 260 | + } |
| 261 | + |
| 262 | + [Fact] |
| 263 | + public void TestManyListValue() |
| 264 | + { |
| 265 | + ParseResult<JsonTokenGeneric,JSon> r = Parser.Parse("[1,2]"); |
| 266 | + Assert.False(r.IsError); |
| 267 | + Assert.NotNull(r.Result); |
| 268 | + Assert.True(r.Result.IsList); |
| 269 | + JList list = (JList) r.Result; |
| 270 | + Assert.Equal(2, list.Count); |
| 271 | + AssertInt(list, 0, 1); |
| 272 | + AssertInt(list, 1, 2); |
| 273 | + } |
| 274 | + |
| 275 | + [Fact] |
| 276 | + public void TestManyMixedListValue() |
| 277 | + { |
| 278 | + ParseResult<JsonTokenGeneric,JSon> r = Parser.Parse("[1,null,{},true,42.58]"); |
| 279 | + Assert.False(r.IsError); |
| 280 | + Assert.NotNull(r.Result); |
| 281 | + Assert.NotNull(r.Result); |
| 282 | + object val = r.Result; |
| 283 | + Assert.True(r.Result.IsList); |
| 284 | + JList list = (JList) r.Result; |
| 285 | + Assert.Equal(5, ((JList)r.Result).Count); |
| 286 | + AssertInt(list,0,1); |
| 287 | + Assert.True(((JList)r.Result)[1].IsNull); |
| 288 | + AssertObject(list,2,0); |
| 289 | + AssertBool(list,3,true); |
| 290 | + AssertDouble(list,4,42.58d); |
| 291 | + } |
| 292 | + |
| 293 | + #endregion |
| 294 | + |
| 295 | + |
| 296 | + } |
| 297 | +} |
0 commit comments