Web Wispers 1.2.0 C++ Unit Test Coverage
Current view: top level - tests/wispers/utility - WebStringsTest.cc (source / functions) Hit Total Coverage
Test: Web Wispers 1.2.0 C++ Unit Tests Lines: 89 89 100.0 %
Date: 2010-05-27 Functions: 18 19 94.7 %
Branches: 402 806 49.9 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright 2006-2009 Savarese Software Research Corporation
       3                 :            :  *
       4                 :            :  * Licensed under the Apache License, Version 2.0 (the "License");
       5                 :            :  * you may not use this file except in compliance with the License.
       6                 :            :  * You may obtain a copy of the License at
       7                 :            :  *
       8                 :            :  *     http://www.savarese.com/software/ApacheLicense-2.0
       9                 :            :  *
      10                 :            :  * Unless required by applicable law or agreed to in writing, software
      11                 :            :  * distributed under the License is distributed on an "AS IS" BASIS,
      12                 :            :  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      13                 :            :  * See the License for the specific language governing permissions and
      14                 :            :  * limitations under the License.
      15                 :            :  */
      16                 :            : 
      17                 :            : #include <tests/TestCommon.h>
      18                 :            : #include <ssrc/wispers/utility/WebStrings.h>
      19                 :            : 
      20                 :            : using namespace NS_SSRC_WSPR_UTILITY;
      21                 :            : 
      22         [ -  + ]:         24 : struct WebStringsTest : public TestCase {
      23                 :            : 
      24                 :          1 :   void test_escape_javascript() {
      25         [ +  - ]:          2 :     const string src("   \"&'/;<>\\\n    ");
      26   [ +  -  +  - ]:          2 :     const string expected("\\x22\\x26\\x27\\x2f\\x3b\\x3c\\x3e\\x5c\\x0a");
      27   [ +  -  +  - ]:          2 :     const string && result = escape_javascript(src);
      28                 :            : 
      29 [ +  -  +  -  + :          1 :     CPPUNIT_ASSERT_EQUAL(expected, result);
          -  +  -  +  -  
             +  -  +  - ]
      30                 :          1 :   }
      31                 :            : 
      32                 :          1 :   void test_escape_url() {
      33         [ +  - ]:          2 :     const string src("$&+,/:;=?@ \"\nFOO<>#%{}|\\^~[]`'");
      34                 :            :     const string expected("%24%26%2b%2c%2f%3a%3b%3d%3f%40%20%22%0aFOO%3c%3e%23"
      35   [ +  -  +  - ]:          2 :                           "%25%7b%7d%7c%5c%5e%7e%5b%5d%60%27");
      36   [ +  -  +  - ]:          2 :     const string && result = escape_url(src);
      37                 :            : 
      38 [ +  -  +  -  + :          1 :     CPPUNIT_ASSERT_EQUAL(expected, result);
          -  +  -  +  -  
             +  -  +  - ]
      39                 :          1 :   }
      40                 :            : 
      41                 :          1 :   void test_unescape_url() {
      42         [ +  - ]:          2 :     const string expected("jklmno");
      43   [ +  -  +  - ]:          2 :     string str("%6a%6B%6c%6D%6e%6f");
      44         [ +  - ]:          1 :     unescape_url(str);
      45 [ +  -  +  -  + :          1 :     CPPUNIT_ASSERT_EQUAL(expected, str);
          -  +  -  +  -  
             +  -  +  - ]
      46                 :          1 :   }
      47                 :            : 
      48                 :          1 :   void test_escape_html() {
      49         [ +  - ]:          2 :     const string src("   &<>\n\"'    ");
      50   [ +  -  +  - ]:          2 :     const string expected("&amp;&lt;&gt;\n&quot;&#39;");
      51   [ +  -  +  - ]:          2 :     const string && result = escape_html(src);
      52                 :            : 
      53 [ +  -  +  -  + :          1 :     CPPUNIT_ASSERT_EQUAL(expected, result);
          -  +  -  +  -  
             +  -  +  - ]
      54                 :          1 :   }
      55                 :            : 
      56                 :          1 :   void test_strip_html() {
      57         [ +  - ]:          2 :     const string src("<html>\n<head><title>FOO &amp; BAR</title></head>\n<body><p>This is a test. &#169;       </p></body></html>");
      58   [ +  -  +  - ]:          2 :     const string expected("FOO &amp; BAR\nThis is a test. &#169;");
      59   [ +  -  +  - ]:          2 :     const string && result = strip_html(src);
      60                 :            : 
      61 [ +  -  +  -  + :          1 :     CPPUNIT_ASSERT_EQUAL(expected, result);
          -  +  -  +  -  
             +  -  +  - ]
      62                 :            : 
      63   [ +  -  +  - ]:          2 :     const string comment_src("foo<!-- <foo>blah</foo> -->bar");
      64   [ +  -  +  - ]:          2 :     const string comment_expected("foobar");
      65   [ +  -  +  - ]:          2 :     const string && comment_result = strip_html(comment_src);
      66                 :            : 
      67 [ +  -  +  -  + :          1 :     CPPUNIT_ASSERT_EQUAL(comment_expected, comment_result);
          -  +  -  +  -  
             +  -  +  - ]
      68                 :          1 :   }
      69                 :            : 
      70                 :          1 :   void test_strip_html_and_unescape() {
      71         [ +  - ]:          2 :     const string src("<html>\n<head><title>FOO &amp; BAR</title><!--<title>FOO &amp; BAR</title>--></head>\n<body><p>This is a test. &#169;&copy;&#xa9; &quot;      </p></body></html>  bar     ");
      72   [ +  -  +  - ]:          2 :     string expected("FOO & BAR\nThis is a test. ");
      73   [ +  -  +  - ]:          2 :     const string && result = strip_html_and_unescape(src);
      74                 :            : 
      75         [ +  - ]:          1 :     expected.append(3, 169);
      76         [ +  - ]:          1 :     expected.append(" \"        bar");
      77                 :            : 
      78 [ +  -  +  -  + :          1 :     CPPUNIT_ASSERT_EQUAL(expected, result);
          -  +  -  +  -  
             +  -  +  - ]
      79                 :          1 :   }
      80                 :            : 
      81                 :          1 :   void test_wrap_lines() {
      82                 :            :     struct test_value {
      83                 :            :       const char * const text;
      84                 :            :       const char * const expected;
      85                 :            :       const unsigned int max_length;
      86                 :            :     };
      87                 :            :     const test_value values[] = {
      88                 :            :       { "01234567890 01234 012345 0123",
      89                 :            :         "01234567890\n01234\n012345\n0123", 10 },
      90                 :            :       { "01234567890\n01234\n012345\n0123\n",
      91                 :            :         "01234567890\n01234\n012345\n0123\n", 10 },
      92                 :            :       { "0 1 2 3 4 5 6 7 8 9 0",
      93                 :            :         "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n0", 0 },
      94                 :            :       { "01234567890", "01234567890", 0 }
      95                 :          1 :     };
      96                 :          1 :     const unsigned int num_values = sizeof(values) / sizeof(test_value);
      97                 :            : 
      98         [ +  + ]:          5 :     for(unsigned int i = 0; i < num_values; ++i) {
      99         [ +  - ]:          8 :       string text(values[i].text);
     100   [ +  -  +  - ]:          8 :       string expected(values[i].expected);
     101                 :          4 :       const unsigned int max_length(values[i].max_length);
     102                 :            : 
     103 [ +  -  +  -  + :          4 :       wrap_lines(&text[0], text.size(), max_length);
                      - ]
     104                 :            : 
     105 [ +  -  +  -  + :          4 :       CPPUNIT_ASSERT_EQUAL(expected, text);
          -  +  -  +  -  
             +  -  +  - ]
     106                 :            :     }
     107                 :          1 :   }
     108                 :            : 
     109                 :          1 :   void test_html_title_and_body() {
     110         [ +  - ]:          2 :     const string src0("<html><head><title>FOO</TITLE>");
     111   [ +  -  +  - ]:          2 :     const string src1("<html><body>bar</BOdy></html>");
     112   [ +  -  +  - ]:          2 :     const string src2("<html><head><title>FOO</TITLE></head><body>bar</body></html>");
     113                 :            : 
     114                 :            :     title_body_type tb =
     115 [ +  -  +  -  + :          1 :       html_title_and_body(src0.c_str(), src0.c_str() + src0.size());
                -  +  - ]
     116                 :            : 
     117 [ +  -  +  -  + :          1 :     CPPUNIT_ASSERT(std::get<0>(tb) != 0);
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
     118 [ +  -  +  -  + :          1 :     CPPUNIT_ASSERT(std::get<1>(tb) == 3);
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
     119 [ +  -  +  -  + :          1 :     CPPUNIT_ASSERT(std::get<2>(tb) == 0);
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
     120 [ +  -  +  -  + :          1 :     CPPUNIT_ASSERT(std::get<3>(tb) == 0);
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
     121                 :            : 
     122 [ +  -  +  -  + :          1 :     CPPUNIT_ASSERT(std::memcmp("FOO", std::get<0>(tb), std::get<1>(tb)) == 0);
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     123                 :            : 
     124 [ +  -  +  -  + :          1 :     tb = html_title_and_body(src1.c_str(), src1.c_str() + src1.size());
          -  +  -  +  - ]
     125                 :            : 
     126 [ +  -  +  -  + :          1 :     CPPUNIT_ASSERT(std::get<0>(tb) == 0);
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
     127 [ +  -  +  -  + :          1 :     CPPUNIT_ASSERT(std::get<1>(tb) == 0);
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
     128 [ +  -  +  -  + :          1 :     CPPUNIT_ASSERT(std::get<2>(tb) != 0);
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
     129 [ +  -  +  -  + :          1 :     CPPUNIT_ASSERT(std::get<3>(tb) == 3);
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
     130                 :            : 
     131 [ +  -  +  -  + :          1 :     CPPUNIT_ASSERT(std::memcmp("bar", std::get<2>(tb), std::get<3>(tb)) == 0);
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     132                 :            : 
     133 [ +  -  +  -  + :          1 :     tb = html_title_and_body(src2.c_str(), src2.c_str() + src2.size());
          -  +  -  +  - ]
     134                 :            : 
     135 [ +  -  +  -  + :          1 :     CPPUNIT_ASSERT(std::get<0>(tb) != 0);
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
     136 [ +  -  +  -  + :          1 :     CPPUNIT_ASSERT(std::get<1>(tb) == 3);
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
     137 [ +  -  +  -  + :          1 :     CPPUNIT_ASSERT(std::get<2>(tb) != 0);
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
     138 [ +  -  +  -  + :          1 :     CPPUNIT_ASSERT(std::get<3>(tb) == 3);
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
     139                 :            : 
     140 [ +  -  +  -  + :          1 :     CPPUNIT_ASSERT(std::memcmp("FOO", std::get<0>(tb), std::get<1>(tb)) == 0);
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     141 [ +  -  +  -  + :          1 :     CPPUNIT_ASSERT(std::memcmp("bar", std::get<2>(tb), std::get<3>(tb)) == 0);
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     142                 :          1 :   }
     143                 :            : 
     144 [ +  -  +  -  + :          4 :   CPPUNIT_TEST_SUITE(WebStringsTest);
                -  #  # ]
     145 [ +  -  +  -  + :          1 :   CPPUNIT_TEST(test_escape_javascript);
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     146 [ +  -  +  -  + :          1 :   CPPUNIT_TEST(test_escape_url);
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     147 [ +  -  +  -  + :          1 :   CPPUNIT_TEST(test_unescape_url);
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     148 [ +  -  +  -  + :          1 :   CPPUNIT_TEST(test_escape_html);
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     149 [ +  -  +  -  + :          1 :   CPPUNIT_TEST(test_strip_html);
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     150 [ +  -  +  -  + :          1 :   CPPUNIT_TEST(test_strip_html_and_unescape);
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     151 [ +  -  +  -  + :          1 :   CPPUNIT_TEST(test_wrap_lines);
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     152 [ +  -  +  -  + :          1 :   CPPUNIT_TEST(test_html_title_and_body);
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     153 [ +  -  +  -  + :          2 :   CPPUNIT_TEST_SUITE_END();
          -  +  -  +  -  
                   +  - ]
     154                 :            : };
     155                 :            : 
     156   [ -  +  #  # ]:          1 : CPPUNIT_TEST_SUITE_REGISTRATION(WebStringsTest);
     157 [ +  -  +  -  + :          4 : WISP_TEST_MAIN()
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]