|
| 1 | +//----------------------------------------------------------------------- |
| 2 | +// <copyright file="JsonElementComparer.cs" company="P.O.S Informatique"> |
| 3 | +// Copyright (c) P.O.S Informatique. All rights reserved. |
| 4 | +// </copyright> |
| 5 | +//----------------------------------------------------------------------- |
| 6 | + |
| 7 | +namespace PosInformatique.FluentAssertions.Json |
| 8 | +{ |
| 9 | + using System.Collections; |
| 10 | + using System.Reflection; |
| 11 | + using System.Text.Json; |
| 12 | + |
| 13 | + internal static class JsonElementComparer |
| 14 | + { |
| 15 | + public static IReadOnlyList<string> Compare(JsonDocument document, object? expected) |
| 16 | + { |
| 17 | + return Compare(document.RootElement, expected, "$"); |
| 18 | + } |
| 19 | + |
| 20 | + public static IReadOnlyList<string> Compare(JsonElement element, object? expected, string initialPath) |
| 21 | + { |
| 22 | + var errors = new List<string>(); |
| 23 | + |
| 24 | + var path = new Stack<string>(); |
| 25 | + |
| 26 | + path.Push(initialPath); |
| 27 | + |
| 28 | + Compare(element, expected, path, errors); |
| 29 | + |
| 30 | + path.Pop(); |
| 31 | + |
| 32 | + return errors; |
| 33 | + } |
| 34 | + |
| 35 | + private static void Compare(JsonElement element, object? expected, Stack<string> path, List<string> errors) |
| 36 | + { |
| 37 | + if (element.ValueKind == JsonValueKind.String) |
| 38 | + { |
| 39 | + var value = element.GetString(); |
| 40 | + |
| 41 | + if (expected is not string expectedStringValue) |
| 42 | + { |
| 43 | + errors.Add($"{GetPath(path)}: Expected property to be '{ReflectionHelper.GetJsonKind(expected)}' type instead of '{element.ValueKind}' type."); |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + if (value != expectedStringValue) |
| 48 | + { |
| 49 | + errors.Add($"{GetPath(path)}: Expected '{expected}' instead of '{value}'."); |
| 50 | + return; |
| 51 | + } |
| 52 | + } |
| 53 | + else if (element.ValueKind == JsonValueKind.Number) |
| 54 | + { |
| 55 | + var value = element.GetDouble(); |
| 56 | + |
| 57 | + if (expected == null || !ReflectionHelper.IsNumeric(expected)) |
| 58 | + { |
| 59 | + errors.Add($"{GetPath(path)}: Expected property to be '{ReflectionHelper.GetJsonKind(expected)}' type instead of '{element.ValueKind}' type."); |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + var expectedDoubleValue = Convert.ToDouble(expected); |
| 64 | + |
| 65 | + if (value != expectedDoubleValue) |
| 66 | + { |
| 67 | + errors.Add($"{GetPath(path)}: Expected '{expected}' instead of '{value}'."); |
| 68 | + return; |
| 69 | + } |
| 70 | + } |
| 71 | + else if (element.ValueKind == JsonValueKind.Object) |
| 72 | + { |
| 73 | + if (expected is null || expected is string || ReflectionHelper.IsNumeric(expected) || expected is IEnumerable || expected is bool) |
| 74 | + { |
| 75 | + errors.Add($"{GetPath(path)}: Expected property to be '{ReflectionHelper.GetJsonKind(expected)}' type instead of '{element.ValueKind}' type."); |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + var expectedPropertyEnumerator = expected.GetType().GetProperties().Cast<PropertyInfo>().GetEnumerator(); |
| 80 | + |
| 81 | + foreach (var property in element.EnumerateObject()) |
| 82 | + { |
| 83 | + if (!expectedPropertyEnumerator.MoveNext()) |
| 84 | + { |
| 85 | + errors.Add($"{GetPath(path)}: Expected no property but found '{property.Name}' property."); |
| 86 | + continue; |
| 87 | + } |
| 88 | + |
| 89 | + var expectedProperty = expectedPropertyEnumerator.Current; |
| 90 | + |
| 91 | + if (property.Name != expectedProperty.Name) |
| 92 | + { |
| 93 | + errors.Add($"{GetPath(path)}: Expected property with the '{expectedProperty.Name}' name but found '{property.Name}' instead."); |
| 94 | + continue; |
| 95 | + } |
| 96 | + |
| 97 | + path.Push("." + property.Name); |
| 98 | + |
| 99 | + var expectedValueOfProperty = expectedProperty.GetValue(expected); |
| 100 | + |
| 101 | + Compare(property.Value, expectedValueOfProperty, path, errors); |
| 102 | + |
| 103 | + path.Pop(); |
| 104 | + } |
| 105 | + |
| 106 | + if (expectedPropertyEnumerator.MoveNext()) |
| 107 | + { |
| 108 | + errors.Add($"{GetPath(path)}: Expected '{expectedPropertyEnumerator.Current.Name}' property but found no property."); |
| 109 | + return; |
| 110 | + } |
| 111 | + } |
| 112 | + else if (element.ValueKind == JsonValueKind.Array) |
| 113 | + { |
| 114 | + if (expected is string || expected is not IEnumerable expectedEnumerableValue) |
| 115 | + { |
| 116 | + errors.Add($"{GetPath(path)}: Expected property to be '{ReflectionHelper.GetJsonKind(expected)}' type instead of '{element.ValueKind}' type."); |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + var expectedArrayEnumerator = expectedEnumerableValue.GetEnumerator(); |
| 121 | + var index = 0; |
| 122 | + |
| 123 | + foreach (var item in element.EnumerateArray()) |
| 124 | + { |
| 125 | + if (!expectedArrayEnumerator.MoveNext()) |
| 126 | + { |
| 127 | + var actualCount = element.EnumerateArray().Count(); |
| 128 | + |
| 129 | + errors.Add($"{GetPath(path)}: Expected {index} item(s) but found {actualCount}."); |
| 130 | + continue; |
| 131 | + } |
| 132 | + |
| 133 | + var expectedItem = expectedArrayEnumerator.Current; |
| 134 | + |
| 135 | + path.Push($"[{index}]"); |
| 136 | + |
| 137 | + Compare(item, expectedItem, path, errors); |
| 138 | + |
| 139 | + path.Pop(); |
| 140 | + |
| 141 | + index++; |
| 142 | + } |
| 143 | + |
| 144 | + if (expectedArrayEnumerator.MoveNext()) |
| 145 | + { |
| 146 | + var expectedCount = ((IEnumerable)expected).Cast<object>().Count(); |
| 147 | + |
| 148 | + errors.Add($"{GetPath(path)}: Expected {expectedCount} item(s) but found {index}."); |
| 149 | + return; |
| 150 | + } |
| 151 | + } |
| 152 | + else if (element.ValueKind == JsonValueKind.True) |
| 153 | + { |
| 154 | + if (expected is not bool expectedBooleanValue) |
| 155 | + { |
| 156 | + errors.Add($"{GetPath(path)}: Expected property to be '{ReflectionHelper.GetJsonKind(expected)}' type instead of '{element.ValueKind}' type."); |
| 157 | + return; |
| 158 | + } |
| 159 | + |
| 160 | + if (expectedBooleanValue != true) |
| 161 | + { |
| 162 | + errors.Add($"{GetPath(path)}: Expected '{expected}' instead of '{true}'."); |
| 163 | + return; |
| 164 | + } |
| 165 | + } |
| 166 | + else if (element.ValueKind == JsonValueKind.False) |
| 167 | + { |
| 168 | + if (expected is not bool expectedBooleanValue) |
| 169 | + { |
| 170 | + errors.Add($"{GetPath(path)}: Expected property to be '{ReflectionHelper.GetJsonKind(expected)}' type instead of '{element.ValueKind}' type."); |
| 171 | + return; |
| 172 | + } |
| 173 | + |
| 174 | + if (expectedBooleanValue != false) |
| 175 | + { |
| 176 | + errors.Add($"{GetPath(path)}: Expected '{expected}' instead of '{false}'."); |
| 177 | + return; |
| 178 | + } |
| 179 | + } |
| 180 | + else |
| 181 | + { |
| 182 | + if (expected is not null) |
| 183 | + { |
| 184 | + errors.Add($"{GetPath(path)}: Expected property to be '{ReflectionHelper.GetJsonKind(expected)}' type instead of '{element.ValueKind}' type."); |
| 185 | + return; |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + private static string GetPath(IEnumerable<string> path) |
| 191 | + { |
| 192 | + return string.Concat(path.Reverse()); |
| 193 | + } |
| 194 | + } |
| 195 | +} |
0 commit comments