⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit_test_result.cpp

📁 boost库提供标准的C++ API 配合dev c++使用,功能更加强大
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//____________________________________________________________________________//

static void safe_delete( unit_test_result_ptr ptr ) { boost::checked_delete<unit_test_result>( ptr ); }

unit_test_result::~unit_test_result()
{
    std::list<unit_test_result_ptr>::iterator beg = m_pimpl->m_children.begin();
    std::list<unit_test_result_ptr>::iterator end = m_pimpl->m_children.end();

    std::for_each( beg, end, &safe_delete );
}

//____________________________________________________________________________//

unit_test_result&
unit_test_result::instance()
{
    assert( Impl::m_head );

    return Impl::m_curr ? *Impl::m_curr : *Impl::m_head;
}

//____________________________________________________________________________//

void
unit_test_result::test_case_start( std::string const& name, unit_test_counter expected_failures )
{
    unit_test_result_ptr new_test_case_result_inst = new unit_test_result( Impl::m_curr, name, expected_failures );

    if( Impl::m_curr )
        Impl::m_curr->m_pimpl->m_children.push_back( new_test_case_result_inst );
    else
        Impl::m_head.reset( new_test_case_result_inst );

    Impl::m_curr = new_test_case_result_inst;
}

//____________________________________________________________________________//

void
unit_test_result::test_case_end()
{
    assert( Impl::m_curr );

    Impl*                curr_impl  = Impl::m_curr->m_pimpl.get();
    unit_test_result_ptr parent     = curr_impl->m_parent;

    if( parent ) {
        // accumulate results
        parent->m_pimpl->m_assertions_passed += curr_impl->m_assertions_passed;
        parent->m_pimpl->m_assertions_failed += curr_impl->m_assertions_failed;
        parent->m_pimpl->m_test_cases_passed += curr_impl->m_test_cases_passed;
        parent->m_pimpl->m_test_cases_failed += curr_impl->m_test_cases_failed;

        // for test_cases (vs. test_suite) //!! need better identification
        if( curr_impl->m_test_cases_passed == 0 && curr_impl->m_test_cases_failed == 0 ) {
            if( curr_impl->has_failed() )
                parent->m_pimpl->m_test_cases_failed++;
            else
                parent->m_pimpl->m_test_cases_passed++;
        }

    }

    Impl::m_curr = parent;
}

//____________________________________________________________________________//

void
unit_test_result::set_report_format( std::string const& reportformat )
{
    struct my_pair {
        c_string_literal    format_name;
        output_format       format_value;
    };

    static const my_pair name_value_map[] = {
        { "HRF" , HRF },
        { "XML" , XML },
    };

    static int const map_size = sizeof(name_value_map)/sizeof(my_pair);

    output_format of = HRF;
    for( int i = 0; i < map_size; i++ ) {
        if( reportformat == name_value_map[i].format_name ) {
            of = name_value_map[i].format_value;
            break;
        }
    }

    if( of == HRF )
        Impl::m_report_formatter.reset( new hrf_report_formatter );
    else
        Impl::m_report_formatter.reset( new xml_report_formatter );
}

//____________________________________________________________________________//

void
unit_test_result::increase_expected_failures( unit_test_counter amount ) 
{
    m_pimpl->m_expected_failures += amount;

    if( m_pimpl->m_parent )
        m_pimpl->m_parent->increase_expected_failures( amount );
}

//____________________________________________________________________________//

void
unit_test_result::inc_failed_assertions()
{
    m_pimpl->m_assertions_failed++;

    if( m_pimpl->m_assertions_failed == 1 )
        first_failed_assertion();
}

//____________________________________________________________________________//

void
unit_test_result::inc_passed_assertions()
{
    m_pimpl->m_assertions_passed++;
}

//____________________________________________________________________________//

void
unit_test_result::caught_exception()
{
    m_pimpl->m_exception_caught = true;
}

//____________________________________________________________________________//

std::string const&
unit_test_result::test_case_name()
{
    return m_pimpl->m_test_case_name;
}

//____________________________________________________________________________//

void
unit_test_result::reset_current_result_set()
{
    static unit_test_result_ptr backup = unit_test_result_ptr();
    static boost::scoped_ptr<unit_test_result> temporary_substitute;

    assert( Impl::m_curr );

    if( backup ) {
        Impl::m_curr = backup;
        backup = unit_test_result_ptr();
        temporary_substitute.reset();
    }
    else {
        backup = Impl::m_curr;
        Impl::m_curr = new unit_test_result( NULL, Impl::m_curr->test_case_name(), 0 );
        temporary_substitute.reset( Impl::m_curr );
    }
}

