diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index b8bfa88..0c69d12 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -15,6 +15,7 @@ target_compile_options(tests PRIVATE $<$: >) target_link_libraries(tests PRIVATE + UI gtest GTest::gtest_main ) diff --git a/tests/Test.hpp b/tests/Test.hpp new file mode 100644 index 0000000..01c9ace --- /dev/null +++ b/tests/Test.hpp @@ -0,0 +1,5 @@ +#include "gtest/gtest.h" +#include "UI.hpp" +#include + +#define UNUSED(x) (void)(x) \ No newline at end of file diff --git a/tests/UI_button.cpp b/tests/UI_button.cpp new file mode 100644 index 0000000..bab5804 --- /dev/null +++ b/tests/UI_button.cpp @@ -0,0 +1,26 @@ +#include "Test.hpp" + +TEST(UI_button, to_from_json) +{ + UI_button button("button_id", "button_text", Color(255, 255, 255), Color(0, 0, 0), nullptr); + json j = button.to_json(); + UI_button button2(j); + EXPECT_EQ(button.id(), button2.id()); + EXPECT_EQ(button.text(), button2.text()); + EXPECT_EQ(button.fg_color(), button2.fg_color()); + EXPECT_EQ(button.bg_color(), button2.bg_color()); + + std::cout << j.dump(4) << std::endl; +} + +void on_click(std::string id) +{ + UNUSED(id); + EXPECT_EQ(id, "button_id"); +} + +TEST(UI_button, on_click) +{ + UI_button button("button_id", "button_text", Color(255, 255, 255), Color(0, 0, 0), on_click); + button.click(); +} \ No newline at end of file diff --git a/tests/UI_item.cpp b/tests/UI_item.cpp new file mode 100644 index 0000000..4b6b2b3 --- /dev/null +++ b/tests/UI_item.cpp @@ -0,0 +1,73 @@ +#include "Test.hpp" + +TEST(UI_item, is_type) +{ + UI_item item(std::string("item")); + json j = {{"type", "item"}}; + EXPECT_TRUE(item.is_type(j)); +} + +TEST(UI_item, set_text) +{ + UI_item item(std::string("item")); + item.set_text("text"); + EXPECT_EQ(item.text(), "text"); +} + +TEST(UI_item, text_size) +{ + UI_item item(std::string("item")); + item.set_text_size(12); + EXPECT_EQ(item.text_size(), 12); +} + +TEST(UI_item, set_fg_color) +{ + UI_item item(std::string("item")); + item.set_fg_color(Color(0, 0, 0)); + EXPECT_EQ(item.fg_color(), Color(0, 0, 0)); + + item.set_fg_color(Color(255, 255, 255)); + EXPECT_EQ(item.fg_color(), Color(255, 255, 255)); +} + +TEST(UI_item, set_bg_color) +{ + UI_item item(std::string("item")); + item.set_bg_color(Color(0, 0, 0)); + EXPECT_EQ(item.bg_color(), Color(0, 0, 0)); + + item.set_bg_color(Color(255, 255, 255)); + EXPECT_EQ(item.bg_color(), Color(255, 255, 255)); +} + +TEST(UI_item, from_json) +{ + UI_item item(std::string("item")); + json j; + j["type"] = "item"; + j["id"] = "item_id"; + j["text"] = "item_text"; + j["text_size"] = 12; + j["fg_color"] = "0xFFFFFF00"; + j["bg_color"] = "0x00000000"; + + item.from_json(j); + EXPECT_EQ(item.id(), "item_id"); + EXPECT_EQ(item.text(), "item_text"); + EXPECT_EQ(item.text_size(), 12); + EXPECT_EQ(item.fg_color(), Color(255, 255, 255)); + EXPECT_EQ(item.bg_color(), Color(0, 0, 0)); +} + +TEST(UI_item, to_json) +{ + UI_item item("item_id", "item", "item_text", 12, Color(255, 255, 255), Color(0, 0, 0)); + json j = item.to_json(); + EXPECT_EQ(j.at("type"), "item"); + EXPECT_EQ(j.at("id"), "item_id"); + EXPECT_EQ(j.at("text"), "item_text"); + EXPECT_EQ(j.at("text_size"), 12); + EXPECT_EQ(j.at("fg_color"), Color(255, 255, 255).to_hex()); + EXPECT_EQ(j.at("bg_color"), Color(0, 0, 0).to_hex()); +} \ No newline at end of file diff --git a/tests/UI_label.cpp b/tests/UI_label.cpp new file mode 100644 index 0000000..5ede9b5 --- /dev/null +++ b/tests/UI_label.cpp @@ -0,0 +1,20 @@ +#include "Test.hpp" + +TEST(UI_label, to_from_json) +{ + UI_label label(std::string("label"), std::string("label_text"), Color(0, 0, 0), Color(255, 255, 255)); + json j = label.to_json(); + EXPECT_EQ(j["type"], "UIlabel"); + EXPECT_EQ(j["id"], "label"); + EXPECT_EQ(j["text"], "label_text"); + EXPECT_EQ(j["text_size"], 12); + EXPECT_EQ(j["fg_color"], "0x00000000"); + EXPECT_EQ(j["bg_color"], "0xFFFFFF00"); + + UI_label label2(j); + EXPECT_EQ(label2.id(), "label"); + EXPECT_EQ(label2.text(), "label_text"); + EXPECT_EQ(label2.text_size(), 12); + EXPECT_EQ(label2.fg_color(), Color(0, 0, 0)); + EXPECT_EQ(label2.bg_color(), Color(255, 255, 255)); +} \ No newline at end of file diff --git a/tests/UI_slider.cpp b/tests/UI_slider.cpp new file mode 100644 index 0000000..4a1632b --- /dev/null +++ b/tests/UI_slider.cpp @@ -0,0 +1,33 @@ +#include "Test.hpp" + +TEST(UI_slider, to_from_json) +{ + UI_slider slider("slider_id", "slider_text", Color(255, 255, 255), Color(0, 0, 0), 0, 100, 50, nullptr); + json j = slider.to_json(); + UI_slider slider2(j); + EXPECT_EQ(slider.id(), slider2.id()); + EXPECT_EQ(slider.text(), slider2.text()); + EXPECT_EQ(slider.fg_color(), slider2.fg_color()); + EXPECT_EQ(slider.bg_color(), slider2.bg_color()); + EXPECT_EQ(slider.value(), slider2.value()); +} + +static double val = 0; +void on_change(std::string id, double value) +{ + EXPECT_EQ(id, "slider_id"); + val = value; +} + +TEST(UI_slider, set_value) +{ + UI_slider slider("slider_id", "slider_text", Color(255, 255, 255), Color(0, 0, 0), 0, 100, 50, on_change); + slider.set_value(75); + EXPECT_EQ(val, 75); +} + +TEST(UI_slider, value) +{ + UI_slider slider("slider_id", "slider_text", Color(255, 255, 255), Color(0, 0, 0), 0, 100, 50, on_change); + EXPECT_EQ(slider.value(), 50); +}