The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

155 lines
4.2KB

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