diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-06-14 17:52:51 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-06-14 17:52:51 +0200 |
commit | a12751695e1c543a71aa896f87dc09c49cb0db5e (patch) | |
tree | 7c2bcb811701513c47471509164529a7b5b95faa | |
parent | 3557f277f68b556e40dc056baa0b1ab6296c6d8d (diff) | |
download | serenity-a12751695e1c543a71aa896f87dc09c49cb0db5e.zip |
AK/Tests: Add a simple EXPECT_EQ macro and use it for the String test.
-rw-r--r-- | AK/Tests/Makefile | 2 | ||||
-rw-r--r-- | AK/Tests/TestHelpers.h | 49 | ||||
-rw-r--r-- | AK/Tests/TestString.cpp | 20 |
3 files changed, 60 insertions, 11 deletions
diff --git a/AK/Tests/Makefile b/AK/Tests/Makefile index fdec39410e..5cde4d3fc7 100644 --- a/AK/Tests/Makefile +++ b/AK/Tests/Makefile @@ -2,7 +2,7 @@ all: TestString CXXFLAGS = -std=c++17 -Wall -Wextra -TestString: TestString.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp +TestString: TestString.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp TestHelpers.h $(CXX) $(CXXFLAGS) -I../ -I../../ -o $@ TestString.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp clean: diff --git a/AK/Tests/TestHelpers.h b/AK/Tests/TestHelpers.h index 2162e17079..311eb6ad1f 100644 --- a/AK/Tests/TestHelpers.h +++ b/AK/Tests/TestHelpers.h @@ -1,6 +1,7 @@ #pragma once #include <stdio.h> +#include <AK/AKString.h> #define LOG_FAIL(cond) \ fprintf(stderr, "\033[31;1mFAIL\033[0m: " #cond "\n") @@ -8,6 +9,24 @@ #define LOG_PASS(cond) \ fprintf(stderr, "\033[32;1mPASS\033[0m: " #cond "\n") +#define LOG_FAIL_EQ(cond, expected_value, actual_value) \ + fprintf(stderr, "\033[31;1mFAIL\033[0m: " #cond " should be " #expected_value ", got "); \ + stringify_for_test(actual_value); \ + fprintf(stderr, "\n") + +#define LOG_PASS_EQ(cond, expected_value) \ + fprintf(stderr, "\033[32;1mPASS\033[0m: " #cond " should be " #expected_value " and it is\n") + +#define EXPECT_EQ(expr, expected_value) \ + do { \ + auto result = (expr); \ + if (!(result == expected_value)) { \ + LOG_FAIL_EQ(expr, expected_value, result); \ + } else { \ + LOG_PASS_EQ(expr, expected_value); \ + } \ + } while(0) + #define EXPECT(cond) \ do { \ if (!(cond)) { \ @@ -17,3 +36,33 @@ } \ } while(0) +inline void stringify_for_test(int value) +{ + fprintf(stderr, "%d", value); +} + +inline void stringify_for_test(unsigned value) +{ + fprintf(stderr, "%u", value); +} + +inline void stringify_for_test(const char* value) +{ + fprintf(stderr, "%s", value); +} + +inline void stringify_for_test(char value) +{ + fprintf(stderr, "%c", value); +} + +inline void stringify_for_test(const AK::String& string) +{ + stringify_for_test(string.characters()); +} + +inline void stringify_for_test(const AK::StringImpl& string) +{ + stringify_for_test(string.characters()); +} + diff --git a/AK/Tests/TestString.cpp b/AK/Tests/TestString.cpp index 7d417abfdf..851971a77e 100644 --- a/AK/Tests/TestString.cpp +++ b/AK/Tests/TestString.cpp @@ -16,8 +16,8 @@ int main() String test_string = "ABCDEF"; EXPECT(!test_string.is_empty()); EXPECT(!test_string.is_null()); - EXPECT(test_string.length() == 6); - EXPECT(test_string.length() == (int)strlen(test_string.characters())); + EXPECT_EQ(test_string.length(), 6); + EXPECT_EQ(test_string.length(), (int)strlen(test_string.characters())); EXPECT(test_string.characters()); EXPECT(!strcmp(test_string.characters(), "ABCDEF")); @@ -25,8 +25,8 @@ int main() EXPECT(test_string != "ABCDE"); EXPECT(test_string != "ABCDEFG"); - EXPECT(test_string[0] == 'A'); - EXPECT(test_string[1] == 'B'); + EXPECT_EQ(test_string[0], 'A'); + EXPECT_EQ(test_string[1], 'B'); EXPECT(test_string.starts_with("AB")); EXPECT(test_string.starts_with("ABCDEF")); @@ -37,16 +37,16 @@ int main() EXPECT(!test_string.ends_with("ABC")); auto test_string_copy = test_string; - EXPECT(test_string == test_string_copy); - EXPECT(test_string.characters() == test_string_copy.characters()); + EXPECT_EQ(test_string, test_string_copy); + EXPECT_EQ(test_string.characters(), test_string_copy.characters()); auto test_string_move = move(test_string_copy); - EXPECT(test_string == test_string_move); + EXPECT_EQ(test_string, test_string_move); EXPECT(test_string_copy.is_null()); - EXPECT(String::repeated('x', 0) == ""); - EXPECT(String::repeated('x', 1) == "x"); - EXPECT(String::repeated('x', 2) == "xx"); + EXPECT_EQ(String::repeated('x', 0), ""); + EXPECT_EQ(String::repeated('x', 1), "x"); + EXPECT_EQ(String::repeated('x', 2), "xx"); bool ok; EXPECT(String("123").to_int(ok) == 123 && ok); |