StringTokenizerTest.cpp
Go to the documentation of this file.00001 #include <gtest/gtest.h>
00002 #include <utils/common/StringTokenizer.h>
00003 #include <utils/common/UtilExceptions.h>
00004
00005 using namespace std;
00006
00007
00008
00009
00010
00011
00012 TEST(StringTokenizer, test_split_with_whitechar) {
00013 StringTokenizer *strTok = new StringTokenizer("Hello World", StringTokenizer::WHITECHARS);
00014 EXPECT_TRUE(strTok->hasNext()) << "There must be more tokens available.";
00015 EXPECT_EQ("Hello",strTok->next());
00016 EXPECT_EQ("World",strTok->next());
00017 EXPECT_FALSE(strTok->hasNext()) << "No tokens should be available.";
00018 }
00019
00020
00021 TEST(StringTokenizer, test_split_with_newline) {
00022 StringTokenizer *strTok = new StringTokenizer("Hello\nWorld", StringTokenizer::NEWLINE);
00023 EXPECT_TRUE(strTok->hasNext()) << "There must be more tokens available.";
00024 EXPECT_EQ("Hello",strTok->next());
00025 EXPECT_EQ("World",strTok->next());
00026 EXPECT_FALSE(strTok->hasNext()) << "No tokens should be available.";
00027 }
00028
00029
00030 TEST(StringTokenizer, test_split_with_x) {
00031 StringTokenizer *strTok = new StringTokenizer("HelloxxWorld", "x");
00032 EXPECT_TRUE(strTok->hasNext()) << "There must be more tokens available.";
00033 EXPECT_EQ("Hello",strTok->next());
00034 EXPECT_EQ("",strTok->next());
00035 EXPECT_EQ("World",strTok->next());
00036 EXPECT_FALSE(strTok->hasNext()) << "No tokens should be available.";
00037 }
00038
00039
00040 TEST(StringTokenizer, test_split_any_char) {
00041 StringTokenizer *strTok = new StringTokenizer("HelloxWyorld", "xy", true);
00042 EXPECT_TRUE(strTok->hasNext()) << "There must be more tokens available.";
00043 EXPECT_EQ("Hello",strTok->next());
00044 EXPECT_EQ("W",strTok->next());
00045 EXPECT_EQ("orld",strTok->next());
00046 EXPECT_FALSE(strTok->hasNext()) << "No tokens should be available.";
00047 }
00048
00049
00050 TEST(StringTokenizer, test_method_reinit) {
00051 StringTokenizer *strTok = new StringTokenizer("Hello");
00052 strTok->next();
00053 EXPECT_FALSE(strTok->hasNext()) << "No tokens should be available.";
00054 strTok->reinit();
00055 EXPECT_TRUE(strTok->hasNext()) << "There must be more tokens available.";
00056 }
00057
00058
00059 TEST(StringTokenizer, test_method_size) {
00060 StringTokenizer *strTok = new StringTokenizer("Hello little World");
00061 EXPECT_EQ(3,strTok->size()) << "The number of the token is not right.";
00062 StringTokenizer *strTok2 = new StringTokenizer("");
00063 EXPECT_EQ(0,strTok2->size()) << "The number of the token is not right.";
00064 }
00065
00066
00067 TEST(StringTokenizer, test_method_front) {
00068 StringTokenizer *strTok = new StringTokenizer("Hello World");
00069 EXPECT_EQ("Hello",strTok->front()) << "The first token is not right.";
00070 strTok->next();
00071 EXPECT_EQ("Hello",strTok->front()) << "The first token is not right.";
00072 }
00073
00074
00075 TEST(StringTokenizer, test_method_get) {
00076 StringTokenizer *strTok = new StringTokenizer("Hello World");
00077 EXPECT_EQ("Hello",strTok->get(0)) << "The first token is not right.";
00078 EXPECT_EQ("World",strTok->get(1)) << "The second token is not right.";
00079 ASSERT_THROW(strTok->get(2),OutOfBoundsException) << "Expect an OutOfBoundsException exception.";
00080 }
00081
00082
00083 TEST(StringTokenizer, test_method_get_with_empty_data) {
00084 StringTokenizer *strTok = new StringTokenizer();
00085 ASSERT_THROW(strTok->get(0),OutOfBoundsException) << "Expect an OutOfBoundsException exception.";
00086 }
00087
00088
00089 TEST(StringTokenizer, test_method_getVector) {
00090 StringTokenizer *strTok = new StringTokenizer("Hello World");
00091 vector<string> strVek = strTok->getVector();
00092 EXPECT_EQ("World",strVek.back());
00093 EXPECT_EQ("Hello",strVek.front());
00094 }
00095
00096
00097
00098
00099