示例#1
0
        public void TestFilePathesInJson()
        {
            var root      = EnvironmentV2.instance.GetOrAddTempFolder("TestFilePathesInJson");
            var file1     = root.GetChildDir("SubDir1").GetChildDir("SubSubDir1").GetChild("child1.txt");
            var savedText = "Test 123";

            file1.SaveAsText(savedText);
            IFileRef x1 = new FileRef();

            x1.SetPath(file1);

            var x2 = x1.DeepCopyViaJson();

            AssertV2.AreEqualJson(x1, x2);
            Assert.NotEmpty(x1.fileName);
            Assert.NotEmpty(x2.fileName);

            // GetChild ensures that no special characters like / are in the file name:
            var fullPathViaGetChild = root.GetChild("" + x2.GetPath());

            Assert.False(fullPathViaGetChild.Exists);

            // ResolveFilePath can be used to resolve full pathes including / characters:
            var file2 = root.ResolveFilePath("" + x2.GetPath());

            Assert.True(file2.Exists);
            Assert.Equal(savedText, file2.LoadAs <string>());
        }
示例#2
0
        public void ExampleUsage1()
        {
            MyClass1 original = new MyClass1()
            {
                name = "1", child = new MyClass1()
                {
                    name = "2"
                }
            };

            MyClass1 copy = original.DeepCopyViaJson();

            Assert.Null(MergeJson.GetDiff(original, copy)); // No diff between original and copy
            AssertV2.AreEqualJson(original, copy);          // WIll use MergeJson.GetDiff internally
            Assert.Equal(original.child.name, copy.child.name);
            // Modify the copy, changing the copy will not change the original:
            copy.child.name = "Some new name..";
            // Check that the change was only done in the copy and not the original:
            Assert.NotEqual(original.child.name, copy.child.name);
            Assert.NotNull(MergeJson.GetDiff(original, copy));

            // Objects that impl. IClonable can also ShallowCopy (will call .Clone internally):
            MyClass1 shallowCopy = original.ShallowCopyViaClone();

            Assert.NotSame(original, shallowCopy);
            Assert.Same(original.child, shallowCopy.child);
        }
示例#3
0
        public void TestFromJsonForReadonlyFields()
        {
            MyClass1 x = new MyClass1(12, ImmutableList.Create(new string[] { "a", "b", "c" }));

            {
                var xAsJson = JsonWriter.GetWriter().Write(x);
                Assert.NotEmpty(xAsJson);

                // The fields are immutable so the json logic has to use the constructor to init them from the json:
                var y = JsonReader.GetReader().Read <MyClass1>(xAsJson);
                Assert.Equal(x.myNumber, y.myNumber);
                AssertV2.AreEqualJson(x, y);
            }

            // The constructor json logic can also be mixed with normal field json logic:
            x.myMutableList = new List <string>()
            {
                "d", "e"
            };
            {
                var xAsJson = JsonWriter.GetWriter().Write(x);
                Assert.NotEmpty(xAsJson);
                var y = JsonReader.GetReader().Read <MyClass1>(xAsJson);

                // myMutableList is not required in the constructor but still correctly set by the json logic:
                Assert.Equal(x.myMutableList, y.myMutableList);
                AssertV2.AreEqualJson(x, y);
            }
        }
示例#4
0
        public void ExampleUsage1()
        {
            MyClass1 x1 = new MyClass1()
            {
                myString = "abc", myString2 = "def"
            };
            // Generate a json object from the object that includes all public fields and props:
            string jsonString = JsonWriter.GetWriter().Write(x1);
            // Parse the json back into a second instance and compare both:
            MyClass1 x2 = JsonReader.GetReader().Read <MyClass1>(jsonString);

            Assert.Equal(x1.myString, x2.myString);
            Assert.Equal(x1.myString2, x2.myString2);
            AssertV2.AreEqualJson(x1, x2);
        }
        public void TestFilePathesInJson()
        {
            var root      = EnvironmentV2.instance.GetOrAddTempFolder("TestFilePathesInJson");
            var file1     = root.GetChildDir("SubDir1").GetChildDir("SubSubDir1").GetChild("child1.txt");
            var savedText = "Test 123";

            file1.SaveAsText(savedText);
            IFileRef x1 = new FileRef();

            x1.SetPath(file1);

            var x2 = x1.DeepCopyViaJson();

            AssertV2.AreEqualJson(x1, x2);
            Assert.NotEmpty(x1.fileName);
            Assert.NotEmpty(x2.fileName);

            var file2 = root.GetChild("" + x2.GetPath());

            Assert.True(file2.Exists);
            Assert.Equal(savedText, file2.LoadAs <string>());
        }
示例#6
0
        public void TestFilePathesInJson()
        {
            var root      = EnvironmentV2.instance.GetOrAddTempFolder("TestFilePathesInJson");
            var file1     = root.GetChildDir("SubDir1").GetChildDir("SubSubDir1").GetChild("child1.txt");
            var savedText = "Test 123";

            file1.SaveAsText(savedText);
            MyClass3 x1 = new MyClass3()
            {
                myPath = file1.FullName
            };

            var x2 = x1.DeepCopyViaJson();

            AssertV2.AreEqualJson(x1, x2);
            Assert.NotEmpty(x1.myPath);
            Assert.NotEmpty(x2.myPath);

            var file2 = root.GetChild(x2.myPath);

            Assert.True(file2.Exists);
            Assert.Equal(savedText, file2.LoadAs <string>());
        }
示例#7
0
        public void ExampleUsage1()
        {
            MyClass1 original = new MyClass1()
            {
                name = "1", child = new MyClass1()
                {
                    name = "2", age = 3
                }
            };

            MyClass1 copy = original.DeepCopyViaJson();

            Assert.Null(MergeJson.GetDiff(original, copy)); // No diff between original and copy
            AssertV2.AreEqualJson(original, copy);          // AreEqualJson will use MergeJson.GetDiff internally
            Assert.Equal(original.child.name, copy.child.name);
            // Modify the copy, changing the copy will not change the original:
            copy.child.name = "Some new name..";
            // Check that the change was only done in the copy and not the original:
            Assert.NotEqual(original.child.name, copy.child.name);
            JToken diffToOriginal = MergeJson.GetDiff(original, copy);

            Assert.NotNull(diffToOriginal);

            // Objects that impl. IClonable can also ShallowCopy (will call .Clone internally):
            MyClass1 shallowCopy = original.ShallowCopyViaClone();

            Assert.NotSame(original, shallowCopy);
            Assert.Same(original.child, shallowCopy.child);

            // Applying a change to an existing target object is done using MergeJson.Patch:
            var oldName = original.child.name;

            MergeJson.Patch(original, diffToOriginal);     // Apply the changes stored in the diff
            Assert.NotEqual(oldName, original.child.name); // The name field was updated
            Assert.Equal(copy.child.name, original.child.name);
            Assert.Equal(3, original.child.age);           // The age field was not changed by the patch
        }