diff --git a/Local.testsettings b/Local.testsettings index 8ff3125..6522637 100644 --- a/Local.testsettings +++ b/Local.testsettings @@ -1,26 +1,26 @@ - - - These are default test settings for a local test run. - - - - - - - - - - - - - - - - - - - - - - + + + These are default test settings for a local test run. + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/TraceAndTestImpact.testsettings b/TraceAndTestImpact.testsettings index 88f9753..99dbff3 100644 --- a/TraceAndTestImpact.testsettings +++ b/TraceAndTestImpact.testsettings @@ -1,21 +1,21 @@ - - - These are test settings for Trace and Test Impact. - - - - - - - - - - - - - - - - - + + + These are test settings for Trace and Test Impact. + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/mustache-sharp.sln b/mustache-sharp.sln index 98d40ad..586f44c 100644 --- a/mustache-sharp.sln +++ b/mustache-sharp.sln @@ -1,36 +1,36 @@ - -Microsoft Visual Studio Solution File, Format Version 11.00 -# Visual Studio 2010 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "mustache-sharp", "mustache-sharp\mustache-sharp.csproj", "{D71B378F-A4BA-4263-A4F0-07A49A0C528D}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "mustache-sharp.test", "mustache-sharp.test\mustache-sharp.test.csproj", "{7F607362-0680-4751-B1DC-621219294AE3}" -EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{25414E49-67E6-4B8D-8AD8-78C70F8992A7}" - ProjectSection(SolutionItems) = preProject - Local.testsettings = Local.testsettings - mustache-sharp.vsmdi = mustache-sharp.vsmdi - TraceAndTestImpact.testsettings = TraceAndTestImpact.testsettings - EndProjectSection -EndProject -Global - GlobalSection(TestCaseManagementSettings) = postSolution - CategoryFile = mustache-sharp.vsmdi - EndGlobalSection - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {D71B378F-A4BA-4263-A4F0-07A49A0C528D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {D71B378F-A4BA-4263-A4F0-07A49A0C528D}.Debug|Any CPU.Build.0 = Debug|Any CPU - {D71B378F-A4BA-4263-A4F0-07A49A0C528D}.Release|Any CPU.ActiveCfg = Release|Any CPU - {D71B378F-A4BA-4263-A4F0-07A49A0C528D}.Release|Any CPU.Build.0 = Release|Any CPU - {7F607362-0680-4751-B1DC-621219294AE3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {7F607362-0680-4751-B1DC-621219294AE3}.Debug|Any CPU.Build.0 = Debug|Any CPU - {7F607362-0680-4751-B1DC-621219294AE3}.Release|Any CPU.ActiveCfg = Release|Any CPU - {7F607362-0680-4751-B1DC-621219294AE3}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection -EndGlobal + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2012 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "mustache-sharp", "mustache-sharp\mustache-sharp.csproj", "{D71B378F-A4BA-4263-A4F0-07A49A0C528D}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "mustache-sharp.test", "mustache-sharp.test\mustache-sharp.test.csproj", "{7F607362-0680-4751-B1DC-621219294AE3}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{25414E49-67E6-4B8D-8AD8-78C70F8992A7}" + ProjectSection(SolutionItems) = preProject + Local.testsettings = Local.testsettings + mustache-sharp.vsmdi = mustache-sharp.vsmdi + TraceAndTestImpact.testsettings = TraceAndTestImpact.testsettings + EndProjectSection +EndProject +Global + GlobalSection(TestCaseManagementSettings) = postSolution + CategoryFile = mustache-sharp.vsmdi + EndGlobalSection + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {D71B378F-A4BA-4263-A4F0-07A49A0C528D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D71B378F-A4BA-4263-A4F0-07A49A0C528D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D71B378F-A4BA-4263-A4F0-07A49A0C528D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D71B378F-A4BA-4263-A4F0-07A49A0C528D}.Release|Any CPU.Build.0 = Release|Any CPU + {7F607362-0680-4751-B1DC-621219294AE3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7F607362-0680-4751-B1DC-621219294AE3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7F607362-0680-4751-B1DC-621219294AE3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7F607362-0680-4751-B1DC-621219294AE3}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/mustache-sharp.test/FormatCompilerTester.cs b/mustache-sharp.test/FormatCompilerTester.cs index 9359192..2010942 100644 --- a/mustache-sharp.test/FormatCompilerTester.cs +++ b/mustache-sharp.test/FormatCompilerTester.cs @@ -1158,6 +1158,32 @@ Item Number: foo
#endregion + + /// + /// If the two values don't match, the content of an eq statement should not be printed. + /// + [TestMethod] + public void TestCompile_Eq_EvaluatesToFalse_SkipsContent() { + FormatCompiler parser = new FormatCompiler(); + const string format = "Before{{#eq OneValue OtherValue}}Content{{/eq}}After"; + Generator generator = parser.Compile(format); + string result = generator.Render(new {OneValue = "Foo", OtherValue = "Bar"} ); + Assert.AreEqual("BeforeAfter", result, "The wrong text was generated."); + } + + /// + /// If the two values match, the content of an eq statement should be printed. + /// + [TestMethod] + public void TestCompile_Eq_EvaluatesToTrue_PrintsContent() { + FormatCompiler parser = new FormatCompiler(); + const string format = "Before{{#eq OneValue OtherValue}}Content{{/eq}}After"; + Generator generator = parser.Compile(format); + string result = generator.Render(new { OneValue = "Foo", OtherValue = "Foo" }); + Assert.AreEqual("BeforeContentAfter", result, "The wrong text was generated."); + } + + #region Compound Tags /// diff --git a/mustache-sharp.vsmdi b/mustache-sharp.vsmdi index 881fd48..c62973b 100644 --- a/mustache-sharp.vsmdi +++ b/mustache-sharp.vsmdi @@ -1,6 +1,6 @@ - - - - - + + + + + \ No newline at end of file diff --git a/mustache-sharp/ElseTagDefinition.cs b/mustache-sharp/ElseTagDefinition.cs index 04635ab..ceebebe 100644 --- a/mustache-sharp/ElseTagDefinition.cs +++ b/mustache-sharp/ElseTagDefinition.cs @@ -29,7 +29,7 @@ namespace Mustache /// protected override IEnumerable GetClosingTags() { - return new string[] { "if" }; + return new string[] { "if","eq" }; } /// diff --git a/mustache-sharp/EqTagDefinition.cs b/mustache-sharp/EqTagDefinition.cs new file mode 100644 index 0000000..be70286 --- /dev/null +++ b/mustache-sharp/EqTagDefinition.cs @@ -0,0 +1,89 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Mustache { + /// + /// Defines a tag that conditionally prints its content, based on whether the passed in values are equal + /// + internal sealed class EqTagDefinition : ConditionTagDefinition { + private const string ConditionParameter = "condition"; + private const string TargetValueParameter = "targetValue"; + + /// + /// Initializes a new instance of a IfTagDefinition. + /// + public EqTagDefinition() + : base("eq") + {} + + /// + /// Gets whether the tag only exists within the scope of its parent. + /// + protected override bool GetIsContextSensitive() + { + return false; + } + + /// + /// Gets the parameters that can be passed to the tag. + /// + /// The parameters. + protected override IEnumerable GetParameters() { + return new[] { new TagParameter(ConditionParameter) { IsRequired = true }, + new TagParameter(TargetValueParameter){IsRequired = true} }; + } + + + /// + /// Gets whether the primary generator group should be used to render the tag. + /// + /// The arguments passed to the tag. + /// + /// True if the primary generator group should be used to render the tag; + /// otherwise, false to use the secondary group. + /// + public override bool ShouldGeneratePrimaryGroup(Dictionary arguments) { + object condition = arguments[ConditionParameter]; + object targetValue = arguments[TargetValueParameter]; + return isConditionSatisfied(condition,targetValue); + } + + private bool isConditionSatisfied(object condition,object targetValue) { + if (condition == null || targetValue == null) { + if (condition == null && targetValue == null) { + return true; + } + return false; + } + + + if ((condition is double || condition is int) && (targetValue is double || targetValue is int) ) { + return Math.Abs((double) condition - (double) targetValue) < 0.0; + } + + if (condition is string && targetValue is string) { + return condition.ToString().Equals(targetValue.ToString(), StringComparison.OrdinalIgnoreCase); + } + + if (condition is bool && targetValue is bool) { + return (bool) condition == (bool) targetValue; + } + + + if (condition is Char && targetValue is Char) { + return (Char)condition == (Char)targetValue; + } + + return false; + } + + /// + /// Gets the parameters that are used to create a new child context. + /// + /// The parameters that are used to create a new child context. + public override IEnumerable GetChildContextParameters() { + return new TagParameter[0]; + } + } +} diff --git a/mustache-sharp/FormatCompiler.cs b/mustache-sharp/FormatCompiler.cs index d667668..a0e9db1 100644 --- a/mustache-sharp/FormatCompiler.cs +++ b/mustache-sharp/FormatCompiler.cs @@ -41,6 +41,9 @@ namespace Mustache _tagLookup.Add(newlineDefinition.Name, newlineDefinition); SetTagDefinition setDefinition = new SetTagDefinition(); _tagLookup.Add(setDefinition.Name, setDefinition); + EqTagDefinition eqTagDefinition = new EqTagDefinition(); + _tagLookup.Add(eqTagDefinition.Name,eqTagDefinition); + } /// diff --git a/mustache-sharp/mustache-sharp.csproj b/mustache-sharp/mustache-sharp.csproj index 04e0435..8162b23 100644 --- a/mustache-sharp/mustache-sharp.csproj +++ b/mustache-sharp/mustache-sharp.csproj @@ -40,6 +40,7 @@ +