Пример #1
0
        public ReadToOneBufferBenchmark()
        {
            if (Directory.Exists(TestDirectory))
            {
                Directory.Delete(TestDirectory, true);
            }

            Directory.CreateDirectory(TestDirectory);
            var fixture       = new Fixture();
            var customization = new SupportMutableValueTypesCustomization();

            var newParser = $"{TestDirectory}newParser";
            var oldParser = $"{TestDirectory}oldParser";

            customization.Customize(fixture);
            _iniParser    = new IniWrapper.ParserWrapper.IniParser(newParser, 1024);
            _oldIniParser = new OldParserWrapper.IniParser(oldParser, 1024);

            var configuration = fixture.Create <OldParserWrapper.Configuration>();

            var iniWrapper = new IniWrapperFactory().CreateWithDefaultIniParser(x =>
            {
                x.IniFilePath = newParser;
            });

            var oldIni = new IniWrapperFactory().CreateWithDefaultIniParser(x =>
            {
                x.IniFilePath = oldParser;
            });

            iniWrapper.SaveConfiguration(configuration);
            oldIni.SaveConfiguration(configuration);
        }
        private AWConfigurationVO init()
        {
            var iniPath = Path.Combine(
                Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
                @"activitywatch\activitywatch\aw-server\aw-server.ini");

            if (!File.Exists(iniPath))
            {
                //Defaults
                return(new AWConfigurationVO()
                {
                    ConfigFileExists = false
                });
            }

            // read from INI
            var iniWrapperFactory = new IniWrapperFactory();
            var iniWrapper        = iniWrapperFactory.CreateWithDefaultIniParser(
                new IniWrapper.Settings.IniSettings()
            {
                IniFilePath = iniPath,
                MissingFileWhenLoadingHandling = IniWrapper.Settings.MissingFileWhenLoadingHandling.DoNotLoad
            });

            var config = iniWrapper.LoadConfiguration <VO.AWConfigurationVO>();

            config.ConfigFileExists = true;
            return(config);
        }
Пример #3
0
        public void ShouldThrow_SaveConfiguration_WhenCustomConverter_DoesNotImplementIIniCustomConverterInterface()
        {
            var iniParser  = Substitute.For <IIniParser>();
            var iniWrapper = new IniWrapperFactory().Create(x => { }, iniParser);

            Action result = () => iniWrapper.SaveConfiguration(new CustomConverterWithoutInterface());

            result.Should().Throw <InvalidCastException>()
            .WithMessage($"Custom converter of type {typeof(ConverterWithoutInterface)} must implement IIniConverter interface.");
        }
Пример #4
0
        public void PropertyDecoratedWithIniConverter_ShouldInstantiateConverter_AndCallFormatToWrite_WhenSaving()
        {
            var iniParser  = Substitute.For <IIniParser>();
            var iniWrapper = new IniWrapperFactory().Create(x =>
            {
            }, iniParser);

            Action result = () => iniWrapper.SaveConfiguration(new IniConverterWithConstructorParameters());

            result.Should().Throw <TestCustomIniHandlerException>().WithMessage("FormatToWrite");
        }
Пример #5
0
        public void ShouldThrow_SaveConfiguration_WhenCustomConverter_DoesNotHaveParameterlessConstructor_And_ConverterParametersWereNotPassed()
        {
            var iniParser  = Substitute.For <IIniParser>();
            var iniWrapper = new IniWrapperFactory().Create(iniParser);

            Action result = () => iniWrapper.SaveConfiguration(new IniConverterWithConstructorParametersNotSpecified());

            result.Should().Throw <MissingMethodException>().WithMessage($"Please provide parameterless constructor for custom converter of type {typeof(CustomIniConverterWithConstructor)} " +
                                                                         "or pass arguments via converterParameters." +
                                                                         " (e.g. [IniConverter(typeof(CustomIniConverterWithConstructor), new object[] { \"Argument\", 10 })])");
        }
