Example #1
0
        public void Should_create_json_example()
        {
            var task = new ConfigGeneratorTask
            {
                AssemblyPath = Assembly.GetAssembly(GetType()).Location,
                // SettingsType = "JsOn",   is default
            };

            var configFile1 = typeof(MyConfig).Name;
            var configFile2 = typeof(MyConfig2).Name;
            var dirPath     = Path.Combine(Path.GetDirectoryName(task.AssemblyPath), "settings");

            if (Directory.Exists(dirPath))
            {
                Directory.Delete(dirPath, true);
            }

            var res = task.Execute();

            res.Should().BeTrue();
            const string ext   = ".example.json";
            var          path1 = Path.Combine(dirPath, configFile1 + ext);
            var          path2 = Path.Combine(dirPath, configFile2 + ext);

            File.Exists(path1).Should().BeTrue();
            File.Exists(path2).Should().BeTrue();
            File.ReadAllText(path1, Encoding.UTF8)
            .Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
            .Select(s => s.Trim())
            .Should()
            .BeEquivalentTo(
                "{",
                "\"Int\": 1,",
                "\"String\": \"str\",",
                "\"SubConfig\": {",
                "\"Long\": 1234567890123,",
                "\"Double\": 1.2345,",
                "\"Colors\": [",
                "0,",
                "15",
                "],",
                $"\"DateTime\": \"{new DateTime(2000, 1, 1):s}\"",
                "}",
                "}");
            File.ReadAllText(path2, Encoding.UTF8)
            .Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
            .Select(s => s.Trim())
            .Should()
            .BeEquivalentTo(
                "{",
                "\"Int\": 2,",
                "\"String\": \"string\",",
                "\"NullString\": null",
                "}");

            Directory.EnumerateFiles(dirPath).Count().Should().Be(2);
        }
Example #2
0
        public void Should_create_ini_example()
        {
            var task = new ConfigGeneratorTask
            {
                AssemblyPath = Assembly.GetAssembly(GetType()).Location,
                ConfigType   = "iNi",
            };

            var configFile1 = typeof(MyConfig).Name;
            var configFile2 = typeof(MyConfig2).Name;
            var dirPath     = Path.Combine(Path.GetDirectoryName(task.AssemblyPath), "settings");

            if (Directory.Exists(dirPath))
            {
                Directory.Delete(dirPath, true);
            }

            var res = task.Execute();

            res.Should().BeTrue();
            const string ext   = ".example.ini";
            var          path1 = Path.Combine(dirPath, configFile1 + ext);
            var          path2 = Path.Combine(dirPath, configFile2 + ext);

            File.Exists(path1).Should().BeTrue();
            File.Exists(path2).Should().BeTrue();
            File.ReadAllText(path1, Encoding.UTF8)
            .Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
            .Should()
            .BeEquivalentTo(
                "Int = 1",
                "String = str",
                "SubConfig.Long = 1234567890123",
                $"SubConfig.Double = {new MySubConfig().Double.ToString(CultureInfo.CurrentCulture)}",
                "SubConfig.Colors = ",
                $"SubConfig.DateTime = {new DateTime(2000, 1, 1)}");
            File.ReadAllText(path2, Encoding.UTF8)
            .Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
            .Should()
            .BeEquivalentTo(
                "Int = 2",
                "String = string",
                "NullString = ");

            Directory.EnumerateFiles(dirPath).Count().Should().Be(2);
        }
Example #3
0
        public void Should_throw_exception_if_type_is_not_specified()
        {
            var task = new ConfigGeneratorTask
            {
                AssemblyPath = Assembly.GetAssembly(GetType()).Location,
                ConfigType   = "wrong_type",
            };

            var dirPath = Path.Combine(Path.GetDirectoryName(task.AssemblyPath), "settings");

            if (Directory.Exists(dirPath))
            {
                Directory.Delete(dirPath, true);
            }

            var res = false;

            new Action(() => res = task.Execute()).Should().Throw <ArgumentException>();
            res.Should().BeFalse();
        }