diff options
author | crupest <crupest@outlook.com> | 2024-06-24 00:06:25 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2024-08-18 16:50:20 +0800 |
commit | 1b30150ab79ff1338f209a8ddb54b3dc60cfb599 (patch) | |
tree | 97e183587b293ecf768476da0edf3fdcf86e4543 /test | |
parent | b756bf519cda0684ec46d0d9404cbc59741ec0cb (diff) | |
download | cru-1b30150ab79ff1338f209a8ddb54b3dc60cfb599.tar.gz cru-1b30150ab79ff1338f209a8ddb54b3dc60cfb599.tar.bz2 cru-1b30150ab79ff1338f209a8ddb54b3dc60cfb599.zip |
fix(SubProcess): fix pipe fs close, add tests.
NEED TEST: BufferStream, AutoReadStream, SubProcess.
Diffstat (limited to 'test')
-rw-r--r-- | test/common/CMakeLists.txt | 13 | ||||
-rw-r--r-- | test/common/StringToNumberConverterTest.cpp | 70 | ||||
-rw-r--r-- | test/common/SubProcessHelper/CruEcho.cpp | 9 | ||||
-rw-r--r-- | test/common/SubProcessHelper/CruTee.cpp | 10 | ||||
-rw-r--r-- | test/common/SubProcessTest.cpp | 21 |
5 files changed, 85 insertions, 38 deletions
diff --git a/test/common/CMakeLists.txt b/test/common/CMakeLists.txt index d2351b38..61222a68 100644 --- a/test/common/CMakeLists.txt +++ b/test/common/CMakeLists.txt @@ -10,6 +10,19 @@ add_executable(CruBaseTest ) target_link_libraries(CruBaseTest PRIVATE CruBase CruTestBase) +add_executable(CruTestHelperEcho + SubProcessHelper/CruEcho.cpp +) + +add_executable(CruTestHelperTee + SubProcessHelper/CruTee.cpp +) + +target_compile_definitions(CruBaseTest PRIVATE + CRU_TEST_HELPER_ECHO_LOCATION="$<TARGET_FILE:CruTestHelperEcho>" + CRU_TEST_HELPER_TEE_LOCATION="$<TARGET_FILE:CruTestHelperTee>" +) + if (UNIX AND NOT EMSCRIPTEN) target_sources(CruBaseTest PRIVATE platform/unix/UnixFileStreamTest.cpp diff --git a/test/common/StringToNumberConverterTest.cpp b/test/common/StringToNumberConverterTest.cpp index dc37bac0..d4bb36f4 100644 --- a/test/common/StringToNumberConverterTest.cpp +++ b/test/common/StringToNumberConverterTest.cpp @@ -5,79 +5,79 @@ TEST_CASE("StringToIntegerConverterImpl Base0", "[string]") { using namespace cru; - StringToIntegerConverterImpl converter(0, 0); + StringToIntegerConverter converter({}, 0); Index processed_characters_count; REQUIRE(converter.Parse("12345678", &processed_characters_count) == - StringToIntegerConverterImplResult(false, 12345678)); + StringToIntegerResult(false, 12345678)); REQUIRE(processed_characters_count == 8); REQUIRE(converter.Parse("0", &processed_characters_count) == - StringToIntegerConverterImplResult(false, 0)); + StringToIntegerResult(false, 0)); REQUIRE(processed_characters_count == 1); REQUIRE(converter.Parse("012", &processed_characters_count) == - StringToIntegerConverterImplResult(false, 012)); + StringToIntegerResult(false, 012)); REQUIRE(processed_characters_count == 3); REQUIRE(converter.Parse("0x12", &processed_characters_count) == - StringToIntegerConverterImplResult(false, 0x12)); + StringToIntegerResult(false, 0x12)); REQUIRE(processed_characters_count == 4); REQUIRE(converter.Parse("0X12", &processed_characters_count) == - StringToIntegerConverterImplResult(false, 0x12)); + StringToIntegerResult(false, 0x12)); REQUIRE(processed_characters_count == 4); REQUIRE(converter.Parse("0b101", &processed_characters_count) == - StringToIntegerConverterImplResult(false, 0b101)); + StringToIntegerResult(false, 0b101)); REQUIRE(processed_characters_count == 5); REQUIRE(converter.Parse("0B101", &processed_characters_count) == - StringToIntegerConverterImplResult(false, 0b101)); + StringToIntegerResult(false, 0b101)); REQUIRE(processed_characters_count == 5); REQUIRE(converter.Parse("-123", &processed_characters_count) == - StringToIntegerConverterImplResult(true, 123)); + StringToIntegerResult(true, 123)); REQUIRE(processed_characters_count == 4); REQUIRE(converter.Parse("-0x10", &processed_characters_count) == - StringToIntegerConverterImplResult(true, 0x10)); + StringToIntegerResult(true, 0x10)); REQUIRE(processed_characters_count == 5); } TEST_CASE("StringToIntegerConverterImpl Base0ForError", "[string]") { using namespace cru; - StringToIntegerConverterImpl converter(0, 0); + StringToIntegerConverter converter({}, 0); Index processed_characters_count; REQUIRE(converter.Parse("a", &processed_characters_count) == - StringToIntegerConverterImplResult(false, 0)); + StringToIntegerResult(false, 0)); REQUIRE(processed_characters_count == 0); REQUIRE(converter.Parse("0a", &processed_characters_count) == - StringToIntegerConverterImplResult(false, 0)); + StringToIntegerResult(false, 0)); REQUIRE(processed_characters_count == 0); REQUIRE(converter.Parse("0x", &processed_characters_count) == - StringToIntegerConverterImplResult(false, 0)); + StringToIntegerResult(false, 0)); REQUIRE(processed_characters_count == 0); REQUIRE(converter.Parse("0xx", &processed_characters_count) == - StringToIntegerConverterImplResult(false, 0)); + StringToIntegerResult(false, 0)); REQUIRE(processed_characters_count == 0); REQUIRE(converter.Parse(" 0", &processed_characters_count) == - StringToIntegerConverterImplResult(false, 0)); + StringToIntegerResult(false, 0)); REQUIRE(processed_characters_count == 0); REQUIRE(converter.Parse("0 ", &processed_characters_count) == - StringToIntegerConverterImplResult(false, 0)); + StringToIntegerResult(false, 0)); REQUIRE(processed_characters_count == 0); } TEST_CASE("StringToIntegerConverterImpl ThrowOnErrorFlag", "[string]") { using namespace cru; - StringToIntegerConverterImpl converter(StringToNumberFlags::kThrowOnError, 0); + StringToIntegerConverter converter(StringToNumberFlags::kThrowOnError, 0); Index processed_characters_count; REQUIRE_THROWS_AS(converter.Parse("?", &processed_characters_count), Exception); @@ -85,56 +85,56 @@ TEST_CASE("StringToIntegerConverterImpl ThrowOnErrorFlag", "[string]") { TEST_CASE("StringToIntegerConverterImpl AllowLeadingZeroFlag", "[string]") { using namespace cru; - StringToIntegerConverterImpl converter( + StringToIntegerConverter converter( StringToNumberFlags::kAllowLeadingSpaces, 0); Index processed_characters_count; REQUIRE(converter.Parse(" 123", &processed_characters_count) == - StringToIntegerConverterImplResult(false, 123)); + StringToIntegerResult(false, 123)); REQUIRE(processed_characters_count == 6); } TEST_CASE("StringToIntegerConverterImpl AllowTrailingZeroFlag", "[string]") { using namespace cru; - StringToIntegerConverterImpl converter( + StringToIntegerConverter converter( StringToNumberFlags::kAllowTrailingSpaces, 0); Index processed_characters_count; REQUIRE(converter.Parse("123 ", &processed_characters_count) == - StringToIntegerConverterImplResult(false, 123)); + StringToIntegerResult(false, 123)); REQUIRE(processed_characters_count == 6); } TEST_CASE("StringToIntegerConverterImpl AllowTrailingJunk", "[string]") { using namespace cru; - StringToIntegerConverterImpl converter( - StringToNumberFlags::kAllowLeadingZeroForInteger, 0); + StringToIntegerConverter converter( + StringToNumberFlags::kAllowTrailingJunk, 0); Index processed_characters_count; REQUIRE(converter.Parse("123 12", &processed_characters_count) == - StringToIntegerConverterImplResult(false, 123)); + StringToIntegerResult(false, 123)); REQUIRE(processed_characters_count == 3); } TEST_CASE("StringToIntegerConverterImpl AllowLeadingZeroForInteger", "[string]") { using namespace cru; - StringToIntegerConverterImpl converter( + StringToIntegerConverter converter( StringToNumberFlags::kAllowLeadingZeroForInteger, 0); Index processed_characters_count; REQUIRE(converter.Parse("0x0012", &processed_characters_count) == - StringToIntegerConverterImplResult(false, 0x12)); + StringToIntegerResult(false, 0x12)); REQUIRE(processed_characters_count == 6); REQUIRE(converter.Parse("000011", &processed_characters_count) == - StringToIntegerConverterImplResult(false, 000011)); + StringToIntegerResult(false, 000011)); REQUIRE(processed_characters_count == 6); REQUIRE(converter.Parse("0b0011", &processed_characters_count) == - StringToIntegerConverterImplResult(false, 0b0011)); + StringToIntegerResult(false, 0b0011)); REQUIRE(processed_characters_count == 6); } TEST_CASE("StringToIntegerConverterImpl CompositeFlags", "[string]") { using namespace cru; - StringToIntegerConverterImpl converter( + StringToIntegerConverter converter( StringToNumberFlags::kAllowLeadingSpaces | StringToNumberFlags::kAllowTrailingJunk | StringToNumberFlags::kAllowLeadingZeroForInteger, @@ -142,25 +142,25 @@ TEST_CASE("StringToIntegerConverterImpl CompositeFlags", "[string]") { Index processed_characters_count; REQUIRE(converter.Parse(" 0x00123!!!", &processed_characters_count) == - StringToIntegerConverterImplResult(false, 0x00123)); + StringToIntegerResult(false, 0x00123)); REQUIRE(processed_characters_count == 10); } TEST_CASE("StringToIntegerConverterImpl OtherBase", "[string]") { using namespace cru; - StringToIntegerConverterImpl converter(0, 7); + StringToIntegerConverter converter({}, 7); Index processed_characters_count; REQUIRE(converter.Parse("12", &processed_characters_count) == - StringToIntegerConverterImplResult(false, 9)); + StringToIntegerResult(false, 9)); REQUIRE(processed_characters_count == 2); REQUIRE(converter.Parse("-12", &processed_characters_count) == - StringToIntegerConverterImplResult(true, 9)); + StringToIntegerResult(true, 9)); REQUIRE(processed_characters_count == 3); converter.base = 11; REQUIRE(converter.Parse("1a", &processed_characters_count) == - StringToIntegerConverterImplResult(false, 21)); + StringToIntegerResult(false, 21)); REQUIRE(processed_characters_count == 2); } diff --git a/test/common/SubProcessHelper/CruEcho.cpp b/test/common/SubProcessHelper/CruEcho.cpp new file mode 100644 index 00000000..5f23c027 --- /dev/null +++ b/test/common/SubProcessHelper/CruEcho.cpp @@ -0,0 +1,9 @@ +#include <iostream> + +int main(int argc, char* argv[]) { + for (int i = 1; i < argc - 1; ++i) { + std::cout << argv[i] << " "; + } + std::cout << argv[argc - 1]; + return 0; +} diff --git a/test/common/SubProcessHelper/CruTee.cpp b/test/common/SubProcessHelper/CruTee.cpp new file mode 100644 index 00000000..4470d2a8 --- /dev/null +++ b/test/common/SubProcessHelper/CruTee.cpp @@ -0,0 +1,10 @@ +#include <iostream> +#include <string> + +int main() { + std::string s; + while (std::cin >> s) { + std::cout << s; + } + return 0; +} diff --git a/test/common/SubProcessTest.cpp b/test/common/SubProcessTest.cpp index e42ee943..03f9d221 100644 --- a/test/common/SubProcessTest.cpp +++ b/test/common/SubProcessTest.cpp @@ -1,15 +1,30 @@ +#include "cru/common/String.h" #include "cru/common/SubProcess.h" #include <catch2/catch_test_macros.hpp> +using cru::String; using cru::SubProcess; TEST_CASE("SubProcess", "[subprocess]") { - SECTION("should work.") { - SubProcess process = SubProcess::Create(u"echo", {u"abc"}); + SECTION("echo should work.") { + SubProcess process = SubProcess::Create( + String::FromUtf8(CRU_TEST_HELPER_ECHO_LOCATION), {u"abc"}); process.Wait(); REQUIRE(process.GetExitResult().IsSuccess()); auto output = process.GetStdoutStream()->ReadToEndAsUtf8String(); - REQUIRE(output == u"abc\n"); + REQUIRE(output == u"abc"); + } + + SECTION("tee should work.") { + constexpr auto str = "abc"; + SubProcess process = + SubProcess::Create(String::FromUtf8(CRU_TEST_HELPER_TEE_LOCATION)); + process.GetStdinStream()->Write(str, 3); + process.GetStdinStream()->Close(); + process.Wait(); + REQUIRE(process.GetExitResult().IsSuccess()); + auto output = process.GetStdoutStream()->ReadToEndAsUtf8String(); + REQUIRE(output == u"abc"); } } |