1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
/*
* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; version 2 of the
* License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#include "tut_stdafx.h"
#include "base/string_utilities.h"
#include "grtpp.h"
using namespace base;
BEGIN_TEST_DATA_CLASS(string_utilities_test)
public:
TEST_DATA_CONSTRUCTOR(string_utilities_test)
{
}
END_TEST_DATA_CLASS;
TEST_MODULE(string_utilities_test, "string utilities");
/* Testing base::quote_identifier */
TEST_FUNCTION(10)
{
std::string test = "first_test";
std::string test_result = base::quote_identifier(test, '`');
ensure_equals("Unexpected result quoting string", test_result, "`first_test`");
test = "second_test";
test_result = base::quote_identifier(test, '\"');
ensure_equals("Unexpected result quoting string", test_result, "\"second_test\"");
test = "";
test_result = base::quote_identifier(test, '\"');
ensure_equals("Unexpected result quoting string", test_result, "\"\"");
test = "Unicode \xE3\x8A\xA8"; // UTF-8 encoded: CIRCLED IDEOGRAPH RIGHT
test_result = base::quote_identifier(test, '%');
ensure_equals("Unexpected result quoting string", test_result, "%Unicode \xE3\x8A\xA8%");
}
/* Testing base::quote_identifier_if_needed */
TEST_FUNCTION(15)
{
std::string test = "first_test";
std::string test_result = base::quote_identifier_if_needed(test, '`');
ensure_equals("Unexpected result quoting string", test_result, "first_test");
test = "second_test";
test_result = base::quote_identifier_if_needed(test, '\"');
ensure_equals("Unexpected result quoting string", test_result, "second_test");
test = "Unicode\xE3\x8A\xA8"; // UTF-8 encoded: CIRCLED IDEOGRAPH RIGHT
test_result = base::quote_identifier_if_needed(test, '%');
ensure_equals("Unexpected result quoting string", test_result, "Unicode\xE3\x8A\xA8");
test = "test.case";
test_result = base::quote_identifier_if_needed(test, '$');
ensure_equals("Unexpected result quoting string", test_result, "$test.case$");
// Note: currently there is no support to check if the given string contains the quote char already.
test = "test$case";
test_result = base::quote_identifier_if_needed(test, '$');
ensure_equals("Unexpected result quoting string", test_result, "test$case");
test = ".test$case";
test_result = base::quote_identifier_if_needed(test, '$');
ensure_equals("Unexpected result quoting string", test_result, "$.test$case$");
test = "test-case";
test_result = base::quote_identifier_if_needed(test, '`');
ensure_equals("Unexpected result quoting string", test_result, "`test-case`");
// Numbers only are valid identifiers too, even tho discouraged.
test = "12345";
test_result = base::quote_identifier_if_needed(test, '`');
ensure_equals("Unexpected result quoting string", test_result, "12345");
}
/* Testing base::unquote_identifier */
TEST_FUNCTION(20)
{
std::string test = "\"first_test\"";
std::string test_result = base::unquote_identifier(test);
ensure_equals("Unexpected result unquoting string", test_result, "first_test");
test = "`second_test`";
test_result = base::unquote_identifier(test);
ensure_equals("Unexpected result unquoting string", test_result, "second_test");
}
static bool compare_string_lists(const std::vector<std::string> &slist,
const char *check[])
{
size_t i;
for (i = 0; i < slist.size(); i++)
{
if (!check[i]) return false;
if (slist[i] != check[i])
return false;
}
if (check[i] != NULL) return false;
return true;
}
TEST_FUNCTION(30)
{
std::vector<std::string> tokens;
const char* empty[] = {NULL};
const char* empty2[] = {"", "", NULL};
const char* empty3[] = {"", "", "", NULL};
const char* null_empty[] = {"NULL", "", NULL};
const char* null_null[] = {"NULL", "NULL", NULL};
const char* emptys_null[] = {"''", "NULL", NULL};
const char* ab_null[] = {"'a,b'", "NULL", NULL};
const char* ab_xxx[] = {"'a,b'", "\"x\\xx\"", "'fo''bar'", NULL};
ensure("split tokens empty1", compare_string_lists(base::split_token_list("", ','), empty));
ensure("split tokens empty2", compare_string_lists(base::split_token_list(",", ','), empty2));
ensure("split tokens empty3", compare_string_lists(base::split_token_list(",,", ','), empty3));
ensure("split tokens empty4", compare_string_lists(base::split_token_list("NULL,", ','), null_empty));
ensure("split tokens null,", compare_string_lists(base::split_token_list("NULL,", ','), null_empty));
ensure("split tokens null,null", compare_string_lists(base::split_token_list("NULL,NULL", ','), null_empty));
ensure("split tokens '',null", compare_string_lists(base::split_token_list("'',NULL", ','), emptys_null));
ensure("split tokens 'a,b',null", compare_string_lists(base::split_token_list("'a,b',NULL", ','), ab_null));
ensure("split tokens 'a,b' , \"x\\xx\",'fo''bar'", compare_string_lists(base::split_token_list("'a,b' , \"x\\xx\",'fo''bar' ", ','), ab_xxx));
}
END_TESTS
|