44using SharpCoding . SharpHelpers ;
55using System ;
66using System . Collections . Generic ;
7- using System . Linq ;
87
98namespace SharpHelpers . UnitTest . Dictionary
109{
11-
1210 [ TestClass ]
1311 public class DictionaryTest
1412 {
1513 [ TestMethod ]
16- public void TestAddFormat ( )
14+ public void AddFormat_ShouldAddFormattedStringToDictionary ( )
1715 {
18- var dic = new Dictionary < int , DateTime > ( ) { { 1 , DateTime . Now } , { 2 , DateTime . Now } } ;
19-
16+ // Arrange
17+ var dictionary = new Dictionary < int , string > ( ) ;
18+
19+ // Act
20+ dictionary . AddFormat ( 1 , "Hello, {0}!" , "World" ) ;
21+
22+ // Assert
23+ Assert . AreEqual ( "Hello, World!" , dictionary [ 1 ] ) ;
2024 }
25+
2126 [ TestMethod ]
22- public void TestRemoveAll ( )
27+ [ ExpectedException ( typeof ( ArgumentNullException ) ) ]
28+ public void AddFormat_ShouldThrowArgumentNullException_WhenDictionaryIsNull ( )
2329 {
24- var dic = new Dictionary < int , DateTime > ( ) { { 1 , DateTime . Now } , { 2 , DateTime . Now } } ;
25- dic . RemoveAll ( a=> a . Key < 2 ) ;
26- Assert . IsTrue ( dic . Count ( ) == 1 ) ;
30+ // Arrange
31+ Dictionary < int , string > dictionary = null ;
2732
33+ // Act
34+ dictionary . AddFormat ( 1 , "Hello, {0}!" , "World" ) ;
35+
36+ // Assert - [ExpectedException] handles the assertion
2837 }
38+
2939 [ TestMethod ]
30- public void TestGetOrCreate ( )
40+ public void RemoveAll_ShouldRemoveItemsBasedOnCondition ( )
3141 {
32- var dic = new Dictionary < int , DateTime > ( ) { { 1 , DateTime . Now } , { 2 , DateTime . Now } } ;
33- Assert . IsNotNull ( dic . GetOrCreate ( 1 ) ) ;
34- Assert . IsNotNull ( dic . GetOrCreate ( 3 ) ) ;
42+ // Arrange
43+ var dictionary = new Dictionary < int , string >
44+ {
45+ { 1 , "Apple" } ,
46+ { 2 , "Banana" } ,
47+ { 3 , "Avocado" }
48+ } ;
3549
36-
50+ // Act
51+ dictionary . RemoveAll ( kvp => kvp . Value . StartsWith ( "A" ) ) ;
52+
53+ // Assert
54+ Assert . AreEqual ( 1 , dictionary . Count ) ;
55+ Assert . IsTrue ( dictionary . ContainsKey ( 2 ) ) ;
3756 }
38- }
3957
40- }
58+ [ TestMethod ]
59+ [ ExpectedException ( typeof ( ArgumentNullException ) ) ]
60+ public void RemoveAll_ShouldThrowArgumentNullException_WhenDictionaryIsNull ( )
61+ {
62+ // Arrange
63+ Dictionary < int , string > dictionary = null ;
64+
65+ // Act
66+ dictionary . RemoveAll ( kvp => kvp . Value . StartsWith ( "A" ) ) ;
67+
68+ // Assert - [ExpectedException] handles the assertion
69+ }
70+
71+ [ TestMethod ]
72+ public void GetOrCreate_ShouldCreateAndReturnNewValue_WhenKeyDoesNotExist ( )
73+ {
74+ // Arrange
75+ var dictionary = new Dictionary < int , List < string > > ( ) ;
76+
77+ // Act
78+ var result = dictionary . GetOrCreate ( 1 ) ;
79+
80+ // Assert
81+ Assert . IsNotNull ( result ) ;
82+ Assert . AreEqual ( 0 , result . Count ) ;
83+ Assert . IsTrue ( dictionary . ContainsKey ( 1 ) ) ;
84+ }
85+
86+ [ TestMethod ]
87+ public void AddOrUpdate_ShouldAddNewItem_WhenKeyDoesNotExist ( )
88+ {
89+ // Arrange
90+ var dictionary = new Dictionary < int , string > ( ) ;
91+
92+ // Act
93+ dictionary . AddOrUpdate ( 1 , "NewValue" ) ;
94+
95+ // Assert
96+ Assert . AreEqual ( "NewValue" , dictionary [ 1 ] ) ;
97+ }
98+
99+ [ TestMethod ]
100+ public void AddOrUpdate_ShouldUpdateExistingItem_WhenKeyExists ( )
101+ {
102+ // Arrange
103+ var dictionary = new Dictionary < int , string >
104+ {
105+ { 1 , "OldValue" }
106+ } ;
107+
108+ // Act
109+ dictionary . AddOrUpdate ( 1 , "UpdatedValue" ) ;
110+
111+ // Assert
112+ Assert . AreEqual ( "UpdatedValue" , dictionary [ 1 ] ) ;
113+ }
114+
115+ [ TestMethod ]
116+ public void RemoveIfExists_ShouldReturnTrueAndRemoveItem_WhenKeyExists ( )
117+ {
118+ // Arrange
119+ var dictionary = new Dictionary < int , string >
120+ {
121+ { 1 , "Value" }
122+ } ;
123+
124+ // Act
125+ var result = dictionary . RemoveIfExists ( 1 ) ;
126+
127+ // Assert
128+ Assert . IsTrue ( result ) ;
129+ Assert . IsFalse ( dictionary . ContainsKey ( 1 ) ) ;
130+ }
131+
132+ [ TestMethod ]
133+ public void RemoveIfExists_ShouldReturnFalse_WhenKeyDoesNotExist ( )
134+ {
135+ // Arrange
136+ var dictionary = new Dictionary < int , string > ( ) ;
137+
138+ // Act
139+ var result = dictionary . RemoveIfExists ( 1 ) ;
140+
141+ // Assert
142+ Assert . IsFalse ( result ) ;
143+ }
144+
145+ [ TestMethod ]
146+ public void Merge_ShouldMergeDictionariesAndUpdateValues ( )
147+ {
148+ // Arrange
149+ var dictionary = new Dictionary < int , string >
150+ {
151+ { 1 , "Value1" } ,
152+ { 2 , "Value2" }
153+ } ;
154+
155+ var otherDictionary = new Dictionary < int , string >
156+ {
157+ { 2 , "UpdatedValue2" } ,
158+ { 3 , "Value3" }
159+ } ;
160+
161+ // Act
162+ dictionary . Merge ( otherDictionary ) ;
163+
164+ // Assert
165+ Assert . AreEqual ( 3 , dictionary . Count ) ;
166+ Assert . AreEqual ( "UpdatedValue2" , dictionary [ 2 ] ) ;
167+ Assert . AreEqual ( "Value3" , dictionary [ 3 ] ) ;
168+ }
169+
170+ [ TestMethod ]
171+ public void ToReadableString_ShouldReturnFormattedStringRepresentationOfDictionary ( )
172+ {
173+ // Arrange
174+ var dictionary = new Dictionary < int , string >
175+ {
176+ { 1 , "Value1" } ,
177+ { 2 , "Value2" }
178+ } ;
179+
180+ // Act
181+ var result = dictionary . ToReadableString ( ) ;
182+
183+ // Assert
184+ Assert . AreEqual ( "{1: Value1, 2: Value2}" , result ) ;
185+ }
186+ }
187+ }
0 commit comments