Пример #6
0
        public void EnumStringConverter_ShouldLoadEnumAsString()
        {
            var iniParser = Substitute.For <IIniParser>();

            iniParser.Read(Arg.Any <string>(), Arg.Any <string>()).Returns("Five");

            var iniWrapper = new IniWrapperFactory().Create(x =>
            {
                x.MissingFileWhenLoadingHandling = MissingFileWhenLoadingHandling.ForceLoad;
            }, iniParser);

            var result = iniWrapper.LoadConfiguration <IniConverterEnumStringConverterConfiguration>();

            result.TestEnum.Should().Be(TestEnum.Five);
        }
Пример #7
0
        public void BoolBinaryConverter_ShouldLoadBool(string readValue, bool expected)
        {
            var iniParser = Substitute.For <IIniParser>();

            iniParser.Read(Arg.Any <string>(), Arg.Any <string>()).Returns(readValue);

            var iniWrapper = new IniWrapperFactory().Create(x =>
            {
                x.MissingFileWhenLoadingHandling = MissingFileWhenLoadingHandling.ForceLoad;
            }, iniParser);

            var result = iniWrapper.LoadConfiguration <BoolBinaryConverterConfiguration>();

            result.TestBool.Should().Be(expected);
        }
Пример #8
0
        public void SettingsNullValueHandling_Ignore_ShouldNotSaveNullValues()
        {
            var iniParser  = Substitute.For <IIniParser>();
            var iniWrapper = new IniWrapperFactory().Create(x =>
            {
                x.MissingFileWhenLoadingHandling = MissingFileWhenLoadingHandling.ForceLoad;
                x.NullValueHandling = NullValueHandling.Ignore;
            }, iniParser);

            var config = new TestConfiguration();

            iniWrapper.SaveConfiguration(config);

            iniParser.Received(5).Write(Arg.Any <string>(), Arg.Any <string>(), Arg.Any <string>());
        }
Пример #9
0
        public void SettingsNullValueHandling_ReplaceWithEmptyString_ShouldSaveDefaultValues_ForComplexType()
        {
            var iniParser  = Substitute.For <IIniParser>();
            var iniWrapper = new IniWrapperFactory().Create(x =>
            {
                x.MissingFileWhenLoadingHandling = MissingFileWhenLoadingHandling.ForceLoad;
                x.NullValueHandling = NullValueHandling.ReplaceWithEmptyString;
            }, iniParser);

            var config = new ComplexNullConfiguration();

            iniWrapper.SaveConfiguration(config);

            iniParser.Received(4).Write(Arg.Any <string>(), Arg.Any <string>(), Arg.Any <string>());
        }
Пример #10
0
        public void LoadConfiguration_When_IgnoreCheckIsSet()
        {
            var iniParser = Substitute.For <IIniParser>();

            var inisettings = new IniSettings()
            {
                MissingFileWhenLoadingHandling = MissingFileWhenLoadingHandling.ForceLoad
            };

            var iniWrapper = new IniWrapperFactory().Create(inisettings, iniParser);

            iniWrapper.LoadConfiguration <ImmutableConfiguration>();

            iniParser.Received(0).Write(Arg.Any <string>(), Arg.Any <string>(), Arg.Any <string>());
            iniParser.Received(10).Read(Arg.Any <string>(), Arg.Any <string>());
        }
Пример #11
0
        public void PropertyDecoratedWithIniConverterShouldInstantiateConverter_AndCallParseReadValue_WhenLoading()
        {
            var iniParser = Substitute.For <IIniParser>();

            iniParser.Read(Arg.Any <string>(), Arg.Any <string>()).Returns("dummy");

            var iniWrapper = new IniWrapperFactory().Create(x =>
            {
                x.MissingFileWhenLoadingHandling =
                    MissingFileWhenLoadingHandling.ForceLoad;
            }, iniParser);

            Action result = () => iniWrapper.LoadConfiguration <IniConverterWithConstructorParameters>();

            result.Should().Throw <TestCustomIniHandlerException>().WithMessage("ParseReadValue");
        }
