Index: test/Util_Test.cpp |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/test/Util_Test.cpp |
@@ -0,0 +1,620 @@ |
+#include <gtest/gtest.h> |
+#include "../src/plugin/PluginUtil.h" |
+#include "../src/plugin/PluginDebug.h" |
+ |
+class Util_Test |
+ : public ::testing::Test |
+{ |
+}; |
+ |
+namespace |
+{ |
+ void equal_ci_same( std::wstring a, std::wstring b ) |
+ { |
+ bool x = ABP::util::wstring_equal_ci( a, b ); |
+ ASSERT_TRUE( x ) << L"Strings '" << a << L"' and '" << "' should be considered equal"; |
+ } |
+ |
+ void equal_ci_different( std::wstring a, std::wstring b ) |
+ { |
+ bool x = ABP::util::wstring_equal_ci( a, b ); |
+ ASSERT_FALSE( x ) << L"Strings '" << a << L"' and '" << "' should be considered not equal"; |
+ } |
+} |
+ |
+TEST( Util_Test, equal_ci_same_00 ) |
+{ |
+ equal_ci_same( L"", L"" ); |
+} |
+ |
+TEST( Util_Test, equal_ci_same_01 ) |
+{ |
+ equal_ci_same( L"same", L"same" ); |
+} |
+ |
+TEST( Util_Test, equal_ci_same_02 ) |
+{ |
+ equal_ci_same( L"Same", L"same" ); |
+} |
+ |
+TEST( Util_Test, equal_ci_same_03 ) |
+{ |
+ equal_ci_same( L"file://foo/firstrun.html", L"file://Foo/firstRun.html" ); |
+} |
+ |
+TEST( Util_Test, equal_ci_different_00 ) |
+{ |
+ equal_ci_different( L"", L" " ); |
+} |
+ |
+TEST( Util_Test, equal_ci_different_01 ) |
+{ |
+ equal_ci_different( L"Different", L"" ); |
+} |
+ |
+TEST( Util_Test, equal_ci_different_02 ) |
+{ |
+ equal_ci_different( L"Different", L"Idferrent" ); |
+} |
+ |
+TEST( Util_Test, equal_ci_different_03 ) |
+{ |
+ equal_ci_different( L"Difference", L"Diferrenc" ); |
+} |
+ |
+TEST( Util_Test, equal_ci_different_04 ) |
+{ |
+ equal_ci_different( L"Different", L"Diferrents" ); |
+} |
+ |
+TEST( Util_Test, begins_with_00 ) |
+{ |
+ ASSERT_TRUE( ABP::util::begins_with( L"", L"" ) ); |
+} |
+ |
+TEST( Util_Test, begins_with_01 ) |
+{ |
+ ASSERT_TRUE( ABP::util::begins_with( L"foo", L"" ) ); |
+} |
+ |
+TEST( Util_Test, begins_with_02 ) |
+{ |
+ ASSERT_TRUE( ABP::util::begins_with( L"foo", L"f" ) ); |
+} |
+ |
+TEST( Util_Test, begins_with_03 ) |
+{ |
+ ASSERT_FALSE( ABP::util::begins_with( L"foo", L"g" ) ); |
+} |
+ |
+//---------------------------------- |
+// trim |
+//---------------------------------- |
+namespace { |
+ void trim_test( std::wstring input, std::wstring expected ) |
+ { |
+ std::wstring copy( input ); |
+ ABP::util::trim( copy ); |
+ ASSERT_EQ( expected, copy ); |
+ } |
+} |
+ |
+TEST( Util_Test, trim_00 ) |
+{ |
+ trim_test( L"", L"" ); |
+} |
+ |
+TEST( Util_Test, trim_01 ) |
+{ |
+ trim_test( L" ", L"" ); |
+} |
+ |
+TEST( Util_Test, trim_02 ) |
+{ |
+ trim_test( L"\n", L"" ); |
+} |
+ |
+TEST( Util_Test, trim_03 ) |
+{ |
+ trim_test( L"\r", L"" ); |
+} |
+ |
+TEST( Util_Test, trim_04 ) |
+{ |
+ trim_test( L"\t", L"" ); |
+} |
+ |
+TEST( Util_Test, trim_05 ) |
+{ |
+ trim_test( L"foo", L"foo" ); |
+} |
+ |
+TEST( Util_Test, trim_06 ) |
+{ |
+ trim_test( L" foo", L"foo" ); |
+} |
+ |
+TEST( Util_Test, trim_07 ) |
+{ |
+ trim_test( L"\r\nfoo", L"foo" ); |
+} |
+ |
+TEST( Util_Test, trim_08 ) |
+{ |
+ trim_test( L"\tfoo", L"foo" ); |
+} |
+ |
+TEST( Util_Test, trim_09 ) |
+{ |
+ trim_test( L"foo ", L"foo" ); |
+} |
+ |
+TEST( Util_Test, trim_10 ) |
+{ |
+ trim_test( L"foo\r\n", L"foo" ); |
+} |
+ |
+TEST( Util_Test, trim_11 ) |
+{ |
+ trim_test( L"foo\t", L"foo" ); |
+} |
+ |
+TEST( Util_Test, trim_12 ) |
+{ |
+ trim_test( L"foo bar", L"foo bar" ); |
+} |
+ |
+TEST( Util_Test, trim_13 ) |
+{ |
+ trim_test( L"foo bar \r\n", L"foo bar" ); |
+} |
+ |
+TEST( Util_Test, trim_14 ) |
+{ |
+ trim_test( L" foo bar \r\n", L"foo bar" ); |
+} |
+ |
+//---------------------------------- |
+// trim_leading |
+//---------------------------------- |
+namespace { |
+ void trim_leading_test( std::wstring input, std::wstring expected ) |
+ { |
+ std::wstring copy( input ); |
+ ABP::util::trim_leading( copy ); |
+ ASSERT_EQ( expected, copy ); |
+ } |
+} |
+ |
+TEST( Util_Test, trim_leading_00 ) |
+{ |
+ trim_leading_test( L"", L"" ); |
+} |
+ |
+TEST( Util_Test, trim_leading_01 ) |
+{ |
+ trim_leading_test( L" ", L"" ); |
+} |
+ |
+TEST( Util_Test, trim_leading_02 ) |
+{ |
+ trim_leading_test( L"\n", L"" ); |
+} |
+ |
+TEST( Util_Test, trim_leading_03 ) |
+{ |
+ trim_leading_test( L"\r", L"" ); |
+} |
+ |
+TEST( Util_Test, trim_leading_04 ) |
+{ |
+ trim_leading_test( L"\t", L"" ); |
+} |
+ |
+TEST( Util_Test, trim_leading_05 ) |
+{ |
+ trim_leading_test( L"foo", L"foo" ); |
+} |
+ |
+TEST( Util_Test, trim_leading_06 ) |
+{ |
+ trim_leading_test( L" foo", L"foo" ); |
+} |
+ |
+TEST( Util_Test, trim_leading_07 ) |
+{ |
+ trim_leading_test( L"\r\nfoo", L"foo" ); |
+} |
+ |
+TEST( Util_Test, trim_leading_08 ) |
+{ |
+ trim_leading_test( L"\tfoo", L"foo" ); |
+} |
+ |
+TEST( Util_Test, trim_leading_09 ) |
+{ |
+ trim_leading_test( L"foo ", L"foo " ); |
+} |
+ |
+TEST( Util_Test, trim_leading_10 ) |
+{ |
+ trim_leading_test( L"foo\r\n", L"foo\r\n" ); |
+} |
+ |
+TEST( Util_Test, trim_leading_11 ) |
+{ |
+ trim_leading_test( L"foo\t", L"foo\t" ); |
+} |
+ |
+TEST( Util_Test, trim_leading_12 ) |
+{ |
+ trim_leading_test( L"foo bar", L"foo bar" ); |
+} |
+ |
+TEST( Util_Test, trim_leading_13 ) |
+{ |
+ trim_leading_test( L"foo bar \r\n", L"foo bar \r\n" ); |
+} |
+ |
+TEST( Util_Test, trim_leading_14 ) |
+{ |
+ trim_leading_test( L" foo bar \r\n", L"foo bar \r\n" ); |
+} |
+ |
+//---------------------------------- |
+// trim_trailing |
+//---------------------------------- |
+namespace { |
+ void trim_trailing_test( std::wstring input, std::wstring expected ) |
+ { |
+ std::wstring copy( input ); |
+ ABP::util::trim_trailing( copy ); |
+ ASSERT_EQ( expected, copy ); |
+ } |
+} |
+ |
+TEST( Util_Test, trim_trailing_00 ) |
+{ |
+ trim_trailing_test( L"", L"" ); |
+} |
+ |
+TEST( Util_Test, trim_trailing_01 ) |
+{ |
+ trim_trailing_test( L" ", L"" ); |
+} |
+ |
+TEST( Util_Test, trim_trailing_02 ) |
+{ |
+ trim_trailing_test( L"\n", L"" ); |
+} |
+ |
+TEST( Util_Test, trim_trailing_03 ) |
+{ |
+ trim_trailing_test( L"\r", L"" ); |
+} |
+ |
+TEST( Util_Test, trim_trailing_04 ) |
+{ |
+ trim_trailing_test( L"\t", L"" ); |
+} |
+ |
+TEST( Util_Test, trim_trailing_05 ) |
+{ |
+ trim_trailing_test( L"foo", L"foo" ); |
+} |
+ |
+TEST( Util_Test, trim_trailing_06 ) |
+{ |
+ trim_trailing_test( L" foo", L" foo" ); |
+} |
+ |
+TEST( Util_Test, trim_trailing_07 ) |
+{ |
+ trim_trailing_test( L"\r\nfoo", L"\r\nfoo" ); |
+} |
+ |
+TEST( Util_Test, trim_trailing_08 ) |
+{ |
+ trim_trailing_test( L"\tfoo", L"\tfoo" ); |
+} |
+ |
+TEST( Util_Test, trim_trailing_09 ) |
+{ |
+ trim_trailing_test( L"foo ", L"foo" ); |
+} |
+ |
+TEST( Util_Test, trim_trailing_10 ) |
+{ |
+ trim_trailing_test( L"foo\r\n", L"foo" ); |
+} |
+ |
+TEST( Util_Test, trim_trailing_11 ) |
+{ |
+ trim_trailing_test( L"foo\t", L"foo" ); |
+} |
+ |
+TEST( Util_Test, trim_trailing_12 ) |
+{ |
+ trim_trailing_test( L"foo bar", L"foo bar" ); |
+} |
+ |
+TEST( Util_Test, trim_trailing_13 ) |
+{ |
+ trim_trailing_test( L"foo bar \r\n", L"foo bar" ); |
+} |
+ |
+TEST( Util_Test, trim_trailing_14 ) |
+{ |
+ trim_trailing_test( L" foo bar \r\n", L" foo bar" ); |
+} |
+ |
+//---------------------------------- |
+// to_lower |
+//---------------------------------- |
+namespace |
+{ |
+ void to_lower_test( std::wstring input, std::wstring expected ) |
+ { |
+ ABP::util::to_lower( input ); |
+ ASSERT_EQ( input, expected ); |
+ } |
+} |
+ |
+TEST( Util_Test, to_lower_00 ) |
+{ |
+ to_lower_test( L"", L"" ); |
+} |
+ |
+TEST( Util_Test, to_lower_01 ) |
+{ |
+ to_lower_test( L"foo", L"foo" ); |
+} |
+ |
+TEST( Util_Test, to_lower_02 ) |
+{ |
+ to_lower_test( L"FOO", L"foo" ); |
+} |
+ |
+TEST( Util_Test, to_lower_03 ) |
+{ |
+ to_lower_test( L". !", L". !" ); |
+} |
+ |
+//---------------------------------- |
+// extract_token |
+//---------------------------------- |
+namespace |
+{ |
+ /* |
+ * Source must consist of nothing but whitespace starting at position 'pos' |
+ */ |
+ void extract_token_test_none( const std::wstring source, size_t pos = 0 ) |
+ { |
+ std::wstring token( ABP::util::extract_token( source, pos ) ); |
+ ASSERT_EQ( L"", token ); |
+ // Scanner must eliminate all leading whitespace, therefore it should reach the end of the string. |
+ ASSERT_EQ( pos, std::wstring::npos ); |
+ } |
+ |
+ /* |
+ * Source must consist of a single token "foo" at or after initial position, |
+ * with optional leading and trailing whitespace. |
+ * pos_end_1 is the position of the character following the terminating whitespace of the token, |
+ * or 'npos' if the source is exhausted. |
+ */ |
+ void extract_token_test_single( const std::wstring source, const size_t pos_end_1, size_t pos = 0 ) |
+ { |
+ std::wstring token( ABP::util::extract_token( source, pos ) ); |
+ ASSERT_EQ( L"foo", token ); |
+ ASSERT_EQ( pos, pos_end_1 ); |
+ token = ABP::util::extract_token( source, pos ); |
+ ASSERT_EQ( L"", token ); |
+ ASSERT_EQ( pos, std::wstring::npos ); |
+ } |
+ |
+ /* |
+ * Source must consist of a two tokens: "foo" at or after initial position, "bar" following, |
+ * with mandatory separating whitespace and optional leading and trailing whitespace. |
+ * pos_end_1 is the position of the character following the terminating whitespace of the first token. |
+ * pos_end_2 is the position of the character following the terminating whitespace of the second token, |
+ * or 'npos' if the source is exhausted. |
+ */ |
+ void extract_token_test_double( const std::wstring source, const size_t pos_end_1, const size_t pos_end_2, size_t pos = 0 ) |
+ { |
+ std::wstring token( ABP::util::extract_token( source, pos ) ); |
+ ASSERT_EQ( L"foo", token ); |
+ ASSERT_EQ( pos, pos_end_1 ); |
+ token = ABP::util::extract_token( source, pos ); |
+ ASSERT_EQ( L"bar", token ); |
+ ASSERT_EQ( pos, pos_end_2 ); |
+ token = ABP::util::extract_token( source, pos ); |
+ ASSERT_EQ( L"", token ); |
+ ASSERT_EQ( pos, std::wstring::npos ); |
+ } |
+} |
+ |
+TEST( Util_Test, extract_token_00 ) |
+{ |
+ extract_token_test_none( L"" ); |
+} |
+ |
+TEST( Util_Test, extract_token_01 ) |
+{ |
+ extract_token_test_none( L" " ); |
+} |
+ |
+TEST( Util_Test, extract_token_02 ) |
+{ |
+ extract_token_test_none( L" " ); |
+} |
+ |
+TEST( Util_Test, extract_token_03 ) |
+{ |
+ extract_token_test_none( L" \r\n" ); |
+} |
+ |
+TEST( Util_Test, extract_token_04 ) |
+{ |
+ extract_token_test_none( L" ", 1 ); |
+} |
+ |
+TEST( Util_Test, extract_token_05 ) |
+{ |
+ extract_token_test_none( L" ", 1 ); |
+} |
+ |
+TEST( Util_Test, extract_token_06 ) |
+{ |
+ extract_token_test_none( L" ", 2 ); |
+} |
+ |
+TEST( Util_Test, extract_token_07 ) |
+{ |
+ extract_token_test_none( L"foo ", 3 ); // Not what a previous call would return, but should still pass |
+} |
+ |
+TEST( Util_Test, extract_token_08 ) |
+{ |
+ extract_token_test_none( L"foo ", 4 ); |
+} |
+ |
+TEST( Util_Test, extract_token_09 ) |
+{ |
+ extract_token_test_none( L"foo ", 4 ); |
+} |
+ |
+TEST( Util_Test, extract_token_10 ) |
+{ |
+ extract_token_test_none( L"foo \r\n", 4 ); |
+} |
+ |
+TEST( Util_Test, extract_token_11 ) |
+{ |
+ extract_token_test_single( L"foo", std::wstring::npos ); |
+} |
+ |
+TEST( Util_Test, extract_token_12 ) |
+{ |
+ extract_token_test_single( L"foo ", std::wstring::npos ); |
+} |
+ |
+TEST( Util_Test, extract_token_13 ) |
+{ |
+ extract_token_test_single( L"foo ", 4 ); |
+} |
+ |
+TEST( Util_Test, extract_token_14 ) |
+{ |
+ extract_token_test_single( L" foo", std::wstring::npos ); |
+} |
+ |
+TEST( Util_Test, extract_token_15 ) |
+{ |
+ extract_token_test_single( L" foo ", std::wstring::npos ); |
+} |
+ |
+TEST( Util_Test, extract_token_16 ) |
+{ |
+ extract_token_test_single( L" foo ", 5 ); |
+} |
+ |
+TEST( Util_Test, extract_token_17 ) |
+{ |
+ extract_token_test_double( L"foo bar", 4, std::wstring::npos ); |
+} |
+ |
+TEST( Util_Test, extract_token_18 ) |
+{ |
+ extract_token_test_double( L"foo bar", 4, std::wstring::npos ); |
+} |
+ |
+TEST( Util_Test, extract_token_19 ) |
+{ |
+ extract_token_test_double( L"foo bar", 4, std::wstring::npos ); |
+} |
+ |
+TEST( Util_Test, extract_token_20 ) |
+{ |
+ extract_token_test_double( L"foo bar ", 4, std::wstring::npos ); |
+} |
+ |
+TEST( Util_Test, extract_token_21 ) |
+{ |
+ extract_token_test_double( L"foo bar ", 4, 9 ); |
+} |
+ |
+TEST( Util_Test, extract_token_22 ) |
+{ |
+ extract_token_test_double( L" foo bar", 5, std::wstring::npos ); |
+} |
+ |
+TEST( Util_Test, extract_token_23 ) |
+{ |
+ extract_token_test_double( L" foo bar", 5, std::wstring::npos ); |
+} |
+ |
+TEST( Util_Test, extract_token_24 ) |
+{ |
+ extract_token_test_double( L" foo bar ", 5, std::wstring::npos ); |
+} |
+ |
+TEST( Util_Test, extract_token_25 ) |
+{ |
+ extract_token_test_double( L" foo bar ", 5, 9 ); |
+} |
+ |
+ |
+//========================================================================================= |
+// Debug utilities |
+//========================================================================================= |
+class Debug_Util_Test |
+ : public ::testing::Test |
+{ |
+}; |
+ |
+namespace |
+{ |
+ void widen_test( std::string a, std::wstring b ) |
+ { |
+ ASSERT_EQ( ABP::debug::widen( a ), b ); |
+ } |
+ |
+ void truncate_test( const std::wstring longer, const std::wstring shorter ) |
+ { |
+ std::wstring longer_copy( longer ); |
+ ABP::debug::truncate_source( longer_copy ); |
+ ASSERT_EQ( longer_copy, shorter ); |
+ } |
+} |
+ |
+TEST( Debug_Util_Test, widen_00 ) |
+{ |
+ widen_test( "", L"" ); |
+} |
+ |
+TEST( Debug_Util_Test, widen_01 ) |
+{ |
+ widen_test( "ABCDEFG", L"ABCDEFG" ); |
+} |
+ |
+ |
+TEST( Debug_Util_Test, truncate_00 ) |
+{ |
+ truncate_test( L"", L"" ); |
+} |
+ |
+TEST( Debug_Util_Test, truncate_01 ) |
+{ |
+ std::wstring length_one_hundred( 100, L'=' ); |
+ truncate_test( length_one_hundred, length_one_hundred ); |
+} |
+ |
+TEST( Debug_Util_Test, truncate_02 ) |
+{ |
+ std::wstring length_one_hundred_and_one( 101, L'=' ); |
+ std::wstring truncated( 67, L'=' ); |
+ truncated += L"..."; |
+ truncated += std::wstring( 30, L'=' ); |
+ truncate_test( length_one_hundred_and_one, truncated ); |
+} |
+ |