public void Test() { const string @string = "My String foo"; const string present = "World"; var nested = new Nested { Foo = "Foo1", Bar = -15 }; var prefix = "Hello"; var array = new[] { " Mickey", "Mouse " }; var settingsLoader = new SettingsLoader(new DictionaryValueProvider(new Dictionary <string, string> { { SettingsLoader.GetSettingKey(prefix, nameof(OptionalSettings.String)), @string }, { SettingsLoader.GetSettingKey(prefix, nameof(OptionalSettings.OptionalPresent)), present }, { SettingsLoader.GetSettingKey(prefix, nameof(OptionalSettings.OptionalNestedPresent), nameof(Nested.Foo)), nested.Foo }, { SettingsLoader.GetSettingKey(prefix, nameof(OptionalSettings.OptionalNestedPresent), nameof(Nested.Bar)), nested.Bar.ToString() }, { SettingsLoader.GetSettingKey(prefix, nameof(OptionalSettings.OptionalArrayPresent), "1"), array[0] }, { SettingsLoader.GetSettingKey(prefix, nameof(OptionalSettings.OptionalArrayPresent), "2"), array[1] } })); var setting = settingsLoader.Create <OptionalSettings>(prefix); Assert.That(setting, Is.Not.Null); Assert.That(setting.String, Is.EqualTo(@string)); Assert.That(setting.OptionalMissing, Is.Null); Assert.That(setting.OptionalPresent, Is.EqualTo(present)); Assert.That(setting.OptionalNestedMissing, Is.Null); Assert.That(setting.OptionalNestedPresent, Is.DeepEqualTo(nested)); Assert.That(setting.OptionalArrayMissing, Is.Null); Assert.That(setting.OptionalArrayPresent, Is.DeepEqualTo(array)); }
public void DictionaryLoadTest() { var settings = SettingsLoader.Create <DictionarySettings>(); // Various Dictionarys Assert.That(settings.MySimpleDictionaryProperty, Is.EquivalentTo( new Dictionary <string, string> { { "Foo", "oof" }, { "Bar", "rab" } })); Assert.That(settings.MyNumericDictionaryProperty, Is.EquivalentTo( new Dictionary <string, byte> { { "Foo", 14 }, { "Bar", 42 } })); Assert.That(settings.MyDictionaryProperty, Is.DeepEqualTo( new Dictionary <string, Nested> { { "Key1", new Nested { Foo = "oof1", Bar = 421 } }, { "Key2", new Nested { Foo = "oof2", Bar = 422 } } }).WithComparisonConfig(new ComparisonConfig() { IgnoreCollectionOrder = true })); }
public void ListLoadTest() { var settings = SettingsLoader.Create <ListSettings>(); // List Assert.That(settings.MySimpleListProperty, Is.DeepEqualTo( new List <string> { "Foo 1", "Foo 2" })); Assert.That(settings.MyListProperty, Is.DeepEqualTo( new List <Nested> { new Nested { Foo = "Foo 1", Bar = 421 }, new Nested { Foo = "Foo 2", Bar = 422 }, })); Assert.That(settings.LostNumbersList, Is.EqualTo( new[] { 4, 8, 15, 16, 23, 42 })); }
public void Test() { const string prefix = "Whatever"; const string foo = "My String foo"; const string bar = "My String bar"; const string baz = "My baz String"; var settingsLoader = new SettingsLoader(new DictionaryValueProvider(new Dictionary <string, string> { { SettingsLoader.GetSettingKey(prefix, nameof(MultiLevel1.Foo)), foo }, { SettingsLoader.GetSettingKey(prefix, nameof(MultiLevel1.Level2), nameof(MultiLevel2.Bar)), bar }, { SettingsLoader.GetSettingKey(prefix, nameof(MultiLevel1.Level2), nameof(MultiLevel2.Level3), nameof(MultiLevel3.Baz)), baz }, })); var setting = settingsLoader.Create <MultiLevel1>(prefix); var expected = new MultiLevel1() { Foo = foo, Level2 = new MultiLevel2() { Bar = bar, Level3 = new MultiLevel3() { Baz = baz } } }; Assert.That(setting, Is.Not.Null); Assert.That(setting, Is.DeepEqualTo(expected)); }
public void ValidationErrorTest() { const int id = 10; const string title = "The Shawshank Redemption 1234567890123456789012345678901234567890123456789012345678901234567890"; const string releaseDate = "1994-02-01"; const string genre = "Drama"; const decimal price = 50; const string rating = "12345R"; var settingsLoader = new SettingsLoader(new DictionaryValueProvider(new Dictionary <string, string> { { nameof(Movie.ID), id.ToString() }, { nameof(Movie.Title), title }, { nameof(Movie.ReleaseDate), releaseDate }, { nameof(Movie.Genre), genre }, { nameof(Movie.Price), price.ToString(CultureInfo.InvariantCulture) }, { nameof(Movie.Rating), rating }, })); var ex = Assert.Throws <SettingsException>(() => { var movie = settingsLoader.Create <Movie>(); }); Console.WriteLine(ex.Message); var expectedMessage = string.Format(Resources.ValidationError, nameof(Movie.Title), ""); Assert.That(ex.Message, Does.StartWith(expectedMessage)); }
public void BadUri() { var prefix = "FooPrefix"; var propPrefix = SettingsLoader.GetSettingKey(prefix, "foo"); var settingKey = SettingsLoader.GetSettingKey(propPrefix, nameof(BadConversion.BadUri)); var value = "[]"; var expectedMessage = string.Format( Resources.ConversionErrorSuffix, string.Format( Resources.ConvertValueErrorFormat, value, typeof(Uri).FullName), settingKey); var settingsLoader = new SettingsLoader(new DictionaryValueProvider(new Dictionary <string, string> { { settingKey, "[]" }, })); Assert.That(() => { settingsLoader.Create <BadConversion>(propPrefix); }, Throws .Exception.TypeOf <SettingsException>() .With.Message.EqualTo(expectedMessage) ); }
public void Test() { const string title = "The Shawshank Redemption"; const string releaseDate = "1994-02-01"; const string genre = "Drama"; const decimal price = 50; const string rating = "R"; var settingsLoader = new SettingsLoader(new DictionaryValueProvider(new Dictionary <string, string> { { nameof(Movie.ID), "10" }, { nameof(Movie.Title), title }, { nameof(Movie.ReleaseDate), releaseDate }, { nameof(Movie.Genre), genre }, { nameof(Movie.Price), price.ToString(CultureInfo.InvariantCulture) }, { nameof(Movie.Rating), rating }, })); var movie = settingsLoader.Create <Movie>(); Assert.That(movie, Is.Not.Null); Assert.That(movie.Title, Is.EqualTo(title)); Assert.That(movie.ReleaseDate, Is.EqualTo(DateTime.ParseExact(releaseDate, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None))); Assert.That(movie.Genre, Is.EqualTo(genre)); Assert.That(movie.Price, Is.EqualTo(price)); Assert.That(movie.Rating, Is.EqualTo(rating)); }
public void InlineTypeConversionErrorSettingTest() { AssertThrowsSettingsExceptionMessageTest( () => SettingsLoader.Create <InlineTypeConversionSetting>(), Resources.ConversionErrorSuffix, string.Format(Resources.ConvertValueErrorFormat, "xyz", typeof(int)), Settings.SettingsLoader.GetSettingKey(nameof(MissingIntSetting.MissingInt))); }
public void GetNestedFromDatabase() { var dictionary = SettingsLoader.Create <Nested>(); Assert.That(dictionary, Is.DeepEqualTo( new Nested { Bar = 14, Foo = "Foo from database" })); }
public void SettingNameLoadTest() { var settings = SettingsLoader.Create <SimpleSettingsWithSettingName>(); //Simple Assert.That(settings.Enum2, Is.EqualTo(BindingFlags.NonPublic | BindingFlags.Static)); Assert.That(settings.String1, Is.EqualTo("Hello world")); Assert.That(settings.DateTime3, Is.EqualTo(new DateTime(2004, 07, 17, 9, 0, 0, DateTimeKind.Local))); Assert.That(settings.TimeSpan4, Is.EqualTo(new TimeSpan(11, 22, 33, 44, 560))); Assert.That(settings.Uri5, Is.EqualTo(new Uri("Http://hello.eu"))); Assert.That(settings.Guid6, Is.EqualTo(Guid.Parse("DCFA0942-0BEC-43E4-8D77-57BA63C7BF7B"))); }
public void BasicFileInfoTest() { Console.WriteLine(Directory.GetCurrentDirectory()); var settings = SettingsLoader.Create <FileSettings>(); // Defaults Assert.That(settings.SimpleFile.FullName, Is.EqualTo(Path.Combine(TestProjectFolder, "App.config"))); Assert.That(settings.FullFileInfo.FullName, Is.EqualTo(@"c:\Windows\regedit.exe")); Assert.That(settings.PartialFile.FullName, Is.EqualTo(Path.GetFullPath(Path.Combine(TestProjectFolder, @"..\Miracle.Settings.sln")))); Assert.That(settings.RelativeFileInfo.FullName, Is.EqualTo(Path.Combine(TestProjectFolder, "TestFolder", "TextFile1.txt"))); }
public void OptionalInlineLoadTest() { var settings = SettingsLoader.Create <OptionalInlineSettings>(); // Nested Assert.That(settings, Is.DeepEqualTo( new OptionalInlineSettings() { MyNestedProperty = null, Baz = TimeSpan.Parse("01:02:03") })); }
public void NoValueTest1() { var key = "Foo"; // Setup mock value provider with type var settingsLoader = new SettingsLoader(new DictionaryValueProvider(new Dictionary <string, string>())); Assert.That( () => settingsLoader.Create <Type>(key), Throws.Exception.TypeOf <SettingsException>() .With.Message.EqualTo(string.Format(Resources.MissingValueFormat, typeof(Type).FullName, key))); }
public void BasicTest() { // Important for test that current directory is the test project folder Directory.SetCurrentDirectory(TestProjectFolder); var settings = SettingsLoader.Create <DirectorySettings>(); // Defaults Assert.That(settings.SimpleDirectory.FullName, Is.EqualTo(Path.Combine(TestProjectFolder, "TestFolder"))); Assert.That(settings.PartialDirectory.FullName, Is.EqualTo(Path.GetFullPath(Path.Combine(TestProjectFolder, "TestFolder\\TestSubFolder")))); Assert.That(settings.FullDirectoryInfo.FullName, Is.EqualTo("c:\\Windows")); Assert.That(settings.RelativeDirectoryInfo.FullName, Is.EqualTo(Path.Combine(TestProjectFolder, "TestFolder", "TestSubFolder"))); }
public void SettingReferenceLoadTest() { var settings = SettingsLoader.Create <ReferenceSettings>("SettingReference"); // Example of loading settings with references to other settings Assert.That(settings, Is.DeepEqualTo( new ReferenceSettings() { MyUrl = new Uri("http://foo.bar"), PageUrl = new Uri(new Uri("http://foo.bar"), "/home/index.aspx"), LoginUrl = new Uri(new Uri("http://foo.bar"), "/login.aspx"), })); }
public void SimpleLoadTest() { var settings = SettingsLoader.Create <SimpleSettings>(); //Simple Assert.That(settings.Enum, Is.EqualTo(BindingFlags.NonPublic | BindingFlags.Static)); Assert.That(settings.String, Is.EqualTo("Hello world")); Assert.That(settings.DateTime, Is.EqualTo(new DateTime(2004, 07, 17, 9, 0, 0, DateTimeKind.Local))); Assert.That(settings.TimeSpan, Is.EqualTo(new TimeSpan(11, 22, 33, 44, 560))); Assert.That(settings.Type, Is.EqualTo(typeof(System.Data.IDbConnection))); Assert.That(settings.Uri, Is.EqualTo(new Uri("Http://hello.eu"))); Assert.That(settings.Guid, Is.EqualTo(Guid.Parse("DCFA0942-0BEC-43E4-8D77-57BA63C7BF7B"))); Assert.That(settings.IPAddress, Is.EqualTo(IPAddress.Parse("192.168.0.42"))); }
public void IpSettingOneValueTest() { var ip = "192.168.1.42"; // Setup mock value provider var settingsLoader = new SettingsLoader(new DictionaryValueProvider(new Dictionary <string, string>() { { nameof(IPSettingTest.IpAddress), ip }, })); var result = settingsLoader.Create <IPSettingTest>(); Assert.That(result.IpAddress, Is.EqualTo(IPAddress.Parse(ip))); Assert.That(result.OptionalIpAddress, Is.Null); }
public void SettingAttributesWithTypeConverterLoadTest() { var settings = SettingsLoader.Create <PathTypeConverterSettings>("TypeConverter"); //Simple // Array Assert.That(settings, Is.DeepEqualTo( new PathTypeConverterSettings() { Upload = @"C:\Foo\Bar\Upload\", Download = @"E:\Download\", Sub = @"C:\Foo\Bar\Upload\Sub\", })); }
public void EmptyValueTest1() { var key = "Foo"; // Setup mock value provider var settingsLoader = new SettingsLoader(new DictionaryValueProvider(new Dictionary <string, string>() { { key, "" } })); Assert.That( () => settingsLoader.Create <IPAddress>(key), Throws.Exception.TypeOf <SettingsException>() .With.Message.EqualTo(string.Format(Resources.ConvertValueErrorFormat, "", typeof(IPAddress).FullName))); }
public void FileAnnotationTest() { // Setup mock value provider with type var settingsLoader = new SettingsLoader(new DictionaryValueProvider(new Dictionary <string, string> { { "Drive", "C:\\" }, { "Folder", "Windows" }, { "FileName", "notepad.exe" }, })); var settings = settingsLoader.Create <FileAnnotationSettings>(); // Defaults Assert.That(settings.FullName, Is.Not.Null); Assert.That(settings.FullName.FullName, Is.EqualTo(@"C:\Windows\notepad.exe")); }
public void IpSettingEmptyNoValueTest() { var ip = ""; // Setup mock value provider var settingsLoader = new SettingsLoader(new DictionaryValueProvider(new Dictionary <string, string>() { { nameof(IPSettingTest.IpAddress), ip }, { nameof(IPSettingTest.OptionalIpAddress), ip } })); Assert.That( () => settingsLoader.Create <IPSettingTest>(), Throws.Exception.TypeOf <SettingsException>() .With.Message.EqualTo(string.Format(Resources.MissingValueFormat, typeof(IPAddress).FullName, nameof(IPSettingTest.IpAddress)))); }
public void DefaultLoadTest() { var settings = SettingsLoader.Create <DefaultSettings>(); // Defaults Assert.That(settings.DefaultString, Is.EqualTo("Default Hello World")); Assert.That(settings.DefaultEnum, Is.EqualTo(BindingFlags.Instance | BindingFlags.Public)); Assert.That(settings.DefaultDateTime, Is.EqualTo(new DateTime(1966, 6, 11, 13, 34, 56, DateTimeKind.Local).AddMilliseconds(789))); Assert.That(settings.DefaultTimeSpan, Is.EqualTo(new TimeSpan(1, 2, 3, 4))); Assert.That(settings.DefaultType, Is.EqualTo(typeof(AccessViolationException))); Assert.That(settings.DefaultUri, Is.EqualTo(new Uri("https://foo.bar"))); Assert.That(settings.DefaultGuid, Is.EqualTo(Guid.Parse("EE58EE2B-4CE6-44A4-8773-EC4E283146EB"))); Assert.That(settings.DefaultIp, Is.EqualTo(IPAddress.Parse("10.42.42.42"))); Assert.That(settings.DefaultArray, Is.EqualTo(new[] { "foo", "bar" })); }
public void NestedLoadTest() { var settings = SettingsLoader.Create <NestedSettings>(); // Nested Assert.That(settings, Is.DeepEqualTo( new NestedSettings { MyNestedProperty = new Nested() { Foo = "Foo string", Bar = 42 } })); }
public void InlineLoadTest() { var settings = SettingsLoader.Create <InlineSettings>(); // Nested Assert.That(settings, Is.DeepEqualTo( new InlineSettings { MyNestedProperty = new Nested() { Foo = "Inline Foo string", Bar = 21 }, Baz = TimeSpan.Parse("01:02:03") })); }
public void IpSettingTwoValuesTest() { var ip1 = "192.168.1.42"; var ip2 = "87.65.43.210"; // Setup mock value provider var settingsLoader = new SettingsLoader(new DictionaryValueProvider(new Dictionary <string, string>() { { nameof(IPSettingTest.IpAddress), ip1 }, { nameof(IPSettingTest.OptionalIpAddress), ip2 }, })); var result = settingsLoader.Create <IPSettingTest>(); Assert.That(result.IpAddress, Is.EqualTo(IPAddress.Parse(ip1))); Assert.That(result.OptionalIpAddress, Is.EqualTo(IPAddress.Parse(ip2))); }
public void Test1() { var key = "FooIp"; var ip = "42.41.40.39"; // Setup mock value provider var settingsLoader = new SettingsLoader(new DictionaryValueProvider(new Dictionary <string, string> { { key, ip } })); var ipAddress = settingsLoader.Create <IPAddress>(key); Assert.That(ipAddress, Is.Not.Null); Assert.That(ipAddress, Is.EqualTo(IPAddress.Parse(ip))); }
public void BadTest() { const string foo = "Testing"; const int bar = 100; var prefix = "MyPrefix"; var propPrefix = SettingsLoader.GetSettingKey(prefix, nameof(InterfaceMapping.Prop)); var settingsLoader = new SettingsLoader(new DictionaryValueProvider(new Dictionary <string, string> { { SettingsLoader.GetSettingKey(propPrefix, nameof(IMyInterface.Foo)), foo }, { SettingsLoader.GetSettingKey(propPrefix, nameof(IMyInterface.Bar)), bar.ToString() }, })); Assert.That(() => { settingsLoader.Create <BadInterfaceMapping>(prefix); }, Throws.Exception.TypeOf <ArgumentException>()); }
public void FailLoadTest() { var key = "Foo.Type"; // Pick any type to use as test type var someType = "Foo, MyType.Dll"; // Setup mock value provider with type var settingsLoader = new SettingsLoader(new DictionaryValueProvider(new Dictionary <string, string> { { key, someType } })); Assert.That( () => settingsLoader.Create <Type>(key), Throws.Exception.TypeOf <SettingsException>() .With.Message.EqualTo(string.Format(Resources.ConvertValueErrorFormat, someType, typeof(Type).FullName))); }
public void Test() { var key = "FooType"; // Pick any type to use as test type var someType = typeof(TestFixtureAttribute); // Setup mock value provider with type var settingsLoader = new SettingsLoader(new DictionaryValueProvider(new Dictionary <string, string> { { key, someType.AssemblyQualifiedName } })); var type = settingsLoader.Create <Type>(key); Assert.That(type, Is.Not.Null); Assert.That(type, Is.EqualTo(someType)); }
public void Test() { const string @string = "My String"; var prefix = "Hello"; var settingsLoader = new SettingsLoader(new DictionaryValueProvider(new Dictionary <string, string> { { SettingsLoader.GetSettingKey(prefix, nameof(IgnoreSettings.String)), @string }, })); var setting = settingsLoader.Create <IgnoreSettings>(prefix); Assert.That(setting, Is.Not.Null); Assert.That(setting.String, Is.EqualTo(@string)); Assert.That(setting.Ignored, Is.Null); Assert.That(setting.NestedIgnored, Is.Null); Assert.That(setting.TimeSpanIgnored, Is.Null); }