diff options
| author | Lexi Winter <lexi@le-fay.org> | 2025-06-21 13:53:10 +0100 |
|---|---|---|
| committer | Lexi Winter <lexi@le-fay.org> | 2025-06-21 13:53:10 +0100 |
| commit | ca90e97a9da4457790262c53515cb5ab58a2ce4f (patch) | |
| tree | a8e184fe9d6af5731c6462ab56a390c4e08274f6 /tests | |
| parent | 243d958df14b85788232aca623b83826115a5eb9 (diff) | |
| download | nihil-ca90e97a9da4457790262c53515cb5ab58a2ce4f.tar.gz nihil-ca90e97a9da4457790262c53515cb5ab58a2ce4f.tar.bz2 | |
add command_map, skipws, next_word
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/CMakeLists.txt | 3 | ||||
| -rw-r--r-- | tests/command_map.cc | 41 | ||||
| -rw-r--r-- | tests/next_word.cc | 65 | ||||
| -rw-r--r-- | tests/skipws.cc | 45 |
4 files changed, 154 insertions, 0 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index c6788a3..13974aa 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,12 +1,15 @@ # This source code is released into the public domain. add_executable(nihil.test + command_map.cc ctype.cc fd.cc generator.cc generic_error.cc getenv.cc guard.cc + next_word.cc + skipws.cc tabulate.cc) target_link_libraries(nihil.test PRIVATE diff --git a/tests/command_map.cc b/tests/command_map.cc new file mode 100644 index 0000000..75b6f0d --- /dev/null +++ b/tests/command_map.cc @@ -0,0 +1,41 @@ +/* + * This source code is released into the public domain. + */ + +#include <vector> + +#include <catch2/catch_test_macros.hpp> + +import nihil; + +namespace { + +auto cmd_sub1_called = false; +auto cmd_sub1 = nihil::command<int>("cmd sub1", [](int, int, char **) -> int +{ + cmd_sub1_called = true; + return 0; +}); + +} // anonymous namespace + +TEST_CASE("command_map: basic", "[command_map]") +{ + auto args = std::vector<char const *>{ + "cmd", "sub1", nullptr + }; + auto argv = const_cast<char **>(args.data()); + nihil::dispatch_command(0, args.size(), argv); + REQUIRE(cmd_sub1_called == true); +} + +TEST_CASE("command_map: unknown command", "[command_map]") +{ + auto args = std::vector<char const *>{ + "cmd", "nonesuch", nullptr + }; + auto argv = const_cast<char **>(args.data()); + + REQUIRE_THROWS_AS(nihil::dispatch_command(0, args.size(), argv), + nihil::usage_error); +} diff --git a/tests/next_word.cc b/tests/next_word.cc new file mode 100644 index 0000000..4055485 --- /dev/null +++ b/tests/next_word.cc @@ -0,0 +1,65 @@ +/* + * This source code is released into the public domain. + */ + +#include <locale> +#include <string> + +#include <catch2/catch_test_macros.hpp> + +import nihil; + +TEST_CASE("next_word: basic", "[next_word]") +{ + using namespace std::literals; + auto s = "foo bar baz"sv; + + auto words = nihil::next_word(s); + REQUIRE(words.first == "foo"); + REQUIRE(words.second == " bar baz"); + + auto word = nihil::next_word(&s); + REQUIRE(word == "foo"); + REQUIRE(s == " bar baz"); +} + +TEST_CASE("next_word: multiple spaces", "[next_word]") +{ + using namespace std::literals; + auto s = "foo bar baz"sv; + + auto words = nihil::next_word(s); + REQUIRE(words.first == "foo"); + REQUIRE(words.second == " bar baz"); + + auto word = nihil::next_word(&s); + REQUIRE(word == "foo"); + REQUIRE(s == " bar baz"); +} + +TEST_CASE("next_word: leading spaces", "[next_word]") +{ + using namespace std::literals; + auto s = " \tfoo bar baz"sv; + + auto words = nihil::next_word(s); + REQUIRE(words.first == "foo"); + REQUIRE(words.second == " bar baz"); + + auto word = nihil::next_word(&s); + REQUIRE(word == "foo"); + REQUIRE(s == " bar baz"); +} + +TEST_CASE("next_word: locale", "[next_word]") +{ + using namespace std::literals; + auto s = L"\u2003foo\u2003bar\u2003baz"sv; + + auto words = nihil::next_word(s); + REQUIRE(words.first == s); + + words = nihil::next_word(s, std::locale("C.UTF-8")); + REQUIRE(words.first == L"foo"); + REQUIRE(words.second == L"\u2003bar\u2003baz"); +} diff --git a/tests/skipws.cc b/tests/skipws.cc new file mode 100644 index 0000000..2159e2e --- /dev/null +++ b/tests/skipws.cc @@ -0,0 +1,45 @@ +/* + * This source code is released into the public domain. + */ + +#include <locale> +#include <string> +using namespace std::literals; + +#include <catch2/catch_test_macros.hpp> + +import nihil; + +TEST_CASE("skipws: basic", "[skipws]") +{ + REQUIRE(nihil::skipws("foo"sv) == "foo"); + REQUIRE(nihil::skipws(" foo"sv) == "foo"); + REQUIRE(nihil::skipws("foo "sv) == "foo "); + REQUIRE(nihil::skipws("foo bar"sv) == "foo bar"); +} + +TEST_CASE("skipws: pointer", "[skipws]") +{ + auto s = "foo"sv; + nihil::skipws(&s); + REQUIRE(s == "foo"); + + s = " foo"sv; + nihil::skipws(&s); + REQUIRE(s == "foo"); + + s = "foo "sv; + nihil::skipws(&s); + REQUIRE(s == "foo "); + + s = "foo bar"sv; + nihil::skipws(&s); + REQUIRE(s == "foo bar"); +} + +TEST_CASE("skipws: locale", "[skipws]") +{ + // Assume the default locale is C. + REQUIRE(nihil::skipws(L"\u2003foo"sv) == L"\u2003foo"); + REQUIRE(nihil::skipws(L"\u2003foo"sv, std::locale("C.UTF-8")) == L"foo"); +} |
