1+ using System . Text . Json ;
2+
13namespace Tests ;
24
35[ TestFixture ]
@@ -17,7 +19,7 @@ public void GivenObjectWithJsonPropertyAttribute_WhenRetrievalUsingReflection_Th
1719 var result = JsonHelper . RetrievalUsingReflection ( sales ) ;
1820
1921 // Assert
20- CollectionAssert . AreEquivalent ( new [ ] { "Slx_PER_Year" , "Slx_PER_DAY" } , result ) ;
22+ Assert . That ( result , Is . EquivalentTo ( new [ ] { "Slx_PER_Year" , "Slx_PER_DAY" } ) ) ;
2123 }
2224
2325 [ Test ]
@@ -34,7 +36,20 @@ public void GivenSalesObject_WhenRetrievingKeyValuesUsingSerialization_ThenRetur
3436 var result = JsonHelper . RetrievalUsingSerialization ( sales ) ;
3537
3638 // Assert
37- CollectionAssert . AreEqual ( new [ ] { "Slx_PER_Year" , "Slx_PER_DAY" } , result ) ;
39+ Assert . That ( result , Is . EqualTo ( new [ ] { "Slx_PER_Year" , "Slx_PER_DAY" } ) ) ;
40+ }
41+
42+ [ Test ]
43+ public void GivenObjectWithNonStringProperty_WhenRetrievingUsingSerialization_ThenReturnsAllNames ( )
44+ {
45+ // Arrange
46+ var testObject = new NewtonsoftObjectWithNumber { Name = "Widget" , Quantity = 3 } ;
47+
48+ // Act
49+ var result = JsonHelper . RetrievalUsingSerialization ( testObject ) ;
50+
51+ // Assert
52+ Assert . That ( result , Is . EqualTo ( new [ ] { "product_name" , "Quantity" } ) ) ;
3853 }
3954
4055 [ Test ]
@@ -51,6 +66,83 @@ public void Given_ObjectWithJsonPropertyAttributes_WhenGetJsonPropertyNames_Then
5166 Assert . That ( result , Is . EqualTo ( expected ) ) ;
5267 }
5368
69+ [ Test ]
70+ public void GivenSalesType_WhenGetSerializedPropertyNames_ThenReturnJsonPropertyNames ( )
71+ {
72+ // Act
73+ string [ ] result = JsonTextImplementation . GetSerializedPropertyNames < Sales > ( ) ;
74+
75+ // Assert
76+ Assert . That ( result , Is . EqualTo ( new [ ] { "Slx_PER_Year" , "Slx_PER_DAY" } ) ) ;
77+ }
78+
79+ [ Test ]
80+ public void GivenWebDefaults_WhenGetSerializedPropertyNames_ThenNamingPolicyAppliesOnlyWithoutAttribute ( )
81+ {
82+ // Arrange
83+ var options = new JsonSerializerOptions ( JsonSerializerDefaults . Web ) ;
84+
85+ // Act
86+ string [ ] result = JsonTextImplementation . GetSerializedPropertyNames < TestObjectWithAttributes > ( options ) ;
87+
88+ // Assert
89+ Assert . That ( result , Is . EqualTo ( new [ ] { "FirstName" , "LastName" , "age" } ) ) ;
90+ }
91+
92+ [ Test ]
93+ public void GivenWebDefaults_WhenSerializing_ThenOutputUsesSameNamesAsGetSerializedPropertyNames ( )
94+ {
95+ // Arrange
96+ var options = new JsonSerializerOptions ( JsonSerializerDefaults . Web ) ;
97+ var testObject = new TestObjectWithAttributes { Name = "Ana" , Surname = "Lee" , Age = 30 } ;
98+
99+ // Act
100+ string json = System . Text . Json . JsonSerializer . Serialize ( testObject , options ) ;
101+ string [ ] namesInJson = JsonDocument . Parse ( json ) . RootElement
102+ . EnumerateObject ( )
103+ . Select ( property => property . Name )
104+ . ToArray ( ) ;
105+
106+ // Assert
107+ Assert . That ( namesInJson ,
108+ Is . EqualTo ( JsonTextImplementation . GetSerializedPropertyNames < TestObjectWithAttributes > ( options ) ) ) ;
109+ }
110+
111+ [ Test ]
112+ public void GivenIgnoredProperty_WhenGetSerializedPropertyNames_ThenSkipsIt ( )
113+ {
114+ // Act
115+ string [ ] result = JsonTextImplementation . GetSerializedPropertyNames < ObjectWithIgnoredProperty > ( ) ;
116+
117+ // Assert
118+ Assert . That ( result , Is . EqualTo ( new [ ] { "id" } ) ) ;
119+ }
120+
121+ [ Test ]
122+ public void GivenIgnoredProperty_WhenGetJsonPropertyNames_ThenReflectionStillReturnsIt ( )
123+ {
124+ // Act
125+ string [ ] result = JsonTextImplementation . GetJsonPropertyNames ( new ObjectWithIgnoredProperty ( ) ) ;
126+
127+ // Assert
128+ Assert . That ( result , Is . EqualTo ( new [ ] { "id" , "Password" } ) ) ;
129+ }
130+
131+ [ Test ]
132+ public void GivenCustomer_WhenComparingReflectionWithWebDefaultsMetadata_ThenOnlyMetadataAppliesPolicyAndIgnore ( )
133+ {
134+ // Arrange
135+ var webOptions = new JsonSerializerOptions ( JsonSerializerDefaults . Web ) ;
136+
137+ // Act
138+ string [ ] fromReflection = JsonTextImplementation . GetJsonPropertyNames ( new Customer ( ) ) ;
139+ string [ ] fromSerializer = JsonTextImplementation . GetSerializedPropertyNames < Customer > ( webOptions ) ;
140+
141+ // Assert
142+ Assert . That ( fromReflection , Is . EqualTo ( new [ ] { "FirstName" , "Password" , "Age" } ) ) ;
143+ Assert . That ( fromSerializer , Is . EqualTo ( new [ ] { "FirstName" , "age" } ) ) ;
144+ }
145+
54146 [ Test ]
55147 public void GivenValidProductObject_WhenGettingJsonPropertyForProductName_ThenReturnCorrectProperty ( )
56148 {
@@ -65,10 +157,9 @@ public void GivenValidProductObject_WhenGettingJsonPropertyForProductName_ThenRe
65157 var property = ResolverHelper . GetJsonProperty ( sales , "Slx_per_day" ) ;
66158
67159 // Assert
68- Assert . IsNotNull ( property ) ;
160+ Assert . That ( property , Is . Not . Null ) ;
69161 Assert . That ( property . PropertyName , Is . EqualTo ( "Slx_PER_DAY" ) ) ;
70162 Assert . That ( property . PropertyType , Is . EqualTo ( typeof ( string ) ) ) ;
71-
72163 }
73164
74165 private class TestObjectWithAttributes
@@ -81,4 +172,21 @@ private class TestObjectWithAttributes
81172
82173 public int Age { get ; set ; }
83174 }
175+
176+ private class NewtonsoftObjectWithNumber
177+ {
178+ [ JsonProperty ( "product_name" ) ]
179+ public string ? Name { get ; set ; }
180+
181+ public int Quantity { get ; set ; }
182+ }
183+
184+ private class ObjectWithIgnoredProperty
185+ {
186+ [ JsonPropertyName ( "id" ) ]
187+ public int Id { get ; set ; }
188+
189+ [ System . Text . Json . Serialization . JsonIgnore ]
190+ public string ? Password { get ; set ; }
191+ }
84192}
0 commit comments