Browse Source

Merge pull request #41363 from Calinou/test-add-gradient

Add a test suite for Gradient
Rémi Verschelde 5 years ago
parent
commit
b0ca859501
2 changed files with 153 additions and 0 deletions
  1. 152 0
      tests/test_gradient.h
  2. 1 0
      tests/test_main.cpp

+ 152 - 0
tests/test_gradient.h

@@ -0,0 +1,152 @@
+/*************************************************************************/
+/*  test_gradient.h                                                      */
+/*************************************************************************/
+/*                       This file is part of:                           */
+/*                           GODOT ENGINE                                */
+/*                      https://godotengine.org                          */
+/*************************************************************************/
+/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
+/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
+/*                                                                       */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the       */
+/* "Software"), to deal in the Software without restriction, including   */
+/* without limitation the rights to use, copy, modify, merge, publish,   */
+/* distribute, sublicense, and/or sell copies of the Software, and to    */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions:                                             */
+/*                                                                       */
+/* The above copyright notice and this permission notice shall be        */
+/* included in all copies or substantial portions of the Software.       */
+/*                                                                       */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
+/*************************************************************************/
+
+#ifndef TEST_GRADIENT_H
+#define TEST_GRADIENT_H
+
+#include "core/class_db.h"
+#include "core/color.h"
+#include "scene/resources/gradient.h"
+
+#include "thirdparty/doctest/doctest.h"
+
+namespace TestGradient {
+
+TEST_CASE("[Gradient] Default gradient") {
+	// Black-white gradient.
+	Ref<Gradient> gradient = memnew(Gradient);
+
+	CHECK_MESSAGE(
+			gradient->get_points_count() == 2,
+			"Default gradient should contain the expected number of points.");
+
+	CHECK_MESSAGE(
+			gradient->get_color_at_offset(0.0).is_equal_approx(Color(0, 0, 0)),
+			"Default gradient should return the expected interpolated value at offset 0.0.");
+	CHECK_MESSAGE(
+			gradient->get_color_at_offset(0.4).is_equal_approx(Color(0.4, 0.4, 0.4)),
+			"Default gradient should return the expected interpolated value at offset 0.4.");
+	CHECK_MESSAGE(
+			gradient->get_color_at_offset(0.8).is_equal_approx(Color(0.8, 0.8, 0.8)),
+			"Default gradient should return the expected interpolated value at offset 0.8.");
+	CHECK_MESSAGE(
+			gradient->get_color_at_offset(1.0).is_equal_approx(Color(1, 1, 1)),
+			"Default gradient should return the expected interpolated value at offset 1.0.");
+
+	// Out of bounds checks.
+	CHECK_MESSAGE(
+			gradient->get_color_at_offset(-1.0).is_equal_approx(Color(0, 0, 0)),
+			"Default gradient should return the expected interpolated value at offset -1.0.");
+	CHECK_MESSAGE(
+			gradient->get_color_at_offset(1234.0).is_equal_approx(Color(1, 1, 1)),
+			"Default gradient should return the expected interpolated value at offset 1234.0.");
+}
+
+TEST_CASE("[Gradient] Custom gradient (points specified in order)") {
+	// Red-yellow-green gradient (with overbright green).
+	Ref<Gradient> gradient = memnew(Gradient);
+	Vector<Gradient::Point> points;
+	points.push_back({ 0.0, Color(1, 0, 0) });
+	points.push_back({ 0.5, Color(1, 1, 0) });
+	points.push_back({ 1.0, Color(0, 2, 0) });
+	gradient->set_points(points);
+
+	CHECK_MESSAGE(
+			gradient->get_points_count() == 3,
+			"Custom gradient should contain the expected number of points.");
+
+	CHECK_MESSAGE(
+			gradient->get_color_at_offset(0.0).is_equal_approx(Color(1, 0, 0)),
+			"Custom gradient should return the expected interpolated value at offset 0.0.");
+	CHECK_MESSAGE(
+			gradient->get_color_at_offset(0.25).is_equal_approx(Color(1, 0.5, 0)),
+			"Custom gradient should return the expected interpolated value at offset 0.25.");
+	CHECK_MESSAGE(
+			gradient->get_color_at_offset(0.5).is_equal_approx(Color(1, 1, 0)),
+			"Custom gradient should return the expected interpolated value at offset 0.5.");
+	CHECK_MESSAGE(
+			gradient->get_color_at_offset(0.75).is_equal_approx(Color(0.5, 1.5, 0)),
+			"Custom gradient should return the expected interpolated value at offset 0.75.");
+	CHECK_MESSAGE(
+			gradient->get_color_at_offset(1.0).is_equal_approx(Color(0, 2, 0)),
+			"Custom gradient should return the expected interpolated value at offset 1.0.");
+
+	gradient->remove_point(1);
+	CHECK_MESSAGE(
+			gradient->get_points_count() == 2,
+			"Custom gradient should contain the expected number of points after removing one point.");
+	CHECK_MESSAGE(
+			gradient->get_color_at_offset(0.5).is_equal_approx(Color(0.5, 1, 0)),
+			"Custom gradient should return the expected interpolated value at offset 0.5 after removing point at index 1.");
+}
+
+TEST_CASE("[Gradient] Custom gradient (points specified out-of-order)") {
+	// HSL rainbow with points specified out of order.
+	// These should be sorted automatically when adding points.
+	Ref<Gradient> gradient = memnew(Gradient);
+	Vector<Gradient::Point> points;
+	points.push_back({ 0.2, Color(1, 0, 0) });
+	points.push_back({ 0.0, Color(1, 1, 0) });
+	points.push_back({ 0.8, Color(0, 1, 0) });
+	points.push_back({ 0.4, Color(0, 1, 1) });
+	points.push_back({ 1.0, Color(0, 0, 1) });
+	points.push_back({ 0.6, Color(1, 0, 1) });
+	gradient->set_points(points);
+
+	CHECK_MESSAGE(
+			gradient->get_points_count() == 6,
+			"Custom out-of-order gradient should contain the expected number of points.");
+
+	CHECK_MESSAGE(
+			gradient->get_color_at_offset(0.0).is_equal_approx(Color(1, 1, 0)),
+			"Custom out-of-order gradient should return the expected interpolated value at offset 0.0.");
+	CHECK_MESSAGE(
+			gradient->get_color_at_offset(0.3).is_equal_approx(Color(0.5, 0.5, 0.5)),
+			"Custom out-of-order gradient should return the expected interpolated value at offset 0.3.");
+	CHECK_MESSAGE(
+			gradient->get_color_at_offset(0.6).is_equal_approx(Color(1, 0, 1)),
+			"Custom out-of-order gradient should return the expected interpolated value at offset 0.6.");
+	CHECK_MESSAGE(
+			gradient->get_color_at_offset(1.0).is_equal_approx(Color(0, 0, 1)),
+			"Custom out-of-order gradient should return the expected interpolated value at offset 1.0.");
+
+	gradient->remove_point(0);
+	CHECK_MESSAGE(
+			gradient->get_points_count() == 5,
+			"Custom out-of-order gradient should contain the expected number of points after removing one point.");
+	// The color will be clamped to the nearest point (which is at offset 0.2).
+	CHECK_MESSAGE(
+			gradient->get_color_at_offset(0.1).is_equal_approx(Color(1, 0, 0)),
+			"Custom out-of-order gradient should return the expected interpolated value at offset 0.1 after removing point at index 0.");
+}
+
+} // namespace TestGradient
+
+#endif // TEST_GRADIENT_H

+ 1 - 0
tests/test_main.cpp

@@ -37,6 +37,7 @@
 #include "test_class_db.h"
 #include "test_class_db.h"
 #include "test_color.h"
 #include "test_color.h"
 #include "test_gdscript.h"
 #include "test_gdscript.h"
+#include "test_gradient.h"
 #include "test_gui.h"
 #include "test_gui.h"
 #include "test_math.h"
 #include "test_math.h"
 #include "test_oa_hash_map.h"
 #include "test_oa_hash_map.h"