foreseen

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

commit f523778763f2a93208018cdd378fa7c32911c4a1
parent 11f5baa1493d561a505d9018268f219323b0532f
Author: Andrew Laack <andrew@laack.co>
Date:   Sun, 27 Sep 2026 15:31:04 -0500

Handle conversion edge case

Diffstat:
Msrc/main.cpp | 3++-
Msrc/utils.cpp | 14++++++++++----
Mtests/pure-test.cpp | 20+++++++++++++++++++-
3 files changed, 31 insertions(+), 6 deletions(-)

diff --git a/src/main.cpp b/src/main.cpp @@ -347,7 +347,7 @@ int main(int argc, char** argv) { // TODO: Refactor to switch statement if(cliDestination.t != NO_DESTINATION) { - assert(cliDestination.linkNumber = 0); + assert(cliDestination.t == STRING_DESTINATION); b.goToSite(cliDestination.destination); } @@ -383,6 +383,7 @@ int main(int argc, char** argv) { } else if(input == 'o') { std::string locationToGo = openPageHandler(ds); Destination destination = handleDestinationResolution(locationToGo, false); + switch(destination.t) { case NUMBER_DESTINATION: b.followLinkNumber(destination.linkNumber); diff --git a/src/utils.cpp b/src/utils.cpp @@ -8,6 +8,7 @@ #include "../include/errors.hpp" #include "../include/plaintext.hpp" #include "../include/preformatted.hpp" +#include <algorithm> #include <cassert> #include <cstddef> #include <cstdlib> @@ -308,10 +309,15 @@ Destination handleDestinationResolution(std::string destination, bool isCli) { } } else { try { - int dest = std::stoi(destination); - ret.linkNumber = dest; - ret.t = NUMBER_DESTINATION; - return ret; + std::size_t pos = 0; + int dest = std::stoi(destination,&pos); + if(pos == destination.size()) { + ret.linkNumber = dest; + ret.t = NUMBER_DESTINATION; + return ret; + } else { + throw std::invalid_argument("Unable to convert fully"); + } } catch (...) { if(destination.find(":") == std::string::npos) { if(destination.find('.') != std::string::npos && destination.find(' ') == std::string::npos) { diff --git a/tests/pure-test.cpp b/tests/pure-test.cpp @@ -36,17 +36,35 @@ TEST_CASE("Test cli input handling") { 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") { + 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); } - + + 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); + + REQUIRE(handleDestinationResolution("gemini://test.com", false).destination == "gemini://test.com"); + // TODO: Should this return a string destination or should it just return something like LOOKUP? + 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") {