Skip to content

Commit ec3ce0c

Browse files
committed
improve color tests
1 parent 86e57ce commit ec3ce0c

File tree

3 files changed

+32
-25
lines changed

3 files changed

+32
-25
lines changed

src/helper/color.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ helper::expected<HSVColor, std::string> HSVColor::from_string(const std::string&
1919
return helper::unexpected<std::string>{ result.error() };
2020
}
2121

22-
helper::expected<std::tuple<HSVColor, color::SerializeMode, bool>, std::string> HSVColor::from_string_with_info(
23-
const std::string& value
24-
) {
22+
helper::expected<HSVColor::InfoType, std::string> HSVColor::from_string_with_info(const std::string& value) {
2523

2624
const auto result = detail::get_hsv_color_from_string(value);
2725

@@ -66,9 +64,7 @@ helper::expected<Color, std::string> Color::from_string(const std::string& value
6664
return helper::unexpected<std::string>{ result.error() };
6765
}
6866

69-
helper::expected<std::tuple<Color, color::SerializeMode, bool>, std::string> Color::from_string_with_info(
70-
const std::string& value
71-
) {
67+
helper::expected<Color::InfoType, std::string> Color::from_string_with_info(const std::string& value) {
7268

7369
const auto result = detail::get_color_from_string(value);
7470

src/helper/color.hpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ struct HSVColor {
5656

5757
[[nodiscard]] static helper::expected<HSVColor, std::string> from_string(const std::string& value);
5858

59-
[[nodiscard]] static helper::expected<std::tuple<HSVColor, color::SerializeMode, bool>, std::string>
60-
from_string_with_info(const std::string& value);
59+
using InfoType = std::tuple<HSVColor, color::SerializeMode, bool>;
60+
61+
[[nodiscard]] static helper::expected<InfoType, std::string> from_string_with_info(const std::string& value);
6162

6263
[[nodiscard]] Color to_rgb_color() const;
6364

@@ -129,8 +130,9 @@ struct Color {
129130

130131
[[nodiscard]] static helper::expected<Color, std::string> from_string(const std::string& value);
131132

132-
[[nodiscard]] static helper::expected<std::tuple<Color, color::SerializeMode, bool>, std::string>
133-
from_string_with_info(const std::string& value);
133+
using InfoType = std::tuple<Color, color::SerializeMode, bool>;
134+
135+
[[nodiscard]] static helper::expected<InfoType, std::string> from_string_with_info(const std::string& value);
134136

135137

136138
[[nodiscard]] HSVColor to_hsv_color() const;

tests/core/color.cpp

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,24 +74,30 @@ TEST(Color, ConstructorProperties) {
7474

7575
TEST(Color, FromStringValid) {
7676

77-
const std::vector<std::tuple<std::string, Color>> valid_strings{
78-
{ "#FFAA33", Color{ 0xFF, 0xAA, 0x33 }},
79-
{ "#FF00FF00", Color{ 0xFF, 0x00, 0xFF, 0x00 }},
80-
{ "rgb(0,0,0)", Color{ 0, 0, 0 }},
81-
{ "rgba(0,0,0,0)", Color{ 0, 0, 0, 0 }},
82-
{ "hsv(0,0,0)", HSVColor{ 0, 0, 0 }},
83-
{ "hsva(340,0,0.5,0)", HSVColor{ 340, 0, 0.5, 0 }},
84-
{ "#ffaa33", Color{ 0xff, 0xaa, 0x33 }},
85-
{"hsv(0, 0.00_000_000_1, 0)", HSVColor{ 0, 0.000000001, 0 }},
86-
{ "hsva(0, 0, 0, 0xFF)", HSVColor{ 0, 0, 0, 0xFF }},
87-
{ "rgba(0, 0xFF, 0, 255)", Color{ 0, 0xFF, 0, 255 }},
88-
{ "rgba(0, 0xFF, 0, 1_0_0)", Color{ 0, 0xFF, 0, 100 }},
77+
const std::vector<std::tuple<std::string, Color::InfoType>> valid_values{
78+
{ "#FFAA33", { Color{ 0xFF, 0xAA, 0x33 }, color::SerializeMode::Hex, false }},
79+
{ "#FF00FF00", { Color{ 0xFF, 0x00, 0xFF, 0x00 }, color::SerializeMode::Hex, true }},
80+
{ "rgb(0,0,0)", { Color{ 0, 0, 0 }, color::SerializeMode::RGB, false }},
81+
{ "rgba(0,0,0,0)", { Color{ 0, 0, 0, 0 }, color::SerializeMode::RGB, true }},
82+
{ "hsv(0,0,0)", { HSVColor{ 0, 0, 0 }, color::SerializeMode::HSV, false }},
83+
{ "hsva(340,0,0.5,0)", { HSVColor{ 340, 0, 0.5, 0 }, color::SerializeMode::HSV, true }},
84+
{ "#ffaa33", { Color{ 0xff, 0xaa, 0x33 }, color::SerializeMode::Hex, false }},
85+
{"hsv(0, 0.00_000_000_1, 0)", { HSVColor{ 0, 0.000000001, 0 }, color::SerializeMode::HSV, false }},
86+
{ "hsva(0, 0, 0, 0xFF)", { HSVColor{ 0, 0, 0, 0xFF }, color::SerializeMode::HSV, true }},
87+
{ "rgba(0, 0xFF, 0, 255)", { Color{ 0, 0xFF, 0, 255 }, color::SerializeMode::RGB, true }},
88+
{ "rgba(0, 0xFF, 0, 1_0_0)", { Color{ 0, 0xFF, 0, 100 }, color::SerializeMode::RGB, true }},
8989
};
9090

91-
for (const auto& [valid_string, expected_color] : valid_strings) {
92-
const auto result = Color::from_string(valid_string);
91+
for (const auto& [valid_string, expected_result] : valid_values) {
92+
const auto result = Color::from_string_with_info(valid_string);
9393
ASSERT_THAT(result, ExpectedHasValue()) << "Input was: " << valid_string;
94-
ASSERT_EQ(result.value(), expected_color) << "Input was: " << valid_string;
94+
ASSERT_EQ(result.value(), expected_result) << "Input was: " << valid_string;
95+
96+
const auto color_string = std::get<0>(result.value()).to_string(color::SerializeMode::Hex);
97+
98+
const auto converted_color = Color::from_string_with_info(color_string);
99+
ASSERT_THAT(converted_color, ExpectedHasValue()) << "Input was: " << color_string;
100+
ASSERT_EQ(result.value(), expected_result) << "Input was: " << color_string;
95101
}
96102
}
97103

@@ -103,6 +109,9 @@ TEST(Color, FromStringInvalid) {
103109
{ "#Z", "Unrecognized HEX literal"},
104110
{ "#ZZFF", "Unrecognized HEX literal"},
105111
{ "u", "Unrecognized color literal"},
112+
{ "#IIFFFFFF", "the input must be a valid hex character"},
113+
{ "#FFIIFFFF", "the input must be a valid hex character"},
114+
{ "#FFFFIIFF", "the input must be a valid hex character"},
106115
{ "#FFFFFFII", "the input must be a valid hex character"},
107116
{ "#FFFF4T", "the input must be a valid hex character"},
108117
{ "#0000001", "Unrecognized HEX literal"},

0 commit comments

Comments
 (0)