|
| 1 | +namespace GitVersion.Configuration.Tests; |
| 2 | + |
| 3 | +[TestFixture] |
| 4 | +public class ConfigurationHelperTests |
| 5 | +{ |
| 6 | + [Test] |
| 7 | + public void Override_Replaces_Nested_Dictionary_With_Scalar_Value() |
| 8 | + { |
| 9 | + Dictionary<object, object?> original = |
| 10 | + new() |
| 11 | + { |
| 12 | + ["key"] = new Dictionary<object, object?> { ["nested"] = "value" } |
| 13 | + }; |
| 14 | + var helper = new ConfigurationHelper(original); |
| 15 | + |
| 16 | + IReadOnlyDictionary<object, object?> source = |
| 17 | + new Dictionary<object, object?> |
| 18 | + { |
| 19 | + ["key"] = "override" |
| 20 | + }; |
| 21 | + |
| 22 | + helper.Override(source); |
| 23 | + |
| 24 | + helper.Dictionary["key"].ShouldBe("override"); |
| 25 | + } |
| 26 | + |
| 27 | + [Test] |
| 28 | + public void Override_Merges_Nested_Dictionaries_Recursively() |
| 29 | + { |
| 30 | + Dictionary<object, object?> original = |
| 31 | + new() |
| 32 | + { |
| 33 | + ["key"] = new Dictionary<object, object?> |
| 34 | + { |
| 35 | + ["a"] = 1, |
| 36 | + ["b"] = 2 |
| 37 | + } |
| 38 | + }; |
| 39 | + var helper = new ConfigurationHelper(original); |
| 40 | + |
| 41 | + IReadOnlyDictionary<object, object?> source = |
| 42 | + new Dictionary<object, object?> |
| 43 | + { |
| 44 | + ["key"] = new Dictionary<object, object?> |
| 45 | + { |
| 46 | + ["b"] = 3, |
| 47 | + ["c"] = 4 |
| 48 | + } |
| 49 | + }; |
| 50 | + |
| 51 | + helper.Override(source); |
| 52 | + |
| 53 | + var nested = (IDictionary<object, object?>)helper.Dictionary["key"]!; |
| 54 | + nested["a"].ShouldBe(1); |
| 55 | + nested["b"].ShouldBe(3); |
| 56 | + nested["c"].ShouldBe(4); |
| 57 | + } |
| 58 | + |
| 59 | + [Test] |
| 60 | + public void Override_Clones_New_Nested_Dictionaries() |
| 61 | + { |
| 62 | + Dictionary<object, object?> original = []; |
| 63 | + var helper = new ConfigurationHelper(original); |
| 64 | + |
| 65 | + Dictionary<object, object?> sourceNested = |
| 66 | + new() |
| 67 | + { |
| 68 | + ["a"] = 1 |
| 69 | + }; |
| 70 | + IReadOnlyDictionary<object, object?> source = |
| 71 | + new Dictionary<object, object?> |
| 72 | + { |
| 73 | + ["key"] = sourceNested |
| 74 | + }; |
| 75 | + |
| 76 | + helper.Override(source); |
| 77 | + sourceNested["a"] = 2; |
| 78 | + |
| 79 | + var nested = (IDictionary<object, object?>)helper.Dictionary["key"]!; |
| 80 | + nested["a"].ShouldBe(1); |
| 81 | + } |
| 82 | +} |
0 commit comments