Commits

Anonymous committed a2f2872

more work

  • Participants
  • Parent commits f739a71

Comments (0)

Files changed (4)

 glob:*.suo
 glob:TemplateStrings/bin/Debug/*
 glob:TemplateStrings/obj/Debug/*
+glob:TemplateStringsTest/bin/Debug/*

File TemplateStrings/Template.cs

 using System;
 using System.Collections.Generic;
-using System.Collections.ObjectModel;
 using System.Text;
 using System.Text.RegularExpressions;
 
 {
     public class TemplateValue
     {
-        private string _StartDelimiter;
-        public string StartDelimiter
+        public string StartDelimiter { get; set; }
+
+        public string StartDelimiterEscape { get; set; }
+
+        public string EndDelimiterEscape { get; set; }
+
+        public string EndDelimiter { get; set; }
+
+        public string IdPattern { get; set; }
+
+        protected internal virtual string GetRegexString()
         {
-            get
-            {
-                return _StartDelimiter;
-            }
-            set
-            {
-                _StartDelimiter = value;
-            }
-        }
-
-        private string _StartDelimiterEscape;
-        public string StartDelimiterEscape
-        {
-            get
-            {
-                return _StartDelimiterEscape;
-            }
-            set
-            {
-                _StartDelimiterEscape = value;
-            }
-        }
-
-        private string _EndDelimiterEscape;
-        public string EndDelimiterEscape
-        {
-            get
-            {
-                return _EndDelimiterEscape;
-            }
-            set
-            {
-                _EndDelimiterEscape = value;
-            }
-        }
-
-        private string _EndDelimiter;
-        public string EndDelimiter
-        {
-            get
-            {
-                return _EndDelimiter;
-            }
-            set
-            {
-                _EndDelimiter = value;
-            }
-        }
-
-        private string _IdPattern;
-        public string IdPattern
-        {
-            get
-            {
-                return _IdPattern;
-            }
-            set
-            {
-                _IdPattern = value;
-            }
-        }
-
-        internal protected virtual string GetRegexString()
-        {
-            StringBuilder builder = new StringBuilder();
+            var builder = new StringBuilder();
 
             bool hasStartDelimiterEscape = !string.IsNullOrEmpty(StartDelimiterEscape);
             bool hasStartDelimiter = !string.IsNullOrEmpty(StartDelimiter);
 
             return builder.ToString();
         }
-
-
     }
 
-    public class TemplateValueCollection : Collection<TemplateValue>
-    { }
 
     public abstract class Template
     {
-        protected abstract IList<TemplateValue> TemplateValues
-        { get;}
-
-        private string _template;
-        public string template
-        {
-            get
-            {
-                return _template;
-            }
-            set
-            {
-                _template = value;
-            }
-        }
-
         protected Template(string template)
         {
             this.template = template;
         }
 
+        protected abstract IList<TemplateValue> TemplateValues { get; }
+
+        public string template { get; private set; }
+
         private void Validate()
         {
             foreach (TemplateValue val in TemplateValues)
                     throw new ArgumentException("When an EndDelimiter is used you must set a StartDelimiter as well.");
 
                 if (!string.IsNullOrEmpty(val.StartDelimiterEscape) && string.IsNullOrEmpty(val.StartDelimiter))
-                    throw new ArgumentException("When an StartDelimiterEscape is used you must use set a StartDelimiter as well.");
+                    throw new ArgumentException(
+                        "When an StartDelimiterEscape is used you must use set a StartDelimiter as well.");
 
                 if (!string.IsNullOrEmpty(val.EndDelimiterEscape) && string.IsNullOrEmpty(val.EndDelimiter))
-                    throw new ArgumentException("When an EndDelimiterEscape is used you must use set a EndDelimiter as well.");
+                    throw new ArgumentException(
+                        "When an EndDelimiterEscape is used you must use set a EndDelimiter as well.");
 
                 if (string.IsNullOrEmpty(val.IdPattern))
                     throw new ArgumentException("You must set an IdPattern");
             }
         }
 
-        public string Substitute(IDictionary<string, string> mapping)
+        public string Substitute(IDictionary<string, object> mapping)
         {
             return Substitute(mapping, false);
         }
 
-        public string GetRegex()
+        private string GetRegex()
         {
-            StringBuilder builder = new StringBuilder(200);
+            var builder = new StringBuilder(200);
 
             for (int i = 0; i < TemplateValues.Count; i++)
             {
             return builder.ToString();
         }
 
-        public static string[,] PythonTestInternal=
-                                 {{"Lorem $ipsum dolor sit amet.","Lorem test1 dolor sit amet."},
-                                  {"Lorem ${ipsum} dolor sit amet.","Lorem test1 dolor sit amet."},
-                                  {"Lorem $$ipsum dolor sit amet.","Lorem $ipsum dolor sit amet."},  //2  //what differ
-                                  {"Lorem $${ipsum} dolor sit amet.","Lorem ${ipsum} dolor sit amet."}};  //2
 
-
-        public static string[,] MvcTestInternal=
-                                 {{"Lorem {ipsum} dolor sit amet.","Lorem test1 dolor sit amet."},
-                                  {"Lorem {{ipsum}} dolor sit amet.","Lorem {ipsum} dolor sit amet."},   //1
-                                  {"Lorem {ipsum}} dolor sit amet.","Lorem test1} dolor sit amet."},
-                                  {"Lorem {{ipsum} dolor sit amet.","Lorem {test1 dolor sit amet."},
-                                  {"Lorem {ipsum dolor sit amet.","Lorem {ipsum dolor sit amet."},
-                                  {"Lorem ipsum} dolor sit amet.","Lorem ipsum} dolor sit amet."},
-                                  {"Lorem {{ipsum dolor sit amet.","Lorem {{ipsum dolor sit amet."},
-                                  {"Lorem ipsum}} dolor sit amet.","Lorem ipsum}} dolor sit amet."}};
-
-       
 //((?<escapedstart>\{\{)  |  (?<start>\{))
 
 //(?<named>[_a-z][_a-z0-9]*)
 
 //((?<escapedend>}})  |  (?<end>}))
 
-        private string GetValueFromDictionary(IDictionary<string, string> mapping,string value)
+        private string GetValueFromDictionary(IDictionary<string, object> mapping, string value)
         {
-            return mapping[value];
+            return mapping[value].ToString();
         }
 
-        public string Substitute(IDictionary<string, string> mapping, bool throwNoException)
+        public string Substitute(IDictionary<string, object> mapping, bool throwNoException)
         {
             IList<TemplateValue> _TemplateValues = TemplateValues;
 
             Validate();
-            Regex regex = new Regex(GetRegex(),RegexOptions.Compiled|RegexOptions.ExplicitCapture|RegexOptions.CultureInvariant);
+            var regex = new Regex(GetRegex(),
+                                  RegexOptions.Compiled | RegexOptions.ExplicitCapture | RegexOptions.CultureInvariant);
 
-            return regex.Replace(this.template, delegate(Match match)
-                                             {
-                                                 TemplateValue templateValue=null;
+            return regex.Replace(template, delegate(Match match)
+                                               {
+                                                   TemplateValue templateValue = null;
 
-                                                 for (int i = 0; i < _TemplateValues.Count; i++)
-                                                 {
-                                                     if (match.Groups[(i + 1).ToString()].Success)
-                                                     {
-                                                         templateValue = _TemplateValues[i];
-                                                         break;
-                                                     }
-                                                 }
+                                                   for (int i = 0; i < _TemplateValues.Count; i++)
+                                                   {
+                                                       if (match.Groups[(i + 1).ToString()].Success)
+                                                       {
+                                                           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)
-                                                         return templateValue.StartDelimiter + match.Groups["named"] + templateValue.EndDelimiter;
-                                                     else if (match.Groups["end"].Success)
-                                                         return templateValue.StartDelimiter + GetValueFromDictionary(mapping, match.Groups["named"].Value); //maybe test for second match here
-                                                 }
+                                                   if (match.Groups["escapedstart"].Success)
+                                                   {
+                                                       if (string.IsNullOrEmpty(templateValue.EndDelimiterEscape))
+                                                           return templateValue.StartDelimiter + match.Groups["named"] +
+                                                                  templateValue.EndDelimiter;
+                                                       else if (match.Groups["escapedend"].Success)
+                                                           return templateValue.StartDelimiter + match.Groups["named"] +
+                                                                  templateValue.EndDelimiter;
+                                                       else if (match.Groups["end"].Success)
+                                                           return templateValue.StartDelimiter +
+                                                                  GetValueFromDictionary(mapping,
+                                                                                         match.Groups["named"].Value);
+                                                       //maybe test for second match here
+                                                   }
 
-                                                 if (match.Groups["start"].Success)
-                                                 {
-                                                     if (string.IsNullOrEmpty(templateValue.EndDelimiter))
-                                                         return GetValueFromDictionary(mapping, match.Groups["named"].Value);
-                                                     else if (match.Groups["escapedend"].Success)
-                                                         return GetValueFromDictionary(mapping, match.Groups["named"].Value) + templateValue.EndDelimiter;
-                                                     else if (match.Groups["end"].Success)
-                                                         return GetValueFromDictionary(mapping, match.Groups["named"].Value);
-                                                 }
+                                                   if (match.Groups["start"].Success)
+                                                   {
+                                                       if (string.IsNullOrEmpty(templateValue.EndDelimiter))
+                                                           return GetValueFromDictionary(mapping,
+                                                                                         match.Groups["named"].Value);
+                                                       else if (match.Groups["escapedend"].Success)
+                                                           return
+                                                               GetValueFromDictionary(mapping,
+                                                                                      match.Groups["named"].Value) +
+                                                               templateValue.EndDelimiter;
+                                                       else if (match.Groups["end"].Success)
+                                                           return GetValueFromDictionary(mapping,
+                                                                                         match.Groups["named"].Value);
+                                                   }
 
-                                                 //what if value not in dictionary but escaped startdelimiter
+                                                   //what if value not in dictionary but escaped startdelimiter
 
-                                                 throw new ApplicationException("Unrecognized named group in pattern");
-                                             });
-
-           
-
-
+                                                   throw new ApplicationException("Unrecognized named group in pattern");
+                                               });
         }
     }
 
     {
         public TemplateStringsPython(string template)
             : base(template)
-        { }
+        {
+        }
 
         protected override IList<TemplateValue> TemplateValues
         {
             get
             {
-                
-
-                TemplateValue value1 = new TemplateValue();
-                value1.StartDelimiter = "$";
-                value1.IdPattern = "[_a-z][_a-z0-9]*";
-                value1.StartDelimiterEscape = "$$";
-
-                 TemplateValue value2 = new TemplateValue();
-                value2.StartDelimiter = "${";
-                value2.IdPattern = "[_a-z][_a-z0-9]*";
-                value2.StartDelimiterEscape = "$${";
-                value2.EndDelimiter = "}";
-
-                return new TemplateValue[]{value1,value2};
+                return new[]
+                           {
+                               new TemplateValue
+                                   {
+                                       StartDelimiter = "$",
+                                       IdPattern = "[_a-z][_a-z0-9]*",
+                                       StartDelimiterEscape = "$$"
+                                   },
+                               new TemplateValue
+                                   {
+                                       StartDelimiter = "${",
+                                       IdPattern = "[_a-z][_a-z0-9]*",
+                                       StartDelimiterEscape = "$${",
+                                       EndDelimiter = "}"
+                                   }
+                           };
             }
         }
     }
     {
         public TemplateStringsMvc(string template)
             : base(template)
-        { }
+        {
+        }
 
         protected override IList<TemplateValue> TemplateValues
         {
             get
             {
-                TemplateValue value2 = new TemplateValue();
-                value2.StartDelimiter = "{";
-                value2.StartDelimiterEscape = "{{";
-                value2.IdPattern = "[_a-z][_a-z0-9]*";
-                value2.EndDelimiter = "}";
-                value2.EndDelimiterEscape = "}}";
-
-
-                return new TemplateValue[] {value2 };
+                return new[]
+                           {
+                               new TemplateValue
+                                   {
+                                       StartDelimiter = "{",
+                                       StartDelimiterEscape = "{{",
+                                       IdPattern = "[_a-z][_a-z0-9]*",
+                                       EndDelimiter = "}",
+                                       EndDelimiterEscape = "}}"
+                                   }
+                           };
             }
         }
     }
-}
+
+#if NET35
+
+    public static class TemplateStringExtensions
+    {
+        public static string Substitute(this string source,IDictionary<string,object> mapping)
+        {
+            return (new TemplateStringsPython(source)).Substitute(mapping);
+        }
+
+        public static string Substitute(this string source, IDictionary<string, object> mapping, Template strategy)
+        {
+            return strategy.Substitute(mapping);
+        }
+    }
+
+#endif
+
+}

File TemplateStrings/TemplateStrings.csproj

     <AppDesignerFolder>Properties</AppDesignerFolder>
     <RootNamespace>TemplateStrings</RootNamespace>
     <AssemblyName>TemplateStrings</AssemblyName>
-    <TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
+    <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
     <FileAlignment>512</FileAlignment>
     <TargetFrameworkSubset>
     </TargetFrameworkSubset>
     <DebugType>full</DebugType>
     <Optimize>false</Optimize>
     <OutputPath>bin\Debug\</OutputPath>
-    <DefineConstants>DEBUG;TRACE</DefineConstants>
+    <DefineConstants>TRACE;DEBUG;NET35</DefineConstants>
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>
   </PropertyGroup>
   </PropertyGroup>
   <ItemGroup>
     <Reference Include="System" />
+    <Reference Include="System.Core">
+      <RequiredTargetFramework>3.5</RequiredTargetFramework>
+    </Reference>
     <Reference Include="System.Data" />
     <Reference Include="System.Xml" />
   </ItemGroup>

File TemplateStringsTest/TemplateTest.cs

-using TemplateStrings;
+using System.Collections.Generic;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
-using System.Collections.Generic;
+using TemplateStrings;
 
 namespace TemplateStringsTest
 {
-
-
-    [TestClass()]
+    [TestClass]
     public class TemplateTest
     {
-
-
-        private TestContext testContextInstance;
-
-        public TestContext TestContext
-        {
-            get
-            {
-                return testContextInstance;
-            }
-            set
-            {
-                testContextInstance = value;
-            }
-        }
+        public TestContext TestContext { get; set; }
 
         #region Additional test attributes
+
         // 
         //You can use the following additional attributes as you write your tests:
         //
         //{
         //}
         //
+
         #endregion
 
+        [TestMethod]
+        public void Substitute_PythonTemplate_ReturnsSubstitutedString()
+        {
+            var pythonTemplateTestDictionary = new Dictionary<string, object> { { "ipsum", "test1" } };
 
-        [TestMethod()]
-        public void Substitute_Paratheses_ReturnsSubstitutedString()
-        {
-            string template = "${replacelorem} ipsum dolor sit amet, ${replacevestibulum} consectetur adipiscing elit. \n bibendum placerat nisl, non placerat quam ${replaceultrices}";
-            Template target = new Template(template);
-            IDictionary<string, string> mapping = new Dictionary<string, string>{   {"replacelorem","Lorem"},
-                                                                                    {"replacevestibulum","Vestibulum"},
-                                                                                    {"replaceultrices","ultrices"},
-                                                                                    {"dummyreplace","dummy"}};
-            string expected =
-                "Lorem ipsum dolor sit amet, Vestibulum consectetur adipiscing elit. \n bibendum placerat nisl, non placerat quam ultrices";
-            string actual;
-            actual = target.Substitute(mapping);
-            Assert.AreEqual(expected, actual);
-            
+            string[,] pythonTemplateTestStrings =
+                {
+                    {"Lorem $ipsum dolor sit amet.", "Lorem test1 dolor sit amet."},
+                    {"Lorem ${ipsum} dolor sit amet.", "Lorem test1 dolor sit amet."},
+                    {"Lorem $$ipsum dolor sit amet.", "Lorem $ipsum dolor sit amet."},
+                    {"Lorem $${ipsum} dolor sit amet.", "Lorem ${ipsum} dolor sit amet."}
+                };
+
+            for (int i = 0; i < pythonTemplateTestStrings.GetLength(0); i++)
+            {
+                Template template = new TemplateStringsPython(pythonTemplateTestStrings[i, 0]);
+
+                Assert.AreEqual(pythonTemplateTestStrings[i, 1], template.Substitute(pythonTemplateTestDictionary));
+            }
         }
 
-        [TestMethod()]
-        public void Substitute_NoParatheses_ReturnsSubstitutedString()
+        [TestMethod]
+        public void Substitute_PythonTemplateReverseFake_ReturnsSubstitutedString()
         {
+            var pythonTemplateTestDictionary = new Dictionary<string, object> { { "ipsum", "test1" } };
+
+            string[,] pythonTemplateTestStrings =
+                {
+                    {"Lorem ipsum$ dolor sit amet.", "Lorem test1 dolor sit amet."},
+                    {"Lorem {ipsum}$ dolor sit amet.", "Lorem test1 dolor sit amet."},
+                    {"Lorem ipsum$$ dolor sit amet.", "Lorem ipsum$ dolor sit amet."},
+                    {"Lorem {ipsum}$$ dolor sit amet.", "Lorem {ipsum}$ dolor sit amet."}
+                };
+
+            for (int i = 0; i < pythonTemplateTestStrings.GetLength(0); i++)
+            {
+                Template template = new PythonTemplateReverseFake(pythonTemplateTestStrings[i, 0]);
+
+                Assert.AreEqual(pythonTemplateTestStrings[i, 1], template.Substitute(pythonTemplateTestDictionary));
+            }
         }
 
-        [TestMethod()]
-        public void Substitute_DollarSubstitution_ReturnsSubstitutedString()
+        class PythonTemplateReverseFake : Template
         {
+            public PythonTemplateReverseFake(string template):base(template)
+            {}
+
+            protected override IList<TemplateValue> TemplateValues
+            {
+                get
+                {
+                    return new[]
+                           {
+                               new TemplateValue
+                                   {
+                                       EndDelimiter = "$",
+                                       IdPattern = "[_a-z][_a-z0-9]*",
+                                       EndDelimiterEscape = "$$"
+                                   },
+                               new TemplateValue
+                                   {
+                                       EndDelimiter = "}$",
+                                       IdPattern = "[_a-z][_a-z0-9]*",
+                                       EndDelimiterEscape = "}$$",
+                                       StartDelimiter = "{"
+                                   }
+                           };
+                }
+            }
         }
 
 
+        [TestMethod]
+        public void Substitute_MvcTemplate_ReturnsSubstitutedString()
+        {
+            var mvcTemplateTestDictionary = new Dictionary<string, object> { { "ipsum", "test1" } };
 
+            string[,] mvcTemplateTestStrings =
+                {
+                    {"Lorem {ipsum} dolor sit amet.", "Lorem test1 dolor sit amet."},
+                    {"Lorem {{ipsum}} dolor sit amet.", "Lorem {ipsum} dolor sit amet."}, //1
+                    {"Lorem {ipsum}} dolor sit amet.", "Lorem test1} dolor sit amet."},
+                    {"Lorem {{ipsum} dolor sit amet.", "Lorem {test1 dolor sit amet."},
+                    {"Lorem {ipsum dolor sit amet.", "Lorem {ipsum dolor sit amet."},
+                    {"Lorem ipsum} dolor sit amet.", "Lorem ipsum} dolor sit amet."},
+                    {"Lorem {{ipsum dolor sit amet.", "Lorem {{ipsum dolor sit amet."},
+                    {"Lorem ipsum}} dolor sit amet.", "Lorem ipsum}} dolor sit amet."}
+                };
 
+            for (int i = 0; i < mvcTemplateTestStrings.GetLength(0); i++)
+            {
+                Template template = new TemplateStringsMvc(mvcTemplateTestStrings[i, 0]);
+
+                Assert.AreEqual(mvcTemplateTestStrings[i, 1], template.Substitute(mvcTemplateTestDictionary));
+            }
+        }
     }
-}
+}