diff --git a/code/test/unit/CMakeLists.txt b/code/test/unit/CMakeLists.txt index c9460dd1..8b90b8f0 100644 --- a/code/test/unit/CMakeLists.txt +++ b/code/test/unit/CMakeLists.txt @@ -103,7 +103,6 @@ target_compile_options(common INTERFACE # - we need to specify bunch of things that the original Makefile does # - there are a lot of cross-dependencies, we need to include a lot of .cpp files here add_library(esp8266 STATIC - src/unity_fixtures.c src/ArduinoMainOverride.cpp ${esp8266git_SOURCE_DIR}/tests/host/common/Arduino.cpp ${esp8266git_SOURCE_DIR}/tests/host/common/ArduinoMainUdp.cpp @@ -190,6 +189,19 @@ target_compile_options(espurna PRIVATE -Wextra ) +# extra code linked w/ the unity for every test executable +add_library(unity-local STATIC + ${CMAKE_SOURCE_DIR}/unity/unity_fixtures.c + ${CMAKE_SOURCE_DIR}/unity/unity_extra.cpp +) +target_link_libraries(unity-local PUBLIC espurna unity) +target_include_directories(unity-local PUBLIC + ${CMAKE_SOURCE_DIR}/unity +) +target_compile_options(unity-local PUBLIC + ${COMMON_FLAGS} +) + # each case is built separately, we expect these to work like a normal executable include(CTest) list(APPEND CMAKE_CTEST_ARGUMENTS "--output-on-failure") @@ -198,7 +210,7 @@ function(build_tests) foreach(ARG IN LISTS ARGN) file(GLOB ${ARG}_sources "src/${ARG}/*.h" "src/${ARG}/*.cpp") add_executable(test-${ARG} ${${ARG}_sources}) - target_link_libraries(test-${ARG} espurna unity) + target_link_libraries(test-${ARG} espurna unity unity-local) target_compile_options(test-${ARG} PRIVATE ${COMMON_FLAGS} -Wall diff --git a/code/test/unit/src/mqtt/mqtt.cpp b/code/test/unit/src/mqtt/mqtt.cpp index 796c22b1..76599a91 100644 --- a/code/test/unit/src/mqtt/mqtt.cpp +++ b/code/test/unit/src/mqtt/mqtt.cpp @@ -1,4 +1,5 @@ #include +#include #include @@ -172,8 +173,7 @@ void test_invalid_suffix() { if (len == 0) {\ TEST_ASSERT_EQUAL(0, result.length());\ } else {\ - TEST_ASSERT_EQUAL_MESSAGE(len, result.length(), "Result length is different");\ - TEST_ASSERT_EQUAL_CHAR_ARRAY((EXPECTED), result.data(), len);\ + TEST_ASSERT_EQUAL_STRING_VIEW((EXPECTED), result);\ }\ })() diff --git a/code/test/unit/src/terminal/terminal.cpp b/code/test/unit/src/terminal/terminal.cpp index 08991f75..893c3a06 100644 --- a/code/test/unit/src/terminal/terminal.cpp +++ b/code/test/unit/src/terminal/terminal.cpp @@ -6,6 +6,8 @@ #include #include +#include + namespace espurna { namespace terminal { namespace test { @@ -463,13 +465,11 @@ void test_line_buffer() { const auto first = buffer.next(); TEST_ASSERT_EQUAL(0, buffer.size()); - TEST_ASSERT_EQUAL(__builtin_strlen(input), first.value.length()); - TEST_ASSERT_EQUAL_CHAR_ARRAY( - &input[0], first.value.data(), __builtin_strlen(input)); + TEST_ASSERT_FALSE(first.overflow); + TEST_ASSERT_EQUAL_STRING_VIEW(input, first.value); StreamString stream; - stream.write( - reinterpret_cast(&input[0]), __builtin_strlen(input)); + stream.concat(&input[0], __builtin_strlen(input)); buffer.append(stream); @@ -486,8 +486,8 @@ void test_line_buffer() { TEST_ASSERT_EQUAL(0, stream.length()); const auto second = buffer.next(); - TEST_ASSERT_EQUAL_CHAR_ARRAY( - &input[0], second.value.data(), __builtin_strlen(input)); + TEST_ASSERT_FALSE(second.overflow); + TEST_ASSERT_EQUAL_STRING_VIEW(input, second.value); } // Ensure that when buffer overflows, we set 'overflow' flags @@ -547,6 +547,74 @@ void test_line_buffer_multiple() { TEST_ASSERT(Second.slice(0, Second.length() - 1) == second.value); } +void test_delimiter_view() { + const char input[] { "255,254,253,123" }; + auto delim = DelimiterView{input, ","}; + + TEST_ASSERT_EQUAL_STRING_VIEW(input, delim.get()); + + const auto r = StringView(&input[0], 3); + const auto g = StringView(r.end() + 1, 3); + const auto b = StringView(g.end() + 1, 3); + const auto a = StringView(b.end() + 1, 3); + + const auto first = delim.next(); + TEST_ASSERT_EQUAL_STRING_VIEW(r, first); + TEST_ASSERT_EQUAL(g.length() + b.length() + a.length() + 2, delim.length()); + + const auto second = delim.next(); + TEST_ASSERT_EQUAL_STRING_VIEW(g, second); + TEST_ASSERT_EQUAL(b.length() + a.length() + 1, delim.length()); + + const auto third = delim.next(); + TEST_ASSERT_EQUAL_STRING_VIEW(b, third); + TEST_ASSERT_EQUAL(a.length(), delim.length()); + + const auto fourth = delim.next(); + TEST_ASSERT_EQUAL(0, fourth.length()); + TEST_ASSERT_EQUAL_STRING_VIEW(a, delim.get()); +} + +void test_split_view() { + const char input[] { "120,75,25" }; + + auto space = SplitView{input, " "}; + TEST_ASSERT(space.next()); + + TEST_ASSERT_EQUAL(0, space.remaining().length()); + TEST_ASSERT_EQUAL_STRING_VIEW(input, space.current()); + + TEST_ASSERT_FALSE(space.next()); + TEST_ASSERT_EQUAL(0, space.current().length()); + TEST_ASSERT_EQUAL(0, space.remaining().length()); + + auto split = SplitView{input, ","}; + + const auto h = StringView(&input[0], 3); + const auto s = StringView(h.end() + 1, 2); + const auto l = StringView(s.end() + 1, 2); + + TEST_ASSERT_EQUAL(0, split.current().length()); + TEST_ASSERT_EQUAL_STRING_VIEW(input, split.remaining()); + + TEST_ASSERT(split.next()); + TEST_ASSERT_EQUAL_STRING_VIEW(h, split.current()); + TEST_ASSERT_EQUAL_STRING_VIEW(StringView(s.begin(), l.end()), split.remaining()); + + TEST_ASSERT(split.next()); + TEST_ASSERT_EQUAL_STRING_VIEW(s, split.current()); + TEST_ASSERT_EQUAL_STRING_VIEW(l, split.remaining()); + + TEST_ASSERT(split.next()); + TEST_ASSERT_EQUAL_STRING_VIEW(l, split.current()); + + TEST_ASSERT_EQUAL(0, split.remaining().length()); + + TEST_ASSERT_FALSE(split.next()); + TEST_ASSERT_EQUAL(0, split.current().length()); + TEST_ASSERT_EQUAL(0, split.remaining().length()); +} + void test_error_output() { PrintString out(64); PrintString err(64); @@ -600,8 +668,10 @@ int main(int, char**) { RUN_TEST(test_case_insensitive); RUN_TEST(test_output); RUN_TEST(test_new_line); - RUN_TEST(test_line_view); + RUN_TEST(test_delimiter_view); + RUN_TEST(test_split_view); RUN_TEST(test_line_buffer); + RUN_TEST(test_line_view); RUN_TEST(test_line_buffer_overflow); RUN_TEST(test_line_buffer_multiple); RUN_TEST(test_error_output); diff --git a/code/test/unit/src/types/types.cpp b/code/test/unit/src/types/types.cpp index 9668312c..7e173cbe 100644 --- a/code/test/unit/src/types/types.cpp +++ b/code/test/unit/src/types/types.cpp @@ -1,4 +1,6 @@ #include +#include + #include #include @@ -13,9 +15,7 @@ void test_view() { TEST_ASSERT(view.c_str() != nullptr); const char expected[] = "123456789"; - TEST_ASSERT_EQUAL(__builtin_strlen(expected), view.length()); - TEST_ASSERT_EQUAL_CHAR_ARRAY( - expected, view.begin(), view.length()); + TEST_ASSERT_EQUAL_STRING_VIEW(expected, view); } void test_view_nullptr() { @@ -41,10 +41,7 @@ void test_view_convert() { TEST_ASSERT(view.equals(copy)); TEST_ASSERT(origin.begin() != copy.begin()); - TEST_ASSERT_EQUAL(origin.length(), copy.length()); - TEST_ASSERT_EQUAL_CHAR_ARRAY( - origin.begin(), copy.begin(), copy.length()); - + TEST_ASSERT_EQUAL_STRING_VIEW(origin, copy); StringView copy_view(copy); TEST_ASSERT_EQUAL(view.length(), copy_view.length()); diff --git a/code/test/unit/unity/unity_extra.cpp b/code/test/unit/unity/unity_extra.cpp new file mode 100644 index 00000000..d2092315 --- /dev/null +++ b/code/test/unit/unity/unity_extra.cpp @@ -0,0 +1,24 @@ +#include + +#include +#include + +void test_assert_equal_string_view(espurna::StringView expected, espurna::StringView actual, unsigned int line, const char* msg) { + UNITY_TEST_ASSERT_EQUAL_INT(expected.length(), actual.length(), line, msg); + if (msg != nullptr) { + UNITY_TEST_ASSERT_EQUAL_CHAR_ARRAY( + expected.data(), actual.data(), actual.length(), line, msg); + } else { + String out; + + out += "'"; + out += expected; + + out += "' vs. '"; + out += actual; + out += "'"; + + UNITY_TEST_ASSERT_EQUAL_CHAR_ARRAY( + expected.data(), actual.data(), actual.length(), line, out.c_str()); + } +} diff --git a/code/test/unit/unity/unity_extra.hpp b/code/test/unit/unity/unity_extra.hpp new file mode 100644 index 00000000..a7a7911c --- /dev/null +++ b/code/test/unit/unity/unity_extra.hpp @@ -0,0 +1,10 @@ +#pragma once + +#include + +#define UNITY_TEST_ASSERT_EQUAL_STRING_VIEW(e, a, l, m) test_assert_equal_string_view(e, a, l, m) +#define TEST_ASSERT_EQUAL_STRING_VIEW_MESSAGE(e, a, m) test_assert_equal_string_view(e, a, __LINE__, m) +#define TEST_ASSERT_EQUAL_STRING_VIEW(e, a) test_assert_equal_string_view(e, a, __LINE__, NULL) + +void test_assert_equal_string_view(espurna::StringView expected, espurna::StringView actual, unsigned int line, const char* msg); + diff --git a/code/test/unit/src/unity_fixtures.c b/code/test/unit/unity/unity_fixtures.c similarity index 100% rename from code/test/unit/src/unity_fixtures.c rename to code/test/unit/unity/unity_fixtures.c