foreseen

A text-based gemini browser with prefetching
git clone git://git.laack.co/foreseen.git
Log | Files | Refs | README | LICENSE

commit 979e149771fecfff157eba75349882435d50060f
parent ac81bb467ca4b4e1146d0a45813276328d3ddf71
Author: Andrew Laack <andrew@laack.co>
Date:   Tue, 29 Sep 2026 11:35:08 -0500

Refactored pure function tests to use sections.

Diffstat:
Mtests/pure-test.cpp | 281+++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------
1 file changed, 190 insertions(+), 91 deletions(-)

diff --git a/tests/pure-test.cpp b/tests/pure-test.cpp @@ -2,70 +2,126 @@ #include <rapidcheck.h> #include <catch2/catch_test_macros.hpp> +#include <catch2/generators/catch_generators.hpp> #include <climits> #include <cstdlib> #include <ctime> #include <filesystem> #include <iostream> #include <optional> +#include <rapidcheck/Check.h> #include <utility> #include "../include/browser.hpp" #include "../include/utils.hpp" TEST_CASE("Test cli input handling") { - std::unordered_map<std::string, std::string> expectations; - - // Maintain arbitrary schemas - expectations["https://github.com"] = "https://github.com"; - expectations["whatever://github.com"] = "whatever://github.com"; - expectations["about://github.com"] = "about://github.com"; - expectations["gopher://github.com"] = "gopher://github.com"; - expectations["gemini://github.com"] = "gemini://github.com"; - expectations["file:///home/whatever"] = "file:///home/whatever"; - - // resolve relative file paths if they exist - std::string cwd = std::filesystem::current_path(); - expectations["tests/sites/basic.gmi"] = "file://" + cwd + "/tests/sites/basic.gmi"; - expectations["tests/sites/basic_2.gmi"] = "file://" + cwd + "/tests/sites/basic_2.gmi"; - - // resolve absolute paths correctly - expectations[cwd + "/tests/sites/basic_2.gmi"] = "file://" + cwd + "/tests/sites/basic_2.gmi"; - - // handle specified inputs without schema that aren't files - expectations["tlgs.one"] = "gemini://tlgs.one"; - expectations["laack.co"] = "gemini://laack.co"; - expectations["blog.laack.co/pygame-vs-raylib.gmi"] = "gemini://blog.laack.co/pygame-vs-raylib.gmi"; - - for(auto& expect : expectations) { - REQUIRE(handleDestinationResolution(expect.first, true).destination == expect.second); - REQUIRE(handleDestinationResolution(expect.first, true).t == STRING_DESTINATION); + + SECTION("CLI inputs maintain arbitrary schemas") { + std::unordered_map<std::string, std::string> expectations; + expectations["https://github.com"] = "https://github.com"; + expectations["whatever://github.com"] = "whatever://github.com"; + expectations["about://github.com"] = "about://github.com"; + expectations["gopher://github.com"] = "gopher://github.com"; + expectations["gemini://github.com"] = "gemini://github.com"; + expectations["file:///home/whatever"] = "file:///home/whatever"; + for(auto& expect : expectations) { + REQUIRE(handleDestinationResolution(expect.first, true).destination == expect.second); + REQUIRE(handleDestinationResolution(expect.first, true).t == STRING_DESTINATION); + } + } + + SECTION("Resolve relative file paths via the CLI if they exist") { + std::unordered_map<std::string, std::string> expectations; + std::string cwd = std::filesystem::current_path(); + expectations["tests/sites/basic.gmi"] = "file://" + cwd + "/tests/sites/basic.gmi"; + expectations["tests/sites/basic_2.gmi"] = "file://" + cwd + "/tests/sites/basic_2.gmi"; + expectations[cwd + "/tests/sites/basic_2.gmi"] = "file://" + cwd + "/tests/sites/basic_2.gmi"; + for(auto& expect : expectations) { + REQUIRE(handleDestinationResolution(expect.first, true).destination == expect.second); + REQUIRE(handleDestinationResolution(expect.first, true).t == STRING_DESTINATION); + } } + SECTION("CLI inputs without schema that aren't files") { + std::unordered_map<std::string, std::string> expectations; + expectations["tlgs.one"] = "gemini://tlgs.one"; + expectations["laack.co"] = "gemini://laack.co"; + expectations["blog.laack.co/pygame-vs-raylib.gmi"] = "gemini://blog.laack.co/pygame-vs-raylib.gmi"; + for(auto& expect : expectations) { + REQUIRE(handleDestinationResolution(expect.first, true).destination == expect.second); + REQUIRE(handleDestinationResolution(expect.first, true).t == STRING_DESTINATION); + } + } + + } -TEST_CASE("Edge cases for input handling") { +TEST_CASE("Verify urls with numeric prefix are resolved correctly") { REQUIRE(handleDestinationResolution("123movies.com",false).t == STRING_DESTINATION); REQUIRE(handleDestinationResolution("123movies.com",false).destination == "gemini://123movies.com"); } TEST_CASE("Test user input handling for destinations") { - for(int i = 0; i < 10000; ++i) { - REQUIRE(handleDestinationResolution(std::to_string(i), false).linkNumber == i); - REQUIRE(handleDestinationResolution(std::to_string(i), false).t == NUMBER_DESTINATION); + SECTION("Convert numbers to link numbers") { + for(int i = 0; i < 10000; ++i) { + REQUIRE(handleDestinationResolution(std::to_string(i), false).linkNumber == i); + REQUIRE(handleDestinationResolution(std::to_string(i), false).t == NUMBER_DESTINATION); + } + } + + SECTION("Transparently resolve gemini:// prefixed urls") { + REQUIRE(handleDestinationResolution("gemini://test.com", false).t == STRING_DESTINATION); + REQUIRE(handleDestinationResolution("gemini://test.com", false).destination == "gemini://test.com"); + } + + SECTION("Convert input with spaces into query") { + REQUIRE(handleDestinationResolution("what is the capital of scotland?", false).t == STRING_DESTINATION); + REQUIRE(handleDestinationResolution("what is the capital of scotland?", false).destination == "gemini://tlgs.one/search?" + urlEncode("what is the capital of scotland?")); } - REQUIRE(handleDestinationResolution("gemini://test.com", false).t == STRING_DESTINATION); - REQUIRE(handleDestinationResolution("what is the capital of scotland?", false).t == STRING_DESTINATION); - REQUIRE(handleDestinationResolution("laack.co", false).t == STRING_DESTINATION); - REQUIRE(handleDestinationResolution("file:///test/whatever", false).t == STRING_DESTINATION); + SECTION("Convert {domain} without scheme -> gemini://{domain}") { + REQUIRE(handleDestinationResolution("laack.co", false).t == STRING_DESTINATION); + REQUIRE(handleDestinationResolution("laack.co", false).destination == "gemini://laack.co"); + } + + SECTION("Transparently resolve file names") { + REQUIRE(handleDestinationResolution("file:///test/whatever", false).t == STRING_DESTINATION); + REQUIRE(handleDestinationResolution("file:///test/whatever", false).destination == "file:///test/whatever"); + } + + SECTION("Convert empty string to no-destination") { + REQUIRE(handleDestinationResolution("", false).t == NO_DESTINATION); + } + + SECTION("Handle arbitrary string inputs for CLI input") { + rc::check("Handle arbitrary string inputs for CLI input", + [](const std::string& st) { + auto dest = handleDestinationResolution(st, true); + RC_ASSERT(dest.t != NUMBER_DESTINATION); + switch (dest.t) { + case NO_DESTINATION: + break; + case STRING_DESTINATION: + break; + case NUMBER_DESTINATION: + break; + } + }); + } + + SECTION("Handle arbitrary string inputs for non-CLI input") { + rc::check("Handle arbitrary string inputs for non-CLI input", + [](const std::string& st) { + auto dest = handleDestinationResolution(st, false); + + if(dest.t == STRING_DESTINATION) { + RC_ASSERT(dest.destination != ""); + } + }); + } - REQUIRE(handleDestinationResolution("gemini://test.com", false).destination == "gemini://test.com"); - REQUIRE(handleDestinationResolution("what is the capital of scotland?", false).destination == "gemini://tlgs.one/search?" + urlEncode("what is the capital of scotland?")); - REQUIRE(handleDestinationResolution("laack.co", false).destination == "gemini://laack.co"); - REQUIRE(handleDestinationResolution("file:///test/whatever", false).destination == "file:///test/whatever"); - REQUIRE(handleDestinationResolution("", false).t == NO_DESTINATION); } TEST_CASE("Test trivial line breaking") { @@ -128,32 +184,50 @@ TEST_CASE("Test lots of spaces") { } TEST_CASE("Test normal line classification") { - auto res1 = lineToLine("", std::nullopt, -1, false); - REQUIRE(res1->type() == PLAINTEXT); + SECTION("Plaintext classification") { + auto res1 = lineToLine("", std::nullopt, -1, false); + REQUIRE(res1->type() == PLAINTEXT); + } - auto res2 = lineToLine("", std::nullopt, -1, true); - REQUIRE(res2->type() == PREFORMATTED); + SECTION("Preformatted classification") { + auto res2 = lineToLine("", std::nullopt, -1, true); + REQUIRE(res2->type() == PREFORMATTED); + } - auto res3 = lineToLine("=> gemini://laack.co", std::nullopt, 1, false); - REQUIRE(res3->type() == LINK); + SECTION("Link classification") { + auto res3 = lineToLine("=> gemini://laack.co", std::nullopt, 1, false); + REQUIRE(res3->type() == LINK); + } - auto res4 = lineToLine("```", std::nullopt, 1, false); - REQUIRE(res4->type() == FORMAT_SWITCH); + SECTION("Format switch classification") { + auto res4 = lineToLine("```", std::nullopt, 1, false); + REQUIRE(res4->type() == FORMAT_SWITCH); + } - auto res5 = lineToLine("# H1 Heading", std::nullopt, 1, false); - REQUIRE(res5->type() == H1); + SECTION("H1 classification") { + auto res5 = lineToLine("# H1 Heading", std::nullopt, 1, false); + REQUIRE(res5->type() == H1); + } - auto res6 = lineToLine("## H2 Heading", std::nullopt, 1, false); - REQUIRE(res6->type() == H2); + SECTION("H2 classification") { + auto res6 = lineToLine("## H2 Heading", std::nullopt, 1, false); + REQUIRE(res6->type() == H2); + } - auto res7 = lineToLine("### H3 Heading", std::nullopt, 1, false); - REQUIRE(res7->type() == H3); + SECTION("H3 classification") { + auto res7 = lineToLine("### H3 Heading", std::nullopt, 1, false); + REQUIRE(res7->type() == H3); + } - auto res8 = lineToLine("> test quote", std::nullopt, 1, false); - REQUIRE(res8->type() == QUOTE); + SECTION("Quote classification") { + auto res8 = lineToLine("> test quote", std::nullopt, 1, false); + REQUIRE(res8->type() == QUOTE); + } - auto res9 = lineToLine("* test li", std::nullopt, 1, false); - REQUIRE(res9->type() == LIST_ITEM); + SECTION("List item classification") { + auto res9 = lineToLine("* test li", std::nullopt, 1, false); + REQUIRE(res9->type() == LIST_ITEM); + } } @@ -215,37 +289,72 @@ TEST_CASE("Test line parsing handles whitespace correctly") { TEST_CASE("Proper whitespace compliance") { - auto res1 = lineToLine("", std::nullopt, -1, false); + SECTION("Line types become plaintext when missing proper spacing") { + auto res1 = lineToLine("", std::nullopt, -1, false); + REQUIRE(res1->type() == PLAINTEXT); + + auto res3 = lineToLine("=>gemini://laack.co", std::nullopt, 1, false); + REQUIRE(res3->type() == PLAINTEXT); + + auto res5 = lineToLine("#H1 Heading", std::nullopt, 1, false); + REQUIRE(res5->type() == PLAINTEXT); + + auto res6 = lineToLine("##H2 Heading", std::nullopt, 1, false); + REQUIRE(res6->type() == PLAINTEXT); + + auto res7 = lineToLine("###H3 Heading", std::nullopt, 1, false); + REQUIRE(res7->type() == PLAINTEXT); + + auto res8 = lineToLine(">test quote", std::nullopt, 1, false); + REQUIRE(res8->type() == PLAINTEXT); + + auto res9 = lineToLine("*test li", std::nullopt, 1, false); + REQUIRE(res9->type() == PLAINTEXT); + } + + SECTION("Non-whitespace based line types don't require whitespace") { + auto res2 = lineToLine("", std::nullopt, -1, true); + REQUIRE(res2->type() == PREFORMATTED); + + auto res4 = lineToLine("```", std::nullopt, 1, false); + REQUIRE(res4->type() == FORMAT_SWITCH); + } + + +} + + +TEST_CASE("Line parser handles lines that are only format characters with whitespaces") { + auto res1 = lineToLine(" ", std::nullopt, -1, false); REQUIRE(res1->type() == PLAINTEXT); - auto res2 = lineToLine("", std::nullopt, -1, true); + auto res2 = lineToLine(" ", std::nullopt, -1, true); REQUIRE(res2->type() == PREFORMATTED); - auto res3 = lineToLine("=>gemini://laack.co", std::nullopt, 1, false); - REQUIRE(res3->type() == PLAINTEXT); + auto res3 = lineToLine("=> ", std::nullopt, 1, false); + REQUIRE(res3->type() == LINK); - auto res4 = lineToLine("```", std::nullopt, 1, false); + auto res4 = lineToLine("``` ", std::nullopt, 1, false); REQUIRE(res4->type() == FORMAT_SWITCH); - auto res5 = lineToLine("#H1 Heading", std::nullopt, 1, false); - REQUIRE(res5->type() == PLAINTEXT); - - auto res6 = lineToLine("##H2 Heading", std::nullopt, 1, false); - REQUIRE(res6->type() == PLAINTEXT); - - auto res7 = lineToLine("###H3 Heading", std::nullopt, 1, false); - REQUIRE(res7->type() == PLAINTEXT); + auto res5 = lineToLine("# ", std::nullopt, 1, false); + REQUIRE(res5->type() == H1); - auto res8 = lineToLine(">test quote", std::nullopt, 1, false); - REQUIRE(res8->type() == PLAINTEXT); + auto res6 = lineToLine("## ", std::nullopt, 1, false); + REQUIRE(res6->type() == H2); - auto res9 = lineToLine("*test li", std::nullopt, 1, false); - REQUIRE(res9->type() == PLAINTEXT); + auto res7 = lineToLine("### ", std::nullopt, 1, false); + REQUIRE(res7->type() == H3); + auto res8 = lineToLine("> ", std::nullopt, 1, false); + REQUIRE(res8->type() == QUOTE); + auto res9 = lineToLine("* ", std::nullopt, 1, false); + REQUIRE(res9->type() == LIST_ITEM); } -TEST_CASE("Crash test") { + +TEST_CASE("Line parser handles lines that are only format characters without whitespaces") { auto res1 = lineToLine("", std::nullopt, -1, false); REQUIRE(res1->type() == PLAINTEXT); @@ -276,20 +385,10 @@ TEST_CASE("Crash test") { static std::string linecharset = "#>=abc## de_-><0193248$#)(&@)(*&$#^*&#^%^&*&fghijklmnopqrstuvwxyz #````ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; -TEST_CASE("Crash test rng") { - for(int x = 0; x < 10000; ++x) { - std::string strRnd = ""; - for(int i = 0; i < 1000; ++i) { - strRnd += linecharset[rand() % linecharset.length()]; - auto* ln = lineToLine(strRnd, std::nullopt, 1, false); - } - } - for(int x = 0; x < 10000; ++x) { - std::string strRnd = ""; - for(int i = 0; i < 1000; ++i) { - strRnd += linecharset[rand() % linecharset.length()]; - auto* ln = lineToLine(strRnd, std::nullopt, 1, true); - } - } - +TEST_CASE("Crash test random line strings") { + rc::check("Crash test random line strings", + [](const std::string& st) { + auto* ln = lineToLine(st, std::nullopt, 1, false); + delete ln; + }); }