|
1 |
| - |
2 | 1 | #if NET8_0_OR_GREATER
|
3 | 2 |
|
4 | 3 | using System;
|
@@ -70,57 +69,136 @@ public void ObjectToDictionary_NullObject_Return_New_Dictionary()
|
70 | 69 | // Act & Assert
|
71 | 70 | Assert.NotNull(() => PowertoolsLoggerHelpers.ObjectToDictionary(null));
|
72 | 71 | }
|
73 |
| - |
| 72 | + |
74 | 73 | [Fact]
|
75 | 74 | public void Should_Log_With_Anonymous()
|
76 | 75 | {
|
77 | 76 | var consoleOut = Substitute.For<StringWriter>();
|
78 | 77 | SystemWrapper.Instance.SetOut(consoleOut);
|
79 |
| - |
| 78 | + |
80 | 79 | // Act & Assert
|
81 |
| - Logger.AppendKey("newKey", new |
| 80 | + Logger.AppendKey("newKey", new |
82 | 81 | {
|
83 | 82 | name = "my name"
|
84 | 83 | });
|
85 |
| - |
| 84 | + |
86 | 85 | Logger.LogInformation("test");
|
87 |
| - |
| 86 | + |
88 | 87 | consoleOut.Received(1).WriteLine(
|
89 | 88 | Arg.Is<string>(i =>
|
90 | 89 | i.Contains("\"new_key\":{\"name\":\"my name\"}"))
|
91 | 90 | );
|
92 | 91 | }
|
93 |
| - |
| 92 | + |
94 | 93 | [Fact]
|
95 | 94 | public void Should_Log_With_Complex_Anonymous()
|
96 | 95 | {
|
97 | 96 | var consoleOut = Substitute.For<StringWriter>();
|
98 | 97 | SystemWrapper.Instance.SetOut(consoleOut);
|
99 |
| - |
| 98 | + |
100 | 99 | // Act & Assert
|
101 |
| - Logger.AppendKey("newKey", new |
| 100 | + Logger.AppendKey("newKey", new |
102 | 101 | {
|
103 | 102 | id = 1,
|
104 | 103 | name = "my name",
|
105 |
| - Adresses = new { |
| 104 | + Adresses = new |
| 105 | + { |
106 | 106 | street = "street 1",
|
107 | 107 | number = 1,
|
108 |
| - city = new |
| 108 | + city = new |
109 | 109 | {
|
110 | 110 | name = "city 1",
|
111 | 111 | state = "state 1"
|
112 | 112 | }
|
113 | 113 | }
|
114 | 114 | });
|
115 |
| - |
| 115 | + |
116 | 116 | Logger.LogInformation("test");
|
117 |
| - |
| 117 | + |
118 | 118 | consoleOut.Received(1).WriteLine(
|
119 | 119 | Arg.Is<string>(i =>
|
120 |
| - i.Contains("\"new_key\":{\"id\":1,\"name\":\"my name\",\"adresses\":{\"street\":\"street 1\",\"number\":1,\"city\":{\"name\":\"city 1\",\"state\":\"state 1\"}")) |
| 120 | + i.Contains( |
| 121 | + "\"new_key\":{\"id\":1,\"name\":\"my name\",\"adresses\":{\"street\":\"street 1\",\"number\":1,\"city\":{\"name\":\"city 1\",\"state\":\"state 1\"}")) |
121 | 122 | );
|
122 | 123 | }
|
123 | 124 |
|
| 125 | + [Fact] |
| 126 | + public void ObjectToDictionary_EnumValue_ReturnsOriginalEnum() |
| 127 | + { |
| 128 | + // Arrange |
| 129 | + var enumValue = DayOfWeek.Monday; |
| 130 | + |
| 131 | + // Act |
| 132 | + var result = PowertoolsLoggerHelpers.ObjectToDictionary(enumValue); |
| 133 | + |
| 134 | + // Assert |
| 135 | + Assert.Equal(enumValue, result); |
| 136 | + } |
| 137 | + |
| 138 | + [Fact] |
| 139 | + public void ObjectToDictionary_ObjectWithNullProperty_ExcludesNullProperty() |
| 140 | + { |
| 141 | + // Arrange |
| 142 | + var objectWithNull = new { name = (string)null, age = 30 }; |
| 143 | + |
| 144 | + // Act |
| 145 | + var result = PowertoolsLoggerHelpers.ObjectToDictionary(objectWithNull); |
| 146 | + |
| 147 | + // Assert |
| 148 | + Assert.IsType<Dictionary<string, object>>(result); |
| 149 | + var dictionary = (Dictionary<string, object>)result; |
| 150 | + Assert.Single(dictionary); |
| 151 | + Assert.Equal(30, dictionary["age"]); |
| 152 | + Assert.False(dictionary.ContainsKey("name")); |
| 153 | + } |
| 154 | + |
| 155 | + [Fact] |
| 156 | + public void ObjectToDictionary_EmptyAnonymousObject_ReturnsEmptyDictionary() |
| 157 | + { |
| 158 | + // Arrange |
| 159 | + var emptyObject = new { }; |
| 160 | + |
| 161 | + // Act |
| 162 | + var result = PowertoolsLoggerHelpers.ObjectToDictionary(emptyObject); |
| 163 | + |
| 164 | + // Assert |
| 165 | + Assert.IsType<Dictionary<string, object>>(result); |
| 166 | + var dictionary = (Dictionary<string, object>)result; |
| 167 | + Assert.Empty(dictionary); |
| 168 | + } |
| 169 | + |
| 170 | + [Fact] |
| 171 | + public void ObjectToDictionary_ObjectWithNestedEnum_ReturnsDictionaryWithEnum() |
| 172 | + { |
| 173 | + // Arrange |
| 174 | + var objectWithEnum = new { name = "test", day = DayOfWeek.Friday }; |
| 175 | + |
| 176 | + // Act |
| 177 | + var result = PowertoolsLoggerHelpers.ObjectToDictionary(objectWithEnum); |
| 178 | + |
| 179 | + // Assert |
| 180 | + Assert.IsType<Dictionary<string, object>>(result); |
| 181 | + var dictionary = (Dictionary<string, object>)result; |
| 182 | + Assert.Equal(2, dictionary.Count); |
| 183 | + Assert.Equal("test", dictionary["name"]); |
| 184 | + Assert.Equal(DayOfWeek.Friday, dictionary["day"]); |
| 185 | + } |
| 186 | + |
| 187 | + [Fact] |
| 188 | + public void ObjectToDictionary_ObjectWithAllNullProperties_ReturnsEmptyDictionary() |
| 189 | + { |
| 190 | + // Arrange |
| 191 | + var allNullObject = new { name = (string)null, address = (string)null }; |
| 192 | + |
| 193 | + // Act |
| 194 | + var result = PowertoolsLoggerHelpers.ObjectToDictionary(allNullObject); |
| 195 | + |
| 196 | + // Assert |
| 197 | + Assert.IsType<Dictionary<string, object>>(result); |
| 198 | + var dictionary = (Dictionary<string, object>)result; |
| 199 | + Assert.Empty(dictionary); |
| 200 | + } |
| 201 | + |
124 | 202 | public void Dispose()
|
125 | 203 | {
|
126 | 204 | PowertoolsLoggingSerializer.ConfigureNamingPolicy(LoggerOutputCase.Default);
|
|
0 commit comments