From ea68676cd2daebe5425af0bdb3f5769e758182af Mon Sep 17 00:00:00 2001 From: Markus Ewald Date: Sun, 20 Jul 2014 12:42:09 +0000 Subject: [PATCH] Added some unit tests; the configuration file parser can now parse categories git-svn-id: file:///srv/devel/repo-conversion/nusu@301 d2e56fa2-650e-0410-a79f-9358c0239efd --- Nuclex.Support (net-4.0).csproj | 9 +++ Source/Parsing/ParserHelper.Test.cs | 4 +- .../ConfigurationFileStore.Parsing.cs | 30 ++++++++- .../Settings/ConfigurationFileStore.Test.cs | 67 +++++++++++++++++++ Source/Settings/MemoryStore.Test.cs | 36 ++++++++++ Source/Settings/WindowsRegistryStore.Test.cs | 36 ++++++++++ 6 files changed, 179 insertions(+), 3 deletions(-) create mode 100644 Source/Settings/ConfigurationFileStore.Test.cs create mode 100644 Source/Settings/MemoryStore.Test.cs create mode 100644 Source/Settings/WindowsRegistryStore.Test.cs diff --git a/Nuclex.Support (net-4.0).csproj b/Nuclex.Support (net-4.0).csproj index 48e83ad..1148e8d 100644 --- a/Nuclex.Support (net-4.0).csproj +++ b/Nuclex.Support (net-4.0).csproj @@ -201,10 +201,19 @@ ConfigurationFileStore.cs + + ConfigurationFileStore.cs + + + MemoryStore.cs + + + WindowsRegistryStore.cs + diff --git a/Source/Parsing/ParserHelper.Test.cs b/Source/Parsing/ParserHelper.Test.cs index 91e12f7..9c3a65d 100644 --- a/Source/Parsing/ParserHelper.Test.cs +++ b/Source/Parsing/ParserHelper.Test.cs @@ -18,12 +18,12 @@ License along with this library */ #endregion +#if UNITTEST + using System; using System.Collections.Generic; using System.Text; -#if UNITTEST - using NUnit.Framework; namespace Nuclex.Support.Parsing { diff --git a/Source/Settings/ConfigurationFileStore.Parsing.cs b/Source/Settings/ConfigurationFileStore.Parsing.cs index e02eb7b..5444622 100644 --- a/Source/Settings/ConfigurationFileStore.Parsing.cs +++ b/Source/Settings/ConfigurationFileStore.Parsing.cs @@ -19,9 +19,11 @@ License along with this library #endregion using System; +using System.Diagnostics; using System.IO; using Nuclex.Support.Parsing; +using System.Collections.Generic; namespace Nuclex.Support.Settings { @@ -105,7 +107,33 @@ namespace Nuclex.Support.Settings { private static void parseCategory( ParserState state, string line, int firstCharacterIndex ) { - throw new NotImplementedException(); + Debug.Assert(line[firstCharacterIndex] == '['); + + int nameStartIndex = firstCharacterIndex + 1; + ParserHelper.SkipSpaces(line, ref nameStartIndex); + + int lastCharacterIndex = line.Length - 1; + if(nameStartIndex >= lastCharacterIndex) { + return; // No space left for closing brace + } + + int nameEndIndex = line.IndexOf(']', nameStartIndex); + if(nameEndIndex == -1) { + return; // No closing brace in line + } + + do { + --nameEndIndex; + } while(char.IsWhiteSpace(line, nameEndIndex)); + + state.Category = new Category() { + LineIndex = state.Store.lines.Count - 1, + CategoryName = new StringSegment( + line, nameStartIndex, nameEndIndex - nameStartIndex + 1 + ), + OptionLookup = new Dictionary() + }; + state.Store.categories.Add(state.Category); } /// Parses an option definition encountered on a line diff --git a/Source/Settings/ConfigurationFileStore.Test.cs b/Source/Settings/ConfigurationFileStore.Test.cs new file mode 100644 index 0000000..bd61cb5 --- /dev/null +++ b/Source/Settings/ConfigurationFileStore.Test.cs @@ -0,0 +1,67 @@ +#region CPL License +/* +Nuclex Framework +Copyright (C) 2002-2014 Nuclex Development Labs + +This library is free software; you can redistribute it and/or +modify it under the terms of the IBM Common Public License as +published by the IBM Corporation; either version 1.0 of the +License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +IBM Common Public License for more details. + +You should have received a copy of the IBM Common Public +License along with this library +*/ +#endregion + +#if UNITTEST + +using System; + +using NUnit.Framework; +using System.IO; +using System.Linq; + +namespace Nuclex.Support.Settings { + + /// Unit tests for the configuration file store + [TestFixture] + internal class ConfigurationFileStoreTest { + + private static ConfigurationFileStore load(string fileContents) { + using(var reader = new StringReader(fileContents)) { + return ConfigurationFileStore.Parse(reader); + } + } + + /// + /// Verifies that loading an empty file doesn't lead to an exception + /// + [Test] + public void CanParseEmptyFile() { + Assert.That(() => load(string.Empty), Throws.Nothing); + } + + /// + /// Verifies that categories can be parsed from a configuration file + /// + [Test] + public void CanParseCategories() { + string[] categoryNames = new string[] { "Category1", "Category 2" }; + string fileContents = + "[" + categoryNames[0] + "]\r\n" + + " [ " + categoryNames[1] + " ] \r\n"; + ConfigurationFileStore configurationFile = load(fileContents); + + Assert.That(configurationFile.EnumerateCategories(), Is.EquivalentTo(categoryNames)); + } + + } + +} // namespace Nuclex.Support.Settings + +#endif // UNITTEST diff --git a/Source/Settings/MemoryStore.Test.cs b/Source/Settings/MemoryStore.Test.cs new file mode 100644 index 0000000..4be3be9 --- /dev/null +++ b/Source/Settings/MemoryStore.Test.cs @@ -0,0 +1,36 @@ +#region CPL License +/* +Nuclex Framework +Copyright (C) 2002-2014 Nuclex Development Labs + +This library is free software; you can redistribute it and/or +modify it under the terms of the IBM Common Public License as +published by the IBM Corporation; either version 1.0 of the +License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +IBM Common Public License for more details. + +You should have received a copy of the IBM Common Public +License along with this library +*/ +#endregion + +#if UNITTEST + +using System; + +using NUnit.Framework; + +namespace Nuclex.Support.Settings { + + /// Unit tests for the memory settings store + [TestFixture] + internal class MemoryStoreTest { + } + +} // namespace Nuclex.Support.Settings + +#endif // UNITTEST diff --git a/Source/Settings/WindowsRegistryStore.Test.cs b/Source/Settings/WindowsRegistryStore.Test.cs new file mode 100644 index 0000000..a99399b --- /dev/null +++ b/Source/Settings/WindowsRegistryStore.Test.cs @@ -0,0 +1,36 @@ +#region CPL License +/* +Nuclex Framework +Copyright (C) 2002-2014 Nuclex Development Labs + +This library is free software; you can redistribute it and/or +modify it under the terms of the IBM Common Public License as +published by the IBM Corporation; either version 1.0 of the +License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +IBM Common Public License for more details. + +You should have received a copy of the IBM Common Public +License along with this library +*/ +#endregion + +#if UNITTEST + +using System; + +using NUnit.Framework; + +namespace Nuclex.Support.Settings { + + /// Unit tests for the windows registry settings store + [TestFixture] + internal class WindowsRegistryStoreTest { + } + +} // namespace Nuclex.Support.Settings + +#endif // UNITTEST