//____________________________________________________________________________//

void
unit_test_result::failures_details( unit_test_counter& num_of_failures, bool& exception_caught )
{
    num_of_failures  = m_pimpl->m_assertions_failed;
    exception_caught = m_pimpl->m_exception_caught;
}

//____________________________________________________________________________//

void
unit_test_result::report( std::string const& reportlevel, std::ostream& where_to_ )
{
    static int const map_size = sizeof(report_level_names)/sizeof(c_string_literal);

    report_level rl = UNDEF_REPORT;
    if( reportlevel.empty() )
        rl = CONFIRMATION_REPORT;
    else {
         for( int i = 0; i < map_size; i++ ) {
            if( reportlevel == report_level_names[i] ) {
                rl = (report_level)i;
                break;
            }
        }
    }

    switch( rl ) {
    case CONFIRMATION_REPORT:
        confirmation_report( where_to_ );
        break;
    case SHORT_REPORT:
    case DETAILED_REPORT:
        m_pimpl->m_report_formatter->start_result_report( where_to_ );
        report_result( where_to_, 0, rl == DETAILED_REPORT );
        m_pimpl->m_report_formatter->finish_result_report( where_to_ );
        break;
    case NO_REPORT:
        break;
    default:
        where_to_ << "*** Unrecognized report level" << std::endl;
        break;
    }
}

//____________________________________________________________________________//

void
unit_test_result::confirmation_report( std::ostream& where_to )
{
    assert( this );

    m_pimpl->m_report_formatter->start_result_report( where_to );

    m_pimpl->m_report_formatter->start_confirmation_report( where_to, 
                                                            m_pimpl->m_test_case_name, m_pimpl->m_children.empty(),
                                                            m_pimpl->m_test_cases_failed != 0 || m_pimpl->has_failed(),
                                                            m_pimpl->m_assertions_failed, m_pimpl->m_expected_failures );

    m_pimpl->m_report_formatter->finish_test_case_report( where_to, 0,
                                                          m_pimpl->m_test_case_name, m_pimpl->m_children.empty(),
                                                          m_pimpl->m_exception_caught );

    m_pimpl->m_report_formatter->finish_result_report( where_to );
}

//____________________________________________________________________________//

void
unit_test_result::report_result( std::ostream& where_to, std::size_t indent, bool detailed )
{
    assert( this );

    m_pimpl->m_report_formatter->start_test_case_report( where_to, indent, 
                                                         m_pimpl->m_test_case_name, m_pimpl->m_children.empty(),
                                                         m_pimpl->m_test_cases_failed != 0 || m_pimpl->has_failed() );

    if( m_pimpl->m_test_cases_passed + m_pimpl->m_test_cases_failed > 1 )
        m_pimpl->m_report_formatter->report_sub_test_cases_stat( where_to, indent, 
                                                                 m_pimpl->m_test_cases_passed, m_pimpl->m_test_cases_failed );

    m_pimpl->m_report_formatter->report_assertions_stat( where_to, indent, 
                                                         m_pimpl->m_assertions_passed, 
                                                         m_pimpl->m_assertions_failed, 
                                                         m_pimpl->m_expected_failures );

    if( detailed ) {
        std::list<unit_test_result_ptr>::iterator it  = m_pimpl->m_children.begin();
        std::list<unit_test_result_ptr>::iterator end = m_pimpl->m_children.end();
        
        while( it != end ) {
            unit_test_result_ptr next = *(it++);
            next->report_result( where_to, indent+2, true );
        }
    }

    m_pimpl->m_report_formatter->finish_test_case_report( where_to, indent, 
                                                          m_pimpl->m_test_case_name, m_pimpl->m_children.empty(),
                                                          m_pimpl->m_exception_caught );
}

//____________________________________________________________________________//

int
unit_test_result::result_code() const
{
    return m_pimpl->result_code();
}

//____________________________________________________________________________//

bool
unit_test_result::has_passed() const
{
    return !m_pimpl->has_failed();
}

//____________________________________________________________________________//

} // namespace unit_test_framework

} // namespace boost

// ***************************************************************************
//  Revision History :
//  
//  $Log: unit_test_result.cpp,v $
//  Revision 1.22  2003/12/01 00:42:37  rogeeff
//  prerelease cleaning
//

// ***************************************************************************

// EOF

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -