SchemaTests.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/DOM/DomUtils.h>
  9. #include <AzCore/std/any.h>
  10. #include <AzFramework/DocumentPropertyEditor/AdapterBuilder.h>
  11. #include <AzFramework/DocumentPropertyEditor/PropertyEditorNodes.h>
  12. #include <Tests/DocumentPropertyEditor/DocumentPropertyEditorFixture.h>
  13. namespace AZ::DocumentPropertyEditor::Tests
  14. {
  15. class SchemaDpeTests : public DocumentPropertyEditorTestFixture
  16. {
  17. public:
  18. static constexpr auto intAttr = AttributeDefinition<int>("intAttr");
  19. static constexpr auto doubleAttr = AttributeDefinition<double>("doubleAttr");
  20. static constexpr auto strAttr = AttributeDefinition<AZStd::string_view>("strAttr");
  21. static constexpr auto fnAttr = CallbackAttributeDefinition<int(int, int)>("fnAttr");
  22. };
  23. TEST_F(SchemaDpeTests, ValueToDom)
  24. {
  25. Dom::Value v;
  26. v = intAttr.ValueToDom(2);
  27. EXPECT_EQ(2, v.GetInt64());
  28. v = doubleAttr.ValueToDom(4.5);
  29. EXPECT_EQ(4.5, v.GetDouble());
  30. v = strAttr.ValueToDom("test string");
  31. EXPECT_EQ("test string", v.GetString());
  32. }
  33. TEST_F(SchemaDpeTests, InvokeCallback)
  34. {
  35. Dom::Value v = fnAttr.ValueToDom(
  36. [](int x, int y)
  37. {
  38. return x + y;
  39. });
  40. EXPECT_EQ(5, fnAttr.InvokeOnDomValue(v, 2, 3).GetValue());
  41. EXPECT_EQ(0, fnAttr.InvokeOnDomValue(v, 5, -5).GetValue());
  42. EXPECT_FALSE(fnAttr.InvokeOnDomValue(Dom::Value(), 1, 2).IsSuccess());
  43. EXPECT_FALSE(fnAttr
  44. .InvokeOnDomValue(
  45. Dom::Value::FromOpaqueValue(AZStd::any(
  46. AZStd::function<int(int)>([](int x)
  47. {
  48. return x;
  49. }))),
  50. 1, 2)
  51. .IsSuccess());
  52. }
  53. } // namespace AZ::DocumentPropertyEditor::Tests