Skip to content

Commit e70ea9b

Browse files
committed
Created PropertyManager::ExpandOnce() that mimic the previous implementation of PropertyManager::Expand().
Modified PropertyManager::Expand() to expand the given string until all property references are expanded. For issue #72.
1 parent 061180f commit e70ea9b

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

src/PropertyManager.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,18 @@ namespace shellanything
129129
}
130130

131131
std::string PropertyManager::Expand(const std::string & value) const
132+
{
133+
std::string previous = value;
134+
std::string output = ExpandOnce(value);
135+
while(output != previous)
136+
{
137+
previous = output;
138+
output = ExpandOnce(output);
139+
}
140+
return output;
141+
}
142+
143+
std::string PropertyManager::ExpandOnce(const std::string & value) const
132144
{
133145
//Process expansion in-place
134146
std::string output;

src/PropertyManager.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,24 @@ namespace shellanything
9090
/// Expands the given string by replacing property variable reference by the actual variable's value.
9191
/// The syntax of a property variable reference is the following: `${variable-name}` where `variable-name` is the name of a variable.
9292
/// </summary>
93+
/// <remarks>
94+
/// The string is expanded until there are no change in the given string.
95+
/// </remarks>
9396
/// <param name="value">The given value to expand.</param>
9497
/// <returns>Returns a copy of the given value with the property references expanded.</returns>
9598
std::string Expand(const std::string & value) const;
9699

100+
/// <summary>
101+
/// Expands the given string by replacing property variable reference by the actual variable's value.
102+
/// The syntax of a property variable reference is the following: `${variable-name}` where `variable-name` is the name of a variable.
103+
/// </summary>
104+
/// <remarks>
105+
/// The string is expanded in a single pass. Some property reference may not be expanded after the first pass.
106+
/// </remarks>
107+
/// <param name="value">The given value to expand.</param>
108+
/// <returns>Returns a copy of the given value with the property references expanded.</returns>
109+
std::string ExpandOnce(const std::string & value) const;
110+
97111
private:
98112

99113
void RegisterEnvironmentVariables();

test/TestPropertyManager.cpp

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ namespace shellanything { namespace test
115115
ASSERT_EQ("The quick ${color fox jumps over the lazy dog.", expanded);
116116
}
117117
//--------------------------------------------------------------------------------------------------
118-
TEST_F(TestPropertyManager, testExpandRecursivePermutations)
118+
TEST_F(TestPropertyManager, testExpandPermutations)
119119
{
120120
PropertyManager & pmgr = PropertyManager::GetInstance();
121121

@@ -213,6 +213,50 @@ namespace shellanything { namespace test
213213
ASSERT_EQ("E.T. phone home", expanded);
214214
}
215215
//--------------------------------------------------------------------------------------------------
216+
TEST_F(TestPropertyManager, testExpandRecursive1)
217+
{
218+
PropertyManager & pmgr = PropertyManager::GetInstance();
219+
220+
pmgr.SetProperty("text1", "${text2}");
221+
pmgr.SetProperty("text2", "${text3}");
222+
pmgr.SetProperty("text3", "You're gonna need a bigger boat.");
223+
224+
//Property ${text1} should expand up to the expected movie quote.
225+
std::string expanded = pmgr.Expand("${text1}");
226+
227+
//Assert the string was properly expanded.
228+
ASSERT_EQ("You're gonna need a bigger boat.", expanded);
229+
}
230+
//--------------------------------------------------------------------------------------------------
231+
TEST_F(TestPropertyManager, testExpandRecursive2)
232+
{
233+
PropertyManager & pmgr = PropertyManager::GetInstance();
234+
235+
pmgr.SetProperty("varname", "foo");
236+
pmgr.SetProperty("foo", "${quote}");
237+
pmgr.SetProperty("quote", "Say 'hello' to my little friend!");
238+
239+
//Property ${varname} should expand up to the expected movie quote.
240+
std::string expanded = pmgr.Expand("${${varname}}");
241+
242+
//Assert the string was properly expanded.
243+
ASSERT_EQ("Say 'hello' to my little friend!", expanded);
244+
}
245+
//--------------------------------------------------------------------------------------------------
246+
TEST_F(TestPropertyManager, testExpandOnceDefault)
247+
{
248+
PropertyManager & pmgr = PropertyManager::GetInstance();
249+
250+
pmgr.SetProperty("varname", "quote");
251+
pmgr.SetProperty("quote", "Say 'hello' to my little friend!");
252+
253+
//Property ${varname} should NOT expand up to the expected movie quote.
254+
std::string expanded = pmgr.ExpandOnce("${${varname}}");
255+
256+
//Assert the string was properly expanded.
257+
ASSERT_EQ("${quote}", expanded);
258+
}
259+
//--------------------------------------------------------------------------------------------------
216260
TEST_F(TestPropertyManager, testEnvironmentVariableProperty)
217261
{
218262
PropertyManager & pmgr = PropertyManager::GetInstance();

0 commit comments

Comments
 (0)