Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
4.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. #if JUCE_UNIT_TESTS
  16. class CachedValueTests : public UnitTest
  17. {
  18. public:
  19. CachedValueTests()
  20. : UnitTest ("CachedValues", UnitTestCategories::values)
  21. {}
  22. void runTest() override
  23. {
  24. beginTest ("default constructor");
  25. {
  26. CachedValue<String> cv;
  27. expect (cv.isUsingDefault());
  28. expect (cv.get() == String());
  29. }
  30. beginTest ("without default value");
  31. {
  32. ValueTree t ("root");
  33. t.setProperty ("testkey", "testvalue", nullptr);
  34. CachedValue<String> cv (t, "testkey", nullptr);
  35. expect (! cv.isUsingDefault());
  36. expect (cv.get() == "testvalue");
  37. cv.resetToDefault();
  38. expect (cv.isUsingDefault());
  39. expect (cv.get() == String());
  40. }
  41. beginTest ("with default value");
  42. {
  43. ValueTree t ("root");
  44. t.setProperty ("testkey", "testvalue", nullptr);
  45. CachedValue<String> cv (t, "testkey", nullptr, "defaultvalue");
  46. expect (! cv.isUsingDefault());
  47. expect (cv.get() == "testvalue");
  48. cv.resetToDefault();
  49. expect (cv.isUsingDefault());
  50. expect (cv.get() == "defaultvalue");
  51. }
  52. beginTest ("with default value (int)");
  53. {
  54. ValueTree t ("root");
  55. t.setProperty ("testkey", 23, nullptr);
  56. CachedValue<int> cv (t, "testkey", nullptr, 34);
  57. expect (! cv.isUsingDefault());
  58. expect (cv == 23);
  59. expectEquals (cv.get(), 23);
  60. cv.resetToDefault();
  61. expect (cv.isUsingDefault());
  62. expect (cv == 34);
  63. }
  64. beginTest ("with void value");
  65. {
  66. ValueTree t ("root");
  67. t.setProperty ("testkey", var(), nullptr);
  68. CachedValue<String> cv (t, "testkey", nullptr, "defaultvalue");
  69. expect (! cv.isUsingDefault());
  70. expect (cv == "");
  71. expectEquals (cv.get(), String());
  72. }
  73. beginTest ("with non-existent value");
  74. {
  75. ValueTree t ("root");
  76. CachedValue<String> cv (t, "testkey", nullptr, "defaultvalue");
  77. expect (cv.isUsingDefault());
  78. expect (cv == "defaultvalue");
  79. expect (cv.get() == "defaultvalue");
  80. }
  81. beginTest ("with value changing");
  82. {
  83. ValueTree t ("root");
  84. t.setProperty ("testkey", "oldvalue", nullptr);
  85. CachedValue<String> cv (t, "testkey", nullptr, "defaultvalue");
  86. expect (cv == "oldvalue");
  87. t.setProperty ("testkey", "newvalue", nullptr);
  88. expect (cv != "oldvalue");
  89. expect (cv == "newvalue");
  90. }
  91. beginTest ("set value");
  92. {
  93. ValueTree t ("root");
  94. t.setProperty ("testkey", 23, nullptr);
  95. CachedValue<int> cv (t, "testkey", nullptr, 45);
  96. cv = 34;
  97. expectEquals ((int) t["testkey"], 34);
  98. cv.resetToDefault();
  99. expect (cv == 45);
  100. expectEquals (cv.get(), 45);
  101. expect (t["testkey"] == var());
  102. }
  103. }
  104. };
  105. static CachedValueTests cachedValueTests;
  106. #endif
  107. } // namespace juce