コード例 #1
0
        public void TestError()
        {
            var cfg = YamlSimpleConfig.FromString(YamlString);

            Assert.Throws <ConfigOptionNotExistsException>(
                () => cfg.GetOption <int>("root.not_existing"));

            Assert.Throws <ConfigInvalidPathException>(
                () => cfg.GetOption <int>("root.num.a"));

            Assert.Throws <InvalidCastException>(
                () => cfg.GetOption <int>("root.txt"));

            Assert.Throws <InvalidOperationException>(
                () => cfg.GetOptionAsPath("root.path_abs"));
        }
コード例 #2
0
        public void Test()
        {
            var cfg1 = YamlSimpleConfig.FromStringDir(YamlString1, "/cfg1");
            var cfg2 = YamlSimpleConfig.FromStringDir(YamlString2, "/cfg2");
            var cfg3 = YamlSimpleConfig.FromStringDir(YamlString3, "/cfg3");

            Assert.Equal("root-val1", cfg1.GetOption <string>("root.val1"));
            Assert.Equal("root-val2", cfg1.GetOption <string>("root.val2"));

            Assert.Equal("custom-val1", cfg2.GetOption <string>("custom.val1"));
            Assert.Equal("custom-val3", cfg2.GetOption <string>("custom.val3"));

            Assert.Equal("third-val1", cfg3.GetOption <string>("val1"));
            Assert.Equal("third-val4", cfg3.GetOption <string>("val4"));

            var stacked = new StackedSimpleConfig();

            stacked.AddConfig("root", cfg1);
            stacked.AddConfig("custom", cfg2);

            Assert.Equal("custom-val1", stacked.GetOption <string>("val1"));
            Assert.Equal("root-val2", stacked.GetOption <string>("val2"));

            var stacked2 = new StackedSimpleConfig();

            stacked2.AddConfig("root", cfg1);
            stacked2.AddConfig("custom", cfg2);
            stacked2.AddConfig(cfg3);

            Assert.Equal("third-val1", stacked2.GetOption <string>("val1"));
            Assert.Equal("root-val2", stacked2.GetOption <string>("val2"));
            Assert.Equal("custom-val3", stacked2.GetOption <string>("val3"));
            Assert.Equal("third-val4", stacked2.GetOption <string>("val4"));

            Assert.Equal("def", stacked2.GetOption <string>("val5", "def"));

            Assert.Equal("/cfg2/c.txt", stacked2.GetOptionAsPath("path1"));
            Assert.Equal("/cfg1/b.txt", stacked2.GetOptionAsPath("path2"));
            Assert.Equal("/cfg2/d.txt", stacked2.GetOptionAsPath("path3", "/a.txt"));
            Assert.Equal("/a.txt", stacked2.GetOptionAsPath("not_existing", "/a.txt"));

            Assert.Throws <ConfigOptionNotExistsException>(
                () => stacked2.GetOptionAsPath("not_existing"));
        }
コード例 #3
0
        public void TestLoadFile()
        {
            File.WriteAllText("/tmp/kdlib_test.yaml", YamlString);
            var cfg = (ISimpleConfig)YamlSimpleConfig.FromFile("/tmp/kdlib_test.yaml");

            File.Delete("/tmp/kdlib_test.yaml");

            var pathRel = cfg.GetOptionAsPath("root.path_rel");

            Assert.Equal("/tmp/a.txt", pathRel);

            var pathAbs = cfg.GetOptionAsPath("root.path_abs");

            Assert.Equal("/a.txt", pathAbs);

            var pathAbs1 = cfg.GetOptionAsPath("root.path_abs", "/default.txt");

            Assert.Equal("/a.txt", pathAbs1);

            var pathAbs2 = cfg.GetOptionAsPath("root.not_existing", "/default.txt");

            Assert.Equal("/default.txt", pathAbs2);
        }
コード例 #4
0
        public void TestLoadString()
        {
            var cfg = (ISimpleConfig)YamlSimpleConfig.FromString(YamlString);

            var num                = cfg.GetOption <int>("root.num");
            var numS               = cfg.GetOption <string>("root.num");
            var txt                = cfg.GetOption <string>("root.txt");
            var arr                = cfg.GetOption <int[]>("root.arr");
            var obj                = cfg.GetOption <Dictionary <string, int> >("root.obj");
            var emptyString        = cfg.GetOption <string>("root.empty");
            var notExistingNull    = cfg.GetOption <string>("root.not_existing", null);
            var notExistingDefault = cfg.GetOption <string>("root.not_existing", "def");

            Assert.Equal(2, num);
            Assert.Equal("2", numS);
            Assert.Equal("A", txt);
            Assert.Equal(new[] { 1, 2 }, arr);
            Assert.Equal(1, obj["a"]);
            Assert.Equal(2, obj["b"]);
            Assert.Equal("", emptyString);
            Assert.Null(notExistingNull);
            Assert.Equal("def", notExistingDefault);
        }