Пример #12
0
        public void EnumStringConverter_ShouldWriteEnumAsString()
        {
            var iniParser  = Substitute.For <IIniParser>();
            var iniWrapper = new IniWrapperFactory().Create(x =>
            {
            }, iniParser);

            var config = new IniConverterEnumStringConverterConfiguration()
            {
                TestEnum = TestEnum.Five
            };

            iniWrapper.SaveConfiguration(config);

            iniParser.Received(1).Write(nameof(IniConverterEnumStringConverterConfiguration), nameof(IniConverterEnumStringConverterConfiguration.TestEnum), "Five");
        }
Пример #13
0
        public void ShouldThrow_LoadConfiguration_WhenCustomConverter_DoesNotImplementIIniCustomConverterInterface()
        {
            var iniParser = Substitute.For <IIniParser>();

            iniParser.Read(Arg.Any <string>(), Arg.Any <string>()).Returns("dummy");

            var iniWrapper = new IniWrapperFactory().Create(x =>
            {
                x.MissingFileWhenLoadingHandling =
                    MissingFileWhenLoadingHandling.ForceLoad;
            }, iniParser);

            Action result = () => iniWrapper.LoadConfiguration <CustomConverterWithoutInterface>();

            result.Should().Throw <InvalidCastException>().WithMessage($"Custom converter of type {typeof(ConverterWithoutInterface)} must implement IIniConverter interface.");
        }
Пример #14
0
        public void BoolBinaryConverter_ShouldSaveBool(string expected, bool value)
        {
            var iniParser  = Substitute.For <IIniParser>();
            var iniWrapper = new IniWrapperFactory().Create(x =>
            {
            }, iniParser);

            var config = new BoolBinaryConverterConfiguration()
            {
                TestBool = value
            };

            iniWrapper.SaveConfiguration(config);

            iniParser.Received(1).Write(nameof(BoolBinaryConverterConfiguration), nameof(BoolBinaryConverterConfiguration.TestBool), expected);
        }
Пример #15
0
        public void SettingsNullValueHandling_ReplaceWithEmptyString_ShouldSaveDefaultValues()
        {
            var iniParser  = Substitute.For <IIniParser>();
            var iniWrapper = new IniWrapperFactory().Create(x =>
            {
                x.MissingFileWhenLoadingHandling = MissingFileWhenLoadingHandling.ForceLoad;
                x.NullValueHandling = NullValueHandling.ReplaceWithEmptyString;
            }, iniParser);

            var config = new TestConfiguration();

            iniWrapper.SaveConfiguration(config);

            var properties = typeof(TestConfiguration).GetProperties().Length;

            iniParser.Received(properties).Write(Arg.Any <string>(), Arg.Any <string>(), Arg.Any <string>());
        }
Пример #16
0
        public void SaveDefaultConfiguration_When_CreateWithDefaultValuesIsSet()
        {
            var iniParser = Substitute.For <IIniParser>();

            var inisettings = new IniSettings()
            {
                MissingFileWhenLoadingHandling = MissingFileWhenLoadingHandling.CreateWithDefaultValues,
                IniFilePath = "dummyPath"
            };

            var iniWrapper = new IniWrapperFactory().Create(inisettings, iniParser);

            iniWrapper.LoadConfiguration <ImmutableConfiguration>();

            iniParser.Received(10).Write(Arg.Any <string>(), Arg.Any <string>(), Arg.Any <string>());
            iniParser.Received(0).Read(Arg.Any <string>(), Arg.Any <string>());
        }
