コード例 #1
0
ファイル: HowToUse.cs プロジェクト: Nucs/JsonSettings
        public void Use_Configure_CasualSettingsExample()
        {
            using (var f = new TempfileLife()) {
                //load using Configure
                var o = JsonSettings.Configure <CasualExampleSettings>(f.FileName).WithBase64().WithEncryption("SuperPassword").LoadNow();
                o.SomeNumeralProperty = 1;
                o.SomeProperty        = "with some value";
                o.SomeClassProperty   = new SmallClass()
                {
                    Name = "Small", Value = "Class"
                };
                o.Save();

                //validate
                o = JsonSettings.Configure <CasualExampleSettings>(f.FileName).WithBase64().WithEncryption("SuperPassword").LoadNow();
                o.SomeProperty.Should().Be("with some value");
                o.SomeNumeralProperty.ShouldBeEquivalentTo(1);
                o.SomeClassProperty.Should().BeOfType(typeof(SmallClass)).And.Match(obj => (obj as SmallClass).Name == "Small");
            }
        }
コード例 #2
0
ファイル: HowToUse.cs プロジェクト: Nucs/JsonSettings
        public void Use__CasualSettingsExample()
        {
            using (var f = new TempfileLife()) {
                //used for autodelete file after test ends
                var o = JsonSettings.Load <CasualExampleSettings>(f.FileName);
                o.SomeNumeralProperty = 1;
                o.SomeProperty        = "with some value";
                o.SomeClassProperty   = new SmallClass()
                {
                    Name = "Small", Value = "Class"
                };
                o.Save();

                //validate
                o = JsonSettings.Load <CasualExampleSettings>(f.FileName);
                o.SomeProperty.Should().Be("with some value");
                o.SomeNumeralProperty.ShouldBeEquivalentTo(1);
                o.SomeClassProperty.Should().BeOfType(typeof(SmallClass)).And.Match(obj => (obj as SmallClass).Name == "Small");
            }
        }
コード例 #3
0
        public void OverrideExistingBySmallerSettingsFile()
        {
            var path = JsonSettings.ResolvePath(new SettingsLarger(), "swaggy.json", true);

            using (var f = new TempfileLife(path)) {
                if (File.Exists(f))
                {
                    File.Delete(f);
                }
                File.Exists(f).Should().BeFalse();

                var o = JsonSettings.Load <SettingsLarger>(f);
                o.Str = o.Str2 = o.Str3 = "lol";
                o.Save();

                File.Exists(f).Should().BeTrue();
                var o2 = JsonSettings.Load <Settings>(f);
                o2.Str.Should().BeEquivalentTo("lol");
            }
        }
コード例 #4
0
ファイル: AutosaveTests.cs プロジェクト: Nucs/JsonSettings
        public void SuspendAutosaving_Case1()
        {
            using (var f = new TempfileLife()) {
                var o = JsonSettings.Load <SettingsBag>(f);

                dynamic d = o.AsDynamic();

                d.SomeProp = "Works";
                d.Num      = 1;
                Assert.IsTrue(d["SomeProp"] == "Works");
                Assert.IsTrue(d.Num == 1);

                o.Save();
                o = JsonSettings.Configure <SettingsBag>(f)
                    .LoadNow()
                    .EnableAutosave();

                o["SomeProp"].Should().Be("Works");
                o["Num"].Should().Be(1L); //newtonsoft deserializes numbers as long.
                StrongBox <int> a = new StrongBox <int>();
                o.AfterSave += (sender, destinition) => { a.Value++; };

                using (o.SuspendAutosave()) {
                    o["SomeProp"] = "Works2";
                    o["Num"]      = 2;
                    a.Value.Should().Be(0);
                    var k = JsonSettings.Load <SettingsBag>(f);
                    k["SomeProp"].Should().Be("Works");
                    k["Num"].Should().Be(1L); //newtonsoft deserializes numbers as long.
                    a.Value.Should().Be(0);
                }

                a.Value.Should().Be(1);

                var kk = JsonSettings.Load <SettingsBag>(f);
                kk["SomeProp"].Should().Be("Works2");
                kk["Num"].Should().Be(2L); //newtonsoft deserializes numbers as long.
            }
        }
