Skip to content

Commit

Permalink
TemplateParser keys not optional and missing keys not replaced
Browse files Browse the repository at this point in the history
  • Loading branch information
mkmurray authored and jeremydmiller committed Dec 12, 2013
1 parent d239392 commit e5efad2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
14 changes: 7 additions & 7 deletions src/FubuCore.Testing/TemplateParserTester.cs
Expand Up @@ -22,29 +22,29 @@ public void should_replace_a_single_variable()
}

[Test]
public void should_return_remaining_string_if_variable_not_found()
public void should_return_replacement_string_with_missing_key_not_replaced()
{
var template = "http://testreplacement.com/{optional}";
var template = "http://testreplacement.com/{notfound}/blah-blah";
var substitiutions = new Dictionary<string, string>();

TemplateParser
.Parse(template, substitiutions)
.ShouldEqual("http://testreplacement.com/");
.ShouldEqual("http://testreplacement.com/{notfound}/blah-blah");
}

[Test]
public void should_replace_variables_that_are_found_and_replace_unknown_with_nothing()
public void should_replace_variables_that_are_found_and_retain_missing_keys()
{
var template = "http://testreplacement.com/{replaced}/{still-replaced}/{optional}";
var template = "http://testreplacement.com/{replaced}/{missing-key}/{goodkey}";
var substitiutions = new Dictionary<string, string>
{
{"replaced", "gotreplaced"},
{"still-replaced", "stillreplaced"}
{"goodkey", "alsoreplaced"}
};

TemplateParser
.Parse(template, substitiutions)
.ShouldEqual("http://testreplacement.com/gotreplaced/stillreplaced/");
.ShouldEqual("http://testreplacement.com/gotreplaced/{missing-key}/alsoreplaced");
}

[Test]
Expand Down
6 changes: 5 additions & 1 deletion src/FubuCore/TemplateParser.cs
Expand Up @@ -66,10 +66,14 @@ static string parse(string template, IKeyValues values)
builder.Append(template.Substring(lastIndex, match.Index - lastIndex));

var key = match.Groups[TemplateGroup].Value;
if ((lastIndex == 0 || match.Index > lastIndex) && values.Has(key)) // Should we warn if we can't find the key?
if ((lastIndex == 0 || match.Index > lastIndex) && values.Has(key))
{
builder.Append(values.Get(key));
}
else
{
builder.Append("{{" + key + "}}"); // escape the missing key so that the while loop ContainsTemplate will terminate
}

lastIndex = match.Index + match.Length;
}
Expand Down

0 comments on commit e5efad2

Please sign in to comment.