|
| 1 | +#define CATCH_CONFIG_MAIN |
| 2 | +#include <catch2/catch.hpp> |
| 3 | + |
| 4 | +#include <string> |
| 5 | +#include <limits> |
| 6 | +#include <functional> |
| 7 | +#include <pangolin/factory/factory_registry.h> |
| 8 | + |
| 9 | +template<typename T> |
| 10 | +inline void ExpectExceptionWithMessageFromAction( |
| 11 | + std::function<void()> action, |
| 12 | + const std::string& exceptionMessageBegin |
| 13 | +) { |
| 14 | + try |
| 15 | + { |
| 16 | + action(); |
| 17 | + |
| 18 | + FAIL("The action succeeded when it should have failed."); |
| 19 | + } |
| 20 | + catch(const T& ex) |
| 21 | + { |
| 22 | + std::string exceptionMessage = std::string(ex.what()); |
| 23 | + std::string exceptionMessageRelevantPortion |
| 24 | + = exceptionMessage.substr(0, exceptionMessageBegin.size()); |
| 25 | + REQUIRE(exceptionMessageRelevantPortion.compare(exceptionMessageBegin) == 0); |
| 26 | + } |
| 27 | + catch(...) |
| 28 | + { |
| 29 | + FAIL("Another kind of exception was thrown to the one expected"); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +TEST_CASE("Missing Closing Bracket Causes Exception") |
| 34 | +{ |
| 35 | + const std::string rawUri = "abc:[..."; |
| 36 | + auto testAction = [&](){pangolin::ParseUri(rawUri);}; |
| 37 | + ExpectExceptionWithMessageFromAction<std::runtime_error>(testAction, |
| 38 | + "Unable to parse URI: '" + rawUri + "'"); |
| 39 | +} |
| 40 | + |
| 41 | +TEST_CASE("Uri Equals Sign Character In Value Truncates The Rest") |
| 42 | +{ |
| 43 | + const std::string fullUri = "abc:[key=value=notfound,key2=value2]"; |
| 44 | + const pangolin::Uri pangoUri = pangolin::ParseUri(fullUri); |
| 45 | + |
| 46 | + // parsing option list should have precendence over parsing individual option |
| 47 | + REQUIRE(pangoUri.params.size() == 2); |
| 48 | + |
| 49 | + REQUIRE( pangoUri.params[0].first == std::string("key")); |
| 50 | + REQUIRE( pangoUri.params[1].first == std::string("key2")); |
| 51 | + |
| 52 | + const std::string truncated = pangoUri.Get<std::string>("key", ""); |
| 53 | + REQUIRE( truncated == std::string("value")); |
| 54 | + |
| 55 | + const std::string otherValue = pangoUri.Get<std::string>("key2", ""); |
| 56 | + REQUIRE( otherValue == std::string("value2")); |
| 57 | +} |
| 58 | + |
| 59 | +TEST_CASE("Uri Multiple Occurrences Of Option Overrides Previous") |
| 60 | +{ |
| 61 | + const std::string fullUri = "abc:[key=value,key=value2]"; |
| 62 | + const pangolin::Uri pangoUri = pangolin::ParseUri(fullUri); |
| 63 | + |
| 64 | + REQUIRE(pangoUri.params.size() == 2); |
| 65 | + REQUIRE( pangoUri.params[0].first == std::string("key")); |
| 66 | + REQUIRE( pangoUri.params[1].first == std::string("key")); |
| 67 | + |
| 68 | + const std::string truncated = pangoUri.Get<std::string>("key", ""); |
| 69 | + REQUIRE( truncated == std::string("value2")); |
| 70 | +} |
| 71 | + |
| 72 | + |
| 73 | +TEST_CASE("Uri Url Contains Everything After Separator") |
| 74 | +{ |
| 75 | + const std::string expectedUrl = "abc:123[],="; |
| 76 | + const std::string fullUri = "abc:[key=value,key=value2]//" + expectedUrl; |
| 77 | + const pangolin::Uri pangoUri = pangolin::ParseUri(fullUri); |
| 78 | + REQUIRE( pangoUri.url == expectedUrl); |
| 79 | +} |
| 80 | + |
| 81 | +TEST_CASE("Uri Invalid Characters After Closed Bracket Are Ignored") |
| 82 | +{ |
| 83 | + const std::string expectedUrl = "abc:123[],="; |
| 84 | + const std::string fullUri = "abc:[key=value,key=value2]xyz//" + expectedUrl; |
| 85 | + const pangolin::Uri pangoUri = pangolin::ParseUri(fullUri); |
| 86 | + REQUIRE( pangoUri.url == expectedUrl); |
| 87 | +} |
0 commit comments