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.

157 lines
4.3KB

  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. namespace juce
  20. {
  21. #if JUCE_UNIT_TESTS
  22. class CachedValueTests : public UnitTest
  23. {
  24. public:
  25. CachedValueTests()
  26. : UnitTest ("CachedValues", UnitTestCategories::values)
  27. {}
  28. void runTest() override
  29. {
  30. beginTest ("default constructor");
  31. {
  32. CachedValue<String> cv;
  33. expect (cv.isUsingDefault());
  34. expect (cv.get() == String());
  35. }
  36. beginTest ("without default value");
  37. {
  38. ValueTree t ("root");
  39. t.setProperty ("testkey", "testvalue", nullptr);
  40. CachedValue<String> cv (t, "testkey", nullptr);
  41. expect (! cv.isUsingDefault());
  42. expect (cv.get() == "testvalue");
  43. cv.resetToDefault();
  44. expect (cv.isUsingDefault());
  45. expect (cv.get() == String());
  46. }
  47. beginTest ("with default value");
  48. {
  49. ValueTree t ("root");
  50. t.setProperty ("testkey", "testvalue", nullptr);
  51. CachedValue<String> cv (t, "testkey", nullptr, "defaultvalue");
  52. expect (! cv.isUsingDefault());
  53. expect (cv.get() == "testvalue");
  54. cv.resetToDefault();
  55. expect (cv.isUsingDefault());
  56. expect (cv.get() == "defaultvalue");
  57. }
  58. beginTest ("with default value (int)");
  59. {
  60. ValueTree t ("root");
  61. t.setProperty ("testkey", 23, nullptr);
  62. CachedValue<int> cv (t, "testkey", nullptr, 34);
  63. expect (! cv.isUsingDefault());
  64. expect (cv == 23);
  65. expectEquals (cv.get(), 23);
  66. cv.resetToDefault();
  67. expect (cv.isUsingDefault());
  68. expect (cv == 34);
  69. }
  70. beginTest ("with void value");
  71. {
  72. ValueTree t ("root");
  73. t.setProperty ("testkey", var(), nullptr);
  74. CachedValue<String> cv (t, "testkey", nullptr, "defaultvalue");
  75. expect (! cv.isUsingDefault());
  76. expect (cv == "");
  77. expectEquals (cv.get(), String());
  78. }
  79. beginTest ("with non-existent value");
  80. {
  81. ValueTree t ("root");
  82. CachedValue<String> cv (t, "testkey", nullptr, "defaultvalue");
  83. expect (cv.isUsingDefault());
  84. expect (cv == "defaultvalue");
  85. expect (cv.get() == "defaultvalue");
  86. }
  87. beginTest ("with value changing");
  88. {
  89. ValueTree t ("root");
  90. t.setProperty ("testkey", "oldvalue", nullptr);
  91. CachedValue<String> cv (t, "testkey", nullptr, "defaultvalue");
  92. expect (cv == "oldvalue");
  93. t.setProperty ("testkey", "newvalue", nullptr);
  94. expect (cv != "oldvalue");
  95. expect (cv == "newvalue");
  96. }
  97. beginTest ("set value");
  98. {
  99. ValueTree t ("root");
  100. t.setProperty ("testkey", 23, nullptr);
  101. CachedValue<int> cv (t, "testkey", nullptr, 45);
  102. cv = 34;
  103. expectEquals ((int) t["testkey"], 34);
  104. cv.resetToDefault();
  105. expect (cv == 45);
  106. expectEquals (cv.get(), 45);
  107. expect (t["testkey"] == var());
  108. }
  109. }
  110. };
  111. static CachedValueTests cachedValueTests;
  112. #endif
  113. } // namespace juce