From 8024fc3b1e66ca73accf69cbe67bec864b6e544e Mon Sep 17 00:00:00 2001 From: KlesakoW Date: Fri, 28 Feb 2014 17:09:52 +0100 Subject: [PATCH 1/2] Issue #15 - Tests --- .../ConfigizerLib.Tests.csproj | 207 +++++++++--------- .../ConfigurationLoaderTests.cs | 43 ++++ src/ConfigizerLib/ConfigurationLoader.cs | 6 +- 3 files changed, 150 insertions(+), 106 deletions(-) create mode 100644 src/ConfigizerLib.Tests/ConfigurationLoaderTests.cs diff --git a/src/ConfigizerLib.Tests/ConfigizerLib.Tests.csproj b/src/ConfigizerLib.Tests/ConfigizerLib.Tests.csproj index 8782557..1548c46 100644 --- a/src/ConfigizerLib.Tests/ConfigizerLib.Tests.csproj +++ b/src/ConfigizerLib.Tests/ConfigizerLib.Tests.csproj @@ -1,110 +1,111 @@ - - - - Debug - AnyCPU - {CECD038C-9C09-4677-9B22-BA55FB2EE60D} - Library - Properties - ConfigizerLib.Tests - ConfigizerLib.Tests - v4.5 - 512 - {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} - 10.0 - $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) - $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages - False - UnitTest - - - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - true - bin\x86\Debug\ - DEBUG;TRACE - full - x86 - prompt - MinimumRecommendedRules.ruleset - false - - - bin\x86\Release\ - TRACE - true - pdbonly - x86 - prompt - MinimumRecommendedRules.ruleset - - - - - - - - - - - - - - - - - - - - - - - - - {1B265A46-BDC6-4FC9-9D1F-96AD3DF36C83} - ConfigizerLib - - - - - - - False - - - False - - - False - - - False - - - - - - + + + + Debug + AnyCPU + {CECD038C-9C09-4677-9B22-BA55FB2EE60D} + Library + Properties + ConfigizerLib.Tests + ConfigizerLib.Tests + v4.5 + 512 + {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 10.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages + False + UnitTest + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + true + bin\x86\Debug\ + DEBUG;TRACE + full + x86 + prompt + MinimumRecommendedRules.ruleset + false + + + bin\x86\Release\ + TRACE + true + pdbonly + x86 + prompt + MinimumRecommendedRules.ruleset + + + + + + + + + + + + + + + + + + + + + + + + + + {1B265A46-BDC6-4FC9-9D1F-96AD3DF36C83} + ConfigizerLib + + + + + + + False + + + False + + + False + + + False + + + + + + + --> \ No newline at end of file diff --git a/src/ConfigizerLib.Tests/ConfigurationLoaderTests.cs b/src/ConfigizerLib.Tests/ConfigurationLoaderTests.cs new file mode 100644 index 0000000..f6337f2 --- /dev/null +++ b/src/ConfigizerLib.Tests/ConfigurationLoaderTests.cs @@ -0,0 +1,43 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Linq; + +namespace ConfigizerLib.Tests +{ + + [TestClass] + public class ConfigurationLoaderTests + { + private const string ContentsLinux = "#base .\\base\npublic override string DbDataSource {get {return \"proddbsrv\\mssql2012\";}}\npublic override string DbPassword {get {return ThrowNotOverriden();}}"; + private const string ContentsWin = "#base .\\base\r\npublic override string DbDataSource {get {return \"proddbsrv\\mssql2012\";}}\r\npublic override string DbPassword {get {return ThrowNotOverriden();}}"; + + [TestMethod] + public void GetTagValuesLinux() + { + var values = ConfigurationLoader.GetTagValues(ContentsLinux, "base"); + Assert.IsTrue(values.Count() == 1); + Assert.AreEqual(values[0], @".\base"); + } + + [TestMethod] + public void GetTagValuesWin() + { + var values = ConfigurationLoader.GetTagValues(ContentsWin, "base"); + Assert.IsTrue(values.Count() == 1); + Assert.AreEqual(values[0], @".\base"); + } + + [TestMethod] + public void RemoveTagsLinux() + { + var contents = ConfigurationLoader.RemoveTags(ContentsLinux, "base"); + Assert.IsFalse(contents.Contains("base")); + } + + [TestMethod] + public void RemoveTagsWin() + { + var contents = ConfigurationLoader.RemoveTags(ContentsWin, "base"); + Assert.IsFalse(contents.Contains("base")); + } + } +} diff --git a/src/ConfigizerLib/ConfigurationLoader.cs b/src/ConfigizerLib/ConfigurationLoader.cs index ebe50aa..c3235bd 100644 --- a/src/ConfigizerLib/ConfigurationLoader.cs +++ b/src/ConfigizerLib/ConfigurationLoader.cs @@ -5,7 +5,7 @@ namespace ConfigizerLib { - public class ConfigurationLoader + public static class ConfigurationLoader { public static ConfigurationFileInfo Load(string path) { @@ -49,13 +49,13 @@ public static ConfigurationFileInfo Load(string path) return cfi; } - private static string RemoveTags(string contents, params string[] tags) + public static string RemoveTags(string contents, params string[] tags) { return Regex.Replace(contents, @"(^#(" + string.Join("|", tags) + @")\s+(?.*)\s+$)", "", RegexOptions.Multiline | RegexOptions.IgnoreCase); } - static string[] GetTagValues(string contents, string tagName) + public static string[] GetTagValues(string contents, string tagName) { var tags = Regex.Matches(contents, @"(^#" + tagName + @"\s+(?.*)\s+$)", RegexOptions.Multiline | RegexOptions.IgnoreCase); From 21e019036187de95a9028441dad519f308980cdd Mon Sep 17 00:00:00 2001 From: KlesakoW Date: Fri, 28 Feb 2014 17:10:57 +0100 Subject: [PATCH 2/2] Issue #15 - Fixed regular expression --- src/ConfigizerLib/ConfigurationLoader.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ConfigizerLib/ConfigurationLoader.cs b/src/ConfigizerLib/ConfigurationLoader.cs index c3235bd..526dbb3 100644 --- a/src/ConfigizerLib/ConfigurationLoader.cs +++ b/src/ConfigizerLib/ConfigurationLoader.cs @@ -51,13 +51,13 @@ public static ConfigurationFileInfo Load(string path) public static string RemoveTags(string contents, params string[] tags) { - return Regex.Replace(contents, @"(^#(" + string.Join("|", tags) + @")\s+(?.*)\s+$)", "", + return Regex.Replace(contents, @"(^#(" + string.Join("|", tags) + @")\s+(?.*)\s*$)", "", RegexOptions.Multiline | RegexOptions.IgnoreCase); } public static string[] GetTagValues(string contents, string tagName) { - var tags = Regex.Matches(contents, @"(^#" + tagName + @"\s+(?.*)\s+$)", + var tags = Regex.Matches(contents, @"(^#" + tagName + @"\s+(?.*)\s*$)", RegexOptions.Multiline | RegexOptions.IgnoreCase); return tags.OfType().Select(m => m.Groups["val"].Value.Trim()).ToArray(); }