Commits

Anonymous committed 4faa4cf

Refactoring

Comments (0)

Files changed (2)

TemplateStrings/TemplateString.cs

 
         private string GetValueFromDictionary(IDictionary<string, string> mapping, string value)
         {
-            return mapping[value].ToString();
+            return mapping[value];
         }
 
         public string Substitute(IDictionary<string, string> mapping, bool throwNoException)
         {
-            IList<TemplateValue> _TemplateValues = TemplateValues;
-
             Validate();
             var regex = new Regex(GetRegex(),
                                   RegexOptions.Compiled | RegexOptions.ExplicitCapture | RegexOptions.CultureInvariant);
                                                {
                                                    TemplateValue templateValue = null;
 
-                                                   for (int i = 0; i < _TemplateValues.Count; i++)
+                                                   for (int i = 0; i < TemplateValues.Count; i++)
                                                    {
                                                        if (match.Groups[(i + 1).ToString()].Success)
                                                        {
-                                                           templateValue = _TemplateValues[i];
+                                                           templateValue = TemplateValues[i];
                                                            break;
                                                        }
                                                    }
 
                                                    if (match.Groups["escapedstart"].Success)
                                                    {
-                                                       if (string.IsNullOrEmpty(templateValue.EndDelimiterEscape))
-                                                           return templateValue.StartDelimiter + match.Groups["named"] +
-                                                                  templateValue.EndDelimiter;
-                                                       else if (match.Groups["escapedend"].Success)
+                                                       if (string.IsNullOrEmpty(templateValue.EndDelimiterEscape) ||
+                                                           match.Groups["escapedend"].Success)
                                                            return templateValue.StartDelimiter + match.Groups["named"] +
                                                                   templateValue.EndDelimiter;
                                                        else if (match.Groups["end"].Success)
 
                                                    if (match.Groups["escapedend"].Success)
                                                    {
-                                                       if (string.IsNullOrEmpty(templateValue.StartDelimiterEscape))
-                                                           return templateValue.StartDelimiter + match.Groups["named"] +
-                                                                  templateValue.EndDelimiter;
-                                                       else if (match.Groups["escapedstart"].Success)
+                                                       if (string.IsNullOrEmpty(templateValue.StartDelimiterEscape) ||
+                                                           match.Groups["escapedstart"].Success)
                                                            return templateValue.StartDelimiter + match.Groups["named"] +
                                                                   templateValue.EndDelimiter;
                                                        else if (match.Groups["start"].Success)
 
                                                    if (match.Groups["start"].Success)
                                                    {
-                                                       if (string.IsNullOrEmpty(templateValue.EndDelimiter))
+                                                       if (string.IsNullOrEmpty(templateValue.EndDelimiter) ||
+                                                           match.Groups["end"].Success)
                                                            return GetValueFromDictionary(mapping,
                                                                                          match.Groups["named"].Value);
                                                        else if (match.Groups["escapedend"].Success)
                                                                GetValueFromDictionary(mapping,
                                                                                       match.Groups["named"].Value) +
                                                                templateValue.EndDelimiter;
-                                                       else if (match.Groups["end"].Success)
-                                                           return GetValueFromDictionary(mapping,
-                                                                                         match.Groups["named"].Value);
                                                    }
 
                                                    if (match.Groups["end"].Success)
                                                    {
-                                                       if (string.IsNullOrEmpty(templateValue.StartDelimiter))
+                                                       if (string.IsNullOrEmpty(templateValue.StartDelimiter) ||
+                                                           match.Groups["start"].Success)
                                                            return GetValueFromDictionary(mapping,
                                                                                          match.Groups["named"].Value);
                                                        else if (match.Groups["escapedstart"].Success)
                                                            return templateValue.StartDelimiter +
                                                                   GetValueFromDictionary(mapping,
                                                                                          match.Groups["named"].Value);
-                                                       else if (match.Groups["start"].Success)
-                                                           return GetValueFromDictionary(mapping,
-                                                                                         match.Groups["named"].Value);
                                                    }
 
                                                    //what if value not in dictionary but escaped startdelimiter
         }
     }
 
-    public class TemplateStringsPython : TemplateString
+    public class PythonsTemplateString : TemplateString
     {
-        public TemplateStringsPython(string template)
+        public PythonsTemplateString(string template)
             : base(template)
         {
         }
         }
     }
 
-    public class TemplateStringsMvc : TemplateString
+    public class MvcTemplateString : TemplateString
     {
-        public TemplateStringsMvc(string template)
+        public MvcTemplateString(string template)
             : base(template)
         {
         }
     {
         public static string Substitute(this string source, IDictionary<string, string> mapping)
         {
-            return (new TemplateStringsPython(source)).Substitute(mapping);
+            return (new PythonsTemplateString(source)).Substitute(mapping);
         }
 
         public static string Substitute(this string source, IDictionary<string, string> mapping, TemplateString strategy)

TemplateStringsTest/TemplateTest.cs

 
             for (int i = 0; i < pythonTemplateTestStrings.GetLength(0); i++)
             {
-                TemplateString templateString = new TemplateStringsPython(pythonTemplateTestStrings[i, 0]);
+                TemplateString templateString = new PythonsTemplateString(pythonTemplateTestStrings[i, 0]);
 
                 Assert.AreEqual(pythonTemplateTestStrings[i, 1], templateString.Substitute(pythonTemplateTestDictionary));
             }
 
             for (int i = 0; i < mvcTemplateTestStrings.GetLength(0); i++)
             {
-                TemplateString templateString = new TemplateStringsMvc(mvcTemplateTestStrings[i, 0]);
+                TemplateString templateString = new MvcTemplateString(mvcTemplateTestStrings[i, 0]);
 
                 Assert.AreEqual(mvcTemplateTestStrings[i, 1], templateString.Substitute(mvcTemplateTestDictionary));
             }