Пример #17
0
        public void DoNotCallIniParser_When_DoNotLoadIsSet()
        {
            var iniParser = Substitute.For <IIniParser>();

            var inisettings = new IniSettings()
            {
                MissingFileWhenLoadingHandling = MissingFileWhenLoadingHandling.DoNotLoad,
                IniFilePath = "dummyPath"
            };

            var iniWrapper = new IniWrapperFactory().Create(inisettings, iniParser);

            iniWrapper.LoadConfiguration <TestConfiguration>();

            iniParser.Received(0).Write(Arg.Any <string>(), Arg.Any <string>(), Arg.Any <string>());
            iniParser.Received(0).Read(Arg.Any <string>(), Arg.Any <string>());
        }
Пример #18
0
        public void LoadConfiguration_When_IgnoreCheckIsSet()
        {
            var iniParser = Substitute.For <IIniParser>();

            var inisettings = new IniSettings()
            {
                MissingFileWhenLoadingHandling = MissingFileWhenLoadingHandling.ForceLoad
            };

            var iniWrapper = new IniWrapperFactory().Create(inisettings, iniParser);

            iniWrapper.LoadConfiguration <TestConfiguration>();

            iniParser.Received(0).Write(Arg.Any <string>(), Arg.Any <string>(), Arg.Any <string>());
            var properties = typeof(TestConfiguration).GetProperties().Length;

            iniParser.Received(properties).Read(Arg.Any <string>(), Arg.Any <string>());
        }
Пример #19
0
        public void ShouldThrow_LoadConfiguration_WhenCustomConverter_DoesNotHaveParameterlessConstructor_And_ConverterParametersWereNotPassed()
        {
            var iniParser = Substitute.For <IIniParser>();

            iniParser.Read(Arg.Any <string>(), Arg.Any <string>()).Returns("dummy");

            var iniWrapper = new IniWrapperFactory().Create(x =>
            {
                x.MissingFileWhenLoadingHandling =
                    MissingFileWhenLoadingHandling.ForceLoad;
            }, iniParser);

            Action result = () => iniWrapper.LoadConfiguration <IniConverterWithConstructorParametersNotSpecified>();

            result.Should().Throw <MissingMethodException>().WithMessage($"Please provide parameterless constructor for custom converter of type {typeof(CustomIniConverterWithConstructor)}" +
                                                                         " or pass arguments via converterParameters. " +
                                                                         "(e.g. [IniConverter(typeof(CustomIniConverterWithConstructor), new object[] { \"Argument\", 10 })])");
        }
        public void SettingsEnumerableEntitySeparator_ShouldDetermineEntitySeparatorWhenLoading(char separator)
        {
            var iniParser  = Substitute.For <IIniParser>();
            var iniWrapper = new IniWrapperFactory().Create(x =>
            {
                x.MissingFileWhenLoadingHandling = MissingFileWhenLoadingHandling.ForceLoad;
                x.EnumerableEntitySeparator      = separator;
            }, iniParser);

            iniParser.Read(nameof(TestConfiguration), nameof(TestConfiguration.TestStringList)).Returns($"a{separator}b{separator}c{separator}d{separator}f");

            var expected = new List <string>()
            {
                "a", "b", "c", "d", "f"
            };

            var result = iniWrapper.LoadConfiguration <TestConfiguration>();

            result.TestStringList.Should().BeEquivalentTo(expected);
        }
        public void SettingsEnumerableEntitySeparator_ShouldDetermineEntitySeparatorWhenSaving(char separator)
        {
            var iniParser  = Substitute.For <IIniParser>();
            var iniWrapper = new IniWrapperFactory().Create(x =>
            {
                x.MissingFileWhenLoadingHandling = MissingFileWhenLoadingHandling.ForceLoad;
                x.EnumerableEntitySeparator      = separator;
            }, iniParser);

            var config = new TestConfiguration()
            {
                TestStringList = new List <string>()
                {
                    "a", "b", "c", "d", "f"
                },
            };

            iniWrapper.SaveConfiguration(config);

            iniParser.Received(1).Write(nameof(TestConfiguration), nameof(TestConfiguration.TestStringList), $"a{separator}b{separator}c{separator}d{separator}f");
        }