@@ -77,25 +77,160 @@ namespace shellanything { namespace test
7777 ASSERT_EQ (" " , pmgr.GetProperty (" unknown" ) );
7878 }
7979 // --------------------------------------------------------------------------------------------------
80- TEST_F (TestPropertyManager, testExpand )
80+ TEST_F (TestPropertyManager, testExpandDefault )
8181 {
8282 PropertyManager & pmgr = PropertyManager::GetInstance ();
83+
8384 pmgr.SetProperty (" job" , " actor" );
8485 pmgr.SetProperty (" name" , " Brad Pitt" );
8586 pmgr.SetProperty (" age" , " 53" );
87+
88+ std::string expanded = pmgr.Expand (" ${name} is a ${age} years old ${job}." );
89+
90+ // Assert all properties was replaced
91+ ASSERT_EQ (" Brad Pitt is a 53 years old actor." , expanded);
92+ }
93+ // --------------------------------------------------------------------------------------------------
94+ TEST_F (TestPropertyManager, testExpandUnknownProperties)
95+ {
96+ PropertyManager & pmgr = PropertyManager::GetInstance ();
97+
8698 pmgr.SetProperty (" animal" , " fox" );
8799
88- // default behavior
100+ // Properties 'color' and 'characteristics' are unknown.
101+ std::string expanded = pmgr.Expand (" The quick ${color} ${animal} jumps over the ${characteristics} dog." );
102+
103+ // Assert unknown properties was not replaced. Left as is.
104+ ASSERT_EQ (" The quick ${color} fox jumps over the ${characteristics} dog." , expanded);
105+ }
106+ // --------------------------------------------------------------------------------------------------
107+ TEST_F (TestPropertyManager, testExpandMalformed)
108+ {
109+ PropertyManager & pmgr = PropertyManager::GetInstance ();
110+
111+ // Property ${color is malformed
112+ std::string expanded = pmgr.Expand (" The quick ${color fox jumps over the lazy dog." );
113+
114+ // Assert the text was left unchanged
115+ ASSERT_EQ (" The quick ${color fox jumps over the lazy dog." , expanded);
116+ }
117+ // --------------------------------------------------------------------------------------------------
118+ TEST_F (TestPropertyManager, testExpandRecursive)
119+ {
120+ PropertyManager & pmgr = PropertyManager::GetInstance ();
121+
122+ // Assert that expanding ${second} to ${third} will also expand ${third} into "Lambs"
89123 {
90- std::string expanded = pmgr.Expand (" ${name} is a ${age} years old ${job}." );
91- ASSERT_EQ (" Brad Pitt is a 53 years old actor." , expanded);
124+ pmgr.Clear ();
125+ pmgr.SetProperty (" first" , " Silence" ); // A
126+ pmgr.SetProperty (" second" , " ${third}" ); // B
127+ pmgr.SetProperty (" third" , " Lambs" ); // C
128+
129+ // The second property should be expanded by a property reference ${third}
130+ std::string expanded = pmgr.Expand (" ${first} of the ${second}" );
131+
132+ // Assert the property was expanded recursively
133+ ASSERT_EQ (" Silence of the Lambs" , expanded);
92134 }
93135
94- // unknown properties
136+ // Assert that property registration order have no impact.
137+ // Test all permutations of ABC.
95138 {
96- std::string expanded = pmgr.Expand (" The quick ${color} ${animal} jumps over the ${characteristics} dog." );
97- ASSERT_EQ (" The quick ${color} fox jumps over the ${characteristics} dog." , expanded);
139+ pmgr.Clear ();
140+ pmgr.SetProperty (" first" , " Silence" ); // A
141+ pmgr.SetProperty (" third" , " Lambs" ); // C
142+ pmgr.SetProperty (" second" , " ${third}" ); // B
143+
144+ // The second property should be expanded by a property reference ${third}
145+ std::string expanded = pmgr.Expand (" ${first} of the ${second}" );
146+
147+ // Assert the property was expanded recursively
148+ ASSERT_EQ (" Silence of the Lambs" , expanded);
98149 }
150+ {
151+ pmgr.Clear ();
152+ pmgr.SetProperty (" second" , " ${third}" ); // B
153+ pmgr.SetProperty (" first" , " Silence" ); // A
154+ pmgr.SetProperty (" third" , " Lambs" ); // C
155+
156+ // The second property should be expanded by a property reference ${third}
157+ std::string expanded = pmgr.Expand (" ${first} of the ${second}" );
158+
159+ // Assert the property was expanded recursively
160+ ASSERT_EQ (" Silence of the Lambs" , expanded);
161+ }
162+ {
163+ pmgr.Clear ();
164+ pmgr.SetProperty (" second" , " ${third}" ); // B
165+ pmgr.SetProperty (" third" , " Lambs" ); // C
166+ pmgr.SetProperty (" first" , " Silence" ); // A
167+
168+ // The second property should be expanded by a property reference ${third}
169+ std::string expanded = pmgr.Expand (" ${first} of the ${second}" );
170+
171+ // Assert the property was expanded recursively
172+ ASSERT_EQ (" Silence of the Lambs" , expanded);
173+ }
174+ {
175+ pmgr.Clear ();
176+ pmgr.SetProperty (" third" , " Lambs" ); // C
177+ pmgr.SetProperty (" second" , " ${third}" ); // B
178+ pmgr.SetProperty (" first" , " Silence" ); // A
179+
180+ // The second property should be expanded by a property reference ${third}
181+ std::string expanded = pmgr.Expand (" ${first} of the ${second}" );
182+
183+ // Assert the property was expanded recursively
184+ ASSERT_EQ (" Silence of the Lambs" , expanded);
185+ }
186+ {
187+ pmgr.Clear ();
188+ pmgr.SetProperty (" third" , " Lambs" ); // C
189+ pmgr.SetProperty (" first" , " Silence" ); // A
190+ pmgr.SetProperty (" second" , " ${third}" ); // B
191+
192+ // The second property should be expanded by a property reference ${third}
193+ std::string expanded = pmgr.Expand (" ${first} of the ${second}" );
194+
195+ // Assert the property was expanded recursively
196+ ASSERT_EQ (" Silence of the Lambs" , expanded);
197+ }
198+
199+ }
200+ // --------------------------------------------------------------------------------------------------
201+ TEST_F (TestPropertyManager, testExpandRecursiveReverseAlphabeticalOrder)
202+ {
203+ PropertyManager & pmgr = PropertyManager::GetInstance ();
204+
205+ // Note:
206+ // Using text1 for the embedded property replacement will not work as properties are replaced in alphabetical order.
207+ // In other words, once text3 is replaced for the string "${text1}", the text1 property is already processed.
208+ // Using text4 instead of text1 for the replacement would fix the issue.
209+
210+ pmgr.Clear ();
211+ pmgr.SetProperty (" text1" , " with you" );
212+ pmgr.SetProperty (" text2" , " the Force" );
213+ pmgr.SetProperty (" text3" , " ${text1}" );
214+
215+ // Try replacement in reverse alphabetical order.
216+ std::string expanded = pmgr.Expand (" May ${text2} be ${text3}" );
217+
218+ // Assert not all place holder was replaced
219+ ASSERT_EQ (" May the Force be ${text1}" , expanded);
220+ }
221+ // --------------------------------------------------------------------------------------------------
222+ TEST_F (TestPropertyManager, testExpandDouble)
223+ {
224+ PropertyManager & pmgr = PropertyManager::GetInstance ();
225+
226+ pmgr.SetProperty (" first" , " James" );
227+ pmgr.SetProperty (" last" , " Bond" );
228+
229+ // Property ${action} should be expanded twice
230+ std::string expanded = pmgr.Expand (" ${last}. ${first} ${last}." );
231+
232+ // Assert the ${action} was left unchanged
233+ ASSERT_EQ (" Bond. James Bond." , expanded);
99234 }
100235 // --------------------------------------------------------------------------------------------------
101236 TEST_F (TestPropertyManager, testEnvironmentVariableProperty)
0 commit comments