| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583 |
- /*
- * Copyright (c) Contributors to the Open 3D Engine Project.
- * For complete copyright and license terms please see the LICENSE at the root of this distribution.
- *
- * SPDX-License-Identifier: Apache-2.0 OR MIT
- *
- */
- #include <AzCore/DOM/DomPatch.h>
- #include <AzCore/DOM/DomComparison.h>
- #include <Tests/DOM/DomFixtures.h>
- namespace AZ::Dom::Tests
- {
- class DomPatchTests : public DomTestFixture
- {
- public:
- void SetUp() override
- {
- DomTestFixture::SetUp();
- m_dataset = Value(Type::Object);
- m_dataset["arr"].SetArray();
- m_dataset["node"].SetNode("SomeNode");
- m_dataset["node"]["int"] = 5;
- m_dataset["node"]["null"] = Value();
- for (int i = 0; i < 5; ++i)
- {
- m_dataset["arr"].ArrayPushBack(Value(i));
- m_dataset["node"].ArrayPushBack(Value(i * 2));
- }
- m_dataset["obj"].SetObject();
- m_dataset["obj"]["foo"] = true;
- m_dataset["obj"]["bar"] = false;
- m_deltaDataset = m_dataset;
- }
- void TearDown() override
- {
- m_dataset = m_deltaDataset = Value();
- DomTestFixture::TearDown();
- }
- PatchUndoRedoInfo GenerateAndVerifyDelta()
- {
- PatchUndoRedoInfo info = GenerateHierarchicalDeltaPatch(m_dataset, m_deltaDataset);
- auto result = info.m_forwardPatches.Apply(m_dataset);
- EXPECT_TRUE(result.IsSuccess());
- EXPECT_TRUE(Utils::DeepCompareIsEqual(result.GetValue(), m_deltaDataset));
- result = info.m_inversePatches.Apply(result.GetValue());
- EXPECT_TRUE(result.IsSuccess());
- EXPECT_TRUE(Utils::DeepCompareIsEqual(result.GetValue(), m_dataset));
- // Verify serialization of the patches
- auto VerifySerialization = [](const Patch& patch)
- {
- Value serializedPatch = patch.GetDomRepresentation();
- auto deserializePatchResult = Patch::CreateFromDomRepresentation(serializedPatch);
- EXPECT_TRUE(deserializePatchResult.IsSuccess());
- EXPECT_EQ(deserializePatchResult.GetValue(), patch);
- };
- VerifySerialization(info.m_forwardPatches);
- VerifySerialization(info.m_inversePatches);
- return info;
- }
- Value m_dataset;
- Value m_deltaDataset;
- };
- TEST_F(DomPatchTests, AddOperation_InsertInObject_Succeeds)
- {
- Path p("/obj/baz");
- PatchOperation op = PatchOperation::AddOperation(p, Value(42));
- auto result = op.Apply(m_dataset);
- ASSERT_TRUE(result.IsSuccess());
- EXPECT_EQ(result.GetValue()[p].GetInt64(), 42);
- }
- TEST_F(DomPatchTests, AddOperation_ReplaceInObject_Succeeds)
- {
- Path p("/obj/foo");
- PatchOperation op = PatchOperation::AddOperation(p, Value(false));
- auto result = op.Apply(m_dataset);
- ASSERT_TRUE(result.IsSuccess());
- EXPECT_EQ(result.GetValue()[p].GetBool(), false);
- }
- TEST_F(DomPatchTests, AddOperation_InsertObjectKeyInArray_Fails)
- {
- Path p("/arr/key");
- PatchOperation op = PatchOperation::AddOperation(p, Value(999));
- auto result = op.Apply(m_dataset);
- ASSERT_FALSE(result.IsSuccess());
- }
- TEST_F(DomPatchTests, AddOperation_AppendInArray_Succeeds)
- {
- Path p("/arr/-");
- PatchOperation op = PatchOperation::AddOperation(p, Value(42));
- auto result = op.Apply(m_dataset);
- ASSERT_TRUE(result.IsSuccess());
- EXPECT_EQ(result.GetValue()["arr"][5].GetInt64(), 42);
- }
- TEST_F(DomPatchTests, AddOperation_InsertKeyInNode_Succeeds)
- {
- Path p("/node/attr");
- PatchOperation op = PatchOperation::AddOperation(p, Value(500));
- auto result = op.Apply(m_dataset);
- ASSERT_TRUE(result.IsSuccess());
- EXPECT_EQ(result.GetValue()[p].GetInt64(), 500);
- }
- TEST_F(DomPatchTests, AddOperation_ReplaceIndexInNode_Succeeds)
- {
- Path p("/node/0");
- PatchOperation op = PatchOperation::AddOperation(p, Value(42));
- auto result = op.Apply(m_dataset);
- ASSERT_TRUE(result.IsSuccess());
- EXPECT_EQ(result.GetValue()[p].GetInt64(), 42);
- }
- TEST_F(DomPatchTests, AddOperation_AppendInNode_Succeeds)
- {
- Path p("/node/-");
- PatchOperation op = PatchOperation::AddOperation(p, Value(42));
- auto result = op.Apply(m_dataset);
- ASSERT_TRUE(result.IsSuccess());
- EXPECT_EQ(result.GetValue()["node"][5].GetInt64(), 42);
- }
- TEST_F(DomPatchTests, AddOperation_InvalidPath_Fails)
- {
- Path p("/non/existent/path");
- PatchOperation op = PatchOperation::AddOperation(p, Value(0));
- auto result = op.Apply(m_dataset);
- ASSERT_FALSE(result.IsSuccess());
- }
- TEST_F(DomPatchTests, RemoveOperation_RemoveKeyFromObject_Succeeds)
- {
- Path p("/obj/foo");
- PatchOperation op = PatchOperation::RemoveOperation(p);
- auto result = op.Apply(m_dataset);
- ASSERT_TRUE(result.IsSuccess());
- EXPECT_FALSE(result.GetValue()["obj"].HasMember("foo"));
- }
- TEST_F(DomPatchTests, RemoveOperation_RemoveIndexFromArray_Succeeds)
- {
- Path p("/arr/0");
- PatchOperation op = PatchOperation::RemoveOperation(p);
- auto result = op.Apply(m_dataset);
- ASSERT_TRUE(result.IsSuccess());
- EXPECT_EQ(result.GetValue()["arr"].ArraySize(), 4);
- EXPECT_EQ(result.GetValue()["arr"][0].GetInt64(), 1);
- }
- TEST_F(DomPatchTests, RemoveOperation_PopArray_Fails)
- {
- Path p("/arr/-");
- PatchOperation op = PatchOperation::RemoveOperation(p);
- auto result = op.Apply(m_dataset);
- ASSERT_FALSE(result.IsSuccess());
- }
- TEST_F(DomPatchTests, RemoveOperation_RemoveKeyFromNode_Succeeds)
- {
- Path p("/node/int");
- PatchOperation op = PatchOperation::RemoveOperation(p);
- auto result = op.Apply(m_dataset);
- ASSERT_TRUE(result.IsSuccess());
- EXPECT_FALSE(result.GetValue()["node"].HasMember("int"));
- }
- TEST_F(DomPatchTests, RemoveOperation_RemoveIndexFromNode_Succeeds)
- {
- Path p("/node/1");
- PatchOperation op = PatchOperation::RemoveOperation(p);
- auto result = op.Apply(m_dataset);
- ASSERT_TRUE(result.IsSuccess());
- EXPECT_EQ(result.GetValue()["node"].ArraySize(), 4);
- EXPECT_EQ(result.GetValue()["node"][1].GetInt64(), 4);
- }
- TEST_F(DomPatchTests, RemoveOperation_PopIndexFromNode_Fails)
- {
- Path p("/node/-");
- PatchOperation op = PatchOperation::RemoveOperation(p);
- auto result = op.Apply(m_dataset);
- ASSERT_FALSE(result.IsSuccess());
- }
- TEST_F(DomPatchTests, RemoveOperation_RemoveKeyFromArray_Fails)
- {
- Path p("/arr/foo");
- PatchOperation op = PatchOperation::RemoveOperation(p);
- auto result = op.Apply(m_dataset);
- ASSERT_FALSE(result.IsSuccess());
- }
- TEST_F(DomPatchTests, RemoveOperation_InvalidPath_Fails)
- {
- Path p("/non/existent/path");
- PatchOperation op = PatchOperation::RemoveOperation(p);
- auto result = op.Apply(m_dataset);
- ASSERT_FALSE(result.IsSuccess());
- }
- TEST_F(DomPatchTests, ReplaceOperation_InsertInObject_Fails)
- {
- Path p("/obj/baz");
- PatchOperation op = PatchOperation::ReplaceOperation(p, Value(42));
- auto result = op.Apply(m_dataset);
- ASSERT_FALSE(result.IsSuccess());
- }
- TEST_F(DomPatchTests, ReplaceOperation_ReplaceInObject_Succeeds)
- {
- Path p("/obj/foo");
- PatchOperation op = PatchOperation::ReplaceOperation(p, Value(false));
- auto result = op.Apply(m_dataset);
- ASSERT_TRUE(result.IsSuccess());
- EXPECT_EQ(result.GetValue()[p].GetBool(), false);
- }
- TEST_F(DomPatchTests, ReplaceOperation_InsertObjectKeyInArray_Fails)
- {
- Path p("/arr/key");
- PatchOperation op = PatchOperation::ReplaceOperation(p, Value(999));
- auto result = op.Apply(m_dataset);
- ASSERT_FALSE(result.IsSuccess());
- }
- TEST_F(DomPatchTests, ReplaceOperation_AppendInArray_Fails)
- {
- Path p("/arr/-");
- PatchOperation op = PatchOperation::ReplaceOperation(p, Value(42));
- auto result = op.Apply(m_dataset);
- ASSERT_FALSE(result.IsSuccess());
- }
- TEST_F(DomPatchTests, ReplaceOperation_InsertKeyInNode_Fails)
- {
- Path p("/node/attr");
- PatchOperation op = PatchOperation::ReplaceOperation(p, Value(500));
- auto result = op.Apply(m_dataset);
- ASSERT_FALSE(result.IsSuccess());
- }
- TEST_F(DomPatchTests, ReplaceOperation_ReplaceIndexInNode_Succeeds)
- {
- Path p("/node/0");
- PatchOperation op = PatchOperation::ReplaceOperation(p, Value(42));
- auto result = op.Apply(m_dataset);
- ASSERT_TRUE(result.IsSuccess());
- EXPECT_EQ(result.GetValue()[p].GetInt64(), 42);
- }
- TEST_F(DomPatchTests, ReplaceOperation_AppendInNode_Fails)
- {
- Path p("/node/-");
- PatchOperation op = PatchOperation::ReplaceOperation(p, Value(42));
- auto result = op.Apply(m_dataset);
- ASSERT_FALSE(result.IsSuccess());
- }
- TEST_F(DomPatchTests, ReplaceOperation_InvalidPath_Fails)
- {
- Path p("/non/existent/path");
- PatchOperation op = PatchOperation::ReplaceOperation(p, Value(0));
- auto result = op.Apply(m_dataset);
- ASSERT_FALSE(result.IsSuccess());
- }
- TEST_F(DomPatchTests, CopyOperation_ArrayToObject_Succeeds)
- {
- Path dest("/obj/arr");
- Path src("/arr");
- PatchOperation op = PatchOperation::CopyOperation(dest, src);
- auto result = op.Apply(m_dataset);
- ASSERT_TRUE(result.IsSuccess());
- EXPECT_TRUE(Utils::DeepCompareIsEqual(m_dataset[src], result.GetValue()[dest]));
- EXPECT_TRUE(Utils::DeepCompareIsEqual(result.GetValue()[src], result.GetValue()[dest]));
- }
- TEST_F(DomPatchTests, CopyOperation_ObjectToArrayInRange_Succeeds)
- {
- Path dest("/arr/0");
- Path src("/obj");
- PatchOperation op = PatchOperation::CopyOperation(dest, src);
- auto result = op.Apply(m_dataset);
- ASSERT_TRUE(result.IsSuccess());
- EXPECT_TRUE(Utils::DeepCompareIsEqual(m_dataset[src], result.GetValue()[dest]));
- EXPECT_TRUE(Utils::DeepCompareIsEqual(result.GetValue()[src], result.GetValue()[dest]));
- }
- TEST_F(DomPatchTests, CopyOperation_ObjectToArrayOutOfRange_Fails)
- {
- Path dest("/arr/5");
- Path src("/obj");
- PatchOperation op = PatchOperation::CopyOperation(dest, src);
- auto result = op.Apply(m_dataset);
- ASSERT_FALSE(result.IsSuccess());
- }
- TEST_F(DomPatchTests, CopyOperation_ObjectToNodeChildInRange_Succeeds)
- {
- Path dest("/node/0");
- Path src("/obj");
- PatchOperation op = PatchOperation::CopyOperation(dest, src);
- auto result = op.Apply(m_dataset);
- ASSERT_TRUE(result.IsSuccess());
- EXPECT_TRUE(Utils::DeepCompareIsEqual(m_dataset[src], result.GetValue()[dest]));
- EXPECT_TRUE(Utils::DeepCompareIsEqual(result.GetValue()[src], result.GetValue()[dest]));
- }
- TEST_F(DomPatchTests, CopyOperation_ObjectToNodeChildOutOfRange_Fails)
- {
- Path dest("/node/5");
- Path src("/obj");
- PatchOperation op = PatchOperation::CopyOperation(dest, src);
- auto result = op.Apply(m_dataset);
- ASSERT_FALSE(result.IsSuccess());
- }
- TEST_F(DomPatchTests, CopyOperation_InvalidSourcePath_Fails)
- {
- Path dest("/node/0");
- Path src("/invalid/path");
- PatchOperation op = PatchOperation::CopyOperation(dest, src);
- auto result = op.Apply(m_dataset);
- ASSERT_FALSE(result.IsSuccess());
- }
- TEST_F(DomPatchTests, CopyOperation_InvalidDestinationPath_Fails)
- {
- Path dest("/invalid/path");
- Path src("/arr/0");
- PatchOperation op = PatchOperation::CopyOperation(dest, src);
- auto result = op.Apply(m_dataset);
- ASSERT_FALSE(result.IsSuccess());
- }
- TEST_F(DomPatchTests, MoveOperation_ArrayToObject_Succeeds)
- {
- Path dest("/obj/arr");
- Path src("/arr");
- PatchOperation op = PatchOperation::MoveOperation(dest, src);
- auto result = op.Apply(m_dataset);
- ASSERT_TRUE(result.IsSuccess());
- EXPECT_TRUE(Utils::DeepCompareIsEqual(m_dataset[src], result.GetValue()[dest]));
- EXPECT_FALSE(result.GetValue().HasMember("arr"));
- }
- TEST_F(DomPatchTests, MoveOperation_ObjectToArrayInRange_Succeeds)
- {
- Path dest("/arr/0");
- Path src("/obj");
- PatchOperation op = PatchOperation::MoveOperation(dest, src);
- auto result = op.Apply(m_dataset);
- ASSERT_TRUE(result.IsSuccess());
- EXPECT_TRUE(Utils::DeepCompareIsEqual(m_dataset[src], result.GetValue()[dest]));
- EXPECT_FALSE(result.GetValue().HasMember("obj"));
- }
- TEST_F(DomPatchTests, MoveOperation_ObjectToArrayOutOfRange_Fails)
- {
- Path dest("/arr/5");
- Path src("/obj");
- PatchOperation op = PatchOperation::MoveOperation(dest, src);
- auto result = op.Apply(m_dataset);
- ASSERT_FALSE(result.IsSuccess());
- }
- TEST_F(DomPatchTests, MoveOperation_ObjectToNodeChildInRange_Succeeds)
- {
- Path dest("/node/0");
- Path src("/obj");
- PatchOperation op = PatchOperation::MoveOperation(dest, src);
- auto result = op.Apply(m_dataset);
- ASSERT_TRUE(result.IsSuccess());
- EXPECT_TRUE(Utils::DeepCompareIsEqual(m_dataset[src], result.GetValue()[dest]));
- EXPECT_FALSE(result.GetValue().HasMember("obj"));
- }
- TEST_F(DomPatchTests, MoveOperation_ObjectToNodeChildOutOfRange_Fails)
- {
- Path dest("/node/5");
- Path src("/obj");
- PatchOperation op = PatchOperation::MoveOperation(dest, src);
- auto result = op.Apply(m_dataset);
- ASSERT_FALSE(result.IsSuccess());
- }
- TEST_F(DomPatchTests, MoveOperation_InvalidSourcePath_Fails)
- {
- Path dest("/node/0");
- Path src("/invalid/path");
- PatchOperation op = PatchOperation::MoveOperation(dest, src);
- auto result = op.Apply(m_dataset);
- ASSERT_FALSE(result.IsSuccess());
- }
- TEST_F(DomPatchTests, MoveOperation_InvalidDestinationPath_Fails)
- {
- Path dest("/invalid/path");
- Path src("/arr/0");
- PatchOperation op = PatchOperation::MoveOperation(dest, src);
- auto result = op.Apply(m_dataset);
- ASSERT_FALSE(result.IsSuccess());
- }
- TEST_F(DomPatchTests, TestOperation_TestCorrectValue_Succeeds)
- {
- Path path("/arr/1");
- Value value(1);
- PatchOperation op = PatchOperation::TestOperation(path, value);
- auto result = op.Apply(m_dataset);
- ASSERT_TRUE(result.IsSuccess());
- }
- TEST_F(DomPatchTests, TestOperation_TestIncorrectValue_Fails)
- {
- Path path("/arr/1");
- Value value(55);
- PatchOperation op = PatchOperation::TestOperation(path, value);
- auto result = op.Apply(m_dataset);
- ASSERT_FALSE(result.IsSuccess());
- }
- TEST_F(DomPatchTests, TestOperation_TestCorrectComplexValue_Succeeds)
- {
- Path path;
- Value value = m_dataset;
- PatchOperation op = PatchOperation::TestOperation(path, value);
- auto result = op.Apply(m_dataset);
- ASSERT_TRUE(result.IsSuccess());
- }
- TEST_F(DomPatchTests, TestOperation_TestIncorrectComplexValue_Fails)
- {
- Path path;
- Value value = m_dataset;
- value["arr"][4] = 9;
- PatchOperation op = PatchOperation::TestOperation(path, value);
- auto result = op.Apply(m_dataset);
- ASSERT_FALSE(result.IsSuccess());
- }
- TEST_F(DomPatchTests, TestOperation_TestInvalidPath_Fails)
- {
- Path path("/invalid/path");
- Value value;
- PatchOperation op = PatchOperation::TestOperation(path, value);
- auto result = op.Apply(m_dataset);
- ASSERT_FALSE(result.IsSuccess());
- }
- TEST_F(DomPatchTests, TestOperation_TestInsertArrayPath_Fails)
- {
- Path path("/arr/-");
- Value value(4);
- PatchOperation op = PatchOperation::TestOperation(path, value);
- auto result = op.Apply(m_dataset);
- ASSERT_FALSE(result.IsSuccess());
- }
- TEST_F(DomPatchTests, TestPatch_ReplaceArrayValue)
- {
- m_deltaDataset["arr"][0] = 5;
- GenerateAndVerifyDelta();
- }
- TEST_F(DomPatchTests, TestPatch_AppendArrayValue)
- {
- m_deltaDataset["arr"].ArrayPushBack(Value(7));
- auto result = GenerateAndVerifyDelta();
- // Ensure the generated patch uses the array append operation
- ASSERT_EQ(result.m_forwardPatches.Size(), 1);
- EXPECT_TRUE(result.m_forwardPatches[0].GetDestinationPath()[1].IsEndOfArray());
- }
- TEST_F(DomPatchTests, TestPatch_AppendArrayValues)
- {
- m_deltaDataset["arr"].ArrayPushBack(Value(7));
- m_deltaDataset["arr"].ArrayPushBack(Value(8));
- m_deltaDataset["arr"].ArrayPushBack(Value(9));
- GenerateAndVerifyDelta();
- }
- TEST_F(DomPatchTests, TestPatch_InsertArrayValue)
- {
- auto& arr = m_deltaDataset["arr"].GetMutableArray();
- arr.insert(arr.begin(), Value(42));
- GenerateAndVerifyDelta();
- }
- TEST_F(DomPatchTests, TestPatch_InsertObjectKey)
- {
- m_deltaDataset["obj"]["newKey"].CopyFromString("test");
- GenerateAndVerifyDelta();
- }
- TEST_F(DomPatchTests, TestPatch_DeleteObjectKey)
- {
- m_deltaDataset["obj"].RemoveMember("foo");
- GenerateAndVerifyDelta();
- }
- TEST_F(DomPatchTests, TestPatch_AppendNodeValues)
- {
- m_deltaDataset["node"].ArrayPushBack(Value(7));
- m_deltaDataset["node"].ArrayPushBack(Value(8));
- m_deltaDataset["node"].ArrayPushBack(Value(9));
- GenerateAndVerifyDelta();
- }
- TEST_F(DomPatchTests, TestPatch_InsertNodeValue)
- {
- auto& node = m_deltaDataset["node"].GetMutableNode();
- node.GetChildren().insert(node.GetChildren().begin(), Value(42));
- GenerateAndVerifyDelta();
- }
- TEST_F(DomPatchTests, TestPatch_InsertNodeKey)
- {
- m_deltaDataset["node"]["newKey"].CopyFromString("test");
- GenerateAndVerifyDelta();
- }
- TEST_F(DomPatchTests, TestPatch_DeleteNodeKey)
- {
- m_deltaDataset["node"].RemoveMember("int");
- GenerateAndVerifyDelta();
- }
- TEST_F(DomPatchTests, TestPatch_RenameNode)
- {
- m_deltaDataset["node"].SetNodeName("RenamedNode");
- GenerateAndVerifyDelta();
- }
- TEST_F(DomPatchTests, TestPatch_ReplaceRoot)
- {
- m_deltaDataset = Value(Type::Array);
- m_deltaDataset.ArrayPushBack(Value(2));
- m_deltaDataset.ArrayPushBack(Value(4));
- m_deltaDataset.ArrayPushBack(Value(6));
- GenerateAndVerifyDelta();
- }
- TEST_F(DomPatchTests, TestPatch_DenormalizeOnApply)
- {
- m_dataset = Value(Type::Array);
- m_dataset.ArrayPushBack(Value(2));
- m_deltaDataset = m_dataset;
- m_deltaDataset.ArrayPushBack(Value(4));
- DeltaPatchGenerationParameters params;
- params.m_generateDenormalizedPaths = false;
- PatchUndoRedoInfo info = GenerateHierarchicalDeltaPatch(m_dataset, m_deltaDataset, params);
- EXPECT_TRUE(info.m_forwardPatches.ContainsNormalizedEntries());
- auto result = info.m_forwardPatches.ApplyAndDenormalize(m_dataset);
- EXPECT_TRUE(result.IsSuccess());
- EXPECT_TRUE(Utils::DeepCompareIsEqual(result.GetValue(), m_deltaDataset));
- EXPECT_FALSE(info.m_forwardPatches.ContainsNormalizedEntries());
- }
- } // namespace AZ::Dom::Tests
|