Skip to content

Commit 89f8dc8

Browse files
Convert the gtest tests to catch2 and relocate.
1 parent 0488215 commit 89f8dc8

File tree

5 files changed

+180
-134
lines changed

5 files changed

+180
-134
lines changed

components/pango_core/CMakeLists.txt

+7-1
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,10 @@ IF(MSVC)
5656
DESTINATION ${CMAKE_INSTALL_PREFIX}/${INSTALL_INCLUDE_DIR}/pangolin
5757
)
5858
target_include_directories(${COMPONENT} PUBLIC "${CMAKE_CURRENT_BINARY_DIR}/include")
59-
ENDIF()
59+
ENDIF()
60+
61+
find_package(Catch2 2 QUIET)
62+
if(Catch2_FOUND)
63+
add_executable(test_uris ${CMAKE_CURRENT_LIST_DIR}/tests/tests_uri.cpp)
64+
target_link_libraries(test_uris PRIVATE Catch2::Catch2 ${COMPONENT})
65+
endif()
+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
}

components/pango_video/CMakeLists.txt

+7
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,10 @@ target_include_directories(${COMPONENT}
219219
)
220220

221221
target_link_libraries(${COMPONENT} PUBLIC pango_core pango_image pango_packetstream)
222+
223+
224+
find_package(Catch2 2 QUIET)
225+
if(Catch2_FOUND)
226+
add_executable(test_video_uris ${CMAKE_CURRENT_LIST_DIR}/tests/tests_video_uri.cpp)
227+
target_link_libraries(test_video_uris PRIVATE Catch2::Catch2 ${COMPONENT})
228+
endif()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
#include <pangolin/video/video.h>
9+
#include <pangolin/video/video_exception.h>
10+
#include <pangolin/video/iostream_operators.h>
11+
12+
template<typename T>
13+
inline void ExpectExceptionWithMessageFromAction(
14+
std::function<void()> action,
15+
const std::string& exceptionMessageBegin
16+
) {
17+
try
18+
{
19+
action();
20+
21+
FAIL("The action succeeded when it should have failed.");
22+
}
23+
catch(const T& ex)
24+
{
25+
std::string exceptionMessage = std::string(ex.what());
26+
std::string exceptionMessageRelevantPortion
27+
= exceptionMessage.substr(0, exceptionMessageBegin.size());
28+
REQUIRE(exceptionMessageRelevantPortion.compare(exceptionMessageBegin) == 0);
29+
}
30+
catch(...)
31+
{
32+
FAIL("Another kind of exception was thrown to the one expected");
33+
}
34+
}
35+
36+
TEST_CASE("Empty Uri Test")
37+
{
38+
REQUIRE_THROWS_AS(pangolin::OpenVideo(""), pangolin::VideoException);
39+
}
40+
41+
TEST_CASE("Uri Beginning With Dot Interpreted As Image")
42+
{
43+
auto testAction = [](){pangolin::OpenVideo(".");};
44+
ExpectExceptionWithMessageFromAction<std::runtime_error>(testAction,
45+
"Unsupported image file type");
46+
}
47+
48+
TEST_CASE("Uri Dimension Legal Separator Character")
49+
{
50+
51+
using char_t = std::string::value_type;
52+
using char_nl = std::numeric_limits<char_t>;
53+
for(char_t c = char_nl::min(); c < char_nl::max(); ++c)
54+
{
55+
if(c == '\0' || c == '=' || c == ']' || c == ',' || (c >= '0' && c <= '9'))
56+
{
57+
continue;
58+
}
59+
60+
const int dimWidth = 123, dimHeight = 456;
61+
const std::string encodedDim = std::to_string(dimWidth) + c
62+
+ std::to_string(dimHeight);
63+
const std::string fullUri = "abc:[size=" + encodedDim + ",other=value]";
64+
const pangolin::Uri pangoUri = pangolin::ParseUri(fullUri);
65+
66+
REQUIRE(pangoUri.params.size() == 2);
67+
REQUIRE( pangoUri.params[0].first == std::string("size"));
68+
REQUIRE( pangoUri.params[1].first == std::string("other"));
69+
70+
const pangolin::ImageDim dim
71+
= pangoUri.Get<pangolin::ImageDim>("size", pangolin::ImageDim(0,0));
72+
REQUIRE( dim.x == dimWidth);
73+
REQUIRE( dim.y == dimHeight);
74+
75+
const std::string otherValue = pangoUri.Get<std::string>("other", "");
76+
REQUIRE( otherValue == std::string("value"));
77+
78+
}
79+
}

test/UriTests.cpp

-133
This file was deleted.

0 commit comments

Comments
 (0)