LCOV - code coverage report
Current view: top level - tests - test_as2js_position.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 94 94 100.0 %
Date: 2014-11-22 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* test_as2js_position.cpp -- written by Alexis WILKE for Made to Order Software Corp. (c) 2005-2014 */
       2             : 
       3             : /*
       4             : 
       5             : Copyright (c) 2005-2014 Made to Order Software Corp.
       6             : 
       7             : http://snapwebsites.org/project/as2js
       8             : 
       9             : Permission is hereby granted, free of charge, to any
      10             : person obtaining a copy of this software and
      11             : associated documentation files (the "Software"), to
      12             : deal in the Software without restriction, including
      13             : without limitation the rights to use, copy, modify,
      14             : merge, publish, distribute, sublicense, and/or sell
      15             : copies of the Software, and to permit persons to whom
      16             : the Software is furnished to do so, subject to the
      17             : following conditions:
      18             : 
      19             : The above copyright notice and this permission notice
      20             : shall be included in all copies or substantial
      21             : portions of the Software.
      22             : 
      23             : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
      24             : ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
      25             : LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
      26             : FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
      27             : EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
      28             : LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
      29             : WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
      30             : ARISING FROM, OUT OF OR IN CONNECTION WITH THE
      31             : SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
      32             : SOFTWARE.
      33             : 
      34             : */
      35             : 
      36             : #include    "test_as2js_position.h"
      37             : #include    "test_as2js_main.h"
      38             : 
      39             : #include    "as2js/position.h"
      40             : #include    "as2js/exceptions.h"
      41             : 
      42             : #include    <cstring>
      43             : #include    <algorithm>
      44             : #include    <sstream>
      45             : 
      46             : #include    <cppunit/config/SourcePrefix.h>
      47           4 : CPPUNIT_TEST_SUITE_REGISTRATION( As2JsPositionUnitTests );
      48             : 
      49             : 
      50             : 
      51             : 
      52             : 
      53           1 : void As2JsPositionUnitTests::test_names()
      54             : {
      55           1 :     as2js::Position pos;
      56             : 
      57             :     // check the filename
      58             :     {
      59             :         // by default it is empty
      60           1 :         CPPUNIT_ASSERT(pos.get_filename() == "");
      61             : 
      62             :         // some long filename
      63           1 :         pos.set_filename("the/filename/can really/be anything.test");
      64           1 :         CPPUNIT_ASSERT(pos.get_filename() == "the/filename/can really/be anything.test");
      65             : 
      66             :         // reset back to empty
      67           1 :         pos.set_filename("");
      68           1 :         CPPUNIT_ASSERT(pos.get_filename() == "");
      69             : 
      70             :         // reset back to empty
      71           1 :         pos.set_filename("file.js");
      72           1 :         CPPUNIT_ASSERT(pos.get_filename() == "file.js");
      73             :     }
      74             : 
      75             :     // check the function name
      76             :     {
      77             :         // by default it is empty
      78           1 :         CPPUNIT_ASSERT(pos.get_function() == "");
      79             : 
      80             :         // some long filename
      81           1 :         pos.set_function("as2js::super::function::name");
      82           1 :         CPPUNIT_ASSERT(pos.get_function() == "as2js::super::function::name");
      83             : 
      84             :         // reset back to empty
      85           1 :         pos.set_function("");
      86           1 :         CPPUNIT_ASSERT(pos.get_function() == "");
      87             : 
      88             :         // reset back to empty
      89           1 :         pos.set_function("add");
      90           1 :         CPPUNIT_ASSERT(pos.get_function() == "add");
      91           1 :     }
      92           1 : }
      93             : 
      94             : 
      95           1 : void As2JsPositionUnitTests::test_counters()
      96             : {
      97           1 :     as2js::Position pos;
      98             : 
      99             :     // frist verify the default
     100             :     {
     101           1 :         CPPUNIT_ASSERT(pos.get_page() == 1);
     102           1 :         CPPUNIT_ASSERT(pos.get_page_line() == 1);
     103           1 :         CPPUNIT_ASSERT(pos.get_paragraph() == 1);
     104           1 :         CPPUNIT_ASSERT(pos.get_line() == 1);
     105             :     }
     106             : 
     107           1 :     int total_line(1);
     108         100 :     for(int page(1); page < 100; ++page)
     109             :     {
     110          99 :         int paragraphs(rand() % 10 + 10);
     111          99 :         int page_line(1);
     112          99 :         int paragraph(1);
     113       99000 :         for(int line(1); line < 1000; ++line)
     114             :         {
     115       98901 :             CPPUNIT_ASSERT(pos.get_page() == page);
     116       98901 :             CPPUNIT_ASSERT(pos.get_page_line() == page_line);
     117       98901 :             CPPUNIT_ASSERT(pos.get_paragraph() == paragraph);
     118       98901 :             CPPUNIT_ASSERT(pos.get_line() == total_line);
     119             : 
     120       98901 :             if(line % paragraphs == 0)
     121             :             {
     122        6983 :                 pos.new_paragraph();
     123        6983 :                 ++paragraph;
     124             :             }
     125       98901 :             pos.new_line();
     126       98901 :             ++total_line;
     127       98901 :             ++page_line;
     128             :         }
     129          99 :         pos.new_page();
     130             :     }
     131             : 
     132             :     // by default, the reset counters resets everything back to 1
     133             :     {
     134           1 :         pos.reset_counters();
     135           1 :         CPPUNIT_ASSERT(pos.get_page() == 1);
     136           1 :         CPPUNIT_ASSERT(pos.get_page_line() == 1);
     137           1 :         CPPUNIT_ASSERT(pos.get_paragraph() == 1);
     138           1 :         CPPUNIT_ASSERT(pos.get_line() == 1);
     139             :     }
     140             : 
     141             :     // we can also define the start line
     142           1 :     int last_line(1);
     143         250 :     for(int idx(1); idx < 250; ++idx)
     144             :     {
     145         249 :         int line(rand() % 20000);
     146         249 :         if(idx % 13 == 0)
     147             :         {
     148             :             // force a negative number to test the throw
     149          19 :             line = -line;
     150             :         }
     151         249 :         if(line < 1)
     152             :         {
     153             :             // this throws because the line # is not valid
     154          19 :             CPPUNIT_ASSERT_THROW(pos.reset_counters(line), as2js::exception_internal_error);
     155             : 
     156             :             // the counters are unchanged in that case
     157          19 :             CPPUNIT_ASSERT(pos.get_page() == 1);
     158          19 :             CPPUNIT_ASSERT(pos.get_page_line() == 1);
     159          19 :             CPPUNIT_ASSERT(pos.get_paragraph() == 1);
     160          19 :             CPPUNIT_ASSERT(pos.get_line() == last_line);
     161             :         }
     162             :         else
     163             :         {
     164         230 :             pos.reset_counters(line);
     165         230 :             CPPUNIT_ASSERT(pos.get_page() == 1);
     166         230 :             CPPUNIT_ASSERT(pos.get_page_line() == 1);
     167         230 :             CPPUNIT_ASSERT(pos.get_paragraph() == 1);
     168         230 :             CPPUNIT_ASSERT(pos.get_line() == line);
     169         230 :             last_line = line;
     170             :         }
     171           1 :     }
     172           1 : }
     173             : 
     174             : 
     175           1 : void As2JsPositionUnitTests::test_output()
     176             : {
     177           1 :     as2js::Position pos;
     178           1 :     pos.set_filename("file.js");
     179           1 :     int total_line(1);
     180         100 :     for(int page(1); page < 100; ++page)
     181             :     {
     182          99 :         int paragraphs(rand() % 10 + 10);
     183          99 :         int page_line(1);
     184          99 :         int paragraph(1);
     185       99000 :         for(int line(1); line < 1000; ++line)
     186             :         {
     187       98901 :             CPPUNIT_ASSERT(pos.get_page() == page);
     188       98901 :             CPPUNIT_ASSERT(pos.get_page_line() == page_line);
     189       98901 :             CPPUNIT_ASSERT(pos.get_paragraph() == paragraph);
     190       98901 :             CPPUNIT_ASSERT(pos.get_line() == total_line);
     191             : 
     192       98901 :             std::stringstream pos_str;
     193       98901 :             pos_str << pos;
     194      197802 :             std::stringstream test_str;
     195       98901 :             test_str << "file.js:" << total_line << ":";
     196       98901 :             CPPUNIT_ASSERT(pos_str.str() == test_str.str());
     197             : 
     198       98901 :             if(line % paragraphs == 0)
     199             :             {
     200        7223 :                 pos.new_paragraph();
     201        7223 :                 ++paragraph;
     202             :             }
     203       98901 :             pos.new_line();
     204       98901 :             ++total_line;
     205       98901 :             ++page_line;
     206       98901 :         }
     207          99 :         pos.new_page();
     208           1 :     }
     209             : 
     210          13 : }
     211             : 
     212             : 
     213             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.10