LCOV - code coverage report
Current view: top level - libreoffice/basic/qa/cppunit - basic_coverage.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 68 80 85.0 %
Date: 2012-12-27 Functions: 15 19 78.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  */
       9             : 
      10             : #include "basictest.hxx"
      11             : #include <osl/file.hxx>
      12             : #include "basic/sbmod.hxx"
      13             : #include "basic/sbmeth.hxx"
      14             : 
      15             : 
      16             : namespace
      17             : {
      18             : 
      19             : class Coverage : public BasicTestBase
      20             : {
      21             : private:
      22             :     bool m_bError;
      23             :     int  m_nb_tests;
      24             :     int  m_nb_tests_ok;
      25             :     int  m_nb_tests_ko;
      26             :     int  m_nb_tests_skipped;
      27             :     OUString m_sCurrentTest;
      28             :     void process_directory(OUString sDirName);
      29             :     void process_file(OUString sFileName);
      30             :     void run_test(OUString sFileName, OUString sCode);
      31             :     void test_start(OUString /* sFileName */);
      32             :     void test_failed(void);
      33             :     void test_success(void);
      34             :     void print_summary() {};
      35             : 
      36             :     DECL_LINK( CoverageErrorHdl, StarBASIC * );
      37             : 
      38             : public:
      39             :     Coverage();
      40             :     ~Coverage();
      41             : 
      42             :     void Coverage_Iterator();
      43             : 
      44             :     // Adds code needed to register the test suite
      45           2 :     CPPUNIT_TEST_SUITE(Coverage);
      46             : 
      47             :     // Declares the method as a test to call
      48           1 :     CPPUNIT_TEST(Coverage_Iterator);
      49             : 
      50             :     // End of test suite definition
      51           2 :     CPPUNIT_TEST_SUITE_END();
      52             : };
      53             : 
      54           0 : IMPL_LINK( Coverage, CoverageErrorHdl, StarBASIC *, /*pBasic*/)
      55             : {
      56             :     fprintf(stderr,"%s:(%d:%d)\n",
      57             :             rtl::OUStringToOString( m_sCurrentTest, RTL_TEXTENCODING_UTF8 ).getStr(),
      58           0 :             StarBASIC::GetLine(), StarBASIC::GetCol1());
      59           0 :     fprintf(stderr,"Basic error: %s\n", rtl::OUStringToOString( StarBASIC::GetErrorText(), RTL_TEXTENCODING_UTF8 ).getStr() );
      60           0 :     m_bError = true;
      61           0 :     return 0;
      62             : }
      63             : 
      64           1 : Coverage::Coverage()
      65             :     : m_bError(false)
      66             :     , m_nb_tests(0)
      67             :     , m_nb_tests_ok(0)
      68             :     , m_nb_tests_ko(0)
      69           1 :     , m_nb_tests_skipped(0)
      70             : {
      71           1 : }
      72             : 
      73           3 : Coverage::~Coverage()
      74             : {
      75           1 :     fprintf(stderr,"basic coverage Summary : skipped:%d pass:%d failed:%d\n", m_nb_tests_skipped, m_nb_tests_ok, m_nb_tests_ko );
      76           1 :     if(m_nb_tests_ko)
      77             :     {
      78           0 :         CPPUNIT_FAIL("");
      79             :     }
      80           2 : }
      81             : 
      82           2 : void Coverage::test_start(OUString sFileName)
      83             : {
      84           2 :     m_nb_tests += 1;
      85           2 :     m_sCurrentTest = sFileName;
      86           2 : }
      87             : 
      88           0 : void Coverage::test_failed()
      89             : {
      90           0 :     m_nb_tests_ko += 1;
      91           0 :     fprintf(stderr,"%s,FAIL\n", rtl::OUStringToOString( m_sCurrentTest, RTL_TEXTENCODING_UTF8 ).getStr() );
      92             : 
      93           0 : }
      94             : 
      95           2 : void Coverage::test_success()
      96             : {
      97           2 :     m_nb_tests_ok += 1;
      98           2 :     fprintf(stderr,"%s,PASS\n", rtl::OUStringToOString( m_sCurrentTest, RTL_TEXTENCODING_UTF8 ).getStr() );
      99           2 : }
     100             : 
     101           2 : void Coverage::run_test(OUString /*sFileName*/, OUString sCode)
     102             : {
     103           2 :     bool result = false;
     104           2 :     CPPUNIT_ASSERT_MESSAGE( "No resource manager", basicDLL().GetBasResMgr() != NULL );
     105           2 :     StarBASICRef pBasic = new StarBASIC();
     106           2 :     ResetError();
     107           2 :     StarBASIC::SetGlobalErrorHdl( LINK( this, Coverage, CoverageErrorHdl ) );
     108             : 
     109           2 :     SbModule* pMod = pBasic->MakeModule( rtl::OUString( "TestModule" ), sCode );
     110           2 :     pMod->Compile();
     111           2 :     if(!m_bError)
     112             :     {
     113           2 :         SbMethod* pMeth = static_cast<SbMethod*>(pMod->Find( rtl::OUString("doUnitTest"),  SbxCLASS_METHOD ));
     114           2 :         if(pMeth)
     115             :         {
     116           2 :             SbxVariableRef refTemp = pMeth;
     117             :             // forces a broadcast
     118           2 :             SbxVariableRef pNew = new  SbxMethod( *((SbxMethod*)pMeth));
     119           2 :             if(pNew->GetInteger() == 1 )
     120             :             {
     121           2 :                 result = true;
     122           2 :             }
     123             :         }
     124             :     }
     125           2 :     if(result)
     126             :     {
     127           2 :         test_success();
     128             :     }
     129             :     else
     130             :     {
     131           0 :         test_failed();
     132           2 :     }
     133           2 : }
     134             : 
     135           2 : void Coverage::process_file(OUString sFileName)
     136             : {
     137           2 :     osl::File aFile(sFileName);
     138             : 
     139           2 :     test_start(sFileName);
     140           2 :     if(osl::FileBase::E_None == aFile.open(osl_File_OpenFlag_Read))
     141             :     {
     142             :         sal_uInt64 size;
     143             :         sal_uInt64 size_read;
     144           2 :         if(osl::FileBase::E_None == aFile.getSize(size))
     145             :         {
     146           2 :             void* buffer = calloc(1, size+1);
     147           2 :             CPPUNIT_ASSERT(buffer);
     148           2 :             if(osl::FileBase::E_None == aFile.read( buffer, size, size_read))
     149             :             {
     150           2 :                 if(size == size_read)
     151             :                 {
     152           2 :                     OUString sCode((sal_Char*)buffer, size, RTL_TEXTENCODING_UTF8);
     153           2 :                     run_test(sFileName, sCode);
     154           4 :                     return;
     155             :                 }
     156             :             }
     157             :         }
     158             :     }
     159           0 :     test_failed();
     160             : }
     161             : 
     162           1 : void Coverage::process_directory(OUString sDirName)
     163             : {
     164           1 :     osl::Directory aDir(sDirName);
     165           1 :     osl::DirectoryItem aItem;
     166           1 :     osl::FileStatus aFileStatus(osl_FileStatus_Mask_FileURL|osl_FileStatus_Mask_Type);
     167             : 
     168           1 :     if(osl::FileBase::E_None == aDir.open())
     169             :     {
     170           4 :         while (aDir.getNextItem(aItem) == osl::FileBase::E_None)
     171             :         {
     172           2 :             aItem.getFileStatus(aFileStatus);
     173           2 :             if(aFileStatus.isRegular())
     174             :             {
     175           2 :                 process_file(aFileStatus.getFileURL());
     176             :             }
     177             :         }
     178             :     }
     179             :     else
     180             :     {
     181             :     }
     182           1 :     fprintf(stderr,"end process directory\n");
     183           1 : }
     184             : 
     185           1 : void Coverage::Coverage_Iterator(void)
     186             : {
     187           1 :     OUString sDirName = getURLFromSrc("/basic/qa/basic_coverage/");
     188             : 
     189           1 :     CPPUNIT_ASSERT(!sDirName.isEmpty());
     190           1 :     process_directory(sDirName);
     191           1 : }
     192             : 
     193           1 :   CPPUNIT_TEST_SUITE_REGISTRATION(Coverage);
     194             : 
     195             : }
     196           4 : CPPUNIT_PLUGIN_IMPLEMENT();
     197             : 
     198             : 
     199             : 
     200             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10