コード例 #5
0
        public void Saving()
        {
            using (var f = new TempfileLife()) {
                var rpath = JsonSettings.ResolvePath(f);

                bool saved = false;
                var  o     = JsonSettings.Load <Settings>(f.FileName).EnableAutosave();
                o.AfterSave += destinition => {
                    saved = true;
                };
                o.Property.Should().BeEquivalentTo(null);
                Console.WriteLine(File.ReadAllText(rpath));

                o.Property = "test";
                saved.Should().BeTrue();
                var o2 = JsonSettings.Load <Settings>(f.FileName).EnableAutosave();
                o2.Property.Should().BeEquivalentTo("test");
                var jsn = File.ReadAllText(rpath);
                jsn.Contains("\"test\"").Should().BeTrue();
                Console.WriteLine(jsn);
            }
        }
コード例 #6
0
        public void RenameAndLoadDefault_Case1()
        {
            using (var f = new TempfileLife(false)) {
                //load
                var cfg = JsonSettings.Configure <VersionedSettings>(f)
                          .WithVersioning(new Version(1, 0, 0, 0), VersioningResultAction.RenameAndLoadDefault)
                          .LoadNow();

                //assert
                cfg.Version.Should().Be(new Version(1, 0, 0, 0));

                //load
                cfg = JsonSettings.Configure <VersionedSettings>(f)
                      .WithVersioning(new Version(1, 2, 0, 0), VersioningResultAction.RenameAndLoadDefault)
                      .LoadNow();

                using var _1_0_0_0 = FindFile(f, new Version(1, 0, 0, 0));

                //change version and save
                cfg.Version = new Version("1.0.0.1");
                cfg.Save();

                //assert
                cfg.Version.Should().Be(new Version(1, 0, 0, 1));
                cfg = JsonSettings.Configure <VersionedSettings>(f)
                      .WithVersioning(new Version(1, 0, 0, 1), VersioningResultAction.RenameAndLoadDefault)
                      .LoadNow();
                cfg.Version.Should().Be(new Version(1, 0, 0, 1));

                //assert
                cfg = JsonSettings.Configure <VersionedSettings>(f)
                      .WithVersioning(new Version(1, 0, 0, 2), VersioningResultAction.RenameAndLoadDefault)
                      .LoadNow();
                using var _1_0_0_1 = FindFile(f, new Version(1, 0, 0, 1));

                cfg.Version.Should().Be(new Version(1, 0, 0, 2));
            }
        }
コード例 #7
0
        public void OnConfigure_Only_WithEncyption_CheckBeforeLoadNow()
        {
            using (var f = new TempfileLife()) {
                //used for autodelete file after test ends
                var o = JsonSettings.Configure <CasualConfiguredSettings>(f.FileName);
                o.Modulation.Modules.Should().ContainItemsAssignableTo <RijndaelModule>();
                Assert.True(o.Modulation.Modules.Count == 1, "o.Modules.Count == 1");
                o.LoadNow();
                o.SomeNumeralProperty = 1;
                o.SomeProperty        = "with some value";
                o.SomeClassProperty   = new SmallClass()
                {
                    Name = "Small", Value = "Class"
                };
                o.Save();

                //validate
                o = JsonSettings.Configure <CasualConfiguredSettings>(f.FileName).LoadNow();
                o.Modulation.Modules.Should().ContainItemsAssignableTo <RijndaelModule>();
                o.SomeProperty.Should().Be("with some value");
                o.SomeNumeralProperty.ShouldBeEquivalentTo(1);
                o.SomeClassProperty.Should().BeOfType(typeof(SmallClass)).And.Match(obj => (obj as SmallClass).Name == "Small");
            }
        }
コード例 #8
0
        public void LoadDefaultAndSave_Case1()
        {
            using (var f = new TempfileLife(false)) {
                //load
                var cfg = JsonSettings.Configure <VersionedSettings>(f)
                          .WithVersioning(new Version(1, 0, 0, 0), VersioningResultAction.Throw)
                          .LoadNow();

                //assert
                cfg.Version.Should().Be(new Version(1, 0, 0, 0));

                //load
                cfg = JsonSettings.Configure <VersionedSettings>(f)
                      .WithVersioning(new Version(1, 2, 0, 0), VersioningResultAction.LoadDefaultAndSave)
                      .LoadNow();
                cfg.Version.Should().Be(new Version(1, 2, 0, 0));

                new Action(() => {
                    cfg = JsonSettings.Configure <VersionedSettings>(f)
                          .WithVersioning(new Version(1, 2, 0, 0), VersioningResultAction.Throw)
                          .LoadNow();
                }).ShouldNotThrow();
            }
        }