diff --git a/.uncrustify b/.uncrustify new file mode 100644 index 0000000..ea1187b --- /dev/null +++ b/.uncrustify @@ -0,0 +1,488 @@ +# Uncrustify 0.61 +newlines = auto +input_tab_size = 4 +output_tab_size = 3 +string_escape_char = 92 +string_escape_char2 = 0 +tok_split_gte = false +utf8_bom = remove +utf8_byte = false +utf8_force = false +indent_columns = 3 +indent_continue = 0 +indent_with_tabs = 0 +indent_cmt_with_tabs = false +indent_align_string = false +indent_xml_string = 0 +indent_brace = 0 +indent_braces = false +indent_braces_no_func = false +indent_braces_no_class = false +indent_braces_no_struct = false +indent_brace_parent = false +indent_paren_open_brace = false +indent_namespace = true +indent_namespace_single_indent = false +indent_namespace_level = 0 +indent_namespace_limit = 0 +indent_extern = false +indent_class = true +indent_class_colon = false +indent_constr_colon = false +indent_ctor_init_leading = 1 +indent_ctor_init = 0 +indent_else_if = false +indent_var_def_blk = 0 +indent_var_def_cont = false +indent_func_def_force_col1 = false +indent_func_call_param = false +indent_func_def_param = false +indent_func_proto_param = false +indent_func_class_param = false +indent_func_ctor_var_param = false +indent_template_param = false +indent_func_param_double = false +indent_func_const = 0 +indent_func_throw = 0 +indent_member = indent_columns +indent_sing_line_comments = 0 +indent_relative_single_line_comments = true +indent_switch_case = 0 +indent_case_shift = 0 +indent_case_brace = 0 +indent_col1_comment = false +indent_label = 1 +indent_access_spec = -3 +indent_access_spec_body = false +indent_paren_nl = false +indent_paren_close = 0 +indent_comma_paren = false +indent_bool_paren = false +indent_first_bool_expr = false +indent_square_nl = false +indent_preserve_sql = false +indent_align_assign = true +indent_oc_block = false +indent_oc_block_msg = 0 +indent_oc_msg_colon = 0 +indent_oc_msg_prioritize_first_colon = true +indent_oc_block_msg_xcode_style = false +indent_oc_block_msg_from_keyword = false +indent_oc_block_msg_from_colon = false +indent_oc_block_msg_from_caret = false +indent_oc_block_msg_from_brace = false +sp_arith = ignore +sp_assign = ignore +sp_cpp_lambda_assign = ignore +sp_cpp_lambda_paren = ignore +sp_assign_default = ignore +sp_before_assign = ignore +sp_after_assign = ignore +sp_enum_paren = ignore +sp_enum_assign = ignore +sp_enum_before_assign = ignore +sp_enum_after_assign = ignore +sp_pp_concat = add +sp_pp_stringify = ignore +sp_before_pp_stringify = ignore +sp_bool = ignore +sp_compare = ignore +sp_inside_paren = ignore +sp_paren_paren = ignore +sp_cparen_oparen = ignore +sp_balance_nested_parens = false +sp_paren_brace = ignore +sp_before_ptr_star = ignore +sp_before_unnamed_ptr_star = ignore +sp_between_ptr_star = ignore +sp_after_ptr_star = ignore +sp_after_ptr_star_qualifier = ignore +sp_after_ptr_star_func = ignore +sp_ptr_star_paren = ignore +sp_before_ptr_star_func = ignore +sp_before_byref = ignore +sp_before_unnamed_byref = ignore +sp_after_byref = ignore +sp_after_byref_func = ignore +sp_before_byref_func = ignore +sp_after_type = force +sp_before_template_paren = ignore +sp_template_angle = ignore +sp_before_angle = ignore +sp_inside_angle = ignore +sp_after_angle = ignore +sp_angle_paren = ignore +sp_angle_word = ignore +sp_angle_shift = add +sp_permit_cpp11_shift = false +sp_before_sparen = ignore +sp_inside_sparen = ignore +sp_inside_sparen_close = ignore +sp_inside_sparen_open = ignore +sp_after_sparen = ignore +sp_sparen_brace = ignore +sp_invariant_paren = ignore +sp_after_invariant_paren = ignore +sp_special_semi = ignore +sp_before_semi = remove +sp_before_semi_for = ignore +sp_before_semi_for_empty = ignore +sp_after_semi = add +sp_after_semi_for = force +sp_after_semi_for_empty = ignore +sp_before_square = ignore +sp_before_squares = ignore +sp_inside_square = ignore +sp_after_comma = ignore +sp_before_comma = remove +sp_paren_comma = force +sp_before_ellipsis = ignore +sp_after_class_colon = ignore +sp_before_class_colon = ignore +sp_after_constr_colon = ignore +sp_before_constr_colon = ignore +sp_before_case_colon = remove +sp_after_operator = ignore +sp_after_operator_sym = ignore +sp_after_cast = ignore +sp_inside_paren_cast = ignore +sp_cpp_cast_paren = ignore +sp_sizeof_paren = ignore +sp_after_tag = ignore +sp_inside_braces_enum = ignore +sp_inside_braces_struct = ignore +sp_inside_braces = ignore +sp_inside_braces_empty = ignore +sp_type_func = ignore +sp_func_proto_paren = ignore +sp_func_def_paren = ignore +sp_inside_fparens = ignore +sp_inside_fparen = ignore +sp_inside_tparen = ignore +sp_after_tparen_close = ignore +sp_square_fparen = ignore +sp_fparen_brace = ignore +sp_fparen_dbrace = ignore +sp_func_call_paren = ignore +sp_func_call_paren_empty = ignore +sp_func_call_user_paren = ignore +sp_func_class_paren = ignore +sp_return_paren = ignore +sp_attribute_paren = ignore +sp_defined_paren = ignore +sp_throw_paren = ignore +sp_after_throw = ignore +sp_catch_paren = ignore +sp_version_paren = ignore +sp_scope_paren = ignore +sp_macro = ignore +sp_macro_func = ignore +sp_else_brace = ignore +sp_brace_else = ignore +sp_brace_typedef = ignore +sp_catch_brace = ignore +sp_brace_catch = ignore +sp_finally_brace = ignore +sp_brace_finally = ignore +sp_try_brace = ignore +sp_getset_brace = ignore +sp_word_brace = add +sp_word_brace_ns = add +sp_before_dc = ignore +sp_after_dc = ignore +sp_d_array_colon = ignore +sp_not = remove +sp_inv = remove +sp_addr = remove +sp_member = remove +sp_deref = remove +sp_sign = remove +sp_incdec = remove +sp_before_nl_cont = add +sp_after_oc_scope = ignore +sp_after_oc_colon = ignore +sp_before_oc_colon = ignore +sp_after_oc_dict_colon = ignore +sp_before_oc_dict_colon = ignore +sp_after_send_oc_colon = ignore +sp_before_send_oc_colon = ignore +sp_after_oc_type = ignore +sp_after_oc_return_type = ignore +sp_after_oc_at_sel = ignore +sp_after_oc_at_sel_parens = ignore +sp_inside_oc_at_sel_parens = ignore +sp_before_oc_block_caret = ignore +sp_after_oc_block_caret = ignore +sp_after_oc_msg_receiver = ignore +sp_after_oc_property = ignore +sp_cond_colon = ignore +sp_cond_colon_before = ignore +sp_cond_colon_after = ignore +sp_cond_question = ignore +sp_cond_question_before = ignore +sp_cond_question_after = ignore +sp_cond_ternary_short = ignore +sp_case_label = ignore +sp_range = ignore +sp_after_for_colon = ignore +sp_before_for_colon = ignore +sp_extern_paren = ignore +sp_cmt_cpp_start = ignore +sp_endif_cmt = ignore +sp_after_new = ignore +sp_before_tr_emb_cmt = ignore +sp_num_before_tr_emb_cmt = 0 +sp_annotation_paren = ignore +align_keep_tabs = false +align_with_tabs = false +align_on_tabstop = false +align_number_left = false +align_keep_extra_space = false +align_func_params = false +align_same_func_call_params = false +align_var_def_span = 0 +align_var_def_star_style = 0 +align_var_def_amp_style = 0 +align_var_def_thresh = 0 +align_var_def_gap = 0 +align_var_def_colon = false +align_var_def_attribute = false +align_var_def_inline = false +align_assign_span = 0 +align_assign_thresh = 0 +align_enum_equ_span = 0 +align_enum_equ_thresh = 0 +align_var_struct_span = 0 +align_var_struct_thresh = 0 +align_var_struct_gap = 0 +align_struct_init_span = 0 +align_typedef_gap = 0 +align_typedef_span = 0 +align_typedef_func = 0 +align_typedef_star_style = 0 +align_typedef_amp_style = 0 +align_right_cmt_span = 0 +align_right_cmt_mix = false +align_right_cmt_gap = 0 +align_right_cmt_at_col = 0 +align_func_proto_span = 0 +align_func_proto_gap = 0 +align_on_operator = false +align_mix_var_proto = false +align_single_line_func = false +align_single_line_brace = false +align_single_line_brace_gap = 0 +align_oc_msg_spec_span = 0 +align_nl_cont = true +align_pp_define_together = false +align_pp_define_gap = 0 +align_pp_define_span = 0 +align_left_shift = true +align_oc_msg_colon_span = 0 +align_oc_msg_colon_first = false +align_oc_decl_colon = false +nl_collapse_empty_body = true +nl_assign_leave_one_liners = false +nl_class_leave_one_liners = false +nl_enum_leave_one_liners = false +nl_getset_leave_one_liners = false +nl_func_leave_one_liners = false +nl_cpp_lambda_leave_one_liners = false +nl_if_leave_one_liners = false +nl_oc_msg_leave_one_liner = false +nl_start_of_file = ignore +nl_start_of_file_min = 0 +nl_end_of_file = ignore +nl_end_of_file_min = 0 +nl_assign_brace = ignore +nl_assign_square = ignore +nl_after_square_assign = ignore +nl_func_var_def_blk = 0 +nl_typedef_blk_start = 0 +nl_typedef_blk_end = 0 +nl_typedef_blk_in = 0 +nl_var_def_blk_start = 0 +nl_var_def_blk_end = 0 +nl_var_def_blk_in = 0 +nl_fcall_brace = ignore +nl_enum_brace = ignore +nl_struct_brace = ignore +nl_union_brace = ignore +nl_if_brace = ignore +nl_brace_else = ignore +nl_elseif_brace = ignore +nl_else_brace = ignore +nl_else_if = ignore +nl_brace_finally = ignore +nl_finally_brace = ignore +nl_try_brace = ignore +nl_getset_brace = ignore +nl_for_brace = ignore +nl_catch_brace = ignore +nl_brace_catch = ignore +nl_brace_square = ignore +nl_brace_fparen = ignore +nl_while_brace = ignore +nl_scope_brace = ignore +nl_unittest_brace = ignore +nl_version_brace = ignore +nl_using_brace = ignore +nl_brace_brace = ignore +nl_do_brace = ignore +nl_brace_while = ignore +nl_switch_brace = ignore +nl_multi_line_cond = false +nl_multi_line_define = false +nl_before_case = false +nl_before_throw = ignore +nl_after_case = false +nl_case_colon_brace = ignore +nl_namespace_brace = ignore +nl_template_class = ignore +nl_class_brace = ignore +nl_class_init_args = ignore +nl_constr_init_args = ignore +nl_func_type_name = ignore +nl_func_type_name_class = ignore +nl_func_scope_name = ignore +nl_func_proto_type_name = ignore +nl_func_paren = ignore +nl_func_def_paren = ignore +nl_func_decl_start = ignore +nl_func_def_start = ignore +nl_func_decl_start_single = ignore +nl_func_def_start_single = ignore +nl_func_decl_args = ignore +nl_func_def_args = ignore +nl_func_decl_end = ignore +nl_func_def_end = ignore +nl_func_decl_end_single = ignore +nl_func_def_end_single = ignore +nl_func_decl_empty = ignore +nl_func_def_empty = ignore +nl_oc_msg_args = false +nl_fdef_brace = ignore +nl_cpp_ldef_brace = ignore +nl_return_expr = ignore +nl_after_semicolon = false +nl_paren_dbrace_open = ignore +nl_after_brace_open = false +nl_after_brace_open_cmt = false +nl_after_vbrace_open = false +nl_after_vbrace_open_empty = false +nl_after_brace_close = false +nl_after_vbrace_close = false +nl_brace_struct_var = ignore +nl_define_macro = false +nl_squeeze_ifdef = false +nl_before_if = ignore +nl_after_if = ignore +nl_before_for = ignore +nl_after_for = ignore +nl_before_while = ignore +nl_after_while = ignore +nl_before_switch = ignore +nl_after_switch = ignore +nl_before_do = ignore +nl_after_do = ignore +nl_ds_struct_enum_cmt = false +nl_ds_struct_enum_close_brace = false +nl_class_colon = ignore +nl_constr_colon = ignore +nl_create_if_one_liner = false +nl_create_for_one_liner = false +nl_create_while_one_liner = false +pos_arith = ignore +pos_assign = ignore +pos_bool = ignore +pos_compare = ignore +pos_conditional = ignore +pos_comma = ignore +pos_class_comma = ignore +pos_constr_comma = ignore +pos_class_colon = ignore +pos_constr_colon = ignore +code_width = 0 +ls_for_split_full = false +ls_func_split_full = false +ls_code_width = false +nl_max = 0 +nl_after_func_proto = 0 +nl_after_func_proto_group = 0 +nl_after_func_body = 0 +nl_after_func_body_class = 0 +nl_after_func_body_one_liner = 0 +nl_before_block_comment = 0 +nl_before_c_comment = 0 +nl_before_cpp_comment = 0 +nl_after_multiline_comment = false +nl_after_struct = 0 +nl_after_class = 0 +nl_before_access_spec = 0 +nl_after_access_spec = 0 +nl_comment_func_def = 0 +nl_after_try_catch_finally = 0 +nl_around_cs_property = 0 +nl_between_get_set = 0 +nl_property_brace = ignore +eat_blanks_after_open_brace = false +eat_blanks_before_close_brace = false +nl_remove_extra_newlines = 0 +nl_before_return = false +nl_after_return = false +nl_after_annotation = ignore +nl_between_annotation = ignore +mod_full_brace_do = ignore +mod_full_brace_for = ignore +mod_full_brace_function = ignore +mod_full_brace_if = ignore +mod_full_brace_if_chain = false +mod_full_brace_nl = 0 +mod_full_brace_while = ignore +mod_full_brace_using = ignore +mod_paren_on_return = ignore +mod_pawn_semicolon = false +mod_full_paren_if_bool = false +mod_remove_extra_semicolon = false +mod_add_long_function_closebrace_comment = 0 +mod_add_long_namespace_closebrace_comment = 0 +mod_add_long_switch_closebrace_comment = 0 +mod_add_long_ifdef_endif_comment = 0 +mod_add_long_ifdef_else_comment = 0 +mod_sort_import = false +mod_sort_using = false +mod_sort_include = false +mod_move_case_break = false +mod_case_brace = ignore +mod_remove_empty_return = false +cmt_width = 0 +cmt_reflow_mode = 0 +cmt_convert_tab_to_spaces = false +cmt_indent_multi = true +cmt_c_group = false +cmt_c_nl_start = false +cmt_c_nl_end = false +cmt_cpp_group = false +cmt_cpp_nl_start = false +cmt_cpp_nl_end = false +cmt_cpp_to_c = false +cmt_star_cont = false +cmt_sp_before_star_cont = 0 +cmt_sp_after_star_cont = 0 +cmt_multi_check_last = true +cmt_insert_file_header = "" +cmt_insert_file_footer = "" +cmt_insert_func_header = "" +cmt_insert_class_header = "" +cmt_insert_oc_msg_header = "" +cmt_insert_before_preproc = false +pp_indent = ignore +pp_indent_at_level = false +pp_indent_count = 3 +pp_space = ignore +pp_space_count = 0 +pp_indent_region = 0 +pp_region_indent_code = false +pp_indent_if = 0 +pp_if_indent_code = false +pp_define_at_level = true diff --git a/UnitTest++/AssertException.cpp b/UnitTest++/AssertException.cpp index 9a7d78f..29b3939 100644 --- a/UnitTest++/AssertException.cpp +++ b/UnitTest++/AssertException.cpp @@ -4,13 +4,11 @@ namespace UnitTest { -AssertException::AssertException() -{ -} + AssertException::AssertException() + {} -AssertException::~AssertException() throw() -{ -} + AssertException::~AssertException() throw() + {} } diff --git a/UnitTest++/AssertException.h b/UnitTest++/AssertException.h index e5d0705..e604f63 100644 --- a/UnitTest++/AssertException.h +++ b/UnitTest++/AssertException.h @@ -9,12 +9,12 @@ namespace UnitTest { -class UNITTEST_LINKAGE AssertException : public std::exception -{ -public: - AssertException(); - virtual ~AssertException() throw(); -}; + class UNITTEST_LINKAGE AssertException : public std::exception + { + public: + AssertException(); + virtual ~AssertException() throw(); + }; } diff --git a/UnitTest++/CheckMacros.h b/UnitTest++/CheckMacros.h index 9d6a759..d79d503 100644 --- a/UnitTest++/CheckMacros.h +++ b/UnitTest++/CheckMacros.h @@ -1,4 +1,4 @@ -#ifndef UNITTEST_CHECKMACROS_H +#ifndef UNITTEST_CHECKMACROS_H #define UNITTEST_CHECKMACROS_H #include "HelperMacros.h" @@ -11,169 +11,171 @@ #include "ReportAssertImpl.h" #ifdef CHECK - #error UnitTest++ redefines CHECK + #error UnitTest++ redefines CHECK #endif #ifdef CHECK_EQUAL - #error UnitTest++ redefines CHECK_EQUAL + #error UnitTest++ redefines CHECK_EQUAL #endif #ifdef CHECK_CLOSE - #error UnitTest++ redefines CHECK_CLOSE + #error UnitTest++ redefines CHECK_CLOSE #endif #ifdef CHECK_ARRAY_EQUAL - #error UnitTest++ redefines CHECK_ARRAY_EQUAL + #error UnitTest++ redefines CHECK_ARRAY_EQUAL #endif #ifdef CHECK_ARRAY_CLOSE - #error UnitTest++ redefines CHECK_ARRAY_CLOSE + #error UnitTest++ redefines CHECK_ARRAY_CLOSE #endif #ifdef CHECK_ARRAY2D_CLOSE - #error UnitTest++ redefines CHECK_ARRAY2D_CLOSE + #error UnitTest++ redefines CHECK_ARRAY2D_CLOSE #endif -#define CHECK(value) \ - UNITTEST_MULTILINE_MACRO_BEGIN \ - UT_TRY \ - ({ \ - if (!UnitTest::Check(value)) \ - UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), #value); \ - }) \ - UT_CATCH (std::exception, e, \ - { \ - UnitTest::MemoryOutStream message; \ - message << "Unhandled exception (" << e.what() << ") in CHECK(" #value ")"; \ - UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ - message.GetText()); \ - }) \ - UT_CATCH_ALL \ - ({ \ - UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ - "Unhandled exception in CHECK(" #value ")"); \ - }) \ - UNITTEST_MULTILINE_MACRO_END - -#define CHECK_EQUAL(expected, actual) \ - UNITTEST_MULTILINE_MACRO_BEGIN \ - UT_TRY \ - ({ \ - UnitTest::CheckEqual(*UnitTest::CurrentTest::Results(), expected, actual, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ - }) \ - UT_CATCH (std::exception, e, \ - { \ - UnitTest::MemoryOutStream message; \ - message << "Unhandled exception (" << e.what() << ") in CHECK_EQUAL(" #expected ", " #actual ")"; \ - UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ - message.GetText()); \ - }) \ - UT_CATCH_ALL \ - ({ \ - UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ - "Unhandled exception in CHECK_EQUAL(" #expected ", " #actual ")"); \ - }) \ - UNITTEST_MULTILINE_MACRO_END - -#define CHECK_CLOSE(expected, actual, tolerance) \ - UNITTEST_MULTILINE_MACRO_BEGIN \ - UT_TRY \ - ({ \ - UnitTest::CheckClose(*UnitTest::CurrentTest::Results(), expected, actual, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ - }) \ - UT_CATCH (std::exception, e, \ - { \ - UnitTest::MemoryOutStream message; \ - message << "Unhandled exception (" << e.what() << ") in CHECK_CLOSE(" #expected ", " #actual ")"; \ - UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ - message.GetText()); \ - }) \ - UT_CATCH_ALL \ - ({ \ - UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ - "Unhandled exception in CHECK_CLOSE(" #expected ", " #actual ")"); \ - }) \ - UNITTEST_MULTILINE_MACRO_END - -#define CHECK_ARRAY_EQUAL(expected, actual, count) \ - UNITTEST_MULTILINE_MACRO_BEGIN \ - UT_TRY \ - ({ \ - UnitTest::CheckArrayEqual(*UnitTest::CurrentTest::Results(), expected, actual, count, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ - }) \ - UT_CATCH (std::exception, e, \ - { \ - UnitTest::MemoryOutStream message; \ - message << "Unhandled exception (" << e.what() << ") in CHECK_ARRAY_EQUAL(" #expected ", " #actual ")"; \ - UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ - message.GetText()); \ - }) \ - UT_CATCH_ALL \ - ({ \ - UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ - "Unhandled exception in CHECK_ARRAY_EQUAL(" #expected ", " #actual ")"); \ - }) \ - UNITTEST_MULTILINE_MACRO_END - -#define CHECK_ARRAY_CLOSE(expected, actual, count, tolerance) \ - UNITTEST_MULTILINE_MACRO_BEGIN \ - UT_TRY \ - ({ \ - UnitTest::CheckArrayClose(*UnitTest::CurrentTest::Results(), expected, actual, count, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ - }) \ - UT_CATCH (std::exception, e, \ - { \ - UnitTest::MemoryOutStream message; \ - message << "Unhandled exception (" << e.what() << ") in CHECK_ARRAY_CLOSE(" #expected ", " #actual ")"; \ - UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ - message.GetText()); \ - }) \ - UT_CATCH_ALL \ - ({ \ - UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ - "Unhandled exception in CHECK_ARRAY_CLOSE(" #expected ", " #actual ")"); \ - }) \ - UNITTEST_MULTILINE_MACRO_END - -#define CHECK_ARRAY2D_CLOSE(expected, actual, rows, columns, tolerance) \ - UNITTEST_MULTILINE_MACRO_BEGIN \ - UT_TRY \ - ({ \ - UnitTest::CheckArray2DClose(*UnitTest::CurrentTest::Results(), expected, actual, rows, columns, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ - }) \ - UT_CATCH (std::exception, e, \ - { \ - UnitTest::MemoryOutStream message; \ - message << "Unhandled exception (" << e.what() << ") in CHECK_ARRAY2D_CLOSE(" #expected ", " #actual ")"; \ - UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ - message.GetText()); \ - }) \ - UT_CATCH_ALL \ - ({ \ - UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ - "Unhandled exception in CHECK_ARRAY2D_CLOSE(" #expected ", " #actual ")"); \ - }) \ - UNITTEST_MULTILINE_MACRO_END +#define CHECK(value) \ + UNITTEST_MULTILINE_MACRO_BEGIN \ + UT_TRY \ + ({ \ + if (!UnitTest::Check(value)) \ + UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), #value); \ + }) \ + UT_CATCH (std::exception, e, \ + { \ + UnitTest::MemoryOutStream message; \ + message << "Unhandled exception (" << e.what() << ") in CHECK(" #value ")"; \ + UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ + message.GetText()); \ + }) \ + UT_CATCH_ALL \ + ({ \ + UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ + "Unhandled exception in CHECK(" #value ")"); \ + }) \ + UNITTEST_MULTILINE_MACRO_END + +#define CHECK_EQUAL(expected, actual) \ + UNITTEST_MULTILINE_MACRO_BEGIN \ + UT_TRY \ + ({ \ + UnitTest::CheckEqual(*UnitTest::CurrentTest::Results(), expected, actual, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ + }) \ + UT_CATCH (std::exception, e, \ + { \ + UnitTest::MemoryOutStream message; \ + message << "Unhandled exception (" << e.what() << ") in CHECK_EQUAL(" #expected ", " #actual ")"; \ + UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ + message.GetText()); \ + }) \ + UT_CATCH_ALL \ + ({ \ + UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ + "Unhandled exception in CHECK_EQUAL(" #expected ", " #actual ")"); \ + }) \ + UNITTEST_MULTILINE_MACRO_END + +#define CHECK_CLOSE(expected, actual, tolerance) \ + UNITTEST_MULTILINE_MACRO_BEGIN \ + UT_TRY \ + ({ \ + UnitTest::CheckClose(*UnitTest::CurrentTest::Results(), expected, actual, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ + }) \ + UT_CATCH (std::exception, e, \ + { \ + UnitTest::MemoryOutStream message; \ + message << "Unhandled exception (" << e.what() << ") in CHECK_CLOSE(" #expected ", " #actual ")"; \ + UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ + message.GetText()); \ + }) \ + UT_CATCH_ALL \ + ({ \ + UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ + "Unhandled exception in CHECK_CLOSE(" #expected ", " #actual ")"); \ + }) \ + UNITTEST_MULTILINE_MACRO_END + +#define CHECK_ARRAY_EQUAL(expected, actual, count) \ + UNITTEST_MULTILINE_MACRO_BEGIN \ + UT_TRY \ + ({ \ + UnitTest::CheckArrayEqual(*UnitTest::CurrentTest::Results(), expected, actual, count, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ + }) \ + UT_CATCH (std::exception, e, \ + { \ + UnitTest::MemoryOutStream message; \ + message << "Unhandled exception (" << e.what() << ") in CHECK_ARRAY_EQUAL(" #expected ", " #actual ")"; \ + UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ + message.GetText()); \ + }) \ + UT_CATCH_ALL \ + ({ \ + UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ + "Unhandled exception in CHECK_ARRAY_EQUAL(" #expected ", " #actual ")"); \ + }) \ + UNITTEST_MULTILINE_MACRO_END + +#define CHECK_ARRAY_CLOSE(expected, actual, count, tolerance) \ + UNITTEST_MULTILINE_MACRO_BEGIN \ + UT_TRY \ + ({ \ + UnitTest::CheckArrayClose(*UnitTest::CurrentTest::Results(), expected, actual, count, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ + }) \ + UT_CATCH (std::exception, e, \ + { \ + UnitTest::MemoryOutStream message; \ + message << "Unhandled exception (" << e.what() << ") in CHECK_ARRAY_CLOSE(" #expected ", " #actual ")"; \ + UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ + message.GetText()); \ + }) \ + UT_CATCH_ALL \ + ({ \ + UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ + "Unhandled exception in CHECK_ARRAY_CLOSE(" #expected ", " #actual ")"); \ + }) \ + UNITTEST_MULTILINE_MACRO_END + +#define CHECK_ARRAY2D_CLOSE(expected, actual, rows, columns, tolerance) \ + UNITTEST_MULTILINE_MACRO_BEGIN \ + UT_TRY \ + ({ \ + UnitTest::CheckArray2DClose(*UnitTest::CurrentTest::Results(), expected, actual, rows, columns, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ + }) \ + UT_CATCH (std::exception, e, \ + { \ + UnitTest::MemoryOutStream message; \ + message << "Unhandled exception (" << e.what() << ") in CHECK_ARRAY2D_CLOSE(" #expected ", " #actual ")"; \ + UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ + message.GetText()); \ + }) \ + UT_CATCH_ALL \ + ({ \ + UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ + "Unhandled exception in CHECK_ARRAY2D_CLOSE(" #expected ", " #actual ")"); \ + }) \ + UNITTEST_MULTILINE_MACRO_END // CHECK_THROW and CHECK_ASSERT only exist when UNITTEST_NO_EXCEPTIONS isn't defined (see config.h) #ifndef UNITTEST_NO_EXCEPTIONS -#define CHECK_THROW(expression, ExpectedExceptionType) \ - UNITTEST_MULTILINE_MACRO_BEGIN \ - bool caught_ = false; \ - try { expression; } \ - catch (ExpectedExceptionType const&) { caught_ = true; } \ - catch (...) {} \ - if (!caught_) \ - UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), "Expected exception: \"" #ExpectedExceptionType "\" not thrown"); \ - UNITTEST_MULTILINE_MACRO_END - - -#define CHECK_ASSERT(expression) \ - UNITTEST_MULTILINE_MACRO_BEGIN \ - UnitTest::Detail::ExpectAssert(true); \ - CHECK_THROW(expression, UnitTest::AssertException); \ - UnitTest::Detail::ExpectAssert(false); \ - UNITTEST_MULTILINE_MACRO_END + +#define CHECK_THROW(expression, ExpectedExceptionType) \ + UNITTEST_MULTILINE_MACRO_BEGIN \ + bool caught_ = false; \ + try { expression; } \ + catch (ExpectedExceptionType const&) { caught_ = true; } \ + catch (...) {} \ + if (!caught_) \ + UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), "Expected exception: \"" #ExpectedExceptionType "\" not thrown"); \ + UNITTEST_MULTILINE_MACRO_END + + +#define CHECK_ASSERT(expression) \ + UNITTEST_MULTILINE_MACRO_BEGIN \ + UnitTest::Detail::ExpectAssert(true); \ + CHECK_THROW(expression, UnitTest::AssertException); \ + UnitTest::Detail::ExpectAssert(false); \ + UNITTEST_MULTILINE_MACRO_END #endif + #endif diff --git a/UnitTest++/Checks.cpp b/UnitTest++/Checks.cpp index 3e09231..9c0a960 100644 --- a/UnitTest++/Checks.cpp +++ b/UnitTest++/Checks.cpp @@ -3,48 +3,48 @@ namespace UnitTest { -namespace { - -void CheckStringsEqual(TestResults& results, char const* expected, char const* actual, - TestDetails const& details) -{ - using namespace std; - - if ((expected && actual) ? strcmp(expected, actual) : (expected || actual)) - { - UnitTest::MemoryOutStream stream; - stream << "Expected " << (expected ? expected : "") << " but was " << (actual ? actual : ""); - - results.OnTestFailure(details, stream.GetText()); - } -} - -} - - -void CheckEqual(TestResults& results, char const* expected, char const* actual, - TestDetails const& details) -{ - CheckStringsEqual(results, expected, actual, details); -} - -void CheckEqual(TestResults& results, char* expected, char* actual, - TestDetails const& details) -{ - CheckStringsEqual(results, expected, actual, details); -} - -void CheckEqual(TestResults& results, char* expected, char const* actual, - TestDetails const& details) -{ - CheckStringsEqual(results, expected, actual, details); -} - -void CheckEqual(TestResults& results, char const* expected, char* actual, - TestDetails const& details) -{ - CheckStringsEqual(results, expected, actual, details); -} + namespace { + + void CheckStringsEqual(TestResults& results, char const* expected, char const* actual, + TestDetails const& details) + { + using namespace std; + + if ((expected && actual) ? strcmp(expected, actual) : (expected || actual)) + { + UnitTest::MemoryOutStream stream; + stream << "Expected " << (expected ? expected : "") << " but was " << (actual ? actual : ""); + + results.OnTestFailure(details, stream.GetText()); + } + } + + } + + + void CheckEqual(TestResults& results, char const* expected, char const* actual, + TestDetails const& details) + { + CheckStringsEqual(results, expected, actual, details); + } + + void CheckEqual(TestResults& results, char* expected, char* actual, + TestDetails const& details) + { + CheckStringsEqual(results, expected, actual, details); + } + + void CheckEqual(TestResults& results, char* expected, char const* actual, + TestDetails const& details) + { + CheckStringsEqual(results, expected, actual, details); + } + + void CheckEqual(TestResults& results, char const* expected, char* actual, + TestDetails const& details) + { + CheckStringsEqual(results, expected, actual, details); + } } diff --git a/UnitTest++/Checks.h b/UnitTest++/Checks.h index b2cc4db..5b09768 100644 --- a/UnitTest++/Checks.h +++ b/UnitTest++/Checks.h @@ -8,150 +8,150 @@ namespace UnitTest { -template< typename Value > -bool Check(Value const value) -{ - return !!value; // doing double negative to avoid silly VS warnings -} + template< typename Value > + bool Check(Value const value) + { + return !!value; // doing double negative to avoid silly VS warnings + } -template< typename Expected, typename Actual > -void CheckEqual(TestResults& results, Expected const& expected, Actual const& actual, TestDetails const& details) -{ - if (!(expected == actual)) - { - UnitTest::MemoryOutStream stream; - stream << "Expected " << expected << " but was " << actual; + template< typename Expected, typename Actual > + void CheckEqual(TestResults& results, Expected const& expected, Actual const& actual, TestDetails const& details) + { + if (!(expected == actual)) + { + UnitTest::MemoryOutStream stream; + stream << "Expected " << expected << " but was " << actual; - results.OnTestFailure(details, stream.GetText()); - } -} + results.OnTestFailure(details, stream.GetText()); + } + } -UNITTEST_LINKAGE void CheckEqual(TestResults& results, char const* expected, char const* actual, TestDetails const& details); + UNITTEST_LINKAGE void CheckEqual(TestResults& results, char const* expected, char const* actual, TestDetails const& details); -UNITTEST_LINKAGE void CheckEqual(TestResults& results, char* expected, char* actual, TestDetails const& details); + UNITTEST_LINKAGE void CheckEqual(TestResults& results, char* expected, char* actual, TestDetails const& details); -UNITTEST_LINKAGE void CheckEqual(TestResults& results, char* expected, char const* actual, TestDetails const& details); + UNITTEST_LINKAGE void CheckEqual(TestResults& results, char* expected, char const* actual, TestDetails const& details); -UNITTEST_LINKAGE void CheckEqual(TestResults& results, char const* expected, char* actual, TestDetails const& details); + UNITTEST_LINKAGE void CheckEqual(TestResults& results, char const* expected, char* actual, TestDetails const& details); -template< typename Expected, typename Actual, typename Tolerance > -bool AreClose(Expected const& expected, Actual const& actual, Tolerance const& tolerance) -{ - return (actual >= (expected - tolerance)) && (actual <= (expected + tolerance)); -} + template< typename Expected, typename Actual, typename Tolerance > + bool AreClose(Expected const& expected, Actual const& actual, Tolerance const& tolerance) + { + return (actual >= (expected - tolerance)) && (actual <= (expected + tolerance)); + } -template< typename Expected, typename Actual, typename Tolerance > -void CheckClose(TestResults& results, Expected const& expected, Actual const& actual, Tolerance const& tolerance, - TestDetails const& details) -{ - if (!AreClose(expected, actual, tolerance)) - { - UnitTest::MemoryOutStream stream; - stream << "Expected " << expected << " +/- " << tolerance << " but was " << actual; - - results.OnTestFailure(details, stream.GetText()); - } -} + template< typename Expected, typename Actual, typename Tolerance > + void CheckClose(TestResults& results, Expected const& expected, Actual const& actual, Tolerance const& tolerance, + TestDetails const& details) + { + if (!AreClose(expected, actual, tolerance)) + { + UnitTest::MemoryOutStream stream; + stream << "Expected " << expected << " +/- " << tolerance << " but was " << actual; + results.OnTestFailure(details, stream.GetText()); + } + } -template< typename Expected, typename Actual > -void CheckArrayEqual(TestResults& results, Expected const& expected, Actual const& actual, - int const count, TestDetails const& details) -{ - bool equal = true; - for (int i = 0; i < count; ++i) - equal &= (expected[i] == actual[i]); - if (!equal) - { - UnitTest::MemoryOutStream stream; + template< typename Expected, typename Actual > + void CheckArrayEqual(TestResults& results, Expected const& expected, Actual const& actual, + int const count, TestDetails const& details) + { + bool equal = true; + for (int i = 0; i < count; ++i) + equal &= (expected[i] == actual[i]); - stream << "Expected [ "; + if (!equal) + { + UnitTest::MemoryOutStream stream; - for (int expectedIndex = 0; expectedIndex < count; ++expectedIndex) + stream << "Expected [ "; + + for (int expectedIndex = 0; expectedIndex < count; ++expectedIndex) stream << expected[expectedIndex] << " "; - stream << "] but was [ "; + stream << "] but was [ "; - for (int actualIndex = 0; actualIndex < count; ++actualIndex) + for (int actualIndex = 0; actualIndex < count; ++actualIndex) stream << actual[actualIndex] << " "; - stream << "]"; - - results.OnTestFailure(details, stream.GetText()); - } -} - -template< typename Expected, typename Actual, typename Tolerance > -bool ArrayAreClose(Expected const& expected, Actual const& actual, int const count, Tolerance const& tolerance) -{ - bool equal = true; - for (int i = 0; i < count; ++i) - equal &= AreClose(expected[i], actual[i], tolerance); - return equal; -} - -template< typename Expected, typename Actual, typename Tolerance > -void CheckArrayClose(TestResults& results, Expected const& expected, Actual const& actual, - int const count, Tolerance const& tolerance, TestDetails const& details) -{ - bool equal = ArrayAreClose(expected, actual, count, tolerance); - - if (!equal) - { - UnitTest::MemoryOutStream stream; - - stream << "Expected [ "; - for (int expectedIndex = 0; expectedIndex < count; ++expectedIndex) + stream << "]"; + + results.OnTestFailure(details, stream.GetText()); + } + } + + template< typename Expected, typename Actual, typename Tolerance > + bool ArrayAreClose(Expected const& expected, Actual const& actual, int const count, Tolerance const& tolerance) + { + bool equal = true; + for (int i = 0; i < count; ++i) + equal &= AreClose(expected[i], actual[i], tolerance); + return equal; + } + + template< typename Expected, typename Actual, typename Tolerance > + void CheckArrayClose(TestResults& results, Expected const& expected, Actual const& actual, + int const count, Tolerance const& tolerance, TestDetails const& details) + { + bool equal = ArrayAreClose(expected, actual, count, tolerance); + + if (!equal) + { + UnitTest::MemoryOutStream stream; + + stream << "Expected [ "; + for (int expectedIndex = 0; expectedIndex < count; ++expectedIndex) stream << expected[expectedIndex] << " "; - stream << "] +/- " << tolerance << " but was [ "; + stream << "] +/- " << tolerance << " but was [ "; - for (int actualIndex = 0; actualIndex < count; ++actualIndex) + for (int actualIndex = 0; actualIndex < count; ++actualIndex) stream << actual[actualIndex] << " "; - stream << "]"; + stream << "]"; - results.OnTestFailure(details, stream.GetText()); - } -} + results.OnTestFailure(details, stream.GetText()); + } + } -template< typename Expected, typename Actual, typename Tolerance > -void CheckArray2DClose(TestResults& results, Expected const& expected, Actual const& actual, - int const rows, int const columns, Tolerance const& tolerance, TestDetails const& details) -{ - bool equal = true; - for (int i = 0; i < rows; ++i) - equal &= ArrayAreClose(expected[i], actual[i], columns, tolerance); + template< typename Expected, typename Actual, typename Tolerance > + void CheckArray2DClose(TestResults& results, Expected const& expected, Actual const& actual, + int const rows, int const columns, Tolerance const& tolerance, TestDetails const& details) + { + bool equal = true; + for (int i = 0; i < rows; ++i) + equal &= ArrayAreClose(expected[i], actual[i], columns, tolerance); - if (!equal) - { - UnitTest::MemoryOutStream stream; + if (!equal) + { + UnitTest::MemoryOutStream stream; - stream << "Expected [ "; + stream << "Expected [ "; - for (int expectedRow = 0; expectedRow < rows; ++expectedRow) - { + for (int expectedRow = 0; expectedRow < rows; ++expectedRow) + { stream << "[ "; for (int expectedColumn = 0; expectedColumn < columns; ++expectedColumn) - stream << expected[expectedRow][expectedColumn] << " "; + stream << expected[expectedRow][expectedColumn] << " "; stream << "] "; - } + } - stream << "] +/- " << tolerance << " but was [ "; + stream << "] +/- " << tolerance << " but was [ "; - for (int actualRow = 0; actualRow < rows; ++actualRow) - { + for (int actualRow = 0; actualRow < rows; ++actualRow) + { stream << "[ "; for (int actualColumn = 0; actualColumn < columns; ++actualColumn) - stream << actual[actualRow][actualColumn] << " "; + stream << actual[actualRow][actualColumn] << " "; stream << "] "; - } + } - stream << "]"; + stream << "]"; - results.OnTestFailure(details, stream.GetText()); - } -} + results.OnTestFailure(details, stream.GetText()); + } + } } diff --git a/UnitTest++/CompositeTestReporter.cpp b/UnitTest++/CompositeTestReporter.cpp index 2dd2ffc..53769f0 100644 --- a/UnitTest++/CompositeTestReporter.cpp +++ b/UnitTest++/CompositeTestReporter.cpp @@ -3,65 +3,64 @@ namespace UnitTest { -CompositeTestReporter::CompositeTestReporter() - : m_reporterCount(0) -{ -} + CompositeTestReporter::CompositeTestReporter() + : m_reporterCount(0) + {} -int CompositeTestReporter::GetReporterCount() const -{ - return m_reporterCount; -} + int CompositeTestReporter::GetReporterCount() const + { + return m_reporterCount; + } -bool CompositeTestReporter::AddReporter(TestReporter* reporter) -{ - if (m_reporterCount == kMaxReporters) - return false; + bool CompositeTestReporter::AddReporter(TestReporter* reporter) + { + if (m_reporterCount == kMaxReporters) + return false; - m_reporters[m_reporterCount++] = reporter; - return true; -} + m_reporters[m_reporterCount++] = reporter; + return true; + } -bool CompositeTestReporter::RemoveReporter(TestReporter* reporter) -{ - for (int index = 0; index < m_reporterCount; ++index) - { - if (m_reporters[index] == reporter) - { + bool CompositeTestReporter::RemoveReporter(TestReporter* reporter) + { + for (int index = 0; index < m_reporterCount; ++index) + { + if (m_reporters[index] == reporter) + { m_reporters[index] = m_reporters[m_reporterCount - 1]; - --m_reporterCount; - return true; - } - } + --m_reporterCount; + return true; + } + } - return false; -} + return false; + } -void CompositeTestReporter::ReportFailure(TestDetails const& details, char const* failure) -{ - for (int index = 0; index < m_reporterCount; ++index) - m_reporters[index]->ReportFailure(details, failure); -} + void CompositeTestReporter::ReportFailure(TestDetails const& details, char const* failure) + { + for (int index = 0; index < m_reporterCount; ++index) + m_reporters[index]->ReportFailure(details, failure); + } -void CompositeTestReporter::ReportTestStart(TestDetails const& test) -{ - for (int index = 0; index < m_reporterCount; ++index) - m_reporters[index]->ReportTestStart(test); -} + void CompositeTestReporter::ReportTestStart(TestDetails const& test) + { + for (int index = 0; index < m_reporterCount; ++index) + m_reporters[index]->ReportTestStart(test); + } -void CompositeTestReporter::ReportTestFinish(TestDetails const& test, float secondsElapsed) -{ - for (int index = 0; index < m_reporterCount; ++index) - m_reporters[index]->ReportTestFinish(test, secondsElapsed); -} + void CompositeTestReporter::ReportTestFinish(TestDetails const& test, float secondsElapsed) + { + for (int index = 0; index < m_reporterCount; ++index) + m_reporters[index]->ReportTestFinish(test, secondsElapsed); + } -void CompositeTestReporter::ReportSummary(int totalTestCount, - int failedTestCount, - int failureCount, - float secondsElapsed) -{ - for (int index = 0; index < m_reporterCount; ++index) - m_reporters[index]->ReportSummary(totalTestCount, failedTestCount, failureCount, secondsElapsed); -} + void CompositeTestReporter::ReportSummary(int totalTestCount, + int failedTestCount, + int failureCount, + float secondsElapsed) + { + for (int index = 0; index < m_reporterCount; ++index) + m_reporters[index]->ReportSummary(totalTestCount, failedTestCount, failureCount, secondsElapsed); + } } diff --git a/UnitTest++/CompositeTestReporter.h b/UnitTest++/CompositeTestReporter.h index a9a0d29..c7e1e6b 100644 --- a/UnitTest++/CompositeTestReporter.h +++ b/UnitTest++/CompositeTestReporter.h @@ -5,30 +5,30 @@ namespace UnitTest { -class UNITTEST_LINKAGE CompositeTestReporter : public TestReporter -{ -public: - CompositeTestReporter(); - - int GetReporterCount() const; - bool AddReporter(TestReporter* reporter); - bool RemoveReporter(TestReporter* reporter); - - virtual void ReportTestStart(TestDetails const& test); - virtual void ReportFailure(TestDetails const& test, char const* failure); - virtual void ReportTestFinish(TestDetails const& test, float secondsElapsed); - virtual void ReportSummary(int totalTestCount, int failedTestCount, int failureCount, float secondsElapsed); - -private: - enum { kMaxReporters = 16 }; - TestReporter* m_reporters[kMaxReporters]; - int m_reporterCount; - - // revoked - CompositeTestReporter(const CompositeTestReporter&); - CompositeTestReporter& operator =(const CompositeTestReporter&); -}; + class UNITTEST_LINKAGE CompositeTestReporter : public TestReporter + { + public: + CompositeTestReporter(); + + int GetReporterCount() const; + bool AddReporter(TestReporter* reporter); + bool RemoveReporter(TestReporter* reporter); + + virtual void ReportTestStart(TestDetails const& test); + virtual void ReportFailure(TestDetails const& test, char const* failure); + virtual void ReportTestFinish(TestDetails const& test, float secondsElapsed); + virtual void ReportSummary(int totalTestCount, int failedTestCount, int failureCount, float secondsElapsed); + + private: + enum { kMaxReporters = 16 }; + TestReporter* m_reporters[kMaxReporters]; + int m_reporterCount; + + // revoked + CompositeTestReporter(const CompositeTestReporter&); + CompositeTestReporter& operator =(const CompositeTestReporter&); + }; } -#endif +#endif diff --git a/UnitTest++/Config.h b/UnitTest++/Config.h index 4f695f7..ff62b4e 100644 --- a/UnitTest++/Config.h +++ b/UnitTest++/Config.h @@ -4,35 +4,36 @@ // Standard defines documented here: http://predef.sourceforge.net #if defined(_MSC_VER) - #pragma warning(disable:4702) // unreachable code - #pragma warning(disable:4722) // destructor never returns, potential memory leak - -#if (_MSC_VER == 1200) // VC6 - #define UNITTEST_COMPILER_IS_MSVC6 - #pragma warning(disable:4786) - #pragma warning(disable:4290) - #endif - - #ifdef _USRDLL - #define UNITTEST_WIN32_DLL - #endif - #define UNITTEST_WIN32 + #pragma warning(disable:4702)// unreachable code + #pragma warning(disable:4722)// destructor never returns, potential memory leak + + #if (_MSC_VER == 1200) // VC6 + #define UNITTEST_COMPILER_IS_MSVC6 + #pragma warning(disable:4786) + #pragma warning(disable:4290) + #endif + + #ifdef _USRDLL + #define UNITTEST_WIN32_DLL + #endif + + #define UNITTEST_WIN32 #endif #if defined(unix) || defined(__unix__) || defined(__unix) || defined(linux) || \ - defined(__APPLE__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__) - #define UNITTEST_POSIX + defined(__APPLE__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__) + #define UNITTEST_POSIX #endif #if defined(__MINGW32__) - #define UNITTEST_MINGW + #define UNITTEST_MINGW #endif // By default, MemoryOutStream is implemented in terms of std::ostringstream. // This is useful if you are using the CHECK macros on objects that have something like this defined: // std::ostringstream& operator<<(std::ostringstream& s, const YourObject& value) -// +// // On the other hand, it can be more expensive. // Un-comment this line to use the custom MemoryOutStream (no deps on std::ostringstream). @@ -43,7 +44,7 @@ // well as Google Code HEAD users that may have used or defined // UNITTEST_MEMORYOUTSTREAM_IS_STD_OSTRINGSTREAM outside of this configuration file. #ifndef UNITTEST_USE_CUSTOM_STREAMS - #define UNITTEST_MEMORYOUTSTREAM_IS_STD_OSTRINGSTREAM + #define UNITTEST_MEMORYOUTSTREAM_IS_STD_OSTRINGSTREAM #endif // DeferredTestReporter uses the STL to collect test results for subsequent export by reporters like @@ -55,19 +56,19 @@ // By default, asserts that you report via UnitTest::ReportAssert() abort the current test and // continue to the next one by throwing an exception, which unwinds the stack naturally, destroying -// all auto variables on its way back down. If you don't want to (or can't) use exceptions for your +// all auto variables on its way back down. If you don't want to (or can't) use exceptions for your // platform/compiler, uncomment this line. All exception code will be removed from UnitTest++, // assert recovery will be done via setjmp/longjmp, and NO correct stack unwinding will happen! //#define UNITTEST_NO_EXCEPTIONS -// std namespace qualification: used for functions like strcpy that +// std namespace qualification: used for functions like strcpy that // may live in std:: namespace (cstring header). #if defined( UNITTEST_COMPILER_IS_MSVC6 ) - #define UNIITEST_NS_QUAL_STD(x) x + #define UNIITEST_NS_QUAL_STD(x) x #else - #define UNIITEST_NS_QUAL_STD(x) ::std::x + #define UNIITEST_NS_QUAL_STD(x) ::std::x #endif #endif diff --git a/UnitTest++/CurrentTest.cpp b/UnitTest++/CurrentTest.cpp index dd9d245..b1b1995 100644 --- a/UnitTest++/CurrentTest.cpp +++ b/UnitTest++/CurrentTest.cpp @@ -3,16 +3,16 @@ namespace UnitTest { -UNITTEST_LINKAGE TestResults*& CurrentTest::Results() -{ - static TestResults* testResults = NULL; - return testResults; -} + UNITTEST_LINKAGE TestResults*& CurrentTest::Results() + { + static TestResults* testResults = NULL; + return testResults; + } -UNITTEST_LINKAGE const TestDetails*& CurrentTest::Details() -{ - static const TestDetails* testDetails = NULL; - return testDetails; -} + UNITTEST_LINKAGE const TestDetails*& CurrentTest::Details() + { + static const TestDetails* testDetails = NULL; + return testDetails; + } } diff --git a/UnitTest++/CurrentTest.h b/UnitTest++/CurrentTest.h index 642b9dc..54c40ac 100644 --- a/UnitTest++/CurrentTest.h +++ b/UnitTest++/CurrentTest.h @@ -5,14 +5,14 @@ namespace UnitTest { -class TestResults; -class TestDetails; + class TestResults; + class TestDetails; -namespace CurrentTest -{ - UNITTEST_LINKAGE TestResults*& Results(); - UNITTEST_LINKAGE const TestDetails*& Details(); -} + namespace CurrentTest + { + UNITTEST_LINKAGE TestResults*& Results(); + UNITTEST_LINKAGE const TestDetails*& Details(); + } } diff --git a/UnitTest++/DeferredTestReporter.cpp b/UnitTest++/DeferredTestReporter.cpp index 60e0ded..165e542 100644 --- a/UnitTest++/DeferredTestReporter.cpp +++ b/UnitTest++/DeferredTestReporter.cpp @@ -8,26 +8,26 @@ using namespace UnitTest; void DeferredTestReporter::ReportTestStart(TestDetails const& details) { - m_results.push_back(DeferredTestResult(details.suiteName, details.testName)); + m_results.push_back(DeferredTestResult(details.suiteName, details.testName)); } void DeferredTestReporter::ReportFailure(TestDetails const& details, char const* failure) { - DeferredTestResult& r = m_results.back(); - r.failed = true; - r.failures.push_back(DeferredTestFailure(details.lineNumber, failure)); - r.failureFile = details.filename; + DeferredTestResult& r = m_results.back(); + r.failed = true; + r.failures.push_back(DeferredTestFailure(details.lineNumber, failure)); + r.failureFile = details.filename; } void DeferredTestReporter::ReportTestFinish(TestDetails const&, float secondsElapsed) { - DeferredTestResult& r = m_results.back(); - r.timeElapsed = secondsElapsed; + DeferredTestResult& r = m_results.back(); + r.timeElapsed = secondsElapsed; } DeferredTestReporter::DeferredTestResultList& DeferredTestReporter::GetResults() { - return m_results; + return m_results; } #endif diff --git a/UnitTest++/DeferredTestReporter.h b/UnitTest++/DeferredTestReporter.h index 757e53f..51ac866 100644 --- a/UnitTest++/DeferredTestReporter.h +++ b/UnitTest++/DeferredTestReporter.h @@ -15,19 +15,19 @@ UNITTEST_STDVECTOR_LINKAGE(UnitTest::DeferredTestResult); namespace UnitTest { -class UNITTEST_LINKAGE DeferredTestReporter : public TestReporter -{ -public: - virtual void ReportTestStart(TestDetails const& details); - virtual void ReportFailure(TestDetails const& details, char const* failure); - virtual void ReportTestFinish(TestDetails const& details, float secondsElapsed); - - typedef std::vector< DeferredTestResult > DeferredTestResultList; - DeferredTestResultList& GetResults(); - -private: - DeferredTestResultList m_results; -}; + class UNITTEST_LINKAGE DeferredTestReporter : public TestReporter + { + public: + virtual void ReportTestStart(TestDetails const& details); + virtual void ReportFailure(TestDetails const& details, char const* failure); + virtual void ReportTestFinish(TestDetails const& details, float secondsElapsed); + + typedef std::vector< DeferredTestResult > DeferredTestResultList; + DeferredTestResultList& GetResults(); + + private: + DeferredTestResultList m_results; + }; } diff --git a/UnitTest++/DeferredTestResult.cpp b/UnitTest++/DeferredTestResult.cpp index cdad9eb..9a3ee93 100644 --- a/UnitTest++/DeferredTestResult.cpp +++ b/UnitTest++/DeferredTestResult.cpp @@ -7,39 +7,36 @@ namespace UnitTest { -DeferredTestFailure::DeferredTestFailure() - : lineNumber(-1) -{ - failureStr[0] = '\0'; -} - -DeferredTestFailure::DeferredTestFailure(int lineNumber_, const char* failureStr_) - : lineNumber(lineNumber_) -{ - UNIITEST_NS_QUAL_STD(strcpy)(failureStr, failureStr_); -} - -DeferredTestResult::DeferredTestResult() - : suiteName("") - , testName("") - , failureFile("") - , timeElapsed(0.0f) - , failed(false) -{ -} - -DeferredTestResult::DeferredTestResult(char const* suite, char const* test) - : suiteName(suite) - , testName(test) - , failureFile("") - , timeElapsed(0.0f) - , failed(false) -{ -} - -DeferredTestResult::~DeferredTestResult() -{ -} + DeferredTestFailure::DeferredTestFailure() + : lineNumber(-1) + { + failureStr[0] = '\0'; + } + + DeferredTestFailure::DeferredTestFailure(int lineNumber_, const char* failureStr_) + : lineNumber(lineNumber_) + { + UNIITEST_NS_QUAL_STD(strcpy)(failureStr, failureStr_); + } + + DeferredTestResult::DeferredTestResult() + : suiteName("") + , testName("") + , failureFile("") + , timeElapsed(0.0f) + , failed(false) + {} + + DeferredTestResult::DeferredTestResult(char const* suite, char const* test) + : suiteName(suite) + , testName(test) + , failureFile("") + , timeElapsed(0.0f) + , failed(false) + {} + + DeferredTestResult::~DeferredTestResult() + {} } diff --git a/UnitTest++/DeferredTestResult.h b/UnitTest++/DeferredTestResult.h index 0929566..e0d74ef 100644 --- a/UnitTest++/DeferredTestResult.h +++ b/UnitTest++/DeferredTestResult.h @@ -11,15 +11,15 @@ namespace UnitTest { -class UNITTEST_LINKAGE DeferredTestFailure -{ -public: - DeferredTestFailure(); - DeferredTestFailure(int lineNumber_, const char* failureStr_); + class UNITTEST_LINKAGE DeferredTestFailure + { + public: + DeferredTestFailure(); + DeferredTestFailure(int lineNumber_, const char* failureStr_); - int lineNumber; - char failureStr[1024]; -}; + int lineNumber; + char failureStr[1024]; + }; } @@ -28,23 +28,23 @@ UNITTEST_STDVECTOR_LINKAGE(UnitTest::DeferredTestFailure); namespace UnitTest { -class UNITTEST_LINKAGE DeferredTestResult -{ -public: - DeferredTestResult(); - DeferredTestResult(char const* suite, char const* test); - ~DeferredTestResult(); - - std::string suiteName; - std::string testName; - std::string failureFile; - - typedef std::vector< DeferredTestFailure > FailureVec; - FailureVec failures; - - float timeElapsed; - bool failed; -}; + class UNITTEST_LINKAGE DeferredTestResult + { + public: + DeferredTestResult(); + DeferredTestResult(char const* suite, char const* test); + ~DeferredTestResult(); + + std::string suiteName; + std::string testName; + std::string failureFile; + + typedef std::vector< DeferredTestFailure > FailureVec; + FailureVec failures; + + float timeElapsed; + bool failed; + }; } diff --git a/UnitTest++/ExceptionMacros.h b/UnitTest++/ExceptionMacros.h index e549c2c..ca8757e 100644 --- a/UnitTest++/ExceptionMacros.h +++ b/UnitTest++/ExceptionMacros.h @@ -4,15 +4,15 @@ #include "Config.h" #ifndef UNITTEST_NO_EXCEPTIONS - #define UT_TRY(x) try x - #define UT_THROW(x) throw x - #define UT_CATCH(ExceptionType, ExceptionName, CatchBody) catch(ExceptionType& ExceptionName) CatchBody - #define UT_CATCH_ALL(CatchBody) catch(...) CatchBody + #define UT_TRY(x) try x + #define UT_THROW(x) throw x + #define UT_CATCH(ExceptionType, ExceptionName, CatchBody) catch(ExceptionType& ExceptionName) CatchBody + #define UT_CATCH_ALL(CatchBody) catch(...) CatchBody #else - #define UT_TRY(x) x - #define UT_THROW(x) - #define UT_CATCH(ExceptionType, ExceptionName, CatchBody) - #define UT_CATCH_ALL(CatchBody) + #define UT_TRY(x) x + #define UT_THROW(x) + #define UT_CATCH(ExceptionType, ExceptionName, CatchBody) + #define UT_CATCH_ALL(CatchBody) #endif #endif diff --git a/UnitTest++/ExecuteTest.h b/UnitTest++/ExecuteTest.h index d683dc0..c6917fc 100644 --- a/UnitTest++/ExecuteTest.h +++ b/UnitTest++/ExecuteTest.h @@ -10,49 +10,49 @@ #include "CurrentTest.h" #ifdef UNITTEST_NO_EXCEPTIONS - #include "ReportAssertImpl.h" +#include "ReportAssertImpl.h" #endif #ifdef UNITTEST_POSIX - #include "Posix/SignalTranslator.h" +#include "Posix/SignalTranslator.h" #endif namespace UnitTest { -template< typename T > -void ExecuteTest(T& testObject, TestDetails const& details, bool isMockTest) -{ - if (isMockTest == false) - CurrentTest::Details() = &details; + template< typename T > + void ExecuteTest(T& testObject, TestDetails const& details, bool isMockTest) + { + if (isMockTest == false) + CurrentTest::Details() = &details; #ifdef UNITTEST_NO_EXCEPTIONS - if (UNITTEST_SET_ASSERT_JUMP_TARGET() == 0) - { + if (UNITTEST_SET_ASSERT_JUMP_TARGET() == 0) + { #endif #ifndef UNITTEST_POSIX - UT_TRY({ testObject.RunImpl(); }) + UT_TRY({ testObject.RunImpl(); }) #else - UT_TRY - ({ - UNITTEST_THROW_SIGNALS_POSIX_ONLY - testObject.RunImpl(); - }) + UT_TRY + ({ + UNITTEST_THROW_SIGNALS_POSIX_ONLY + testObject.RunImpl(); + }) #endif - UT_CATCH(AssertException, e, { (void)e; }) - UT_CATCH(std::exception, e, - { - MemoryOutStream stream; - stream << "Unhandled exception: " << e.what(); - CurrentTest::Results()->OnTestFailure(details, stream.GetText()); - }) - UT_CATCH_ALL - ({ - CurrentTest::Results()->OnTestFailure(details, "Unhandled exception: test crashed"); - }) + UT_CATCH(AssertException, e, { (void)e; }) + UT_CATCH(std::exception, e, + { + MemoryOutStream stream; + stream << "Unhandled exception: " << e.what(); + CurrentTest::Results()->OnTestFailure(details, stream.GetText()); + }) + UT_CATCH_ALL + ({ + CurrentTest::Results()->OnTestFailure(details, "Unhandled exception: test crashed"); + }) #ifdef UNITTEST_NO_EXCEPTIONS - } + } #endif -} + } } diff --git a/UnitTest++/HelperMacros.h b/UnitTest++/HelperMacros.h index 699d99d..6d72f22 100644 --- a/UnitTest++/HelperMacros.h +++ b/UnitTest++/HelperMacros.h @@ -6,47 +6,47 @@ #define UNITTEST_MULTILINE_MACRO_BEGIN do { #if defined(UNITTEST_WIN32) && !defined(UNITTEST_COMPILER_IS_MSVC6) - #define UNITTEST_MULTILINE_MACRO_END \ - } __pragma(warning(push)) __pragma(warning(disable:4127)) while (0) __pragma(warning(pop)) + #define UNITTEST_MULTILINE_MACRO_END \ + } __pragma(warning(push)) __pragma(warning(disable: 4127)) while (0) __pragma(warning(pop)) #else - #define UNITTEST_MULTILINE_MACRO_END } while(0) + #define UNITTEST_MULTILINE_MACRO_END } while(0) #endif #ifdef UNITTEST_WIN32_DLL - #define UNITTEST_IMPORT __declspec(dllimport) - #define UNITTEST_EXPORT __declspec(dllexport) - - #ifdef UNITTEST_DLL_EXPORT - #define UNITTEST_LINKAGE UNITTEST_EXPORT - #define UNITTEST_IMPEXP_TEMPLATE - #else - #define UNITTEST_LINKAGE UNITTEST_IMPORT - #define UNITTEST_IMPEXP_TEMPLATE extern - #endif - - #define UNITTEST_STDVECTOR_LINKAGE(T) \ - __pragma(warning(push)) \ - __pragma(warning(disable:4231)) \ - UNITTEST_IMPEXP_TEMPLATE template class UNITTEST_LINKAGE std::allocator< T >; \ - UNITTEST_IMPEXP_TEMPLATE template class UNITTEST_LINKAGE std::vector< T >; \ - __pragma(warning(pop)) + #define UNITTEST_IMPORT __declspec(dllimport) + #define UNITTEST_EXPORT __declspec(dllexport) + + #ifdef UNITTEST_DLL_EXPORT + #define UNITTEST_LINKAGE UNITTEST_EXPORT + #define UNITTEST_IMPEXP_TEMPLATE + #else + #define UNITTEST_LINKAGE UNITTEST_IMPORT + #define UNITTEST_IMPEXP_TEMPLATE extern + #endif + + #define UNITTEST_STDVECTOR_LINKAGE(T) \ + __pragma(warning(push)) \ + __pragma(warning(disable: 4231)) \ + UNITTEST_IMPEXP_TEMPLATE template class UNITTEST_LINKAGE std::allocator< T >; \ + UNITTEST_IMPEXP_TEMPLATE template class UNITTEST_LINKAGE std::vector< T >; \ + __pragma(warning(pop)) #else - #define UNITTEST_IMPORT - #define UNITTEST_EXPORT - #define UNITTEST_LINKAGE - #define UNITTEST_IMPEXP_TEMPLATE - #define UNITTEST_STDVECTOR_LINKAGE(T) + #define UNITTEST_IMPORT + #define UNITTEST_EXPORT + #define UNITTEST_LINKAGE + #define UNITTEST_IMPEXP_TEMPLATE + #define UNITTEST_STDVECTOR_LINKAGE(T) #endif #ifdef UNITTEST_WIN32 - #define UNITTEST_JMPBUF jmp_buf - #define UNITTEST_SETJMP setjmp - #define UNITTEST_LONGJMP longjmp + #define UNITTEST_JMPBUF jmp_buf + #define UNITTEST_SETJMP setjmp + #define UNITTEST_LONGJMP longjmp #elif defined UNITTEST_POSIX - #define UNITTEST_JMPBUF std::jmp_buf - #define UNITTEST_SETJMP setjmp - #define UNITTEST_LONGJMP std::longjmp + #define UNITTEST_JMPBUF std::jmp_buf + #define UNITTEST_SETJMP setjmp + #define UNITTEST_LONGJMP std::longjmp #endif #endif diff --git a/UnitTest++/MemoryOutStream.cpp b/UnitTest++/MemoryOutStream.cpp index 319dfda..98e52e6 100644 --- a/UnitTest++/MemoryOutStream.cpp +++ b/UnitTest++/MemoryOutStream.cpp @@ -4,42 +4,42 @@ namespace UnitTest { -char const* MemoryOutStream::GetText() const -{ - m_text = this->str(); - return m_text.c_str(); -} - -void MemoryOutStream::Clear() -{ - this->str(std::string()); - m_text = this->str(); -} + char const* MemoryOutStream::GetText() const + { + m_text = this->str(); + return m_text.c_str(); + } + + void MemoryOutStream::Clear() + { + this->str(std::string()); + m_text = this->str(); + } #ifdef UNITTEST_COMPILER_IS_MSVC6 -#define snprintf _snprintf + #define snprintf _snprintf -template -std::ostream& FormatToStream(std::ostream& stream, char const* format, ValueType const& value) -{ - using namespace std; - - const size_t BUFFER_SIZE=32; - char txt[BUFFER_SIZE]; - snprintf(txt, BUFFER_SIZE, format, value); - return stream << txt; -} + template + std::ostream& FormatToStream(std::ostream& stream, char const* format, ValueType const& value) + { + using namespace std; -std::ostream& operator<<(std::ostream& stream, __int64 const n) -{ - return FormatToStream(stream, "%I64d", n); -} + const size_t BUFFER_SIZE=32; + char txt[BUFFER_SIZE]; + snprintf(txt, BUFFER_SIZE, format, value); + return stream << txt; + } -std::ostream& operator<<(std::ostream& stream, unsigned __int64 const n) -{ - return FormatToStream(stream, "%I64u", n); -} + std::ostream& operator<<(std::ostream& stream, __int64 const n) + { + return FormatToStream(stream, "%I64d", n); + } + + std::ostream& operator<<(std::ostream& stream, unsigned __int64 const n) + { + return FormatToStream(stream, "%I64u", n); + } #endif @@ -56,161 +56,161 @@ std::ostream& operator<<(std::ostream& stream, unsigned __int64 const n) namespace UnitTest { -namespace { - -template -void FormatToStream(MemoryOutStream& stream, char const* format, ValueType const& value) -{ - using namespace std; - - const size_t BUFFER_SIZE=32; - char txt[BUFFER_SIZE]; - snprintf(txt, BUFFER_SIZE, format, value); - stream << txt; -} - -int RoundUpToMultipleOfPow2Number (int n, int pow2Number) -{ - return (n + (pow2Number - 1)) & ~(pow2Number - 1); -} - -} - - -MemoryOutStream::MemoryOutStream(int const size) - : m_capacity (0) - , m_buffer (0) - -{ - GrowBuffer(size); -} - -MemoryOutStream::~MemoryOutStream() -{ - delete [] m_buffer; -} - -void MemoryOutStream::Clear() -{ - m_buffer[0] = '\0'; -} - -char const* MemoryOutStream::GetText() const -{ - return m_buffer; -} - -MemoryOutStream& MemoryOutStream::operator <<(char const* txt) -{ - using namespace std; - - int const bytesLeft = m_capacity - (int)strlen(m_buffer); - int const bytesRequired = (int)strlen(txt) + 1; - - if (bytesRequired > bytesLeft) - { - int const requiredCapacity = bytesRequired + m_capacity - bytesLeft; - GrowBuffer(requiredCapacity); - } - - strcat(m_buffer, txt); - return *this; -} - -MemoryOutStream& MemoryOutStream::operator <<(int const n) -{ - FormatToStream(*this, "%i", n); - return *this; -} - -MemoryOutStream& MemoryOutStream::operator <<(long const n) -{ - FormatToStream(*this, "%li", n); - return *this; -} - -MemoryOutStream& MemoryOutStream::operator <<(unsigned long const n) -{ - FormatToStream(*this, "%lu", n); - return *this; -} + namespace { + + template + void FormatToStream(MemoryOutStream& stream, char const* format, ValueType const& value) + { + using namespace std; + + const size_t BUFFER_SIZE=32; + char txt[BUFFER_SIZE]; + snprintf(txt, BUFFER_SIZE, format, value); + stream << txt; + } + + int RoundUpToMultipleOfPow2Number (int n, int pow2Number) + { + return (n + (pow2Number - 1)) & ~(pow2Number - 1); + } + + } + + + MemoryOutStream::MemoryOutStream(int const size) + : m_capacity (0) + , m_buffer (0) + + { + GrowBuffer(size); + } + + MemoryOutStream::~MemoryOutStream() + { + delete [] m_buffer; + } + + void MemoryOutStream::Clear() + { + m_buffer[0] = '\0'; + } + + char const* MemoryOutStream::GetText() const + { + return m_buffer; + } + + MemoryOutStream& MemoryOutStream::operator <<(char const* txt) + { + using namespace std; + + int const bytesLeft = m_capacity - (int)strlen(m_buffer); + int const bytesRequired = (int)strlen(txt) + 1; + + if (bytesRequired > bytesLeft) + { + int const requiredCapacity = bytesRequired + m_capacity - bytesLeft; + GrowBuffer(requiredCapacity); + } + + strcat(m_buffer, txt); + return *this; + } + + MemoryOutStream& MemoryOutStream::operator <<(int const n) + { + FormatToStream(*this, "%i", n); + return *this; + } + + MemoryOutStream& MemoryOutStream::operator <<(long const n) + { + FormatToStream(*this, "%li", n); + return *this; + } + + MemoryOutStream& MemoryOutStream::operator <<(unsigned long const n) + { + FormatToStream(*this, "%lu", n); + return *this; + } #ifdef UNITTEST_COMPILER_IS_MSVC6 -MemoryOutStream& MemoryOutStream::operator <<(__int64 const n) + MemoryOutStream& MemoryOutStream::operator <<(__int64 const n) #else -MemoryOutStream& MemoryOutStream::operator <<(long long const n) + MemoryOutStream& MemoryOutStream::operator <<(long long const n) #endif -{ + { #ifdef UNITTEST_WIN32 - FormatToStream(*this, "%I64d", n); + FormatToStream(*this, "%I64d", n); #else - FormatToStream(*this, "%lld", n); + FormatToStream(*this, "%lld", n); #endif - return *this; -} + return *this; + } #ifdef UNITTEST_COMPILER_IS_MSVC6 -MemoryOutStream& MemoryOutStream::operator <<(unsigned __int64 const n) + MemoryOutStream& MemoryOutStream::operator <<(unsigned __int64 const n) #else -MemoryOutStream& MemoryOutStream::operator <<(unsigned long long const n) + MemoryOutStream& MemoryOutStream::operator <<(unsigned long long const n) #endif -{ + { #ifdef UNITTEST_WIN32 - FormatToStream(*this, "%I64u", n); + FormatToStream(*this, "%I64u", n); #else - FormatToStream(*this, "%llu", n); + FormatToStream(*this, "%llu", n); #endif - return *this; -} - -MemoryOutStream& MemoryOutStream::operator <<(float const f) -{ - FormatToStream(*this, "%0.6f", f); - return *this; -} - -MemoryOutStream& MemoryOutStream::operator <<(void const* p) -{ - FormatToStream(*this, "%p", p); - return *this; -} - -MemoryOutStream& MemoryOutStream::operator <<(unsigned int const s) -{ - FormatToStream(*this, "%u", s); - return *this; -} - -MemoryOutStream& MemoryOutStream::operator <<(double const d) -{ - FormatToStream(*this, "%0.6f", d); - return *this; -} - -int MemoryOutStream::GetCapacity() const -{ - return m_capacity; -} - - -void MemoryOutStream::GrowBuffer(int const desiredCapacity) -{ - int const newCapacity = RoundUpToMultipleOfPow2Number(desiredCapacity, GROW_CHUNK_SIZE); - - using namespace std; - - char* buffer = new char[newCapacity]; - if (m_buffer) - strcpy(buffer, m_buffer); - else - strcpy(buffer, ""); - - delete [] m_buffer; - m_buffer = buffer; - m_capacity = newCapacity; -} + return *this; + } + + MemoryOutStream& MemoryOutStream::operator <<(float const f) + { + FormatToStream(*this, "%0.6f", f); + return *this; + } + + MemoryOutStream& MemoryOutStream::operator <<(void const* p) + { + FormatToStream(*this, "%p", p); + return *this; + } + + MemoryOutStream& MemoryOutStream::operator <<(unsigned int const s) + { + FormatToStream(*this, "%u", s); + return *this; + } + + MemoryOutStream& MemoryOutStream::operator <<(double const d) + { + FormatToStream(*this, "%0.6f", d); + return *this; + } + + int MemoryOutStream::GetCapacity() const + { + return m_capacity; + } + + + void MemoryOutStream::GrowBuffer(int const desiredCapacity) + { + int const newCapacity = RoundUpToMultipleOfPow2Number(desiredCapacity, GROW_CHUNK_SIZE); + + using namespace std; + + char* buffer = new char[newCapacity]; + if (m_buffer) + strcpy(buffer, m_buffer); + else + strcpy(buffer, ""); + + delete [] m_buffer; + m_buffer = buffer; + m_capacity = newCapacity; + } } diff --git a/UnitTest++/MemoryOutStream.h b/UnitTest++/MemoryOutStream.h index b9cea19..8a18721 100644 --- a/UnitTest++/MemoryOutStream.h +++ b/UnitTest++/MemoryOutStream.h @@ -11,24 +11,24 @@ namespace UnitTest { -class UNITTEST_LINKAGE MemoryOutStream : public std::ostringstream -{ -public: - MemoryOutStream() {} - ~MemoryOutStream() {} - void Clear(); - char const* GetText() const; + class UNITTEST_LINKAGE MemoryOutStream : public std::ostringstream + { + public: + MemoryOutStream() {} + ~MemoryOutStream() {} + void Clear(); + char const* GetText() const; -private: - MemoryOutStream(MemoryOutStream const&); - void operator =(MemoryOutStream const&); + private: + MemoryOutStream(MemoryOutStream const&); + void operator =(MemoryOutStream const&); - mutable std::string m_text; -}; + mutable std::string m_text; + }; #ifdef UNITTEST_COMPILER_IS_MSVC6 -std::ostream& operator<<(std::ostream& stream, __int64 const n); -std::ostream& operator<<(std::ostream& stream, unsigned __int64 const n); + std::ostream& operator<<(std::ostream& stream, __int64 const n); + std::ostream& operator<<(std::ostream& stream, unsigned __int64 const n); #endif } @@ -44,41 +44,41 @@ namespace std {} namespace UnitTest { -class UNITTEST_LINKAGE MemoryOutStream -{ -public: - explicit MemoryOutStream(int const size = 256); - ~MemoryOutStream(); + class UNITTEST_LINKAGE MemoryOutStream + { + public: + explicit MemoryOutStream(int const size = 256); + ~MemoryOutStream(); - void Clear(); - char const* GetText() const; + void Clear(); + char const* GetText() const; - MemoryOutStream& operator <<(char const* txt); - MemoryOutStream& operator <<(int n); - MemoryOutStream& operator <<(long n); - MemoryOutStream& operator <<(unsigned long n); + MemoryOutStream& operator <<(char const* txt); + MemoryOutStream& operator <<(int n); + MemoryOutStream& operator <<(long n); + MemoryOutStream& operator <<(unsigned long n); #ifdef UNITTEST_COMPILER_IS_MSVC6 - MemoryOutStream& operator <<(__int64 n); - MemoryOutStream& operator <<(unsigned __int64 n); + MemoryOutStream& operator <<(__int64 n); + MemoryOutStream& operator <<(unsigned __int64 n); #else - MemoryOutStream& operator <<(long long n); - MemoryOutStream& operator <<(unsigned long long n); + MemoryOutStream& operator <<(long long n); + MemoryOutStream& operator <<(unsigned long long n); #endif - MemoryOutStream& operator <<(float f); - MemoryOutStream& operator <<(double d); - MemoryOutStream& operator <<(void const* p); - MemoryOutStream& operator <<(unsigned int s); + MemoryOutStream& operator <<(float f); + MemoryOutStream& operator <<(double d); + MemoryOutStream& operator <<(void const* p); + MemoryOutStream& operator <<(unsigned int s); - enum { GROW_CHUNK_SIZE = 32 }; - int GetCapacity() const; + enum { GROW_CHUNK_SIZE = 32 }; + int GetCapacity() const; -private: - void operator= (MemoryOutStream const&); - void GrowBuffer(int capacity); + private: + void operator= (MemoryOutStream const&); + void GrowBuffer(int capacity); - int m_capacity; - char* m_buffer; -}; + int m_capacity; + char* m_buffer; + }; } diff --git a/UnitTest++/Posix/SignalTranslator.cpp b/UnitTest++/Posix/SignalTranslator.cpp index 3689c8c..4039896 100644 --- a/UnitTest++/Posix/SignalTranslator.cpp +++ b/UnitTest++/Posix/SignalTranslator.cpp @@ -2,45 +2,45 @@ namespace UnitTest { -sigjmp_buf* SignalTranslator::s_jumpTarget = 0; + sigjmp_buf* SignalTranslator::s_jumpTarget = 0; -namespace { + namespace { -void SignalHandler(int sig) -{ - siglongjmp(*SignalTranslator::s_jumpTarget, sig ); -} + void SignalHandler(int sig) + { + siglongjmp(*SignalTranslator::s_jumpTarget, sig ); + } -} + } -SignalTranslator::SignalTranslator() -{ - m_oldJumpTarget = s_jumpTarget; - s_jumpTarget = &m_currentJumpTarget; + SignalTranslator::SignalTranslator() + { + m_oldJumpTarget = s_jumpTarget; + s_jumpTarget = &m_currentJumpTarget; - struct sigaction action; - action.sa_flags = 0; - action.sa_handler = SignalHandler; - sigemptyset( &action.sa_mask ); + struct sigaction action; + action.sa_flags = 0; + action.sa_handler = SignalHandler; + sigemptyset( &action.sa_mask ); - sigaction( SIGSEGV, &action, &m_old_SIGSEGV_action ); - sigaction( SIGFPE , &action, &m_old_SIGFPE_action ); - sigaction( SIGTRAP, &action, &m_old_SIGTRAP_action ); - sigaction( SIGBUS , &action, &m_old_SIGBUS_action ); - sigaction( SIGILL , &action, &m_old_SIGBUS_action ); -} + sigaction( SIGSEGV, &action, &m_old_SIGSEGV_action ); + sigaction( SIGFPE, &action, &m_old_SIGFPE_action ); + sigaction( SIGTRAP, &action, &m_old_SIGTRAP_action ); + sigaction( SIGBUS, &action, &m_old_SIGBUS_action ); + sigaction( SIGILL, &action, &m_old_SIGBUS_action ); + } -SignalTranslator::~SignalTranslator() -{ - sigaction( SIGILL , &m_old_SIGBUS_action , 0 ); - sigaction( SIGBUS , &m_old_SIGBUS_action , 0 ); - sigaction( SIGTRAP, &m_old_SIGTRAP_action, 0 ); - sigaction( SIGFPE , &m_old_SIGFPE_action , 0 ); - sigaction( SIGSEGV, &m_old_SIGSEGV_action, 0 ); + SignalTranslator::~SignalTranslator() + { + sigaction( SIGILL, &m_old_SIGBUS_action, 0 ); + sigaction( SIGBUS, &m_old_SIGBUS_action, 0 ); + sigaction( SIGTRAP, &m_old_SIGTRAP_action, 0 ); + sigaction( SIGFPE, &m_old_SIGFPE_action, 0 ); + sigaction( SIGSEGV, &m_old_SIGSEGV_action, 0 ); - s_jumpTarget = m_oldJumpTarget; -} + s_jumpTarget = m_oldJumpTarget; + } } diff --git a/UnitTest++/Posix/SignalTranslator.h b/UnitTest++/Posix/SignalTranslator.h index 50da5f9..efb3618 100644 --- a/UnitTest++/Posix/SignalTranslator.h +++ b/UnitTest++/Posix/SignalTranslator.h @@ -6,36 +6,36 @@ namespace UnitTest { -class SignalTranslator -{ -public: - SignalTranslator(); - ~SignalTranslator(); - - static sigjmp_buf* s_jumpTarget; - -private: - sigjmp_buf m_currentJumpTarget; - sigjmp_buf* m_oldJumpTarget; - - struct sigaction m_old_SIGFPE_action; - struct sigaction m_old_SIGTRAP_action; - struct sigaction m_old_SIGSEGV_action; - struct sigaction m_old_SIGBUS_action; - struct sigaction m_old_SIGABRT_action; - struct sigaction m_old_SIGALRM_action; -}; + class SignalTranslator + { + public: + SignalTranslator(); + ~SignalTranslator(); + + static sigjmp_buf* s_jumpTarget; + + private: + sigjmp_buf m_currentJumpTarget; + sigjmp_buf* m_oldJumpTarget; + + struct sigaction m_old_SIGFPE_action; + struct sigaction m_old_SIGTRAP_action; + struct sigaction m_old_SIGSEGV_action; + struct sigaction m_old_SIGBUS_action; + struct sigaction m_old_SIGABRT_action; + struct sigaction m_old_SIGALRM_action; + }; #if !defined (__GNUC__) - #define UNITTEST_EXTENSION + #define UNITTEST_EXTENSION #else - #define UNITTEST_EXTENSION __extension__ + #define UNITTEST_EXTENSION __extension__ #endif -#define UNITTEST_THROW_SIGNALS_POSIX_ONLY \ - UnitTest::SignalTranslator sig; \ - if (UNITTEST_EXTENSION sigsetjmp(*UnitTest::SignalTranslator::s_jumpTarget, 1) != 0) \ - throw ("Unhandled system exception"); + #define UNITTEST_THROW_SIGNALS_POSIX_ONLY \ + UnitTest::SignalTranslator sig; \ + if (UNITTEST_EXTENSION sigsetjmp(*UnitTest::SignalTranslator::s_jumpTarget, 1) != 0) \ + throw ("Unhandled system exception"); } diff --git a/UnitTest++/Posix/TimeHelpers.cpp b/UnitTest++/Posix/TimeHelpers.cpp index f977ca7..d9a78eb 100644 --- a/UnitTest++/Posix/TimeHelpers.cpp +++ b/UnitTest++/Posix/TimeHelpers.cpp @@ -3,31 +3,31 @@ namespace UnitTest { -Timer::Timer() -{ - m_startTime.tv_sec = 0; - m_startTime.tv_usec = 0; -} - -void Timer::Start() -{ - gettimeofday(&m_startTime, 0); -} - -double Timer::GetTimeInMs() const -{ - struct timeval currentTime; - gettimeofday(¤tTime, 0); - - double const dsecs = currentTime.tv_sec - m_startTime.tv_sec; - double const dus = currentTime.tv_usec - m_startTime.tv_usec; - - return (dsecs * 1000.0) + (dus / 1000.0); -} - -void TimeHelpers::SleepMs(int ms) -{ - usleep(static_cast(ms * 1000)); -} + Timer::Timer() + { + m_startTime.tv_sec = 0; + m_startTime.tv_usec = 0; + } + + void Timer::Start() + { + gettimeofday(&m_startTime, 0); + } + + double Timer::GetTimeInMs() const + { + struct timeval currentTime; + gettimeofday(¤tTime, 0); + + double const dsecs = currentTime.tv_sec - m_startTime.tv_sec; + double const dus = currentTime.tv_usec - m_startTime.tv_usec; + + return (dsecs * 1000.0) + (dus / 1000.0); + } + + void TimeHelpers::SleepMs(int ms) + { + usleep(static_cast(ms * 1000)); + } } diff --git a/UnitTest++/Posix/TimeHelpers.h b/UnitTest++/Posix/TimeHelpers.h index 0de4b1a..630260d 100644 --- a/UnitTest++/Posix/TimeHelpers.h +++ b/UnitTest++/Posix/TimeHelpers.h @@ -5,22 +5,22 @@ namespace UnitTest { -class Timer -{ -public: - Timer(); - void Start(); - double GetTimeInMs() const; - -private: - struct timeval m_startTime; -}; - - -namespace TimeHelpers -{ - void SleepMs(int ms); -} + class Timer + { + public: + Timer(); + void Start(); + double GetTimeInMs() const; + + private: + struct timeval m_startTime; + }; + + + namespace TimeHelpers + { + void SleepMs(int ms); + } } diff --git a/UnitTest++/ReportAssert.cpp b/UnitTest++/ReportAssert.cpp index 7a9a63a..47d0c5f 100644 --- a/UnitTest++/ReportAssert.cpp +++ b/UnitTest++/ReportAssert.cpp @@ -6,65 +6,66 @@ #include "TestDetails.h" #ifdef UNITTEST_NO_EXCEPTIONS - #include "ReportAssertImpl.h" + #include "ReportAssertImpl.h" #endif namespace UnitTest { -namespace -{ - bool& AssertExpectedFlag() - { - static bool s_assertExpected = false; - return s_assertExpected; - } -} + namespace + { + bool& AssertExpectedFlag() + { + static bool s_assertExpected = false; + return s_assertExpected; + } + } -UNITTEST_LINKAGE void ReportAssert(char const* description, char const* filename, int lineNumber) -{ - Detail::ReportAssertEx(CurrentTest::Results(), CurrentTest::Details(), - description, filename, lineNumber); -} + UNITTEST_LINKAGE void ReportAssert(char const* description, char const* filename, int lineNumber) + { + Detail::ReportAssertEx(CurrentTest::Results(), CurrentTest::Details(), + description, filename, lineNumber); + } -namespace Detail { + namespace Detail { #ifdef UNITTEST_NO_EXCEPTIONS -UNITTEST_JMPBUF* GetAssertJmpBuf() -{ - static UNITTEST_JMPBUF s_jmpBuf; - return &s_jmpBuf; -} + UNITTEST_JMPBUF* GetAssertJmpBuf() + { + static UNITTEST_JMPBUF s_jmpBuf; + return &s_jmpBuf; + } #endif -UNITTEST_LINKAGE void ReportAssertEx(TestResults* testResults, - const TestDetails* testDetails, - char const* description, - char const* filename, - int lineNumber) -{ - if (AssertExpectedFlag() == false) - { - TestDetails assertDetails(testDetails->testName, testDetails->suiteName, filename, lineNumber); - testResults->OnTestFailure(assertDetails, description); - } + UNITTEST_LINKAGE void ReportAssertEx(TestResults* testResults, + const TestDetails* testDetails, + char const* description, + char const* filename, + int lineNumber) + { + if (AssertExpectedFlag() == false) + { + TestDetails assertDetails(testDetails->testName, testDetails->suiteName, filename, lineNumber); + testResults->OnTestFailure(assertDetails, description); + } - ExpectAssert(false); + ExpectAssert(false); #ifndef UNITTEST_NO_EXCEPTIONS - throw AssertException(); + throw AssertException(); #else - UNITTEST_JUMP_TO_ASSERT_JUMP_TARGET(); + UNITTEST_JUMP_TO_ASSERT_JUMP_TARGET(); #endif -} + } -UNITTEST_LINKAGE void ExpectAssert(bool expected) -{ - AssertExpectedFlag() = expected; -} + UNITTEST_LINKAGE void ExpectAssert(bool expected) + { + AssertExpectedFlag() = expected; + } -UNITTEST_LINKAGE bool AssertExpected() -{ - return AssertExpectedFlag(); -} + UNITTEST_LINKAGE bool AssertExpected() + { + return AssertExpectedFlag(); + } -}} + } +} diff --git a/UnitTest++/ReportAssert.h b/UnitTest++/ReportAssert.h index 0b5d6dd..c22faab 100644 --- a/UnitTest++/ReportAssert.h +++ b/UnitTest++/ReportAssert.h @@ -5,7 +5,7 @@ namespace UnitTest { -UNITTEST_LINKAGE void ReportAssert(char const* description, char const* filename, int lineNumber); + UNITTEST_LINKAGE void ReportAssert(char const* description, char const* filename, int lineNumber); } diff --git a/UnitTest++/ReportAssertImpl.h b/UnitTest++/ReportAssertImpl.h index 88efc03..865292f 100644 --- a/UnitTest++/ReportAssertImpl.h +++ b/UnitTest++/ReportAssertImpl.h @@ -5,42 +5,42 @@ #include "HelperMacros.h" #ifdef UNITTEST_NO_EXCEPTIONS - #include + #include #endif namespace UnitTest { -class TestResults; -class TestDetails; + class TestResults; + class TestDetails; -namespace Detail { + namespace Detail { -UNITTEST_LINKAGE void ExpectAssert(bool expected); + UNITTEST_LINKAGE void ExpectAssert(bool expected); -UNITTEST_LINKAGE void ReportAssertEx(TestResults* testResults, - const TestDetails* testDetails, - char const* description, - char const* filename, - int lineNumber); + UNITTEST_LINKAGE void ReportAssertEx(TestResults* testResults, + const TestDetails* testDetails, + char const* description, + char const* filename, + int lineNumber); -UNITTEST_LINKAGE bool AssertExpected(); + UNITTEST_LINKAGE bool AssertExpected(); #ifdef UNITTEST_NO_EXCEPTIONS - UNITTEST_LINKAGE UNITTEST_JMPBUF* GetAssertJmpBuf(); - - #ifdef UNITTEST_WIN32 - #define UNITTEST_SET_ASSERT_JUMP_TARGET() \ - __pragma(warning(push)) __pragma(warning(disable:4611)) \ - UNITTEST_SETJMP(*UnitTest::Detail::GetAssertJmpBuf()) \ - __pragma(warning(pop)) - #else - #define UNITTEST_SET_ASSERT_JUMP_TARGET() UNITTEST_SETJMP(*UnitTest::Detail::GetAssertJmpBuf()) - #endif - - #define UNITTEST_JUMP_TO_ASSERT_JUMP_TARGET() UNITTEST_LONGJMP(*UnitTest::Detail::GetAssertJmpBuf(), 1) + UNITTEST_LINKAGE UNITTEST_JMPBUF* GetAssertJmpBuf(); + +#ifdef UNITTEST_WIN32 + #define UNITTEST_SET_ASSERT_JUMP_TARGET() \ + __pragma(warning(push)) __pragma(warning(disable: 4611)) \ + UNITTEST_SETJMP(*UnitTest::Detail::GetAssertJmpBuf()) \ + __pragma(warning(pop)) +#else + #define UNITTEST_SET_ASSERT_JUMP_TARGET() UNITTEST_SETJMP(*UnitTest::Detail::GetAssertJmpBuf()) #endif -} + #define UNITTEST_JUMP_TO_ASSERT_JUMP_TARGET() UNITTEST_LONGJMP(*UnitTest::Detail::GetAssertJmpBuf(), 1) +#endif + + } } #endif diff --git a/UnitTest++/Test.cpp b/UnitTest++/Test.cpp index 6941722..82ed710 100644 --- a/UnitTest++/Test.cpp +++ b/UnitTest++/Test.cpp @@ -7,35 +7,32 @@ #include "ExecuteTest.h" #ifdef UNITTEST_POSIX - #include "Posix/SignalTranslator.h" +#include "Posix/SignalTranslator.h" #endif namespace UnitTest { -TestList& Test::GetTestList() -{ - static TestList s_list; - return s_list; -} + TestList& Test::GetTestList() + { + static TestList s_list; + return s_list; + } -Test::Test(char const* testName, char const* suiteName, char const* filename, int lineNumber) - : m_details(testName, suiteName, filename, lineNumber) - , m_nextTest(0) - , m_isMockTest(false) -{ -} + Test::Test(char const* testName, char const* suiteName, char const* filename, int lineNumber) + : m_details(testName, suiteName, filename, lineNumber) + , m_nextTest(0) + , m_isMockTest(false) + {} -Test::~Test() -{ -} + Test::~Test() + {} -void Test::Run() -{ - ExecuteTest(*this, m_details, m_isMockTest); -} + void Test::Run() + { + ExecuteTest(*this, m_details, m_isMockTest); + } -void Test::RunImpl() const -{ -} + void Test::RunImpl() const + {} } diff --git a/UnitTest++/Test.h b/UnitTest++/Test.h index bf7c4a3..beb0769 100644 --- a/UnitTest++/Test.h +++ b/UnitTest++/Test.h @@ -5,29 +5,29 @@ namespace UnitTest { -class TestResults; -class TestList; + class TestResults; + class TestList; -class UNITTEST_LINKAGE Test -{ -public: - explicit Test(char const* testName, char const* suiteName = "DefaultSuite", char const* filename = "", int lineNumber = 0); - virtual ~Test(); - void Run(); + class UNITTEST_LINKAGE Test + { + public: + explicit Test(char const* testName, char const* suiteName = "DefaultSuite", char const* filename = "", int lineNumber = 0); + virtual ~Test(); + void Run(); - TestDetails const m_details; - Test* m_nextTest; + TestDetails const m_details; + Test* m_nextTest; - mutable bool m_isMockTest; + mutable bool m_isMockTest; - static TestList& GetTestList(); + static TestList& GetTestList(); - virtual void RunImpl() const; + virtual void RunImpl() const; -private: - Test(Test const&); - Test& operator =(Test const&); -}; + private: + Test(Test const&); + Test& operator =(Test const&); + }; } diff --git a/UnitTest++/TestDetails.cpp b/UnitTest++/TestDetails.cpp index 3af0446..deb6639 100644 --- a/UnitTest++/TestDetails.cpp +++ b/UnitTest++/TestDetails.cpp @@ -2,23 +2,21 @@ namespace UnitTest { -TestDetails::TestDetails(char const* testName_, char const* suiteName_, char const* filename_, int lineNumber_) - : suiteName(suiteName_) - , testName(testName_) - , filename(filename_) - , lineNumber(lineNumber_) - , timeConstraintExempt(false) -{ -} + TestDetails::TestDetails(char const* testName_, char const* suiteName_, char const* filename_, int lineNumber_) + : suiteName(suiteName_) + , testName(testName_) + , filename(filename_) + , lineNumber(lineNumber_) + , timeConstraintExempt(false) + {} -TestDetails::TestDetails(const TestDetails& details, int lineNumber_) - : suiteName(details.suiteName) - , testName(details.testName) - , filename(details.filename) - , lineNumber(lineNumber_) - , timeConstraintExempt(details.timeConstraintExempt) -{ -} + TestDetails::TestDetails(const TestDetails& details, int lineNumber_) + : suiteName(details.suiteName) + , testName(details.testName) + , filename(details.filename) + , lineNumber(lineNumber_) + , timeConstraintExempt(details.timeConstraintExempt) + {} } diff --git a/UnitTest++/TestDetails.h b/UnitTest++/TestDetails.h index eb7d596..cf6f1d3 100644 --- a/UnitTest++/TestDetails.h +++ b/UnitTest++/TestDetails.h @@ -5,22 +5,22 @@ namespace UnitTest { -class UNITTEST_LINKAGE TestDetails -{ -public: - TestDetails(char const* testName, char const* suiteName, char const* filename, int lineNumber); - TestDetails(const TestDetails& details, int lineNumber); + class UNITTEST_LINKAGE TestDetails + { + public: + TestDetails(char const* testName, char const* suiteName, char const* filename, int lineNumber); + TestDetails(const TestDetails& details, int lineNumber); - char const* const suiteName; - char const* const testName; - char const* const filename; - int const lineNumber; - mutable bool timeConstraintExempt; + char const* const suiteName; + char const* const testName; + char const* const filename; + int const lineNumber; + mutable bool timeConstraintExempt; - TestDetails(TestDetails const&); // Why is it public? --> http://gcc.gnu.org/bugs.html#cxx_rvalbind -private: - TestDetails& operator=(TestDetails const&); -}; + TestDetails(TestDetails const&); // Why is it public? --> http://gcc.gnu.org/bugs.html#cxx_rvalbind + private: + TestDetails& operator=(TestDetails const&); + }; } diff --git a/UnitTest++/TestList.cpp b/UnitTest++/TestList.cpp index 4583cbb..a2bc4b7 100644 --- a/UnitTest++/TestList.cpp +++ b/UnitTest++/TestList.cpp @@ -5,35 +5,34 @@ namespace UnitTest { -TestList::TestList() - : m_head(0) - , m_tail(0) -{ -} + TestList::TestList() + : m_head(0) + , m_tail(0) + {} -void TestList::Add(Test* test) -{ - if (m_tail == 0) - { - assert(m_head == 0); - m_head = test; - m_tail = test; - } - else - { - m_tail->m_nextTest = test; - m_tail = test; - } -} + void TestList::Add(Test* test) + { + if (m_tail == 0) + { + assert(m_head == 0); + m_head = test; + m_tail = test; + } + else + { + m_tail->m_nextTest = test; + m_tail = test; + } + } -Test* TestList::GetHead() const -{ - return m_head; -} + Test* TestList::GetHead() const + { + return m_head; + } -ListAdder::ListAdder(TestList& list, Test* test) -{ - list.Add(test); -} + ListAdder::ListAdder(TestList& list, Test* test) + { + list.Add(test); + } } diff --git a/UnitTest++/TestList.h b/UnitTest++/TestList.h index a001d3f..d52ab51 100644 --- a/UnitTest++/TestList.h +++ b/UnitTest++/TestList.h @@ -5,27 +5,27 @@ namespace UnitTest { -class Test; + class Test; -class UNITTEST_LINKAGE TestList -{ -public: - TestList(); - void Add (Test* test); + class UNITTEST_LINKAGE TestList + { + public: + TestList(); + void Add (Test* test); - Test* GetHead() const; + Test* GetHead() const; -private: - Test* m_head; - Test* m_tail; -}; + private: + Test* m_head; + Test* m_tail; + }; -class UNITTEST_LINKAGE ListAdder -{ -public: - ListAdder(TestList& list, Test* test); -}; + class UNITTEST_LINKAGE ListAdder + { + public: + ListAdder(TestList& list, Test* test); + }; } diff --git a/UnitTest++/TestMacros.h b/UnitTest++/TestMacros.h index facc94e..738c56b 100644 --- a/UnitTest++/TestMacros.h +++ b/UnitTest++/TestMacros.h @@ -10,106 +10,106 @@ #include "MemoryOutStream.h" #ifndef UNITTEST_POSIX - #define UNITTEST_THROW_SIGNALS_POSIX_ONLY +#define UNITTEST_THROW_SIGNALS_POSIX_ONLY #else - #include "Posix/SignalTranslator.h" +#include "Posix/SignalTranslator.h" #endif #ifdef TEST - #error UnitTest++ redefines TEST +#error UnitTest++ redefines TEST #endif #ifdef TEST_EX - #error UnitTest++ redefines TEST_EX +#error UnitTest++ redefines TEST_EX #endif #ifdef TEST_FIXTURE_EX - #error UnitTest++ redefines TEST_FIXTURE_EX +#error UnitTest++ redefines TEST_FIXTURE_EX #endif -#define SUITE(Name) \ - namespace Suite##Name { \ - namespace UnitTestSuite { \ - inline char const* GetSuiteName () { \ - return #Name ; \ - } \ - } \ - } \ - namespace Suite##Name +#define SUITE(Name) \ + namespace Suite ## Name { \ + namespace UnitTestSuite { \ + inline char const* GetSuiteName () { \ + return #Name; \ + } \ + } \ + } \ + namespace Suite ## Name -#define TEST_EX(Name, List) \ - class Test##Name : public UnitTest::Test \ - { \ - public: \ - Test##Name() : Test(#Name, UnitTestSuite::GetSuiteName(), __FILE__, __LINE__) {} \ - private: \ - virtual void RunImpl() const; \ - } test##Name##Instance; \ - \ - UnitTest::ListAdder adder##Name (List, &test##Name##Instance); \ - \ - void Test##Name::RunImpl() const +#define TEST_EX(Name, List) \ + class Test ## Name : public UnitTest::Test \ + { \ + public: \ + Test ## Name() : Test(#Name, UnitTestSuite::GetSuiteName(), __FILE__, __LINE__) {} \ + private: \ + virtual void RunImpl() const; \ + } test ## Name ## Instance; \ + \ + UnitTest::ListAdder adder ## Name (List, &test ## Name ## Instance); \ + \ + void Test ## Name::RunImpl() const #define TEST(Name) TEST_EX(Name, UnitTest::Test::GetTestList()) -#define TEST_FIXTURE_EX(Fixture, Name, List) \ - class Fixture##Name##Helper : public Fixture \ - { \ - public: \ - explicit Fixture##Name##Helper(UnitTest::TestDetails const& details) : m_details(details) {} \ - void RunImpl(); \ - UnitTest::TestDetails const& m_details; \ - private: \ - Fixture##Name##Helper(Fixture##Name##Helper const&); \ - Fixture##Name##Helper& operator =(Fixture##Name##Helper const&); \ - }; \ - \ - class Test##Fixture##Name : public UnitTest::Test \ - { \ - public: \ - Test##Fixture##Name() : Test(#Name, UnitTestSuite::GetSuiteName(), __FILE__, __LINE__) {} \ - private: \ - virtual void RunImpl() const; \ - } test##Fixture##Name##Instance; \ - \ - UnitTest::ListAdder adder##Fixture##Name (List, &test##Fixture##Name##Instance); \ - \ - void Test##Fixture##Name::RunImpl() const \ - { \ - volatile bool ctorOk = false; \ - UT_TRY \ - ({ \ - Fixture##Name##Helper fixtureHelper(m_details); \ - ctorOk = true; \ - UnitTest::ExecuteTest(fixtureHelper, m_details, false); \ - }) \ - UT_CATCH (UnitTest::AssertException, e, \ - { \ - (void)e; \ - }) \ - UT_CATCH (std::exception, e, \ - { \ - UnitTest::MemoryOutStream stream; \ - stream << "Unhandled exception: " << e.what(); \ - UnitTest::CurrentTest::Results()->OnTestFailure(m_details, stream.GetText()); \ - }) \ - UT_CATCH_ALL \ - ({ \ - if (ctorOk) \ - { \ - UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(m_details, __LINE__), \ - "Unhandled exception while destroying fixture " #Fixture); \ - } \ - else \ - { \ - UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(m_details, __LINE__), \ - "Unhandled exception while constructing fixture " #Fixture); \ - } \ - }) \ - } \ - void Fixture##Name##Helper::RunImpl() +#define TEST_FIXTURE_EX(Fixture, Name, List) \ + class Fixture ## Name ## Helper : public Fixture \ + { \ + public: \ + explicit Fixture ## Name ## Helper(UnitTest::TestDetails const& details) : m_details(details) {} \ + void RunImpl(); \ + UnitTest::TestDetails const& m_details; \ + private: \ + Fixture ## Name ## Helper(Fixture ## Name ## Helper const&); \ + Fixture ## Name ## Helper& operator =(Fixture ## Name ## Helper const&); \ + }; \ + \ + class Test ## Fixture ## Name : public UnitTest::Test \ + { \ + public: \ + Test ## Fixture ## Name() : Test(#Name, UnitTestSuite::GetSuiteName(), __FILE__, __LINE__) {} \ + private: \ + virtual void RunImpl() const; \ + } test ## Fixture ## Name ## Instance; \ + \ + UnitTest::ListAdder adder ## Fixture ## Name (List, &test ## Fixture ## Name ## Instance); \ + \ + void Test ## Fixture ## Name::RunImpl() const \ + { \ + volatile bool ctorOk = false; \ + UT_TRY \ + ({ \ + Fixture ## Name ## Helper fixtureHelper(m_details); \ + ctorOk = true; \ + UnitTest::ExecuteTest(fixtureHelper, m_details, false); \ + }) \ + UT_CATCH (UnitTest::AssertException, e, \ + { \ + (void)e; \ + }) \ + UT_CATCH (std::exception, e, \ + { \ + UnitTest::MemoryOutStream stream; \ + stream << "Unhandled exception: " << e.what(); \ + UnitTest::CurrentTest::Results()->OnTestFailure(m_details, stream.GetText()); \ + }) \ + UT_CATCH_ALL \ + ({ \ + if (ctorOk) \ + { \ + UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(m_details, __LINE__), \ + "Unhandled exception while destroying fixture " #Fixture); \ + } \ + else \ + { \ + UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(m_details, __LINE__), \ + "Unhandled exception while constructing fixture " #Fixture); \ + } \ + }) \ + } \ + void Fixture ## Name ## Helper::RunImpl() #define TEST_FIXTURE(Fixture,Name) TEST_FIXTURE_EX(Fixture, Name, UnitTest::Test::GetTestList()) diff --git a/UnitTest++/TestReporter.cpp b/UnitTest++/TestReporter.cpp index acfca3e..362da93 100644 --- a/UnitTest++/TestReporter.cpp +++ b/UnitTest++/TestReporter.cpp @@ -2,8 +2,7 @@ namespace UnitTest { -TestReporter::~TestReporter() -{ -} + TestReporter::~TestReporter() + {} } diff --git a/UnitTest++/TestReporter.h b/UnitTest++/TestReporter.h index 2100524..f296a59 100644 --- a/UnitTest++/TestReporter.h +++ b/UnitTest++/TestReporter.h @@ -5,18 +5,18 @@ namespace UnitTest { -class TestDetails; + class TestDetails; -class UNITTEST_LINKAGE TestReporter -{ -public: - virtual ~TestReporter(); + class UNITTEST_LINKAGE TestReporter + { + public: + virtual ~TestReporter(); - virtual void ReportTestStart(TestDetails const& test) = 0; - virtual void ReportFailure(TestDetails const& test, char const* failure) = 0; - virtual void ReportTestFinish(TestDetails const& test, float secondsElapsed) = 0; - virtual void ReportSummary(int totalTestCount, int failedTestCount, int failureCount, float secondsElapsed) = 0; -}; + virtual void ReportTestStart(TestDetails const& test) = 0; + virtual void ReportFailure(TestDetails const& test, char const* failure) = 0; + virtual void ReportTestFinish(TestDetails const& test, float secondsElapsed) = 0; + virtual void ReportSummary(int totalTestCount, int failedTestCount, int failureCount, float secondsElapsed) = 0; + }; } #endif diff --git a/UnitTest++/TestReporterStdout.cpp b/UnitTest++/TestReporterStdout.cpp index 7d08903..97b0f14 100644 --- a/UnitTest++/TestReporterStdout.cpp +++ b/UnitTest++/TestReporterStdout.cpp @@ -5,42 +5,40 @@ // cstdio doesn't pull in namespace std on VC6, so we do it here. #if defined(UNITTEST_WIN32) && (_MSC_VER == 1200) - namespace std {} +namespace std {} #endif namespace UnitTest { -void TestReporterStdout::ReportFailure(TestDetails const& details, char const* failure) -{ - using namespace std; + void TestReporterStdout::ReportFailure(TestDetails const& details, char const* failure) + { + using namespace std; #if defined(__APPLE__) || defined(__GNUG__) - char const* const errorFormat = "%s:%d:%d: error: Failure in %s: %s\n"; - fprintf(stderr, errorFormat, details.filename, details.lineNumber, 1, details.testName, failure); + char const* const errorFormat = "%s:%d:%d: error: Failure in %s: %s\n"; + fprintf(stderr, errorFormat, details.filename, details.lineNumber, 1, details.testName, failure); #else - char const* const errorFormat = "%s(%d): error: Failure in %s: %s\n"; - fprintf(stderr, errorFormat, details.filename, details.lineNumber, details.testName, failure); + char const* const errorFormat = "%s(%d): error: Failure in %s: %s\n"; + fprintf(stderr, errorFormat, details.filename, details.lineNumber, details.testName, failure); #endif -} + } -void TestReporterStdout::ReportTestStart(TestDetails const& /*test*/) -{ -} + void TestReporterStdout::ReportTestStart(TestDetails const& /*test*/) + {} -void TestReporterStdout::ReportTestFinish(TestDetails const& /*test*/, float) -{ -} + void TestReporterStdout::ReportTestFinish(TestDetails const& /*test*/, float) + {} -void TestReporterStdout::ReportSummary(int const totalTestCount, int const failedTestCount, - int const failureCount, float const secondsElapsed) -{ - using namespace std; + void TestReporterStdout::ReportSummary(int const totalTestCount, int const failedTestCount, + int const failureCount, float const secondsElapsed) + { + using namespace std; - if (failureCount > 0) - printf("FAILURE: %d out of %d tests failed (%d failures).\n", failedTestCount, totalTestCount, failureCount); - else - printf("Success: %d tests passed.\n", totalTestCount); + if (failureCount > 0) + printf("FAILURE: %d out of %d tests failed (%d failures).\n", failedTestCount, totalTestCount, failureCount); + else + printf("Success: %d tests passed.\n", totalTestCount); - printf("Test time: %.2f seconds.\n", secondsElapsed); -} + printf("Test time: %.2f seconds.\n", secondsElapsed); + } } diff --git a/UnitTest++/TestReporterStdout.h b/UnitTest++/TestReporterStdout.h index e8d7bbf..6cedfc5 100644 --- a/UnitTest++/TestReporterStdout.h +++ b/UnitTest++/TestReporterStdout.h @@ -5,15 +5,15 @@ namespace UnitTest { -class UNITTEST_LINKAGE TestReporterStdout : public TestReporter -{ -private: - virtual void ReportTestStart(TestDetails const& test); - virtual void ReportFailure(TestDetails const& test, char const* failure); - virtual void ReportTestFinish(TestDetails const& test, float secondsElapsed); - virtual void ReportSummary(int totalTestCount, int failedTestCount, int failureCount, float secondsElapsed); -}; + class UNITTEST_LINKAGE TestReporterStdout : public TestReporter + { + private: + virtual void ReportTestStart(TestDetails const& test); + virtual void ReportFailure(TestDetails const& test, char const* failure); + virtual void ReportTestFinish(TestDetails const& test, float secondsElapsed); + virtual void ReportSummary(int totalTestCount, int failedTestCount, int failureCount, float secondsElapsed); + }; } -#endif +#endif diff --git a/UnitTest++/TestResults.cpp b/UnitTest++/TestResults.cpp index b3b67c0..e40ea70 100644 --- a/UnitTest++/TestResults.cpp +++ b/UnitTest++/TestResults.cpp @@ -5,56 +5,55 @@ namespace UnitTest { -TestResults::TestResults(TestReporter* testReporter) - : m_testReporter(testReporter) - , m_totalTestCount(0) - , m_failedTestCount(0) - , m_failureCount(0) - , m_currentTestFailed(false) -{ -} - -void TestResults::OnTestStart(TestDetails const& test) -{ - ++m_totalTestCount; - m_currentTestFailed = false; - if (m_testReporter) - m_testReporter->ReportTestStart(test); -} - -void TestResults::OnTestFailure(TestDetails const& test, char const* failure) -{ - ++m_failureCount; - if (!m_currentTestFailed) - { - ++m_failedTestCount; - m_currentTestFailed = true; - } - - if (m_testReporter) - m_testReporter->ReportFailure(test, failure); -} - -void TestResults::OnTestFinish(TestDetails const& test, float secondsElapsed) -{ - if (m_testReporter) - m_testReporter->ReportTestFinish(test, secondsElapsed); -} - -int TestResults::GetTotalTestCount() const -{ - return m_totalTestCount; -} - -int TestResults::GetFailedTestCount() const -{ - return m_failedTestCount; -} - -int TestResults::GetFailureCount() const -{ - return m_failureCount; -} + TestResults::TestResults(TestReporter* testReporter) + : m_testReporter(testReporter) + , m_totalTestCount(0) + , m_failedTestCount(0) + , m_failureCount(0) + , m_currentTestFailed(false) + {} + + void TestResults::OnTestStart(TestDetails const& test) + { + ++m_totalTestCount; + m_currentTestFailed = false; + if (m_testReporter) + m_testReporter->ReportTestStart(test); + } + + void TestResults::OnTestFailure(TestDetails const& test, char const* failure) + { + ++m_failureCount; + if (!m_currentTestFailed) + { + ++m_failedTestCount; + m_currentTestFailed = true; + } + + if (m_testReporter) + m_testReporter->ReportFailure(test, failure); + } + + void TestResults::OnTestFinish(TestDetails const& test, float secondsElapsed) + { + if (m_testReporter) + m_testReporter->ReportTestFinish(test, secondsElapsed); + } + + int TestResults::GetTotalTestCount() const + { + return m_totalTestCount; + } + + int TestResults::GetFailedTestCount() const + { + return m_failedTestCount; + } + + int TestResults::GetFailureCount() const + { + return m_failureCount; + } } diff --git a/UnitTest++/TestResults.h b/UnitTest++/TestResults.h index a24aaf5..c56a632 100644 --- a/UnitTest++/TestResults.h +++ b/UnitTest++/TestResults.h @@ -5,33 +5,33 @@ namespace UnitTest { -class TestReporter; -class TestDetails; + class TestReporter; + class TestDetails; -class UNITTEST_LINKAGE TestResults -{ -public: - explicit TestResults(TestReporter* reporter = 0); + class UNITTEST_LINKAGE TestResults + { + public: + explicit TestResults(TestReporter* reporter = 0); - void OnTestStart(TestDetails const& test); - void OnTestFailure(TestDetails const& test, char const* failure); - void OnTestFinish(TestDetails const& test, float secondsElapsed); + void OnTestStart(TestDetails const& test); + void OnTestFailure(TestDetails const& test, char const* failure); + void OnTestFinish(TestDetails const& test, float secondsElapsed); - int GetTotalTestCount() const; - int GetFailedTestCount() const; - int GetFailureCount() const; + int GetTotalTestCount() const; + int GetFailedTestCount() const; + int GetFailureCount() const; -private: - TestReporter* m_testReporter; - int m_totalTestCount; - int m_failedTestCount; - int m_failureCount; + private: + TestReporter* m_testReporter; + int m_totalTestCount; + int m_failedTestCount; + int m_failureCount; - bool m_currentTestFailed; + bool m_currentTestFailed; - TestResults(TestResults const&); - TestResults& operator =(TestResults const&); -}; + TestResults(TestResults const&); + TestResults& operator =(TestResults const&); + }; } diff --git a/UnitTest++/TestRunner.cpp b/UnitTest++/TestRunner.cpp index a8151e6..8e0581d 100644 --- a/UnitTest++/TestRunner.cpp +++ b/UnitTest++/TestRunner.cpp @@ -10,73 +10,73 @@ namespace UnitTest { -int RunAllTests() -{ - TestReporterStdout reporter; - TestRunner runner(reporter); - return runner.RunTestsIf(Test::GetTestList(), NULL, True(), 0); -} - - -TestRunner::TestRunner(TestReporter& reporter) - : m_reporter(&reporter) - , m_result(new TestResults(&reporter)) - , m_timer(new Timer) -{ - m_timer->Start(); -} - -TestRunner::~TestRunner() -{ - delete m_result; - delete m_timer; -} - -TestResults* TestRunner::GetTestResults() -{ - return m_result; -} - -int TestRunner::Finish() const -{ - float const secondsElapsed = static_cast(m_timer->GetTimeInMs() / 1000.0); - m_reporter->ReportSummary(m_result->GetTotalTestCount(), - m_result->GetFailedTestCount(), - m_result->GetFailureCount(), - secondsElapsed); - - return m_result->GetFailureCount(); -} - -bool TestRunner::IsTestInSuite(const Test* const curTest, char const* suiteName) const -{ - using namespace std; - return (suiteName == NULL) || !strcmp(curTest->m_details.suiteName, suiteName); -} - -void TestRunner::RunTest(TestResults* const result, Test* const curTest, int const maxTestTimeInMs) const -{ - if (curTest->m_isMockTest == false) - CurrentTest::Results() = result; - - Timer testTimer; - testTimer.Start(); - - result->OnTestStart(curTest->m_details); - - curTest->Run(); - - double const testTimeInMs = testTimer.GetTimeInMs(); - if (maxTestTimeInMs > 0 && testTimeInMs > maxTestTimeInMs && !curTest->m_details.timeConstraintExempt) - { - MemoryOutStream stream; - stream << "Global time constraint failed. Expected under " << maxTestTimeInMs << - "ms but took " << testTimeInMs << "ms."; - - result->OnTestFailure(curTest->m_details, stream.GetText()); - } - - result->OnTestFinish(curTest->m_details, static_cast< float >(testTimeInMs / 1000.0)); -} + int RunAllTests() + { + TestReporterStdout reporter; + TestRunner runner(reporter); + return runner.RunTestsIf(Test::GetTestList(), NULL, True(), 0); + } + + + TestRunner::TestRunner(TestReporter& reporter) + : m_reporter(&reporter) + , m_result(new TestResults(&reporter)) + , m_timer(new Timer) + { + m_timer->Start(); + } + + TestRunner::~TestRunner() + { + delete m_result; + delete m_timer; + } + + TestResults* TestRunner::GetTestResults() + { + return m_result; + } + + int TestRunner::Finish() const + { + float const secondsElapsed = static_cast(m_timer->GetTimeInMs() / 1000.0); + m_reporter->ReportSummary(m_result->GetTotalTestCount(), + m_result->GetFailedTestCount(), + m_result->GetFailureCount(), + secondsElapsed); + + return m_result->GetFailureCount(); + } + + bool TestRunner::IsTestInSuite(const Test* const curTest, char const* suiteName) const + { + using namespace std; + return (suiteName == NULL) || !strcmp(curTest->m_details.suiteName, suiteName); + } + + void TestRunner::RunTest(TestResults* const result, Test* const curTest, int const maxTestTimeInMs) const + { + if (curTest->m_isMockTest == false) + CurrentTest::Results() = result; + + Timer testTimer; + testTimer.Start(); + + result->OnTestStart(curTest->m_details); + + curTest->Run(); + + double const testTimeInMs = testTimer.GetTimeInMs(); + if (maxTestTimeInMs > 0 && testTimeInMs > maxTestTimeInMs && !curTest->m_details.timeConstraintExempt) + { + MemoryOutStream stream; + stream << "Global time constraint failed. Expected under " << maxTestTimeInMs << + "ms but took " << testTimeInMs << "ms."; + + result->OnTestFailure(curTest->m_details, stream.GetText()); + } + + result->OnTestFinish(curTest->m_details, static_cast< float >(testTimeInMs / 1000.0)); + } } diff --git a/UnitTest++/TestRunner.h b/UnitTest++/TestRunner.h index f88b9cc..914249a 100644 --- a/UnitTest++/TestRunner.h +++ b/UnitTest++/TestRunner.h @@ -7,54 +7,54 @@ namespace UnitTest { -class TestReporter; -class TestResults; -class Timer; - -UNITTEST_LINKAGE int RunAllTests(); - -struct True -{ - bool operator()(const Test* const) const - { - return true; - } -}; - -class UNITTEST_LINKAGE TestRunner -{ -public: - explicit TestRunner(TestReporter& reporter); - ~TestRunner(); - - template< class Predicate > - int RunTestsIf(TestList const& list, char const* suiteName, - const Predicate& predicate, int maxTestTimeInMs) const - { - Test* curTest = list.GetHead(); - - while (curTest != 0) - { - if (IsTestInSuite(curTest, suiteName) && predicate(curTest)) - RunTest(m_result, curTest, maxTestTimeInMs); - - curTest = curTest->m_nextTest; - } - - return Finish(); - } - - TestResults* GetTestResults(); - -private: - TestReporter* m_reporter; - TestResults* m_result; - Timer* m_timer; - - int Finish() const; - bool IsTestInSuite(const Test* const curTest, char const* suiteName) const; - void RunTest(TestResults* const result, Test* const curTest, int const maxTestTimeInMs) const; -}; + class TestReporter; + class TestResults; + class Timer; + + UNITTEST_LINKAGE int RunAllTests(); + + struct True + { + bool operator()(const Test* const) const + { + return true; + } + }; + + class UNITTEST_LINKAGE TestRunner + { + public: + explicit TestRunner(TestReporter& reporter); + ~TestRunner(); + + template< class Predicate > + int RunTestsIf(TestList const& list, char const* suiteName, + const Predicate& predicate, int maxTestTimeInMs) const + { + Test* curTest = list.GetHead(); + + while (curTest != 0) + { + if (IsTestInSuite(curTest, suiteName) && predicate(curTest)) + RunTest(m_result, curTest, maxTestTimeInMs); + + curTest = curTest->m_nextTest; + } + + return Finish(); + } + + TestResults* GetTestResults(); + + private: + TestReporter* m_reporter; + TestResults* m_result; + Timer* m_timer; + + int Finish() const; + bool IsTestInSuite(const Test* const curTest, char const* suiteName) const; + void RunTest(TestResults* const result, Test* const curTest, int const maxTestTimeInMs) const; + }; } diff --git a/UnitTest++/TestSuite.h b/UnitTest++/TestSuite.h index 2812d8c..7b70122 100644 --- a/UnitTest++/TestSuite.h +++ b/UnitTest++/TestSuite.h @@ -1,12 +1,12 @@ #ifndef UNITTEST_TESTSUITE_H #define UNITTEST_TESTSUITE_H -namespace UnitTestSuite +namespace UnitTestSuite { - inline char const* GetSuiteName () - { - return "DefaultSuite"; - } + inline char const* GetSuiteName () + { + return "DefaultSuite"; + } } #endif diff --git a/UnitTest++/TimeConstraint.cpp b/UnitTest++/TimeConstraint.cpp index f806aeb..e45dd97 100644 --- a/UnitTest++/TimeConstraint.cpp +++ b/UnitTest++/TimeConstraint.cpp @@ -6,24 +6,24 @@ namespace UnitTest { -TimeConstraint::TimeConstraint(int ms, TestDetails const& details, int lineNumber) - : m_details(details, lineNumber) - , m_maxMs(ms) -{ - m_timer.Start(); -} + TimeConstraint::TimeConstraint(int ms, TestDetails const& details, int lineNumber) + : m_details(details, lineNumber) + , m_maxMs(ms) + { + m_timer.Start(); + } -TimeConstraint::~TimeConstraint() -{ - double const totalTimeInMs = m_timer.GetTimeInMs(); - if (totalTimeInMs > m_maxMs) - { - MemoryOutStream stream; - stream << "Time constraint failed. Expected to run test under " << m_maxMs << - "ms but took " << totalTimeInMs << "ms."; + TimeConstraint::~TimeConstraint() + { + double const totalTimeInMs = m_timer.GetTimeInMs(); + if (totalTimeInMs > m_maxMs) + { + MemoryOutStream stream; + stream << "Time constraint failed. Expected to run test under " << m_maxMs << + "ms but took " << totalTimeInMs << "ms."; - CurrentTest::Results()->OnTestFailure(m_details, stream.GetText()); - } -} + CurrentTest::Results()->OnTestFailure(m_details, stream.GetText()); + } + } } diff --git a/UnitTest++/TimeConstraint.h b/UnitTest++/TimeConstraint.h index cf6d68a..be9d255 100644 --- a/UnitTest++/TimeConstraint.h +++ b/UnitTest++/TimeConstraint.h @@ -7,30 +7,30 @@ namespace UnitTest { -class TestResults; - -class UNITTEST_LINKAGE TimeConstraint -{ -public: - TimeConstraint(int ms, TestDetails const& details, int lineNumber); - ~TimeConstraint(); - -private: - void operator=(TimeConstraint const&); - TimeConstraint(TimeConstraint const&); - - Timer m_timer; - TestDetails const m_details; - int const m_maxMs; -}; - -#define UNITTEST_TIME_CONSTRAINT(ms) \ - UnitTest::TimeConstraint unitTest__timeConstraint__(ms, m_details, __LINE__) - -#define UNITTEST_TIME_CONSTRAINT_EXEMPT() \ - UNITTEST_MULTILINE_MACRO_BEGIN \ - m_details.timeConstraintExempt = true; \ - UNITTEST_MULTILINE_MACRO_END + class TestResults; + + class UNITTEST_LINKAGE TimeConstraint + { + public: + TimeConstraint(int ms, TestDetails const& details, int lineNumber); + ~TimeConstraint(); + + private: + void operator=(TimeConstraint const&); + TimeConstraint(TimeConstraint const&); + + Timer m_timer; + TestDetails const m_details; + int const m_maxMs; + }; + + #define UNITTEST_TIME_CONSTRAINT(ms) \ + UnitTest::TimeConstraint unitTest__timeConstraint__(ms, m_details, __LINE__) + + #define UNITTEST_TIME_CONSTRAINT_EXEMPT() \ + UNITTEST_MULTILINE_MACRO_BEGIN \ + m_details.timeConstraintExempt = true; \ + UNITTEST_MULTILINE_MACRO_END } diff --git a/UnitTest++/TimeHelpers.h b/UnitTest++/TimeHelpers.h index f34ed00..2f186ff 100644 --- a/UnitTest++/TimeHelpers.h +++ b/UnitTest++/TimeHelpers.h @@ -1,7 +1,7 @@ #include "Config.h" #if defined UNITTEST_POSIX - #include "Posix/TimeHelpers.h" + #include "Posix/TimeHelpers.h" #else - #include "Win32/TimeHelpers.h" + #include "Win32/TimeHelpers.h" #endif diff --git a/UnitTest++/Win32/TimeHelpers.cpp b/UnitTest++/Win32/TimeHelpers.cpp index aa97b89..684ffdf 100644 --- a/UnitTest++/Win32/TimeHelpers.cpp +++ b/UnitTest++/Win32/TimeHelpers.cpp @@ -5,45 +5,45 @@ namespace UnitTest { -Timer::Timer() - : m_threadHandle(::GetCurrentThread()) - , m_startTime(0) -{ + Timer::Timer() + : m_threadHandle(::GetCurrentThread()) + , m_startTime(0) + { #if defined(UNITTEST_WIN32) && (_MSC_VER == 1200) // VC6 doesn't have DWORD_PTR - typedef unsigned long DWORD_PTR; + typedef unsigned long DWORD_PTR; #endif - DWORD_PTR systemMask; - ::GetProcessAffinityMask(GetCurrentProcess(), &m_processAffinityMask, &systemMask); - ::SetThreadAffinityMask(m_threadHandle, 1); - ::QueryPerformanceFrequency(reinterpret_cast< LARGE_INTEGER* >(&m_frequency)); - ::SetThreadAffinityMask(m_threadHandle, m_processAffinityMask); -} - -void Timer::Start() -{ - m_startTime = GetTime(); -} - -double Timer::GetTimeInMs() const -{ - __int64 const elapsedTime = GetTime() - m_startTime; - double const seconds = double(elapsedTime) / double(m_frequency); - return seconds * 1000.0; -} - -__int64 Timer::GetTime() const -{ - LARGE_INTEGER curTime; - ::SetThreadAffinityMask(m_threadHandle, 1); - ::QueryPerformanceCounter(&curTime); - ::SetThreadAffinityMask(m_threadHandle, m_processAffinityMask); - return curTime.QuadPart; -} - -void TimeHelpers::SleepMs(int ms) -{ - ::Sleep(ms); -} + DWORD_PTR systemMask; + ::GetProcessAffinityMask(GetCurrentProcess(), &m_processAffinityMask, &systemMask); + ::SetThreadAffinityMask(m_threadHandle, 1); + ::QueryPerformanceFrequency(reinterpret_cast< LARGE_INTEGER* >(&m_frequency)); + ::SetThreadAffinityMask(m_threadHandle, m_processAffinityMask); + } + + void Timer::Start() + { + m_startTime = GetTime(); + } + + double Timer::GetTimeInMs() const + { + __int64 const elapsedTime = GetTime() - m_startTime; + double const seconds = double(elapsedTime) / double(m_frequency); + return seconds * 1000.0; + } + + __int64 Timer::GetTime() const + { + LARGE_INTEGER curTime; + ::SetThreadAffinityMask(m_threadHandle, 1); + ::QueryPerformanceCounter(&curTime); + ::SetThreadAffinityMask(m_threadHandle, m_processAffinityMask); + return curTime.QuadPart; + } + + void TimeHelpers::SleepMs(int ms) + { + ::Sleep(ms); + } } diff --git a/UnitTest++/Win32/TimeHelpers.h b/UnitTest++/Win32/TimeHelpers.h index 2bbe75f..11d4352 100644 --- a/UnitTest++/Win32/TimeHelpers.h +++ b/UnitTest++/Win32/TimeHelpers.h @@ -5,40 +5,40 @@ #include "../HelperMacros.h" #ifdef UNITTEST_MINGW - #ifndef __int64 - #define __int64 long long - #endif +#ifndef __int64 +#define __int64 long long +#endif #endif namespace UnitTest { -class UNITTEST_LINKAGE Timer -{ -public: - Timer(); - void Start(); - double GetTimeInMs() const; + class UNITTEST_LINKAGE Timer + { + public: + Timer(); + void Start(); + double GetTimeInMs() const; -private: - __int64 GetTime() const; + private: + __int64 GetTime() const; - void* m_threadHandle; + void* m_threadHandle; #if defined(_WIN64) - unsigned __int64 m_processAffinityMask; + unsigned __int64 m_processAffinityMask; #else - unsigned long m_processAffinityMask; + unsigned long m_processAffinityMask; #endif - __int64 m_startTime; - __int64 m_frequency; -}; + __int64 m_startTime; + __int64 m_frequency; + }; -namespace TimeHelpers -{ - UNITTEST_LINKAGE void SleepMs(int ms); -} + namespace TimeHelpers + { + UNITTEST_LINKAGE void SleepMs(int ms); + } } diff --git a/UnitTest++/XmlTestReporter.cpp b/UnitTest++/XmlTestReporter.cpp index c3312ea..d44de05 100644 --- a/UnitTest++/XmlTestReporter.cpp +++ b/UnitTest++/XmlTestReporter.cpp @@ -13,118 +13,117 @@ using std::ostream; namespace { -void ReplaceChar(string& str, char c, string const& replacement) -{ - for (size_t pos = str.find(c); pos != string::npos; pos = str.find(c, pos + 1)) - str.replace(pos, 1, replacement); -} - -string XmlEscape(string const& value) -{ - string escaped = value; - - ReplaceChar(escaped, '&', "&"); - ReplaceChar(escaped, '<', "<"); - ReplaceChar(escaped, '>', ">"); - ReplaceChar(escaped, '\'', "'"); - ReplaceChar(escaped, '\"', """); - - return escaped; -} - -string BuildFailureMessage(string const& file, int line, string const& message) -{ - ostringstream failureMessage; - failureMessage << file << "(" << line << ") : " << message; - return failureMessage.str(); -} + void ReplaceChar(string& str, char c, string const& replacement) + { + for (size_t pos = str.find(c); pos != string::npos; pos = str.find(c, pos + 1)) + str.replace(pos, 1, replacement); + } + + string XmlEscape(string const& value) + { + string escaped = value; + + ReplaceChar(escaped, '&', "&"); + ReplaceChar(escaped, '<', "<"); + ReplaceChar(escaped, '>', ">"); + ReplaceChar(escaped, '\'', "'"); + ReplaceChar(escaped, '\"', """); + + return escaped; + } + + string BuildFailureMessage(string const& file, int line, string const& message) + { + ostringstream failureMessage; + failureMessage << file << "(" << line << ") : " << message; + return failureMessage.str(); + } } namespace UnitTest { -XmlTestReporter::XmlTestReporter(ostream& ostream) - : m_ostream(ostream) -{ -} + XmlTestReporter::XmlTestReporter(ostream& ostream) + : m_ostream(ostream) + {} -void XmlTestReporter::ReportSummary(int totalTestCount, int failedTestCount, - int failureCount, float secondsElapsed) -{ - AddXmlElement(m_ostream, NULL); + void XmlTestReporter::ReportSummary(int totalTestCount, int failedTestCount, + int failureCount, float secondsElapsed) + { + AddXmlElement(m_ostream, NULL); - BeginResults(m_ostream, totalTestCount, failedTestCount, failureCount, secondsElapsed); + BeginResults(m_ostream, totalTestCount, failedTestCount, failureCount, secondsElapsed); - DeferredTestResultList const& results = GetResults(); - for (DeferredTestResultList::const_iterator i = results.begin(); i != results.end(); ++i) - { - BeginTest(m_ostream, *i); + DeferredTestResultList const& results = GetResults(); + for (DeferredTestResultList::const_iterator i = results.begin(); i != results.end(); ++i) + { + BeginTest(m_ostream, *i); - if (i->failed) + if (i->failed) AddFailure(m_ostream, *i); - EndTest(m_ostream, *i); - } - - EndResults(m_ostream); -} - -void XmlTestReporter::AddXmlElement(ostream& os, char const* encoding) -{ - os << ""; -} - -void XmlTestReporter::BeginResults(std::ostream& os, int totalTestCount, int failedTestCount, - int failureCount, float secondsElapsed) -{ - os << ""; -} - -void XmlTestReporter::EndResults(std::ostream& os) -{ - os << ""; -} - -void XmlTestReporter::BeginTest(std::ostream& os, DeferredTestResult const& result) -{ - os << ""; - else - os << "/>"; -} - -void XmlTestReporter::AddFailure(std::ostream& os, DeferredTestResult const& result) -{ - os << ">"; // close element - - for (DeferredTestResult::FailureVec::const_iterator it = result.failures.begin(); - it != result.failures.end(); - ++it) - { - string const escapedMessage = XmlEscape(std::string(it->failureStr)); - string const message = BuildFailureMessage(result.failureFile, it->lineNumber, escapedMessage); - - os << ""; - } -} + EndTest(m_ostream, *i); + } + + EndResults(m_ostream); + } + + void XmlTestReporter::AddXmlElement(ostream& os, char const* encoding) + { + os << ""; + } + + void XmlTestReporter::BeginResults(std::ostream& os, int totalTestCount, int failedTestCount, + int failureCount, float secondsElapsed) + { + os << ""; + } + + void XmlTestReporter::EndResults(std::ostream& os) + { + os << ""; + } + + void XmlTestReporter::BeginTest(std::ostream& os, DeferredTestResult const& result) + { + os << ""; + else + os << "/>"; + } + + void XmlTestReporter::AddFailure(std::ostream& os, DeferredTestResult const& result) + { + os << ">"; // close element + + for (DeferredTestResult::FailureVec::const_iterator it = result.failures.begin(); + it != result.failures.end(); + ++it) + { + string const escapedMessage = XmlEscape(std::string(it->failureStr)); + string const message = BuildFailureMessage(result.failureFile, it->lineNumber, escapedMessage); + + os << ""; + } + } } diff --git a/UnitTest++/XmlTestReporter.h b/UnitTest++/XmlTestReporter.h index 63d424e..9466903 100644 --- a/UnitTest++/XmlTestReporter.h +++ b/UnitTest++/XmlTestReporter.h @@ -11,26 +11,26 @@ namespace UnitTest { -class UNITTEST_LINKAGE XmlTestReporter : public DeferredTestReporter -{ -public: - explicit XmlTestReporter(std::ostream& ostream); - - virtual void ReportSummary(int totalTestCount, int failedTestCount, int failureCount, float secondsElapsed); - -private: - XmlTestReporter(XmlTestReporter const&); - XmlTestReporter& operator=(XmlTestReporter const&); - - void AddXmlElement(std::ostream& os, char const* encoding); - void BeginResults(std::ostream& os, int totalTestCount, int failedTestCount, int failureCount, float secondsElapsed); - void EndResults(std::ostream& os); - void BeginTest(std::ostream& os, DeferredTestResult const& result); - void AddFailure(std::ostream& os, DeferredTestResult const& result); - void EndTest(std::ostream& os, DeferredTestResult const& result); - - std::ostream& m_ostream; -}; + class UNITTEST_LINKAGE XmlTestReporter : public DeferredTestReporter + { + public: + explicit XmlTestReporter(std::ostream& ostream); + + virtual void ReportSummary(int totalTestCount, int failedTestCount, int failureCount, float secondsElapsed); + + private: + XmlTestReporter(XmlTestReporter const&); + XmlTestReporter& operator=(XmlTestReporter const&); + + void AddXmlElement(std::ostream& os, char const* encoding); + void BeginResults(std::ostream& os, int totalTestCount, int failedTestCount, int failureCount, float secondsElapsed); + void EndResults(std::ostream& os); + void BeginTest(std::ostream& os, DeferredTestResult const& result); + void AddFailure(std::ostream& os, DeferredTestResult const& result); + void EndTest(std::ostream& os, DeferredTestResult const& result); + + std::ostream& m_ostream; + }; } diff --git a/tests/Main.cpp b/tests/Main.cpp index ae9cc54..4a0f402 100644 --- a/tests/Main.cpp +++ b/tests/Main.cpp @@ -2,5 +2,5 @@ int main(int, char const *[]) { - return UnitTest::RunAllTests(); + return UnitTest::RunAllTests(); } diff --git a/tests/RecordingReporter.h b/tests/RecordingReporter.h index ea899a2..a2e4839 100644 --- a/tests/RecordingReporter.h +++ b/tests/RecordingReporter.h @@ -9,89 +9,89 @@ struct RecordingReporter : public UnitTest::TestReporter { private: - enum { kMaxStringLength = 256 }; + enum { kMaxStringLength = 256 }; public: - RecordingReporter() - : testRunCount(0) - , testFailedCount(0) - , lastFailedLine(0) - , testFinishedCount(0) - , lastFinishedTestTime(0) - , summaryTotalTestCount(0) - , summaryFailedTestCount(0) - , summaryFailureCount(0) - , summarySecondsElapsed(0) - { - lastStartedSuite[0] = '\0'; - lastStartedTest[0] = '\0'; - lastFailedFile[0] = '\0'; - lastFailedSuite[0] = '\0'; - lastFailedTest[0] = '\0'; - lastFailedMessage[0] = '\0'; - lastFinishedSuite[0] = '\0'; - lastFinishedTest[0] = '\0'; - } - - virtual void ReportTestStart(UnitTest::TestDetails const& test) - { - using namespace std; - - ++testRunCount; - strcpy(lastStartedSuite, test.suiteName); - strcpy(lastStartedTest, test.testName); - } - - virtual void ReportFailure(UnitTest::TestDetails const& test, char const* failure) - { - using namespace std; - - ++testFailedCount; - strcpy(lastFailedFile, test.filename); - lastFailedLine = test.lineNumber; - strcpy(lastFailedSuite, test.suiteName); - strcpy(lastFailedTest, test.testName); - strcpy(lastFailedMessage, failure); - } - - virtual void ReportTestFinish(UnitTest::TestDetails const& test, float testDuration) - { - using namespace std; - - ++testFinishedCount; - strcpy(lastFinishedSuite, test.suiteName); - strcpy(lastFinishedTest, test.testName); - lastFinishedTestTime = testDuration; - } - - virtual void ReportSummary(int totalTestCount, int failedTestCount, int failureCount, float secondsElapsed) - { - summaryTotalTestCount = totalTestCount; - summaryFailedTestCount = failedTestCount; - summaryFailureCount = failureCount; - summarySecondsElapsed = secondsElapsed; - } - - int testRunCount; - char lastStartedSuite[kMaxStringLength]; - char lastStartedTest[kMaxStringLength]; - - int testFailedCount; - char lastFailedFile[kMaxStringLength]; - int lastFailedLine; - char lastFailedSuite[kMaxStringLength]; - char lastFailedTest[kMaxStringLength]; - char lastFailedMessage[kMaxStringLength]; - - int testFinishedCount; - char lastFinishedSuite[kMaxStringLength]; - char lastFinishedTest[kMaxStringLength]; - float lastFinishedTestTime; - - int summaryTotalTestCount; - int summaryFailedTestCount; - int summaryFailureCount; - float summarySecondsElapsed; + RecordingReporter() + : testRunCount(0) + , testFailedCount(0) + , lastFailedLine(0) + , testFinishedCount(0) + , lastFinishedTestTime(0) + , summaryTotalTestCount(0) + , summaryFailedTestCount(0) + , summaryFailureCount(0) + , summarySecondsElapsed(0) + { + lastStartedSuite[0] = '\0'; + lastStartedTest[0] = '\0'; + lastFailedFile[0] = '\0'; + lastFailedSuite[0] = '\0'; + lastFailedTest[0] = '\0'; + lastFailedMessage[0] = '\0'; + lastFinishedSuite[0] = '\0'; + lastFinishedTest[0] = '\0'; + } + + virtual void ReportTestStart(UnitTest::TestDetails const& test) + { + using namespace std; + + ++testRunCount; + strcpy(lastStartedSuite, test.suiteName); + strcpy(lastStartedTest, test.testName); + } + + virtual void ReportFailure(UnitTest::TestDetails const& test, char const* failure) + { + using namespace std; + + ++testFailedCount; + strcpy(lastFailedFile, test.filename); + lastFailedLine = test.lineNumber; + strcpy(lastFailedSuite, test.suiteName); + strcpy(lastFailedTest, test.testName); + strcpy(lastFailedMessage, failure); + } + + virtual void ReportTestFinish(UnitTest::TestDetails const& test, float testDuration) + { + using namespace std; + + ++testFinishedCount; + strcpy(lastFinishedSuite, test.suiteName); + strcpy(lastFinishedTest, test.testName); + lastFinishedTestTime = testDuration; + } + + virtual void ReportSummary(int totalTestCount, int failedTestCount, int failureCount, float secondsElapsed) + { + summaryTotalTestCount = totalTestCount; + summaryFailedTestCount = failedTestCount; + summaryFailureCount = failureCount; + summarySecondsElapsed = secondsElapsed; + } + + int testRunCount; + char lastStartedSuite[kMaxStringLength]; + char lastStartedTest[kMaxStringLength]; + + int testFailedCount; + char lastFailedFile[kMaxStringLength]; + int lastFailedLine; + char lastFailedSuite[kMaxStringLength]; + char lastFailedTest[kMaxStringLength]; + char lastFailedMessage[kMaxStringLength]; + + int testFinishedCount; + char lastFinishedSuite[kMaxStringLength]; + char lastFinishedTest[kMaxStringLength]; + float lastFinishedTestTime; + + int summaryTotalTestCount; + int summaryFailedTestCount; + int summaryFailureCount; + float summarySecondsElapsed; }; diff --git a/tests/ScopedCurrentTest.h b/tests/ScopedCurrentTest.h index 60b1a8e..4070252 100644 --- a/tests/ScopedCurrentTest.h +++ b/tests/ScopedCurrentTest.h @@ -7,31 +7,32 @@ class ScopedCurrentTest { public: - ScopedCurrentTest() - : m_oldTestResults(UnitTest::CurrentTest::Results()) - , m_oldTestDetails(UnitTest::CurrentTest::Details()) - { - } + ScopedCurrentTest() + : m_oldTestResults(UnitTest::CurrentTest::Results()) + , m_oldTestDetails(UnitTest::CurrentTest::Details()) + {} - explicit ScopedCurrentTest(UnitTest::TestResults& newResults, const UnitTest::TestDetails* newDetails = NULL) - : m_oldTestResults(UnitTest::CurrentTest::Results()) - , m_oldTestDetails(UnitTest::CurrentTest::Details()) - { - UnitTest::CurrentTest::Results() = &newResults; + explicit ScopedCurrentTest(UnitTest::TestResults& newResults, const UnitTest::TestDetails* newDetails = NULL) + : m_oldTestResults(UnitTest::CurrentTest::Results()) + , m_oldTestDetails(UnitTest::CurrentTest::Details()) + { + UnitTest::CurrentTest::Results() = &newResults; - if (newDetails != NULL) - UnitTest::CurrentTest::Details() = newDetails; - } + if (newDetails != NULL) + { + UnitTest::CurrentTest::Details() = newDetails; + } + } - ~ScopedCurrentTest() - { - UnitTest::CurrentTest::Results() = m_oldTestResults; - UnitTest::CurrentTest::Details() = m_oldTestDetails; - } + ~ScopedCurrentTest() + { + UnitTest::CurrentTest::Results() = m_oldTestResults; + UnitTest::CurrentTest::Details() = m_oldTestDetails; + } private: - UnitTest::TestResults* m_oldTestResults; - const UnitTest::TestDetails* m_oldTestDetails; + UnitTest::TestResults* m_oldTestResults; + const UnitTest::TestDetails* m_oldTestDetails; }; #endif diff --git a/tests/TestAssertHandler.cpp b/tests/TestAssertHandler.cpp index c9286f3..4c7f2b5 100644 --- a/tests/TestAssertHandler.cpp +++ b/tests/TestAssertHandler.cpp @@ -12,124 +12,121 @@ using namespace UnitTest; namespace { -TEST(CanSetAssertExpected) -{ - Detail::ExpectAssert(true); - CHECK(Detail::AssertExpected()); + TEST(CanSetAssertExpected) + { + Detail::ExpectAssert(true); + CHECK(Detail::AssertExpected()); - Detail::ExpectAssert(false); - CHECK(!Detail::AssertExpected()); -} + Detail::ExpectAssert(false); + CHECK(!Detail::AssertExpected()); + } #ifndef UNITTEST_NO_EXCEPTIONS -TEST(ReportAssertThrowsAssertException) -{ - bool caught = false; - - try - { - TestResults testResults; - TestDetails testDetails("", "", "", 0); - Detail::ReportAssertEx(&testResults, &testDetails, "", "", 0); - } - catch(AssertException const&) - { - caught = true; - } - - CHECK(true == caught); -} - -TEST(ReportAssertClearsExpectAssertFlag) -{ - RecordingReporter reporter; - TestResults testResults(&reporter); - TestDetails testDetails("", "", "", 0); - - try - { - Detail::ExpectAssert(true); - Detail::ReportAssertEx(&testResults, &testDetails, "", "", 0); - } - catch(AssertException const&) - { - } - - CHECK(Detail::AssertExpected() == false); - CHECK_EQUAL(0, reporter.testFailedCount); -} - -TEST(ReportAssertWritesFailureToResultsAndDetailsWhenAssertIsNotExpected) -{ - const int lineNumber = 12345; - const char* description = "description"; - const char* filename = "filename"; - - RecordingReporter reporter; - TestResults testResults(&reporter); - TestDetails testDetails("", "", "", 0); - - try - { - Detail::ReportAssertEx(&testResults, &testDetails, description, filename, lineNumber); - } - catch(AssertException const&) - { - } - - CHECK_EQUAL(description, reporter.lastFailedMessage); - CHECK_EQUAL(filename, reporter.lastFailedFile); - CHECK_EQUAL(lineNumber, reporter.lastFailedLine); -} - -TEST(ReportAssertReportsNoErrorsWhenAssertIsExpected) -{ - Detail::ExpectAssert(true); - - RecordingReporter reporter; - TestResults testResults(&reporter); - TestDetails testDetails("", "", "", 0); - - try - { - Detail::ReportAssertEx(&testResults, &testDetails, "", "", 0); - } - catch(AssertException const&) - { - } - - CHECK_EQUAL(0, reporter.testFailedCount); -} - -TEST(CheckAssertMacroSetsAssertExpectationToFalseAfterRunning) -{ - Detail::ExpectAssert(true); - CHECK_ASSERT(ReportAssert("", "", 0)); - CHECK(!Detail::AssertExpected()); - Detail::ExpectAssert(false); -} + TEST(ReportAssertThrowsAssertException) + { + bool caught = false; + + try + { + TestResults testResults; + TestDetails testDetails("", "", "", 0); + Detail::ReportAssertEx(&testResults, &testDetails, "", "", 0); + } + catch(AssertException const&) + { + caught = true; + } + + CHECK(true == caught); + } + + TEST(ReportAssertClearsExpectAssertFlag) + { + RecordingReporter reporter; + TestResults testResults(&reporter); + TestDetails testDetails("", "", "", 0); + + try + { + Detail::ExpectAssert(true); + Detail::ReportAssertEx(&testResults, &testDetails, "", "", 0); + } + catch(AssertException const&) + {} + + CHECK(Detail::AssertExpected() == false); + CHECK_EQUAL(0, reporter.testFailedCount); + } + + TEST(ReportAssertWritesFailureToResultsAndDetailsWhenAssertIsNotExpected) + { + const int lineNumber = 12345; + const char* description = "description"; + const char* filename = "filename"; + + RecordingReporter reporter; + TestResults testResults(&reporter); + TestDetails testDetails("", "", "", 0); + + try + { + Detail::ReportAssertEx(&testResults, &testDetails, description, filename, lineNumber); + } + catch(AssertException const&) + {} + + CHECK_EQUAL(description, reporter.lastFailedMessage); + CHECK_EQUAL(filename, reporter.lastFailedFile); + CHECK_EQUAL(lineNumber, reporter.lastFailedLine); + } + + TEST(ReportAssertReportsNoErrorsWhenAssertIsExpected) + { + Detail::ExpectAssert(true); + + RecordingReporter reporter; + TestResults testResults(&reporter); + TestDetails testDetails("", "", "", 0); + + try + { + Detail::ReportAssertEx(&testResults, &testDetails, "", "", 0); + } + catch(AssertException const&) + {} + + CHECK_EQUAL(0, reporter.testFailedCount); + } + + TEST(CheckAssertMacroSetsAssertExpectationToFalseAfterRunning) + { + Detail::ExpectAssert(true); + CHECK_ASSERT(ReportAssert("", "", 0)); + CHECK(!Detail::AssertExpected()); + Detail::ExpectAssert(false); + } #else -TEST(SetAssertJumpTargetReturnsFalseWhenSettingJumpTarget) -{ - CHECK(UNITTEST_SET_ASSERT_JUMP_TARGET() == false); -} + TEST(SetAssertJumpTargetReturnsFalseWhenSettingJumpTarget) + { + CHECK(UNITTEST_SET_ASSERT_JUMP_TARGET() == false); + } -TEST(JumpToAssertJumpTarget_JumpsToSetPoint_ReturnsTrue) -{ - const volatile bool taken = !!UNITTEST_SET_ASSERT_JUMP_TARGET(); + TEST(JumpToAssertJumpTarget_JumpsToSetPoint_ReturnsTrue) + { + const volatile bool taken = !!UNITTEST_SET_ASSERT_JUMP_TARGET(); - volatile bool set = false; - if (taken == false) - { - UNITTEST_JUMP_TO_ASSERT_JUMP_TARGET(); - set = true; - } + volatile bool set = false; + if (taken == false) + { + UNITTEST_JUMP_TO_ASSERT_JUMP_TARGET(); + set = true; + } - CHECK(set == false); -} + CHECK(set == false); + } #endif diff --git a/tests/TestCheckMacros.cpp b/tests/TestCheckMacros.cpp index 45ea0e9..f9c46c4 100644 --- a/tests/TestCheckMacros.cpp +++ b/tests/TestCheckMacros.cpp @@ -7,512 +7,512 @@ using namespace std; namespace { -TEST(CheckSucceedsOnTrue) -{ - bool failure = true; - { - RecordingReporter reporter; - UnitTest::TestResults testResults(&reporter); - - ScopedCurrentTest scopedResults(testResults); - CHECK(true); - - failure = (testResults.GetFailureCount() > 0); - } - - CHECK(!failure); -} - -TEST(CheckFailsOnFalse) -{ - bool failure = false; - { - RecordingReporter reporter; - UnitTest::TestResults testResults(&reporter); - ScopedCurrentTest scopedResults(testResults); - CHECK(false); - failure = (testResults.GetFailureCount() > 0); - } - - CHECK(failure); -} - -TEST(FailureReportsCorrectTestName) -{ - RecordingReporter reporter; - { - UnitTest::TestResults testResults(&reporter); - ScopedCurrentTest scopedResults(testResults); - CHECK(false); - } - - CHECK_EQUAL(m_details.testName, reporter.lastFailedTest); -} - -TEST(CheckFailureIncludesCheckContents) -{ - RecordingReporter reporter; - { - UnitTest::TestResults testResults(&reporter); - ScopedCurrentTest scopedResults(testResults); - const bool yaddayadda = false; - CHECK(yaddayadda); - } - - CHECK(strstr(reporter.lastFailedMessage, "yaddayadda")); -} - -TEST(CheckEqualSucceedsOnEqual) -{ - bool failure = true; - { - RecordingReporter reporter; - UnitTest::TestResults testResults(&reporter); - ScopedCurrentTest scopedResults(testResults); - CHECK_EQUAL(1, 1); - failure = (testResults.GetFailureCount() > 0); - } - - CHECK(!failure); -} - -TEST(CheckEqualFailsOnNotEqual) -{ - bool failure = false; - { - RecordingReporter reporter; - UnitTest::TestResults testResults(&reporter); - ScopedCurrentTest scopedResults(testResults); - CHECK_EQUAL(1, 2); - failure = (testResults.GetFailureCount() > 0); - } - - CHECK(failure); -} - -TEST(CheckEqualFailureContainsCorrectDetails) -{ - int line = 0; - RecordingReporter reporter; - { - UnitTest::TestResults testResults(&reporter); - UnitTest::TestDetails const testDetails("testName", "suiteName", "filename", -1); - ScopedCurrentTest scopedResults(testResults, &testDetails); - - CHECK_EQUAL(1, 123); line = __LINE__; - } - - CHECK_EQUAL("testName", reporter.lastFailedTest); - CHECK_EQUAL("suiteName", reporter.lastFailedSuite); - CHECK_EQUAL("filename", reporter.lastFailedFile); - CHECK_EQUAL(line, reporter.lastFailedLine); -} - -int g_sideEffect = 0; -int FunctionWithSideEffects() -{ - ++g_sideEffect; - return 1; -} - -TEST(CheckEqualDoesNotHaveSideEffectsWhenPassing) -{ - g_sideEffect = 0; - { - UnitTest::TestResults testResults; - ScopedCurrentTest scopedResults(testResults); - CHECK_EQUAL(1, FunctionWithSideEffects()); - } - CHECK_EQUAL(1, g_sideEffect); -} - -TEST(CheckEqualDoesNotHaveSideEffectsWhenFailing) -{ - g_sideEffect = 0; - { - UnitTest::TestResults testResults; - ScopedCurrentTest scopedResults(testResults); - CHECK_EQUAL(2, FunctionWithSideEffects()); - } - CHECK_EQUAL(1, g_sideEffect); -} - - -TEST(CheckCloseSucceedsOnEqual) -{ - bool failure = true; - { - RecordingReporter reporter; - UnitTest::TestResults testResults(&reporter); - ScopedCurrentTest scopedResults(testResults); - CHECK_CLOSE (1.0f, 1.001f, 0.01f); - failure = (testResults.GetFailureCount() > 0); - } - - CHECK(!failure); -} - -TEST(CheckCloseFailsOnNotEqual) -{ - bool failure = false; - { - RecordingReporter reporter; - UnitTest::TestResults testResults(&reporter); - ScopedCurrentTest scopedResults(testResults); - CHECK_CLOSE (1.0f, 1.1f, 0.01f); - failure = (testResults.GetFailureCount() > 0); - } - - CHECK(failure); -} - -TEST(CheckCloseFailureContainsCorrectDetails) -{ - int line = 0; - RecordingReporter reporter; - { - UnitTest::TestResults testResults(&reporter); - UnitTest::TestDetails testDetails("test", "suite", "filename", -1); - ScopedCurrentTest scopedResults(testResults, &testDetails); - - CHECK_CLOSE (1.0f, 1.1f, 0.01f); line = __LINE__; - } - - CHECK_EQUAL("test", reporter.lastFailedTest); - CHECK_EQUAL("suite", reporter.lastFailedSuite); - CHECK_EQUAL("filename", reporter.lastFailedFile); - CHECK_EQUAL(line, reporter.lastFailedLine); -} - -TEST(CheckCloseDoesNotHaveSideEffectsWhenPassing) -{ - g_sideEffect = 0; - { - UnitTest::TestResults testResults; - ScopedCurrentTest scopedResults(testResults); - CHECK_CLOSE (1, FunctionWithSideEffects(), 0.1f); - } - CHECK_EQUAL(1, g_sideEffect); -} - -TEST(CheckCloseDoesNotHaveSideEffectsWhenFailing) -{ - g_sideEffect = 0; - { - UnitTest::TestResults testResults; - ScopedCurrentTest scopedResults(testResults); - CHECK_CLOSE (2, FunctionWithSideEffects(), 0.1f); - } - CHECK_EQUAL(1, g_sideEffect); -} - -TEST(CheckArrayCloseSucceedsOnEqual) -{ - bool failure = true; - { - RecordingReporter reporter; - UnitTest::TestResults testResults(&reporter); - ScopedCurrentTest scopedResults(testResults); - const float data[4] = { 0, 1, 2, 3 }; - CHECK_ARRAY_CLOSE (data, data, 4, 0.01f); - failure = (testResults.GetFailureCount() > 0); - } - - CHECK(!failure); -} - -TEST(CheckArrayCloseFailsOnNotEqual) -{ - bool failure = false; - { - RecordingReporter reporter; - UnitTest::TestResults testResults(&reporter); - ScopedCurrentTest scopedResults(testResults); - - int const data1[4] = { 0, 1, 2, 3 }; - int const data2[4] = { 0, 1, 3, 3 }; - CHECK_ARRAY_CLOSE (data1, data2, 4, 0.01f); - - failure = (testResults.GetFailureCount() > 0); - } - - CHECK(failure); -} - -TEST(CheckArrayCloseFailureIncludesCheckExpectedAndActual) -{ - RecordingReporter reporter; - { - UnitTest::TestResults testResults(&reporter); - ScopedCurrentTest scopedResults(testResults); - - int const data1[4] = { 0, 1, 2, 3 }; - int const data2[4] = { 0, 1, 3, 3 }; - CHECK_ARRAY_CLOSE (data1, data2, 4, 0.01f); - } - - CHECK(strstr(reporter.lastFailedMessage, "xpected [ 0 1 2 3 ]")); - CHECK(strstr(reporter.lastFailedMessage, "was [ 0 1 3 3 ]")); -} - -TEST(CheckArrayCloseFailureContainsCorrectDetails) -{ - int line = 0; - RecordingReporter reporter; - { - UnitTest::TestResults testResults(&reporter); - UnitTest::TestDetails testDetails("arrayCloseTest", "arrayCloseSuite", "filename", -1); - ScopedCurrentTest scopedResults(testResults, &testDetails); - - int const data1[4] = { 0, 1, 2, 3 }; - int const data2[4] = { 0, 1, 3, 3 }; - CHECK_ARRAY_CLOSE (data1, data2, 4, 0.01f); line = __LINE__; - } - - CHECK_EQUAL("arrayCloseTest", reporter.lastFailedTest); - CHECK_EQUAL("arrayCloseSuite", reporter.lastFailedSuite); - CHECK_EQUAL("filename", reporter.lastFailedFile); - CHECK_EQUAL(line, reporter.lastFailedLine); -} - -TEST(CheckArrayCloseFailureIncludesTolerance) -{ - RecordingReporter reporter; - { - UnitTest::TestResults testResults(&reporter); - ScopedCurrentTest scopedResults(testResults); - - float const data1[4] = { 0, 1, 2, 3 }; - float const data2[4] = { 0, 1, 3, 3 }; - CHECK_ARRAY_CLOSE (data1, data2, 4, 0.01f); - } - - CHECK(strstr(reporter.lastFailedMessage, "0.01")); -} - -TEST(CheckArrayEqualSuceedsOnEqual) -{ - bool failure = true; - { - RecordingReporter reporter; - UnitTest::TestResults testResults(&reporter); - ScopedCurrentTest scopedResults(testResults); - - const float data[4] = { 0, 1, 2, 3 }; - CHECK_ARRAY_EQUAL (data, data, 4); - - failure = (testResults.GetFailureCount() > 0); - } - - CHECK(!failure); -} - -TEST(CheckArrayEqualFailsOnNotEqual) -{ - bool failure = false; - { - RecordingReporter reporter; - UnitTest::TestResults testResults(&reporter); - ScopedCurrentTest scopedResults(testResults); - - int const data1[4] = { 0, 1, 2, 3 }; - int const data2[4] = { 0, 1, 3, 3 }; - CHECK_ARRAY_EQUAL (data1, data2, 4); - - failure = (testResults.GetFailureCount() > 0); - } - - CHECK(failure); -} - -TEST(CheckArrayEqualFailureIncludesCheckExpectedAndActual) -{ - RecordingReporter reporter; - { - UnitTest::TestResults testResults(&reporter); - ScopedCurrentTest scopedResults(testResults); - - int const data1[4] = { 0, 1, 2, 3 }; - int const data2[4] = { 0, 1, 3, 3 }; - CHECK_ARRAY_EQUAL (data1, data2, 4); - } - - CHECK(strstr(reporter.lastFailedMessage, "xpected [ 0 1 2 3 ]")); - CHECK(strstr(reporter.lastFailedMessage, "was [ 0 1 3 3 ]")); -} - -TEST(CheckArrayEqualFailureContainsCorrectInfo) -{ - int line = 0; - RecordingReporter reporter; - { - UnitTest::TestResults testResults(&reporter); - ScopedCurrentTest scopedResults(testResults); - - int const data1[4] = { 0, 1, 2, 3 }; - int const data2[4] = { 0, 1, 3, 3 }; - CHECK_ARRAY_EQUAL (data1, data2, 4); line = __LINE__; - } - - CHECK_EQUAL("CheckArrayEqualFailureContainsCorrectInfo", reporter.lastFailedTest); - CHECK_EQUAL(__FILE__, reporter.lastFailedFile); - CHECK_EQUAL(line, reporter.lastFailedLine); -} - -float const* FunctionWithSideEffects2() -{ - ++g_sideEffect; - static float const data[] = {1,2,3,4}; - return data; -} - -TEST(CheckArrayCloseDoesNotHaveSideEffectsWhenPassing) -{ - g_sideEffect = 0; - { - UnitTest::TestResults testResults; - ScopedCurrentTest scopedResults(testResults); - - const float data[] = { 0, 1, 2, 3 }; - CHECK_ARRAY_CLOSE (data, FunctionWithSideEffects2(), 4, 0.01f); - } - CHECK_EQUAL(1, g_sideEffect); -} - -TEST(CheckArrayCloseDoesNotHaveSideEffectsWhenFailing) -{ - g_sideEffect = 0; - { - UnitTest::TestResults testResults; - ScopedCurrentTest scopedResults(testResults); - - const float data[] = { 0, 1, 3, 3 }; - CHECK_ARRAY_CLOSE (data, FunctionWithSideEffects2(), 4, 0.01f); - } - - CHECK_EQUAL(1, g_sideEffect); -} - -TEST(CheckArray2DCloseSucceedsOnEqual) -{ - bool failure = true; - { - RecordingReporter reporter; - UnitTest::TestResults testResults(&reporter); - ScopedCurrentTest scopedResults(testResults); - - const float data[2][2] = { {0, 1}, {2, 3} }; - CHECK_ARRAY2D_CLOSE (data, data, 2, 2, 0.01f); - - failure = (testResults.GetFailureCount() > 0); - } - - CHECK(!failure); -} - -TEST(CheckArray2DCloseFailsOnNotEqual) -{ - bool failure = false; - { - RecordingReporter reporter; - UnitTest::TestResults testResults(&reporter); - ScopedCurrentTest scopedResults(testResults); - - int const data1[2][2] = { {0, 1}, {2, 3} }; - int const data2[2][2] = { {0, 1}, {3, 3} }; - CHECK_ARRAY2D_CLOSE (data1, data2, 2, 2, 0.01f); - - failure = (testResults.GetFailureCount() > 0); - } - - CHECK(failure); -} - -TEST(CheckArray2DCloseFailureIncludesCheckExpectedAndActual) -{ - RecordingReporter reporter; - { - UnitTest::TestResults testResults(&reporter); - ScopedCurrentTest scopedResults(testResults); - - int const data1[2][2] = { {0, 1}, {2, 3} }; - int const data2[2][2] = { {0, 1}, {3, 3} }; - - CHECK_ARRAY2D_CLOSE (data1, data2, 2, 2, 0.01f); - } - - CHECK(strstr(reporter.lastFailedMessage, "xpected [ [ 0 1 ] [ 2 3 ] ]")); - CHECK(strstr(reporter.lastFailedMessage, "was [ [ 0 1 ] [ 3 3 ] ]")); -} - -TEST(CheckArray2DCloseFailureContainsCorrectDetails) -{ - int line = 0; - RecordingReporter reporter; - { - UnitTest::TestResults testResults(&reporter); - UnitTest::TestDetails testDetails("array2DCloseTest", "array2DCloseSuite", "filename", -1); - ScopedCurrentTest scopedResults(testResults, &testDetails); - - int const data1[2][2] = { {0, 1}, {2, 3} }; - int const data2[2][2] = { {0, 1}, {3, 3} }; - CHECK_ARRAY2D_CLOSE (data1, data2, 2, 2, 0.01f); line = __LINE__; - } - - CHECK_EQUAL("array2DCloseTest", reporter.lastFailedTest); - CHECK_EQUAL("array2DCloseSuite", reporter.lastFailedSuite); - CHECK_EQUAL("filename", reporter.lastFailedFile); - CHECK_EQUAL(line, reporter.lastFailedLine); -} - -TEST(CheckArray2DCloseFailureIncludesTolerance) -{ - RecordingReporter reporter; - { - UnitTest::TestResults testResults(&reporter); - ScopedCurrentTest scopedResults(testResults); - - float const data1[2][2] = { {0, 1}, {2, 3} }; - float const data2[2][2] = { {0, 1}, {3, 3} }; - CHECK_ARRAY2D_CLOSE (data1, data2, 2, 2, 0.01f); - } - - CHECK(strstr(reporter.lastFailedMessage, "0.01")); -} - -float const* const* FunctionWithSideEffects3() -{ - ++g_sideEffect; - static float const data1[] = {0,1}; - static float const data2[] = {2,3}; - static const float* const data[] = {data1, data2}; - return data; -} - -TEST(CheckArray2DCloseDoesNotHaveSideEffectsWhenPassing) -{ - g_sideEffect = 0; - { - UnitTest::TestResults testResults; - ScopedCurrentTest scopedResults(testResults); - - const float data[2][2] = { {0, 1}, {2, 3} }; - CHECK_ARRAY2D_CLOSE (data, FunctionWithSideEffects3(), 2, 2, 0.01f); - } - CHECK_EQUAL(1, g_sideEffect); -} - -TEST(CheckArray2DCloseDoesNotHaveSideEffectsWhenFailing) -{ - g_sideEffect = 0; - { - UnitTest::TestResults testResults; - ScopedCurrentTest scopedResults(testResults); - - const float data[2][2] = { {0, 1}, {3, 3} }; - CHECK_ARRAY2D_CLOSE (data, FunctionWithSideEffects3(), 2, 2, 0.01f); - } - CHECK_EQUAL(1, g_sideEffect); -} + TEST(CheckSucceedsOnTrue) + { + bool failure = true; + { + RecordingReporter reporter; + UnitTest::TestResults testResults(&reporter); + + ScopedCurrentTest scopedResults(testResults); + CHECK(true); + + failure = (testResults.GetFailureCount() > 0); + } + + CHECK(!failure); + } + + TEST(CheckFailsOnFalse) + { + bool failure = false; + { + RecordingReporter reporter; + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + CHECK(false); + failure = (testResults.GetFailureCount() > 0); + } + + CHECK(failure); + } + + TEST(FailureReportsCorrectTestName) + { + RecordingReporter reporter; + { + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + CHECK(false); + } + + CHECK_EQUAL(m_details.testName, reporter.lastFailedTest); + } + + TEST(CheckFailureIncludesCheckContents) + { + RecordingReporter reporter; + { + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + const bool yaddayadda = false; + CHECK(yaddayadda); + } + + CHECK(strstr(reporter.lastFailedMessage, "yaddayadda")); + } + + TEST(CheckEqualSucceedsOnEqual) + { + bool failure = true; + { + RecordingReporter reporter; + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + CHECK_EQUAL(1, 1); + failure = (testResults.GetFailureCount() > 0); + } + + CHECK(!failure); + } + + TEST(CheckEqualFailsOnNotEqual) + { + bool failure = false; + { + RecordingReporter reporter; + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + CHECK_EQUAL(1, 2); + failure = (testResults.GetFailureCount() > 0); + } + + CHECK(failure); + } + + TEST(CheckEqualFailureContainsCorrectDetails) + { + int line = 0; + RecordingReporter reporter; + { + UnitTest::TestResults testResults(&reporter); + UnitTest::TestDetails const testDetails("testName", "suiteName", "filename", -1); + ScopedCurrentTest scopedResults(testResults, &testDetails); + + CHECK_EQUAL(1, 123); line = __LINE__; + } + + CHECK_EQUAL("testName", reporter.lastFailedTest); + CHECK_EQUAL("suiteName", reporter.lastFailedSuite); + CHECK_EQUAL("filename", reporter.lastFailedFile); + CHECK_EQUAL(line, reporter.lastFailedLine); + } + + int g_sideEffect = 0; + int FunctionWithSideEffects() + { + ++g_sideEffect; + return 1; + } + + TEST(CheckEqualDoesNotHaveSideEffectsWhenPassing) + { + g_sideEffect = 0; + { + UnitTest::TestResults testResults; + ScopedCurrentTest scopedResults(testResults); + CHECK_EQUAL(1, FunctionWithSideEffects()); + } + CHECK_EQUAL(1, g_sideEffect); + } + + TEST(CheckEqualDoesNotHaveSideEffectsWhenFailing) + { + g_sideEffect = 0; + { + UnitTest::TestResults testResults; + ScopedCurrentTest scopedResults(testResults); + CHECK_EQUAL(2, FunctionWithSideEffects()); + } + CHECK_EQUAL(1, g_sideEffect); + } + + + TEST(CheckCloseSucceedsOnEqual) + { + bool failure = true; + { + RecordingReporter reporter; + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + CHECK_CLOSE (1.0f, 1.001f, 0.01f); + failure = (testResults.GetFailureCount() > 0); + } + + CHECK(!failure); + } + + TEST(CheckCloseFailsOnNotEqual) + { + bool failure = false; + { + RecordingReporter reporter; + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + CHECK_CLOSE (1.0f, 1.1f, 0.01f); + failure = (testResults.GetFailureCount() > 0); + } + + CHECK(failure); + } + + TEST(CheckCloseFailureContainsCorrectDetails) + { + int line = 0; + RecordingReporter reporter; + { + UnitTest::TestResults testResults(&reporter); + UnitTest::TestDetails testDetails("test", "suite", "filename", -1); + ScopedCurrentTest scopedResults(testResults, &testDetails); + + CHECK_CLOSE (1.0f, 1.1f, 0.01f); line = __LINE__; + } + + CHECK_EQUAL("test", reporter.lastFailedTest); + CHECK_EQUAL("suite", reporter.lastFailedSuite); + CHECK_EQUAL("filename", reporter.lastFailedFile); + CHECK_EQUAL(line, reporter.lastFailedLine); + } + + TEST(CheckCloseDoesNotHaveSideEffectsWhenPassing) + { + g_sideEffect = 0; + { + UnitTest::TestResults testResults; + ScopedCurrentTest scopedResults(testResults); + CHECK_CLOSE (1, FunctionWithSideEffects(), 0.1f); + } + CHECK_EQUAL(1, g_sideEffect); + } + + TEST(CheckCloseDoesNotHaveSideEffectsWhenFailing) + { + g_sideEffect = 0; + { + UnitTest::TestResults testResults; + ScopedCurrentTest scopedResults(testResults); + CHECK_CLOSE (2, FunctionWithSideEffects(), 0.1f); + } + CHECK_EQUAL(1, g_sideEffect); + } + + TEST(CheckArrayCloseSucceedsOnEqual) + { + bool failure = true; + { + RecordingReporter reporter; + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + const float data[4] = { 0, 1, 2, 3 }; + CHECK_ARRAY_CLOSE (data, data, 4, 0.01f); + failure = (testResults.GetFailureCount() > 0); + } + + CHECK(!failure); + } + + TEST(CheckArrayCloseFailsOnNotEqual) + { + bool failure = false; + { + RecordingReporter reporter; + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + int const data1[4] = { 0, 1, 2, 3 }; + int const data2[4] = { 0, 1, 3, 3 }; + CHECK_ARRAY_CLOSE (data1, data2, 4, 0.01f); + + failure = (testResults.GetFailureCount() > 0); + } + + CHECK(failure); + } + + TEST(CheckArrayCloseFailureIncludesCheckExpectedAndActual) + { + RecordingReporter reporter; + { + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + int const data1[4] = { 0, 1, 2, 3 }; + int const data2[4] = { 0, 1, 3, 3 }; + CHECK_ARRAY_CLOSE (data1, data2, 4, 0.01f); + } + + CHECK(strstr(reporter.lastFailedMessage, "xpected [ 0 1 2 3 ]")); + CHECK(strstr(reporter.lastFailedMessage, "was [ 0 1 3 3 ]")); + } + + TEST(CheckArrayCloseFailureContainsCorrectDetails) + { + int line = 0; + RecordingReporter reporter; + { + UnitTest::TestResults testResults(&reporter); + UnitTest::TestDetails testDetails("arrayCloseTest", "arrayCloseSuite", "filename", -1); + ScopedCurrentTest scopedResults(testResults, &testDetails); + + int const data1[4] = { 0, 1, 2, 3 }; + int const data2[4] = { 0, 1, 3, 3 }; + CHECK_ARRAY_CLOSE (data1, data2, 4, 0.01f); line = __LINE__; + } + + CHECK_EQUAL("arrayCloseTest", reporter.lastFailedTest); + CHECK_EQUAL("arrayCloseSuite", reporter.lastFailedSuite); + CHECK_EQUAL("filename", reporter.lastFailedFile); + CHECK_EQUAL(line, reporter.lastFailedLine); + } + + TEST(CheckArrayCloseFailureIncludesTolerance) + { + RecordingReporter reporter; + { + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + float const data1[4] = { 0, 1, 2, 3 }; + float const data2[4] = { 0, 1, 3, 3 }; + CHECK_ARRAY_CLOSE (data1, data2, 4, 0.01f); + } + + CHECK(strstr(reporter.lastFailedMessage, "0.01")); + } + + TEST(CheckArrayEqualSuceedsOnEqual) + { + bool failure = true; + { + RecordingReporter reporter; + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + const float data[4] = { 0, 1, 2, 3 }; + CHECK_ARRAY_EQUAL (data, data, 4); + + failure = (testResults.GetFailureCount() > 0); + } + + CHECK(!failure); + } + + TEST(CheckArrayEqualFailsOnNotEqual) + { + bool failure = false; + { + RecordingReporter reporter; + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + int const data1[4] = { 0, 1, 2, 3 }; + int const data2[4] = { 0, 1, 3, 3 }; + CHECK_ARRAY_EQUAL (data1, data2, 4); + + failure = (testResults.GetFailureCount() > 0); + } + + CHECK(failure); + } + + TEST(CheckArrayEqualFailureIncludesCheckExpectedAndActual) + { + RecordingReporter reporter; + { + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + int const data1[4] = { 0, 1, 2, 3 }; + int const data2[4] = { 0, 1, 3, 3 }; + CHECK_ARRAY_EQUAL (data1, data2, 4); + } + + CHECK(strstr(reporter.lastFailedMessage, "xpected [ 0 1 2 3 ]")); + CHECK(strstr(reporter.lastFailedMessage, "was [ 0 1 3 3 ]")); + } + + TEST(CheckArrayEqualFailureContainsCorrectInfo) + { + int line = 0; + RecordingReporter reporter; + { + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + int const data1[4] = { 0, 1, 2, 3 }; + int const data2[4] = { 0, 1, 3, 3 }; + CHECK_ARRAY_EQUAL (data1, data2, 4); line = __LINE__; + } + + CHECK_EQUAL("CheckArrayEqualFailureContainsCorrectInfo", reporter.lastFailedTest); + CHECK_EQUAL(__FILE__, reporter.lastFailedFile); + CHECK_EQUAL(line, reporter.lastFailedLine); + } + + float const* FunctionWithSideEffects2() + { + ++g_sideEffect; + static float const data[] = {1,2,3,4}; + return data; + } + + TEST(CheckArrayCloseDoesNotHaveSideEffectsWhenPassing) + { + g_sideEffect = 0; + { + UnitTest::TestResults testResults; + ScopedCurrentTest scopedResults(testResults); + + const float data[] = { 0, 1, 2, 3 }; + CHECK_ARRAY_CLOSE (data, FunctionWithSideEffects2(), 4, 0.01f); + } + CHECK_EQUAL(1, g_sideEffect); + } + + TEST(CheckArrayCloseDoesNotHaveSideEffectsWhenFailing) + { + g_sideEffect = 0; + { + UnitTest::TestResults testResults; + ScopedCurrentTest scopedResults(testResults); + + const float data[] = { 0, 1, 3, 3 }; + CHECK_ARRAY_CLOSE (data, FunctionWithSideEffects2(), 4, 0.01f); + } + + CHECK_EQUAL(1, g_sideEffect); + } + + TEST(CheckArray2DCloseSucceedsOnEqual) + { + bool failure = true; + { + RecordingReporter reporter; + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + const float data[2][2] = { {0, 1}, {2, 3} }; + CHECK_ARRAY2D_CLOSE (data, data, 2, 2, 0.01f); + + failure = (testResults.GetFailureCount() > 0); + } + + CHECK(!failure); + } + + TEST(CheckArray2DCloseFailsOnNotEqual) + { + bool failure = false; + { + RecordingReporter reporter; + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + int const data1[2][2] = { {0, 1}, {2, 3} }; + int const data2[2][2] = { {0, 1}, {3, 3} }; + CHECK_ARRAY2D_CLOSE (data1, data2, 2, 2, 0.01f); + + failure = (testResults.GetFailureCount() > 0); + } + + CHECK(failure); + } + + TEST(CheckArray2DCloseFailureIncludesCheckExpectedAndActual) + { + RecordingReporter reporter; + { + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + int const data1[2][2] = { {0, 1}, {2, 3} }; + int const data2[2][2] = { {0, 1}, {3, 3} }; + + CHECK_ARRAY2D_CLOSE (data1, data2, 2, 2, 0.01f); + } + + CHECK(strstr(reporter.lastFailedMessage, "xpected [ [ 0 1 ] [ 2 3 ] ]")); + CHECK(strstr(reporter.lastFailedMessage, "was [ [ 0 1 ] [ 3 3 ] ]")); + } + + TEST(CheckArray2DCloseFailureContainsCorrectDetails) + { + int line = 0; + RecordingReporter reporter; + { + UnitTest::TestResults testResults(&reporter); + UnitTest::TestDetails testDetails("array2DCloseTest", "array2DCloseSuite", "filename", -1); + ScopedCurrentTest scopedResults(testResults, &testDetails); + + int const data1[2][2] = { {0, 1}, {2, 3} }; + int const data2[2][2] = { {0, 1}, {3, 3} }; + CHECK_ARRAY2D_CLOSE (data1, data2, 2, 2, 0.01f); line = __LINE__; + } + + CHECK_EQUAL("array2DCloseTest", reporter.lastFailedTest); + CHECK_EQUAL("array2DCloseSuite", reporter.lastFailedSuite); + CHECK_EQUAL("filename", reporter.lastFailedFile); + CHECK_EQUAL(line, reporter.lastFailedLine); + } + + TEST(CheckArray2DCloseFailureIncludesTolerance) + { + RecordingReporter reporter; + { + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + float const data1[2][2] = { {0, 1}, {2, 3} }; + float const data2[2][2] = { {0, 1}, {3, 3} }; + CHECK_ARRAY2D_CLOSE (data1, data2, 2, 2, 0.01f); + } + + CHECK(strstr(reporter.lastFailedMessage, "0.01")); + } + + float const* const* FunctionWithSideEffects3() + { + ++g_sideEffect; + static float const data1[] = {0,1}; + static float const data2[] = {2,3}; + static const float* const data[] = {data1, data2}; + return data; + } + + TEST(CheckArray2DCloseDoesNotHaveSideEffectsWhenPassing) + { + g_sideEffect = 0; + { + UnitTest::TestResults testResults; + ScopedCurrentTest scopedResults(testResults); + + const float data[2][2] = { {0, 1}, {2, 3} }; + CHECK_ARRAY2D_CLOSE (data, FunctionWithSideEffects3(), 2, 2, 0.01f); + } + CHECK_EQUAL(1, g_sideEffect); + } + + TEST(CheckArray2DCloseDoesNotHaveSideEffectsWhenFailing) + { + g_sideEffect = 0; + { + UnitTest::TestResults testResults; + ScopedCurrentTest scopedResults(testResults); + + const float data[2][2] = { {0, 1}, {3, 3} }; + CHECK_ARRAY2D_CLOSE (data, FunctionWithSideEffects3(), 2, 2, 0.01f); + } + CHECK_EQUAL(1, g_sideEffect); + } } diff --git a/tests/TestChecks.cpp b/tests/TestChecks.cpp index 9554b31..1ec64ee 100644 --- a/tests/TestChecks.cpp +++ b/tests/TestChecks.cpp @@ -9,310 +9,310 @@ using namespace UnitTest; namespace { -TEST(CheckEqualWithUnsignedLong) -{ - TestResults results; - unsigned long something = 2; - CHECK_EQUAL(something, something); -} - -TEST(CheckEqualsWithStringsFailsOnDifferentStrings) -{ - char txt1[] = "Hello"; - char txt2[] = "Hallo"; - TestResults results; - CheckEqual(results, txt1, txt2, TestDetails("", "", "", 0)); - CHECK_EQUAL(1, results.GetFailureCount()); -} - -char txt1[] = "Hello"; // non-const on purpose so no folding of duplicate data -char txt2[] = "Hello"; - -TEST(CheckEqualsWithStringsWorksOnContentsNonConstNonConst) -{ - char const* const p1 = txt1; - char const* const p2 = txt2; - TestResults results; - CheckEqual(results, p1, p2, TestDetails("", "", "", 0)); - CHECK_EQUAL(0, results.GetFailureCount()); -} - -TEST(CheckEqualsWithStringsWorksOnContentsConstConst) -{ - char* const p1 = txt1; - char* const p2 = txt2; - TestResults results; - CheckEqual(results, p1, p2, TestDetails("", "", "", 0)); - CHECK_EQUAL(0, results.GetFailureCount()); -} - -TEST(CheckEqualsWithStringsWorksOnContentsNonConstConst) -{ - char* const p1 = txt1; - char const* const p2 = txt2; - TestResults results; - CheckEqual(results, p1, p2, TestDetails("", "", "", 0)); - CHECK_EQUAL(0, results.GetFailureCount()); -} - -TEST(CheckEqualsWithStringsWorksOnContentsConstNonConst) -{ - char const* const p1 = txt1; - char* const p2 = txt2; - TestResults results; - CheckEqual(results, p1, p2, TestDetails("", "", "", 0)); - CHECK_EQUAL(0, results.GetFailureCount()); -} - -TEST(CheckEqualsWithStringsWorksOnContentsWithALiteral) -{ - char const* const p1 = txt1; - TestResults results; - CheckEqual(results, "Hello", p1, TestDetails("", "", "", 0)); - CHECK_EQUAL(0, results.GetFailureCount()); -} - -TEST(CheckEqualsWithStringsWorksOnNullExpected) -{ - char const* const expected = "hi"; - char const* const actual = NULL; - TestResults results; - CheckEqual(results, expected, actual, TestDetails("", "", "", 0)); - CHECK_EQUAL (1, results.GetFailureCount()); -} - -TEST(CheckEqualsWithStringsWorksOnNullActual) -{ - char const* const expected = NULL; - char const* const actual = "hi"; - TestResults results; - CheckEqual(results, expected, actual, TestDetails("", "", "", 0)); - CHECK_EQUAL (1, results.GetFailureCount()); -} - -TEST(CheckEqualsWithStringsWorksOnNullExpectedAndActual) -{ - char const* const expected = NULL; - char const* const actual = NULL; - TestResults results; - CheckEqual(results, expected, actual, TestDetails("", "", "", 0)); - CHECK_EQUAL (0, results.GetFailureCount()); -} - -TEST(CheckEqualFailureIncludesCheckExpectedAndActual) -{ - RecordingReporter reporter; - TestResults results(&reporter); - const int something = 2; - CheckEqual(results, 1, something, TestDetails("", "", "", 0)); - - using namespace std; - CHECK(strstr(reporter.lastFailedMessage, "xpected 1")); - CHECK(strstr(reporter.lastFailedMessage, "was 2")); -} - -TEST(CheckEqualFailureIncludesDetails) -{ - RecordingReporter reporter; - TestResults results(&reporter); - TestDetails const details("mytest", "mysuite", "file.h", 101); - - CheckEqual(results, 1, 2, details); - - CHECK_EQUAL("mytest", reporter.lastFailedTest); - CHECK_EQUAL("mysuite", reporter.lastFailedSuite); - CHECK_EQUAL("file.h", reporter.lastFailedFile); - CHECK_EQUAL(101, reporter.lastFailedLine); -} - -TEST(CheckCloseTrue) -{ - TestResults results; - CheckClose(results, 3.001f, 3.0f, 0.1f, TestDetails("", "", "", 0)); - CHECK_EQUAL(0, results.GetFailureCount()); -} - -TEST(CheckCloseFalse) -{ - TestResults results; - CheckClose(results, 3.12f, 3.0f, 0.1f, TestDetails("", "", "", 0)); - CHECK_EQUAL(1, results.GetFailureCount()); -} - -TEST(CheckCloseWithZeroEpsilonWorksForSameNumber) -{ - TestResults results; - CheckClose(results, 0.1f, 0.1f, 0, TestDetails("", "", "", 0)); - CHECK_EQUAL(0, results.GetFailureCount()); -} - -TEST(CheckCloseWithNaNFails) -{ - const unsigned int bitpattern = 0xFFFFFFFF; - float nan; - UNIITEST_NS_QUAL_STD(memcpy)(&nan, &bitpattern, sizeof(bitpattern)); - - TestResults results; - CheckClose(results, 3.0f, nan, 0.1f, TestDetails("", "", "", 0)); - CHECK_EQUAL(1, results.GetFailureCount()); -} - -TEST(CheckCloseWithNaNAgainstItselfFails) -{ - const unsigned int bitpattern = 0xFFFFFFFF; - float nan; - UNIITEST_NS_QUAL_STD(memcpy)(&nan, &bitpattern, sizeof(bitpattern)); - - TestResults results; - CheckClose(results, nan, nan, 0.1f, TestDetails("", "", "", 0)); - CHECK_EQUAL(1, results.GetFailureCount()); -} - -TEST(CheckCloseFailureIncludesCheckExpectedAndActual) -{ - RecordingReporter reporter; - TestResults results(&reporter); - const float expected = 0.9f; - const float actual = 1.1f; - CheckClose(results, expected, actual, 0.01f, TestDetails("", "", "", 0)); - - using namespace std; - CHECK(strstr(reporter.lastFailedMessage, "xpected 0.9")); - CHECK(strstr(reporter.lastFailedMessage, "was 1.1")); -} - -TEST(CheckCloseFailureIncludesTolerance) -{ - RecordingReporter reporter; - TestResults results(&reporter); - CheckClose(results, 2, 3, 0.01f, TestDetails("", "", "", 0)); - - using namespace std; - CHECK(strstr(reporter.lastFailedMessage, "0.01")); -} - -TEST(CheckCloseFailureIncludesDetails) -{ - RecordingReporter reporter; - TestResults results(&reporter); - TestDetails const details("mytest", "mysuite", "header.h", 10); - - CheckClose(results, 2, 3, 0.01f, details); - - CHECK_EQUAL("mytest", reporter.lastFailedTest); - CHECK_EQUAL("mysuite", reporter.lastFailedSuite); - CHECK_EQUAL("header.h", reporter.lastFailedFile); - CHECK_EQUAL(10, reporter.lastFailedLine); -} - - -TEST(CheckArrayEqualTrue) -{ - TestResults results; - - int const array[3] = { 1, 2, 3 }; - CheckArrayEqual(results, array, array, 3, TestDetails("", "", "", 0)); - CHECK_EQUAL(0, results.GetFailureCount()); -} - -TEST(CheckArrayEqualFalse) -{ - TestResults results; - - int const array1[3] = { 1, 2, 3 }; - int const array2[3] = { 1, 2, 2 }; - CheckArrayEqual(results, array1, array2, 3, TestDetails("", "", "", 0)); - CHECK_EQUAL(1, results.GetFailureCount()); -} - -TEST(CheckArrayCloseTrue) -{ - TestResults results; - - float const array1[3] = { 1.0f, 1.5f, 2.0f }; - float const array2[3] = { 1.01f, 1.51f, 2.01f }; - CheckArrayClose(results, array1, array2, 3, 0.02f, TestDetails("", "", "", 0)); - CHECK_EQUAL(0, results.GetFailureCount()); -} - -TEST(CheckArrayCloseFalse) -{ - TestResults results; - - float const array1[3] = { 1.0f, 1.5f, 2.0f }; - float const array2[3] = { 1.01f, 1.51f, 2.01f }; - CheckArrayClose(results, array1, array2, 3, 0.001f, TestDetails("", "", "", 0)); - CHECK_EQUAL(1, results.GetFailureCount()); -} - -TEST(CheckArrayCloseFailureIncludesDetails) -{ - RecordingReporter reporter; - TestResults results(&reporter); - TestDetails const details("arrayCloseTest", "arrayCloseSuite", "file", 1337); - - float const array1[3] = { 1.0f, 1.5f, 2.0f }; - float const array2[3] = { 1.01f, 1.51f, 2.01f }; - CheckArrayClose(results, array1, array2, 3, 0.001f, details); - - CHECK_EQUAL("arrayCloseTest", reporter.lastFailedTest); - CHECK_EQUAL("arrayCloseSuite", reporter.lastFailedSuite); - CHECK_EQUAL("file", reporter.lastFailedFile); - CHECK_EQUAL(1337, reporter.lastFailedLine); -} - - -TEST(CheckArray2DCloseTrue) -{ - TestResults results; - - float const array1[3][3] = { { 1.0f, 1.5f, 2.0f }, - { 2.0f, 2.5f, 3.0f }, - { 3.0f, 3.5f, 4.0f } }; - float const array2[3][3] = { { 1.01f, 1.51f, 2.01f }, - { 2.01f, 2.51f, 3.01f }, - { 3.01f, 3.51f, 4.01f } }; - CheckArray2DClose(results, array1, array2, 3, 3, 0.02f, TestDetails("", "", "", 0)); - CHECK_EQUAL(0, results.GetFailureCount()); -} - -TEST(CheckArray2DCloseFalse) -{ - TestResults results; - - float const array1[3][3] = { { 1.0f, 1.5f, 2.0f }, - { 2.0f, 2.5f, 3.0f }, - { 3.0f, 3.5f, 4.0f } }; - float const array2[3][3] = { { 1.01f, 1.51f, 2.01f }, - { 2.01f, 2.51f, 3.01f }, - { 3.01f, 3.51f, 4.01f } }; - CheckArray2DClose(results, array1, array2, 3, 3, 0.001f, TestDetails("", "", "", 0)); - CHECK_EQUAL(1, results.GetFailureCount()); -} - -TEST(CheckCloseWithDoublesSucceeds) -{ - CHECK_CLOSE(0.5, 0.5, 0.0001); -} - -TEST(CheckArray2DCloseFailureIncludesDetails) -{ - RecordingReporter reporter; - TestResults results(&reporter); - TestDetails const details("array2DCloseTest", "array2DCloseSuite", "file", 1234); - - float const array1[3][3] = { { 1.0f, 1.5f, 2.0f }, - { 2.0f, 2.5f, 3.0f }, - { 3.0f, 3.5f, 4.0f } }; - float const array2[3][3] = { { 1.01f, 1.51f, 2.01f }, - { 2.01f, 2.51f, 3.01f }, - { 3.01f, 3.51f, 4.01f } }; - CheckArray2DClose(results, array1, array2, 3, 3, 0.001f, details); - - CHECK_EQUAL("array2DCloseTest", reporter.lastFailedTest); - CHECK_EQUAL("array2DCloseSuite", reporter.lastFailedSuite); - CHECK_EQUAL("file", reporter.lastFailedFile); - CHECK_EQUAL(1234, reporter.lastFailedLine); -} + TEST(CheckEqualWithUnsignedLong) + { + TestResults results; + unsigned long something = 2; + CHECK_EQUAL(something, something); + } + + TEST(CheckEqualsWithStringsFailsOnDifferentStrings) + { + char txt1[] = "Hello"; + char txt2[] = "Hallo"; + TestResults results; + CheckEqual(results, txt1, txt2, TestDetails("", "", "", 0)); + CHECK_EQUAL(1, results.GetFailureCount()); + } + + char txt1[] = "Hello"; // non-const on purpose so no folding of duplicate data + char txt2[] = "Hello"; + + TEST(CheckEqualsWithStringsWorksOnContentsNonConstNonConst) + { + char const* const p1 = txt1; + char const* const p2 = txt2; + TestResults results; + CheckEqual(results, p1, p2, TestDetails("", "", "", 0)); + CHECK_EQUAL(0, results.GetFailureCount()); + } + + TEST(CheckEqualsWithStringsWorksOnContentsConstConst) + { + char* const p1 = txt1; + char* const p2 = txt2; + TestResults results; + CheckEqual(results, p1, p2, TestDetails("", "", "", 0)); + CHECK_EQUAL(0, results.GetFailureCount()); + } + + TEST(CheckEqualsWithStringsWorksOnContentsNonConstConst) + { + char* const p1 = txt1; + char const* const p2 = txt2; + TestResults results; + CheckEqual(results, p1, p2, TestDetails("", "", "", 0)); + CHECK_EQUAL(0, results.GetFailureCount()); + } + + TEST(CheckEqualsWithStringsWorksOnContentsConstNonConst) + { + char const* const p1 = txt1; + char* const p2 = txt2; + TestResults results; + CheckEqual(results, p1, p2, TestDetails("", "", "", 0)); + CHECK_EQUAL(0, results.GetFailureCount()); + } + + TEST(CheckEqualsWithStringsWorksOnContentsWithALiteral) + { + char const* const p1 = txt1; + TestResults results; + CheckEqual(results, "Hello", p1, TestDetails("", "", "", 0)); + CHECK_EQUAL(0, results.GetFailureCount()); + } + + TEST(CheckEqualsWithStringsWorksOnNullExpected) + { + char const* const expected = "hi"; + char const* const actual = NULL; + TestResults results; + CheckEqual(results, expected, actual, TestDetails("", "", "", 0)); + CHECK_EQUAL (1, results.GetFailureCount()); + } + + TEST(CheckEqualsWithStringsWorksOnNullActual) + { + char const* const expected = NULL; + char const* const actual = "hi"; + TestResults results; + CheckEqual(results, expected, actual, TestDetails("", "", "", 0)); + CHECK_EQUAL (1, results.GetFailureCount()); + } + + TEST(CheckEqualsWithStringsWorksOnNullExpectedAndActual) + { + char const* const expected = NULL; + char const* const actual = NULL; + TestResults results; + CheckEqual(results, expected, actual, TestDetails("", "", "", 0)); + CHECK_EQUAL (0, results.GetFailureCount()); + } + + TEST(CheckEqualFailureIncludesCheckExpectedAndActual) + { + RecordingReporter reporter; + TestResults results(&reporter); + const int something = 2; + CheckEqual(results, 1, something, TestDetails("", "", "", 0)); + + using namespace std; + CHECK(strstr(reporter.lastFailedMessage, "xpected 1")); + CHECK(strstr(reporter.lastFailedMessage, "was 2")); + } + + TEST(CheckEqualFailureIncludesDetails) + { + RecordingReporter reporter; + TestResults results(&reporter); + TestDetails const details("mytest", "mysuite", "file.h", 101); + + CheckEqual(results, 1, 2, details); + + CHECK_EQUAL("mytest", reporter.lastFailedTest); + CHECK_EQUAL("mysuite", reporter.lastFailedSuite); + CHECK_EQUAL("file.h", reporter.lastFailedFile); + CHECK_EQUAL(101, reporter.lastFailedLine); + } + + TEST(CheckCloseTrue) + { + TestResults results; + CheckClose(results, 3.001f, 3.0f, 0.1f, TestDetails("", "", "", 0)); + CHECK_EQUAL(0, results.GetFailureCount()); + } + + TEST(CheckCloseFalse) + { + TestResults results; + CheckClose(results, 3.12f, 3.0f, 0.1f, TestDetails("", "", "", 0)); + CHECK_EQUAL(1, results.GetFailureCount()); + } + + TEST(CheckCloseWithZeroEpsilonWorksForSameNumber) + { + TestResults results; + CheckClose(results, 0.1f, 0.1f, 0, TestDetails("", "", "", 0)); + CHECK_EQUAL(0, results.GetFailureCount()); + } + + TEST(CheckCloseWithNaNFails) + { + const unsigned int bitpattern = 0xFFFFFFFF; + float nan; + UNIITEST_NS_QUAL_STD(memcpy)(&nan, &bitpattern, sizeof(bitpattern)); + + TestResults results; + CheckClose(results, 3.0f, nan, 0.1f, TestDetails("", "", "", 0)); + CHECK_EQUAL(1, results.GetFailureCount()); + } + + TEST(CheckCloseWithNaNAgainstItselfFails) + { + const unsigned int bitpattern = 0xFFFFFFFF; + float nan; + UNIITEST_NS_QUAL_STD(memcpy)(&nan, &bitpattern, sizeof(bitpattern)); + + TestResults results; + CheckClose(results, nan, nan, 0.1f, TestDetails("", "", "", 0)); + CHECK_EQUAL(1, results.GetFailureCount()); + } + + TEST(CheckCloseFailureIncludesCheckExpectedAndActual) + { + RecordingReporter reporter; + TestResults results(&reporter); + const float expected = 0.9f; + const float actual = 1.1f; + CheckClose(results, expected, actual, 0.01f, TestDetails("", "", "", 0)); + + using namespace std; + CHECK(strstr(reporter.lastFailedMessage, "xpected 0.9")); + CHECK(strstr(reporter.lastFailedMessage, "was 1.1")); + } + + TEST(CheckCloseFailureIncludesTolerance) + { + RecordingReporter reporter; + TestResults results(&reporter); + CheckClose(results, 2, 3, 0.01f, TestDetails("", "", "", 0)); + + using namespace std; + CHECK(strstr(reporter.lastFailedMessage, "0.01")); + } + + TEST(CheckCloseFailureIncludesDetails) + { + RecordingReporter reporter; + TestResults results(&reporter); + TestDetails const details("mytest", "mysuite", "header.h", 10); + + CheckClose(results, 2, 3, 0.01f, details); + + CHECK_EQUAL("mytest", reporter.lastFailedTest); + CHECK_EQUAL("mysuite", reporter.lastFailedSuite); + CHECK_EQUAL("header.h", reporter.lastFailedFile); + CHECK_EQUAL(10, reporter.lastFailedLine); + } + + + TEST(CheckArrayEqualTrue) + { + TestResults results; + + int const array[3] = { 1, 2, 3 }; + CheckArrayEqual(results, array, array, 3, TestDetails("", "", "", 0)); + CHECK_EQUAL(0, results.GetFailureCount()); + } + + TEST(CheckArrayEqualFalse) + { + TestResults results; + + int const array1[3] = { 1, 2, 3 }; + int const array2[3] = { 1, 2, 2 }; + CheckArrayEqual(results, array1, array2, 3, TestDetails("", "", "", 0)); + CHECK_EQUAL(1, results.GetFailureCount()); + } + + TEST(CheckArrayCloseTrue) + { + TestResults results; + + float const array1[3] = { 1.0f, 1.5f, 2.0f }; + float const array2[3] = { 1.01f, 1.51f, 2.01f }; + CheckArrayClose(results, array1, array2, 3, 0.02f, TestDetails("", "", "", 0)); + CHECK_EQUAL(0, results.GetFailureCount()); + } + + TEST(CheckArrayCloseFalse) + { + TestResults results; + + float const array1[3] = { 1.0f, 1.5f, 2.0f }; + float const array2[3] = { 1.01f, 1.51f, 2.01f }; + CheckArrayClose(results, array1, array2, 3, 0.001f, TestDetails("", "", "", 0)); + CHECK_EQUAL(1, results.GetFailureCount()); + } + + TEST(CheckArrayCloseFailureIncludesDetails) + { + RecordingReporter reporter; + TestResults results(&reporter); + TestDetails const details("arrayCloseTest", "arrayCloseSuite", "file", 1337); + + float const array1[3] = { 1.0f, 1.5f, 2.0f }; + float const array2[3] = { 1.01f, 1.51f, 2.01f }; + CheckArrayClose(results, array1, array2, 3, 0.001f, details); + + CHECK_EQUAL("arrayCloseTest", reporter.lastFailedTest); + CHECK_EQUAL("arrayCloseSuite", reporter.lastFailedSuite); + CHECK_EQUAL("file", reporter.lastFailedFile); + CHECK_EQUAL(1337, reporter.lastFailedLine); + } + + + TEST(CheckArray2DCloseTrue) + { + TestResults results; + + float const array1[3][3] = { { 1.0f, 1.5f, 2.0f }, + { 2.0f, 2.5f, 3.0f }, + { 3.0f, 3.5f, 4.0f } }; + float const array2[3][3] = { { 1.01f, 1.51f, 2.01f }, + { 2.01f, 2.51f, 3.01f }, + { 3.01f, 3.51f, 4.01f } }; + CheckArray2DClose(results, array1, array2, 3, 3, 0.02f, TestDetails("", "", "", 0)); + CHECK_EQUAL(0, results.GetFailureCount()); + } + + TEST(CheckArray2DCloseFalse) + { + TestResults results; + + float const array1[3][3] = { { 1.0f, 1.5f, 2.0f }, + { 2.0f, 2.5f, 3.0f }, + { 3.0f, 3.5f, 4.0f } }; + float const array2[3][3] = { { 1.01f, 1.51f, 2.01f }, + { 2.01f, 2.51f, 3.01f }, + { 3.01f, 3.51f, 4.01f } }; + CheckArray2DClose(results, array1, array2, 3, 3, 0.001f, TestDetails("", "", "", 0)); + CHECK_EQUAL(1, results.GetFailureCount()); + } + + TEST(CheckCloseWithDoublesSucceeds) + { + CHECK_CLOSE(0.5, 0.5, 0.0001); + } + + TEST(CheckArray2DCloseFailureIncludesDetails) + { + RecordingReporter reporter; + TestResults results(&reporter); + TestDetails const details("array2DCloseTest", "array2DCloseSuite", "file", 1234); + + float const array1[3][3] = { { 1.0f, 1.5f, 2.0f }, + { 2.0f, 2.5f, 3.0f }, + { 3.0f, 3.5f, 4.0f } }; + float const array2[3][3] = { { 1.01f, 1.51f, 2.01f }, + { 2.01f, 2.51f, 3.01f }, + { 3.01f, 3.51f, 4.01f } }; + CheckArray2DClose(results, array1, array2, 3, 3, 0.001f, details); + + CHECK_EQUAL("array2DCloseTest", reporter.lastFailedTest); + CHECK_EQUAL("array2DCloseSuite", reporter.lastFailedSuite); + CHECK_EQUAL("file", reporter.lastFailedFile); + CHECK_EQUAL(1234, reporter.lastFailedLine); + } } diff --git a/tests/TestCompositeTestReporter.cpp b/tests/TestCompositeTestReporter.cpp index 9440a9e..d2be918 100644 --- a/tests/TestCompositeTestReporter.cpp +++ b/tests/TestCompositeTestReporter.cpp @@ -5,172 +5,171 @@ using namespace UnitTest; namespace { -TEST(ZeroReportersByDefault) -{ - CHECK_EQUAL(0, CompositeTestReporter().GetReporterCount()); -} - -struct MockReporter : TestReporter -{ - MockReporter() - : testStartCalled(false) - , testStartDetails(NULL) - , failureCalled(false) - , failureDetails(NULL) - , failureStr(NULL) - , testFinishCalled(false) - , testFinishDetails(NULL) - , testFinishSecondsElapsed(-1.0f) - , summaryCalled(false) - , summaryTotalTestCount(-1) - , summaryFailureCount(-1) - , summarySecondsElapsed(-1.0f) - { - } - - virtual void ReportTestStart(TestDetails const& test) - { - testStartCalled = true; - testStartDetails = &test; - } - - virtual void ReportFailure(TestDetails const& test, char const* failure) - { - failureCalled = true; - failureDetails = &test; - failureStr = failure; - } - - virtual void ReportTestFinish(TestDetails const& test, float secondsElapsed) - { - testFinishCalled = true; - testFinishDetails = &test; - testFinishSecondsElapsed = secondsElapsed; - } - - virtual void ReportSummary(int totalTestCount, - int failedTestCount, - int failureCount, - float secondsElapsed) - { - summaryCalled = true; - summaryTotalTestCount = totalTestCount; - summaryFailedTestCount = failedTestCount; - summaryFailureCount = failureCount; - summarySecondsElapsed = secondsElapsed; - } - - bool testStartCalled; - TestDetails const* testStartDetails; - - bool failureCalled; - TestDetails const* failureDetails; - const char* failureStr; - - bool testFinishCalled; - TestDetails const* testFinishDetails; - float testFinishSecondsElapsed; - - bool summaryCalled; - int summaryTotalTestCount; - int summaryFailedTestCount; - int summaryFailureCount; - float summarySecondsElapsed; -}; - -TEST(AddReporter) -{ - MockReporter r; - CompositeTestReporter c; - - CHECK(c.AddReporter(&r)); - CHECK_EQUAL(1, c.GetReporterCount()); -} - -TEST(RemoveReporter) -{ - MockReporter r; - CompositeTestReporter c; - - c.AddReporter(&r); - CHECK(c.RemoveReporter(&r)); - CHECK_EQUAL(0, c.GetReporterCount()); -} - -struct Fixture -{ - Fixture() - { - c.AddReporter(&r0); - c.AddReporter(&r1); - } - - MockReporter r0, r1; - CompositeTestReporter c; -}; - -TEST_FIXTURE(Fixture, ReportTestStartCallsReportTestStartOnAllAggregates) -{ - TestDetails t("", "", "", 0); - c.ReportTestStart(t); - - CHECK(r0.testStartCalled); - CHECK_EQUAL(&t, r0.testStartDetails); - CHECK(r1.testStartCalled); - CHECK_EQUAL(&t, r1.testStartDetails); -} - -TEST_FIXTURE(Fixture, ReportFailureCallsReportFailureOnAllAggregates) -{ - TestDetails t("", "", "", 0); - const char* failStr = "fail"; - c.ReportFailure(t, failStr); - - CHECK(r0.failureCalled); - CHECK_EQUAL(&t, r0.failureDetails); - CHECK_EQUAL(failStr, r0.failureStr); - - CHECK(r1.failureCalled); - CHECK_EQUAL(&t, r1.failureDetails); - CHECK_EQUAL(failStr, r1.failureStr); -} - -TEST_FIXTURE(Fixture, ReportTestFinishCallsReportTestFinishOnAllAggregates) -{ - TestDetails t("", "", "", 0); - const float s = 1.2345f; - c.ReportTestFinish(t, s); - - CHECK(r0.testFinishCalled); - CHECK_EQUAL(&t, r0.testFinishDetails); - CHECK_CLOSE(s, r0.testFinishSecondsElapsed, 0.00001f); - - CHECK(r1.testFinishCalled); - CHECK_EQUAL(&t, r1.testFinishDetails); - CHECK_CLOSE(s, r1.testFinishSecondsElapsed, 0.00001f); -} - -TEST_FIXTURE(Fixture, ReportSummaryCallsReportSummaryOnAllAggregates) -{ - TestDetails t("", "", "", 0); - const int testCount = 3; - const int failedTestCount = 4; - const int failureCount = 5; - const float secondsElapsed = 3.14159f; - - c.ReportSummary(testCount, failedTestCount, failureCount, secondsElapsed); - - CHECK(r0.summaryCalled); - CHECK_EQUAL(testCount, r0.summaryTotalTestCount); - CHECK_EQUAL(failedTestCount, r0.summaryFailedTestCount); - CHECK_EQUAL(failureCount, r0.summaryFailureCount); - CHECK_CLOSE(secondsElapsed, r0.summarySecondsElapsed, 0.00001f); - - CHECK(r1.summaryCalled); - CHECK_EQUAL(testCount, r1.summaryTotalTestCount); - CHECK_EQUAL(failedTestCount, r1.summaryFailedTestCount); - CHECK_EQUAL(failureCount, r1.summaryFailureCount); - CHECK_CLOSE(secondsElapsed, r1.summarySecondsElapsed, 0.00001f); -} + TEST(ZeroReportersByDefault) + { + CHECK_EQUAL(0, CompositeTestReporter().GetReporterCount()); + } + + struct MockReporter : TestReporter + { + MockReporter() + : testStartCalled(false) + , testStartDetails(NULL) + , failureCalled(false) + , failureDetails(NULL) + , failureStr(NULL) + , testFinishCalled(false) + , testFinishDetails(NULL) + , testFinishSecondsElapsed(-1.0f) + , summaryCalled(false) + , summaryTotalTestCount(-1) + , summaryFailureCount(-1) + , summarySecondsElapsed(-1.0f) + {} + + virtual void ReportTestStart(TestDetails const& test) + { + testStartCalled = true; + testStartDetails = &test; + } + + virtual void ReportFailure(TestDetails const& test, char const* failure) + { + failureCalled = true; + failureDetails = &test; + failureStr = failure; + } + + virtual void ReportTestFinish(TestDetails const& test, float secondsElapsed) + { + testFinishCalled = true; + testFinishDetails = &test; + testFinishSecondsElapsed = secondsElapsed; + } + + virtual void ReportSummary(int totalTestCount, + int failedTestCount, + int failureCount, + float secondsElapsed) + { + summaryCalled = true; + summaryTotalTestCount = totalTestCount; + summaryFailedTestCount = failedTestCount; + summaryFailureCount = failureCount; + summarySecondsElapsed = secondsElapsed; + } + + bool testStartCalled; + TestDetails const* testStartDetails; + + bool failureCalled; + TestDetails const* failureDetails; + const char* failureStr; + + bool testFinishCalled; + TestDetails const* testFinishDetails; + float testFinishSecondsElapsed; + + bool summaryCalled; + int summaryTotalTestCount; + int summaryFailedTestCount; + int summaryFailureCount; + float summarySecondsElapsed; + }; + + TEST(AddReporter) + { + MockReporter r; + CompositeTestReporter c; + + CHECK(c.AddReporter(&r)); + CHECK_EQUAL(1, c.GetReporterCount()); + } + + TEST(RemoveReporter) + { + MockReporter r; + CompositeTestReporter c; + + c.AddReporter(&r); + CHECK(c.RemoveReporter(&r)); + CHECK_EQUAL(0, c.GetReporterCount()); + } + + struct Fixture + { + Fixture() + { + c.AddReporter(&r0); + c.AddReporter(&r1); + } + + MockReporter r0, r1; + CompositeTestReporter c; + }; + + TEST_FIXTURE(Fixture, ReportTestStartCallsReportTestStartOnAllAggregates) + { + TestDetails t("", "", "", 0); + c.ReportTestStart(t); + + CHECK(r0.testStartCalled); + CHECK_EQUAL(&t, r0.testStartDetails); + CHECK(r1.testStartCalled); + CHECK_EQUAL(&t, r1.testStartDetails); + } + + TEST_FIXTURE(Fixture, ReportFailureCallsReportFailureOnAllAggregates) + { + TestDetails t("", "", "", 0); + const char* failStr = "fail"; + c.ReportFailure(t, failStr); + + CHECK(r0.failureCalled); + CHECK_EQUAL(&t, r0.failureDetails); + CHECK_EQUAL(failStr, r0.failureStr); + + CHECK(r1.failureCalled); + CHECK_EQUAL(&t, r1.failureDetails); + CHECK_EQUAL(failStr, r1.failureStr); + } + + TEST_FIXTURE(Fixture, ReportTestFinishCallsReportTestFinishOnAllAggregates) + { + TestDetails t("", "", "", 0); + const float s = 1.2345f; + c.ReportTestFinish(t, s); + + CHECK(r0.testFinishCalled); + CHECK_EQUAL(&t, r0.testFinishDetails); + CHECK_CLOSE(s, r0.testFinishSecondsElapsed, 0.00001f); + + CHECK(r1.testFinishCalled); + CHECK_EQUAL(&t, r1.testFinishDetails); + CHECK_CLOSE(s, r1.testFinishSecondsElapsed, 0.00001f); + } + + TEST_FIXTURE(Fixture, ReportSummaryCallsReportSummaryOnAllAggregates) + { + TestDetails t("", "", "", 0); + const int testCount = 3; + const int failedTestCount = 4; + const int failureCount = 5; + const float secondsElapsed = 3.14159f; + + c.ReportSummary(testCount, failedTestCount, failureCount, secondsElapsed); + + CHECK(r0.summaryCalled); + CHECK_EQUAL(testCount, r0.summaryTotalTestCount); + CHECK_EQUAL(failedTestCount, r0.summaryFailedTestCount); + CHECK_EQUAL(failureCount, r0.summaryFailureCount); + CHECK_CLOSE(secondsElapsed, r0.summarySecondsElapsed, 0.00001f); + + CHECK(r1.summaryCalled); + CHECK_EQUAL(testCount, r1.summaryTotalTestCount); + CHECK_EQUAL(failedTestCount, r1.summaryFailedTestCount); + CHECK_EQUAL(failureCount, r1.summaryFailureCount); + CHECK_CLOSE(secondsElapsed, r1.summarySecondsElapsed, 0.00001f); + } } diff --git a/tests/TestCurrentTest.cpp b/tests/TestCurrentTest.cpp index 7de1a72..6cfb99d 100644 --- a/tests/TestCurrentTest.cpp +++ b/tests/TestCurrentTest.cpp @@ -2,37 +2,37 @@ #include "UnitTest++/CurrentTest.h" #include "ScopedCurrentTest.h" -namespace +namespace { -TEST(CanSetandGetDetails) -{ - bool ok = false; - { - ScopedCurrentTest scopedTest; + TEST(CanSetandGetDetails) + { + bool ok = false; + { + ScopedCurrentTest scopedTest; - const UnitTest::TestDetails* details = reinterpret_cast< const UnitTest::TestDetails* >(12345); - UnitTest::CurrentTest::Details() = details; + const UnitTest::TestDetails* details = reinterpret_cast< const UnitTest::TestDetails* >(12345); + UnitTest::CurrentTest::Details() = details; - ok = (UnitTest::CurrentTest::Details() == details); - } + ok = (UnitTest::CurrentTest::Details() == details); + } - CHECK(ok); -} + CHECK(ok); + } -TEST(CanSetAndGetResults) -{ - bool ok = false; - { - ScopedCurrentTest scopedTest; + TEST(CanSetAndGetResults) + { + bool ok = false; + { + ScopedCurrentTest scopedTest; - UnitTest::TestResults results; - UnitTest::CurrentTest::Results() = &results; + UnitTest::TestResults results; + UnitTest::CurrentTest::Results() = &results; - ok = (UnitTest::CurrentTest::Results() == &results); - } + ok = (UnitTest::CurrentTest::Results() == &results); + } - CHECK(ok); -} + CHECK(ok); + } } diff --git a/tests/TestDeferredTestReporter.cpp b/tests/TestDeferredTestReporter.cpp index 60c347c..46b4d63 100644 --- a/tests/TestDeferredTestReporter.cpp +++ b/tests/TestDeferredTestReporter.cpp @@ -9,114 +9,113 @@ namespace UnitTest { -namespace -{ + namespace + { #ifndef UNITTEST_MEMORYOUTSTREAM_IS_STD_OSTRINGSTREAM - MemoryOutStream& operator <<(MemoryOutStream& lhs, const std::string& rhs) - { - lhs << rhs.c_str(); - return lhs; - } + MemoryOutStream& operator <<(MemoryOutStream& lhs, const std::string& rhs) + { + lhs << rhs.c_str(); + return lhs; + } #endif -struct MockDeferredTestReporter : public DeferredTestReporter -{ - virtual void ReportSummary(int, int, int, float) - { - } -}; - -struct DeferredTestReporterFixture -{ - DeferredTestReporterFixture() - : testName("UniqueTestName") - , testSuite("UniqueTestSuite") - , fileName("filename.h") - , lineNumber(12) - , details(testName.c_str(), testSuite.c_str(), fileName.c_str(), lineNumber) - { - } - - MockDeferredTestReporter reporter; - std::string const testName; - std::string const testSuite; - std::string const fileName; - int const lineNumber; - TestDetails const details; -}; - -TEST_FIXTURE(DeferredTestReporterFixture, ReportTestStartCreatesANewDeferredTest) -{ - reporter.ReportTestStart(details); - CHECK_EQUAL(1, (int)reporter.GetResults().size()); -} - -TEST_FIXTURE(DeferredTestReporterFixture, ReportTestStartCapturesTestNameAndSuite) -{ - reporter.ReportTestStart(details); - - DeferredTestResult const& result = reporter.GetResults().at(0); - CHECK_EQUAL(testName.c_str(), result.testName.c_str()); - CHECK_EQUAL(testSuite.c_str(), result.suiteName.c_str()); -} - -TEST_FIXTURE(DeferredTestReporterFixture, ReportTestEndCapturesTestTime) -{ - float const elapsed = 123.45f; - reporter.ReportTestStart(details); - reporter.ReportTestFinish(details, elapsed); - - DeferredTestResult const& result = reporter.GetResults().at(0); - CHECK_CLOSE(elapsed, result.timeElapsed, 0.0001f); -} - -TEST_FIXTURE(DeferredTestReporterFixture, ReportFailureSavesFailureDetails) -{ - char const* failure = "failure"; - - reporter.ReportTestStart(details); - reporter.ReportFailure(details, failure); - - DeferredTestResult const& result = reporter.GetResults().at(0); - CHECK(result.failed == true); - CHECK_EQUAL(fileName.c_str(), result.failureFile.c_str()); -} - -TEST_FIXTURE(DeferredTestReporterFixture, ReportFailureSavesFailureDetailsForMultipleFailures) -{ - char const* failure1 = "failure 1"; - char const* failure2 = "failure 2"; - - reporter.ReportTestStart(details); - reporter.ReportFailure(details, failure1); - reporter.ReportFailure(details, failure2); - - DeferredTestResult const& result = reporter.GetResults().at(0); - CHECK_EQUAL(2, (int)result.failures.size()); - CHECK_EQUAL(failure1, result.failures[0].failureStr); - CHECK_EQUAL(failure2, result.failures[1].failureStr); + struct MockDeferredTestReporter : public DeferredTestReporter + { + virtual void ReportSummary(int, int, int, float) + {} + }; + + struct DeferredTestReporterFixture + { + DeferredTestReporterFixture() + : testName("UniqueTestName") + , testSuite("UniqueTestSuite") + , fileName("filename.h") + , lineNumber(12) + , details(testName.c_str(), testSuite.c_str(), fileName.c_str(), lineNumber) + {} + + MockDeferredTestReporter reporter; + std::string const testName; + std::string const testSuite; + std::string const fileName; + int const lineNumber; + TestDetails const details; + }; + + TEST_FIXTURE(DeferredTestReporterFixture, ReportTestStartCreatesANewDeferredTest) + { + reporter.ReportTestStart(details); + CHECK_EQUAL(1, (int)reporter.GetResults().size()); + } + + TEST_FIXTURE(DeferredTestReporterFixture, ReportTestStartCapturesTestNameAndSuite) + { + reporter.ReportTestStart(details); + + DeferredTestResult const& result = reporter.GetResults().at(0); + CHECK_EQUAL(testName.c_str(), result.testName.c_str()); + CHECK_EQUAL(testSuite.c_str(), result.suiteName.c_str()); + } + + TEST_FIXTURE(DeferredTestReporterFixture, ReportTestEndCapturesTestTime) + { + float const elapsed = 123.45f; + reporter.ReportTestStart(details); + reporter.ReportTestFinish(details, elapsed); + + DeferredTestResult const& result = reporter.GetResults().at(0); + CHECK_CLOSE(elapsed, result.timeElapsed, 0.0001f); + } + + TEST_FIXTURE(DeferredTestReporterFixture, ReportFailureSavesFailureDetails) + { + char const* failure = "failure"; + + reporter.ReportTestStart(details); + reporter.ReportFailure(details, failure); + + DeferredTestResult const& result = reporter.GetResults().at(0); + CHECK(result.failed == true); + CHECK_EQUAL(fileName.c_str(), result.failureFile.c_str()); + } + + TEST_FIXTURE(DeferredTestReporterFixture, ReportFailureSavesFailureDetailsForMultipleFailures) + { + char const* failure1 = "failure 1"; + char const* failure2 = "failure 2"; + + reporter.ReportTestStart(details); + reporter.ReportFailure(details, failure1); + reporter.ReportFailure(details, failure2); + + DeferredTestResult const& result = reporter.GetResults().at(0); + CHECK_EQUAL(2, (int)result.failures.size()); + CHECK_EQUAL(failure1, result.failures[0].failureStr); + CHECK_EQUAL(failure2, result.failures[1].failureStr); + } + + TEST_FIXTURE(DeferredTestReporterFixture, DeferredTestReporterTakesCopyOfFailureMessage) + { + reporter.ReportTestStart(details); + + char failureMessage[128]; + char const* goodStr = "Real failure message"; + char const* badStr = "Bogus failure message"; + + using namespace std; + + strcpy(failureMessage, goodStr); + reporter.ReportFailure(details, failureMessage); + strcpy(failureMessage, badStr); + + DeferredTestResult const& result = reporter.GetResults().at(0); + DeferredTestFailure const& failure = result.failures.at(0); + CHECK_EQUAL(goodStr, failure.failureStr); + } + + } } -TEST_FIXTURE(DeferredTestReporterFixture, DeferredTestReporterTakesCopyOfFailureMessage) -{ - reporter.ReportTestStart(details); - - char failureMessage[128]; - char const* goodStr = "Real failure message"; - char const* badStr = "Bogus failure message"; - - using namespace std; - - strcpy(failureMessage, goodStr); - reporter.ReportFailure(details, failureMessage); - strcpy(failureMessage, badStr); - - DeferredTestResult const& result = reporter.GetResults().at(0); - DeferredTestFailure const& failure = result.failures.at(0); - CHECK_EQUAL(goodStr, failure.failureStr); -} - -}} - #endif diff --git a/tests/TestExceptions.cpp b/tests/TestExceptions.cpp index 1a0ab04..972e64d 100644 --- a/tests/TestExceptions.cpp +++ b/tests/TestExceptions.cpp @@ -12,552 +12,546 @@ using namespace std; namespace { -int ThrowingFunction() -{ - throw "Doh"; -} - -int ThrowingStdExceptionFunction() -{ - throw std::logic_error("Doh"); -} - -SUITE(CheckExceptionTests) -{ - struct CheckFixture - { - CheckFixture() - : reporter() - , testResults(&reporter) - { - } - - void PerformCheckWithNonStdThrow() - { + int ThrowingFunction() + { + throw "Doh"; + } + + int ThrowingStdExceptionFunction() + { + throw std::logic_error("Doh"); + } + + SUITE(CheckExceptionTests) + { + struct CheckFixture + { + CheckFixture() + : reporter() + , testResults(&reporter) + {} + + void PerformCheckWithNonStdThrow() + { ScopedCurrentTest scopedResults(testResults); CHECK(ThrowingFunction() == 1); - } + } - void PerformCheckWithStdThrow() - { + void PerformCheckWithStdThrow() + { ScopedCurrentTest scopedResults(testResults); CHECK(ThrowingStdExceptionFunction() == 1); - } - - RecordingReporter reporter; - UnitTest::TestResults testResults; - }; - - TEST_FIXTURE(CheckFixture, CheckFailsOnException) - { - PerformCheckWithNonStdThrow(); - CHECK(testResults.GetFailureCount() > 0); - } - - TEST_FIXTURE(CheckFixture, CheckFailsOnStdException) - { - PerformCheckWithStdThrow(); - CHECK(testResults.GetFailureCount() > 0); - } - - TEST_FIXTURE(CheckFixture, CheckFailureBecauseOfExceptionIncludesCheckContents) - { - PerformCheckWithNonStdThrow(); - CHECK(strstr(reporter.lastFailedMessage, "ThrowingFunction() == 1")); - } - - TEST_FIXTURE(CheckFixture, CheckFailureBecauseOfStdExceptionIncludesCheckContents) - { - PerformCheckWithStdThrow(); - CHECK(strstr(reporter.lastFailedMessage, "ThrowingStdExceptionFunction() == 1")); - } - - TEST_FIXTURE(CheckFixture, CheckFailureBecauseOfStandardExceptionIncludesWhat) - { - PerformCheckWithStdThrow(); - CHECK(strstr(reporter.lastFailedMessage, "exception (Doh)")); - } -} - -SUITE(CheckEqualExceptionTests) -{ - struct CheckEqualFixture - { - CheckEqualFixture() - : reporter() - , testResults(&reporter) - , line(-1) - { - } - - void PerformCheckWithNonStdThrow() - { + } + + RecordingReporter reporter; + UnitTest::TestResults testResults; + }; + + TEST_FIXTURE(CheckFixture, CheckFailsOnException) + { + PerformCheckWithNonStdThrow(); + CHECK(testResults.GetFailureCount() > 0); + } + + TEST_FIXTURE(CheckFixture, CheckFailsOnStdException) + { + PerformCheckWithStdThrow(); + CHECK(testResults.GetFailureCount() > 0); + } + + TEST_FIXTURE(CheckFixture, CheckFailureBecauseOfExceptionIncludesCheckContents) + { + PerformCheckWithNonStdThrow(); + CHECK(strstr(reporter.lastFailedMessage, "ThrowingFunction() == 1")); + } + + TEST_FIXTURE(CheckFixture, CheckFailureBecauseOfStdExceptionIncludesCheckContents) + { + PerformCheckWithStdThrow(); + CHECK(strstr(reporter.lastFailedMessage, "ThrowingStdExceptionFunction() == 1")); + } + + TEST_FIXTURE(CheckFixture, CheckFailureBecauseOfStandardExceptionIncludesWhat) + { + PerformCheckWithStdThrow(); + CHECK(strstr(reporter.lastFailedMessage, "exception (Doh)")); + } + } + + SUITE(CheckEqualExceptionTests) + { + struct CheckEqualFixture + { + CheckEqualFixture() + : reporter() + , testResults(&reporter) + , line(-1) + {} + + void PerformCheckWithNonStdThrow() + { UnitTest::TestDetails const testDetails("testName", "suiteName", "filename", -1); ScopedCurrentTest scopedResults(testResults, &testDetails); CHECK_EQUAL(ThrowingFunction(), 123); line = __LINE__; - } + } - void PerformCheckWithStdThrow() - { + void PerformCheckWithStdThrow() + { UnitTest::TestDetails const testDetails("testName", "suiteName", "filename", -1); ScopedCurrentTest scopedResults(testResults, &testDetails); CHECK_EQUAL(ThrowingStdExceptionFunction(), 123); line = __LINE__; - } - - RecordingReporter reporter; - UnitTest::TestResults testResults; - int line; - }; - - TEST_FIXTURE(CheckEqualFixture, CheckEqualFailsOnException) - { - PerformCheckWithNonStdThrow(); - CHECK(testResults.GetFailureCount() > 0); - } - - TEST_FIXTURE(CheckEqualFixture, CheckEqualFailsOnStdException) - { - PerformCheckWithStdThrow(); - CHECK(testResults.GetFailureCount() > 0); - } - - TEST_FIXTURE(CheckEqualFixture, CheckEqualFailureBecauseOfExceptionContainsCorrectDetails) - { - PerformCheckWithNonStdThrow(); - - CHECK_EQUAL("testName", reporter.lastFailedTest); - CHECK_EQUAL("suiteName", reporter.lastFailedSuite); - CHECK_EQUAL("filename", reporter.lastFailedFile); - CHECK_EQUAL(line, reporter.lastFailedLine); - } - - TEST_FIXTURE(CheckEqualFixture, CheckEqualFailureBecauseOfStdExceptionContainsCorrectDetails) - { - PerformCheckWithStdThrow(); - - CHECK_EQUAL("testName", reporter.lastFailedTest); - CHECK_EQUAL("suiteName", reporter.lastFailedSuite); - CHECK_EQUAL("filename", reporter.lastFailedFile); - CHECK_EQUAL(line, reporter.lastFailedLine); - } - - TEST_FIXTURE(CheckEqualFixture, CheckEqualFailureBecauseOfExceptionIncludesCheckContents) - { - PerformCheckWithNonStdThrow(); - - CHECK(strstr(reporter.lastFailedMessage, "ThrowingFunction()")); - CHECK(strstr(reporter.lastFailedMessage, "123")); - } - - TEST_FIXTURE(CheckEqualFixture, CheckEqualFailureBecauseOfStdExceptionIncludesCheckContents) - { - PerformCheckWithStdThrow(); - - CHECK(strstr(reporter.lastFailedMessage, "ThrowingStdExceptionFunction()")); - CHECK(strstr(reporter.lastFailedMessage, "123")); - } - - TEST_FIXTURE(CheckEqualFixture, CheckEqualFailureBecauseOfStandardExceptionIncludesWhat) - { - PerformCheckWithStdThrow(); - - CHECK(strstr(reporter.lastFailedMessage, "exception (Doh)")); - } -} - -SUITE(CheckCloseExceptionTests) -{ - struct CheckCloseFixture - { - CheckCloseFixture() - : reporter() - , testResults(&reporter) - , line(-1) - { - } - - void PerformCheckWithNonStdThrow() - { + } + + RecordingReporter reporter; + UnitTest::TestResults testResults; + int line; + }; + + TEST_FIXTURE(CheckEqualFixture, CheckEqualFailsOnException) + { + PerformCheckWithNonStdThrow(); + CHECK(testResults.GetFailureCount() > 0); + } + + TEST_FIXTURE(CheckEqualFixture, CheckEqualFailsOnStdException) + { + PerformCheckWithStdThrow(); + CHECK(testResults.GetFailureCount() > 0); + } + + TEST_FIXTURE(CheckEqualFixture, CheckEqualFailureBecauseOfExceptionContainsCorrectDetails) + { + PerformCheckWithNonStdThrow(); + + CHECK_EQUAL("testName", reporter.lastFailedTest); + CHECK_EQUAL("suiteName", reporter.lastFailedSuite); + CHECK_EQUAL("filename", reporter.lastFailedFile); + CHECK_EQUAL(line, reporter.lastFailedLine); + } + + TEST_FIXTURE(CheckEqualFixture, CheckEqualFailureBecauseOfStdExceptionContainsCorrectDetails) + { + PerformCheckWithStdThrow(); + + CHECK_EQUAL("testName", reporter.lastFailedTest); + CHECK_EQUAL("suiteName", reporter.lastFailedSuite); + CHECK_EQUAL("filename", reporter.lastFailedFile); + CHECK_EQUAL(line, reporter.lastFailedLine); + } + + TEST_FIXTURE(CheckEqualFixture, CheckEqualFailureBecauseOfExceptionIncludesCheckContents) + { + PerformCheckWithNonStdThrow(); + + CHECK(strstr(reporter.lastFailedMessage, "ThrowingFunction()")); + CHECK(strstr(reporter.lastFailedMessage, "123")); + } + + TEST_FIXTURE(CheckEqualFixture, CheckEqualFailureBecauseOfStdExceptionIncludesCheckContents) + { + PerformCheckWithStdThrow(); + + CHECK(strstr(reporter.lastFailedMessage, "ThrowingStdExceptionFunction()")); + CHECK(strstr(reporter.lastFailedMessage, "123")); + } + + TEST_FIXTURE(CheckEqualFixture, CheckEqualFailureBecauseOfStandardExceptionIncludesWhat) + { + PerformCheckWithStdThrow(); + + CHECK(strstr(reporter.lastFailedMessage, "exception (Doh)")); + } + } + + SUITE(CheckCloseExceptionTests) + { + struct CheckCloseFixture + { + CheckCloseFixture() + : reporter() + , testResults(&reporter) + , line(-1) + {} + + void PerformCheckWithNonStdThrow() + { UnitTest::TestDetails const testDetails("closeTest", "closeSuite", "filename", -1); ScopedCurrentTest scopedResults(testResults, &testDetails); CHECK_CLOSE(static_cast(ThrowingFunction()), 1.0001f, 0.1f); line = __LINE__; - } + } - void PerformCheckWithStdThrow() - { + void PerformCheckWithStdThrow() + { UnitTest::TestDetails const testDetails("closeTest", "closeSuite", "filename", -1); ScopedCurrentTest scopedResults(testResults, &testDetails); CHECK_CLOSE(static_cast(ThrowingStdExceptionFunction()), 1.0001f, 0.1f); line = __LINE__; - } - - RecordingReporter reporter; - UnitTest::TestResults testResults; - int line; - }; - - TEST_FIXTURE(CheckCloseFixture, CheckCloseFailsOnException) - { - PerformCheckWithNonStdThrow(); - - CHECK(testResults.GetFailureCount() > 0); - } - - TEST_FIXTURE(CheckCloseFixture, CheckCloseFailsOnStdException) - { - PerformCheckWithStdThrow(); - - CHECK(testResults.GetFailureCount() > 0); - } - - TEST_FIXTURE(CheckCloseFixture, CheckCloseFailureBecauseOfExceptionContainsCorrectDetails) - { - PerformCheckWithNonStdThrow(); - - CHECK_EQUAL("closeTest", reporter.lastFailedTest); - CHECK_EQUAL("closeSuite", reporter.lastFailedSuite); - CHECK_EQUAL("filename", reporter.lastFailedFile); - CHECK_EQUAL(line, reporter.lastFailedLine); - } - - TEST_FIXTURE(CheckCloseFixture, CheckCloseFailureBecauseOfStdExceptionContainsCorrectDetails) - { - PerformCheckWithStdThrow(); - - CHECK_EQUAL("closeTest", reporter.lastFailedTest); - CHECK_EQUAL("closeSuite", reporter.lastFailedSuite); - CHECK_EQUAL("filename", reporter.lastFailedFile); - CHECK_EQUAL(line, reporter.lastFailedLine); - } - - TEST_FIXTURE(CheckCloseFixture, CheckCloseFailureBecauseOfExceptionIncludesCheckContents) - { - PerformCheckWithNonStdThrow(); - - CHECK(strstr(reporter.lastFailedMessage, "static_cast(ThrowingFunction())")); - CHECK(strstr(reporter.lastFailedMessage, "1.0001f")); - } - - TEST_FIXTURE(CheckCloseFixture, CheckCloseFailureBecauseOfStdExceptionIncludesCheckContents) - { - PerformCheckWithStdThrow(); - - CHECK(strstr(reporter.lastFailedMessage, "static_cast(ThrowingStdExceptionFunction())")); - CHECK(strstr(reporter.lastFailedMessage, "1.0001f")); - } - - TEST_FIXTURE(CheckCloseFixture, CheckCloseFailureBecauseOfStandardExceptionIncludesWhat) - { - PerformCheckWithStdThrow(); - - CHECK(strstr(reporter.lastFailedMessage, "exception (Doh)")); - } -} - -class ThrowingObject -{ -public: - float operator[](int) const - { - throw "Test throw"; - } -}; - -class StdThrowingObject -{ -public: - float operator[](int) const - { - throw std::runtime_error("Test throw"); - } -}; - -SUITE(CheckArrayCloseExceptionTests) -{ - struct CheckArrayCloseFixture - { - CheckArrayCloseFixture() - : reporter() - , testResults(&reporter) - , line(-1) - { - } - - void PerformCheckWithNonStdThrow() - { + } + + RecordingReporter reporter; + UnitTest::TestResults testResults; + int line; + }; + + TEST_FIXTURE(CheckCloseFixture, CheckCloseFailsOnException) + { + PerformCheckWithNonStdThrow(); + + CHECK(testResults.GetFailureCount() > 0); + } + + TEST_FIXTURE(CheckCloseFixture, CheckCloseFailsOnStdException) + { + PerformCheckWithStdThrow(); + + CHECK(testResults.GetFailureCount() > 0); + } + + TEST_FIXTURE(CheckCloseFixture, CheckCloseFailureBecauseOfExceptionContainsCorrectDetails) + { + PerformCheckWithNonStdThrow(); + + CHECK_EQUAL("closeTest", reporter.lastFailedTest); + CHECK_EQUAL("closeSuite", reporter.lastFailedSuite); + CHECK_EQUAL("filename", reporter.lastFailedFile); + CHECK_EQUAL(line, reporter.lastFailedLine); + } + + TEST_FIXTURE(CheckCloseFixture, CheckCloseFailureBecauseOfStdExceptionContainsCorrectDetails) + { + PerformCheckWithStdThrow(); + + CHECK_EQUAL("closeTest", reporter.lastFailedTest); + CHECK_EQUAL("closeSuite", reporter.lastFailedSuite); + CHECK_EQUAL("filename", reporter.lastFailedFile); + CHECK_EQUAL(line, reporter.lastFailedLine); + } + + TEST_FIXTURE(CheckCloseFixture, CheckCloseFailureBecauseOfExceptionIncludesCheckContents) + { + PerformCheckWithNonStdThrow(); + + CHECK(strstr(reporter.lastFailedMessage, "static_cast(ThrowingFunction())")); + CHECK(strstr(reporter.lastFailedMessage, "1.0001f")); + } + + TEST_FIXTURE(CheckCloseFixture, CheckCloseFailureBecauseOfStdExceptionIncludesCheckContents) + { + PerformCheckWithStdThrow(); + + CHECK(strstr(reporter.lastFailedMessage, "static_cast(ThrowingStdExceptionFunction())")); + CHECK(strstr(reporter.lastFailedMessage, "1.0001f")); + } + + TEST_FIXTURE(CheckCloseFixture, CheckCloseFailureBecauseOfStandardExceptionIncludesWhat) + { + PerformCheckWithStdThrow(); + + CHECK(strstr(reporter.lastFailedMessage, "exception (Doh)")); + } + } + + class ThrowingObject + { + public: + float operator[](int) const + { + throw "Test throw"; + } + }; + + class StdThrowingObject + { + public: + float operator[](int) const + { + throw std::runtime_error("Test throw"); + } + }; + + SUITE(CheckArrayCloseExceptionTests) + { + struct CheckArrayCloseFixture + { + CheckArrayCloseFixture() + : reporter() + , testResults(&reporter) + , line(-1) + {} + + void PerformCheckWithNonStdThrow() + { UnitTest::TestDetails const testDetails("arrayCloseTest", "arrayCloseSuite", "filename", -1); ScopedCurrentTest scopedResults(testResults, &testDetails); int const data[4] = { 0, 1, 2, 3 }; CHECK_ARRAY_CLOSE(data, ThrowingObject(), 4, 0.01f); line = __LINE__; - } + } - void PerformCheckWithStdThrow() - { + void PerformCheckWithStdThrow() + { UnitTest::TestDetails const testDetails("arrayCloseTest", "arrayCloseSuite", "filename", -1); ScopedCurrentTest scopedResults(testResults, &testDetails); int const data[4] = { 0, 1, 2, 3 }; CHECK_ARRAY_CLOSE(data, StdThrowingObject(), 4, 0.01f); line = __LINE__; - } - - RecordingReporter reporter; - UnitTest::TestResults testResults; - int line; - }; - - TEST_FIXTURE(CheckArrayCloseFixture, CheckFailureBecauseOfExceptionContainsCorrectDetails) - { - PerformCheckWithNonStdThrow(); - - CHECK_EQUAL("arrayCloseTest", reporter.lastFailedTest); - CHECK_EQUAL("arrayCloseSuite", reporter.lastFailedSuite); - CHECK_EQUAL("filename", reporter.lastFailedFile); - CHECK_EQUAL(line, reporter.lastFailedLine); - } - - TEST_FIXTURE(CheckArrayCloseFixture, CheckFailureBecauseOfStdExceptionContainsCorrectDetails) - { - PerformCheckWithStdThrow(); - - CHECK_EQUAL("arrayCloseTest", reporter.lastFailedTest); - CHECK_EQUAL("arrayCloseSuite", reporter.lastFailedSuite); - CHECK_EQUAL("filename", reporter.lastFailedFile); - CHECK_EQUAL(line, reporter.lastFailedLine); - } - - TEST_FIXTURE(CheckArrayCloseFixture, CheckFailsOnException) - { - PerformCheckWithNonStdThrow(); - - CHECK(testResults.GetFailureCount() > 0); - } - - TEST_FIXTURE(CheckArrayCloseFixture, CheckFailsOnStdException) - { - PerformCheckWithStdThrow(); - - CHECK(testResults.GetFailureCount() > 0); - } - - TEST_FIXTURE(CheckArrayCloseFixture, CheckFailureOnExceptionIncludesCheckContents) - { - PerformCheckWithNonStdThrow(); - - CHECK(strstr(reporter.lastFailedMessage, "data")); - CHECK(strstr(reporter.lastFailedMessage, "ThrowingObject()")); - } - - TEST_FIXTURE(CheckArrayCloseFixture, CheckFailureOnStdExceptionIncludesCheckContents) - { - PerformCheckWithStdThrow(); - - CHECK(strstr(reporter.lastFailedMessage, "data")); - CHECK(strstr(reporter.lastFailedMessage, "StdThrowingObject()")); - } - - TEST_FIXTURE(CheckArrayCloseFixture, CheckFailureOnStdExceptionIncludesWhat) - { - PerformCheckWithStdThrow(); - - CHECK(strstr(reporter.lastFailedMessage, "exception (Test throw)")); - } -} - -SUITE(CheckArrayEqualExceptionTests) -{ - struct CheckArrayEqualFixture - { - CheckArrayEqualFixture() - : reporter() - , testResults(&reporter) - , line(-1) - { - } - - void PerformCheckWithNonStdThrow() - { + } + + RecordingReporter reporter; + UnitTest::TestResults testResults; + int line; + }; + + TEST_FIXTURE(CheckArrayCloseFixture, CheckFailureBecauseOfExceptionContainsCorrectDetails) + { + PerformCheckWithNonStdThrow(); + + CHECK_EQUAL("arrayCloseTest", reporter.lastFailedTest); + CHECK_EQUAL("arrayCloseSuite", reporter.lastFailedSuite); + CHECK_EQUAL("filename", reporter.lastFailedFile); + CHECK_EQUAL(line, reporter.lastFailedLine); + } + + TEST_FIXTURE(CheckArrayCloseFixture, CheckFailureBecauseOfStdExceptionContainsCorrectDetails) + { + PerformCheckWithStdThrow(); + + CHECK_EQUAL("arrayCloseTest", reporter.lastFailedTest); + CHECK_EQUAL("arrayCloseSuite", reporter.lastFailedSuite); + CHECK_EQUAL("filename", reporter.lastFailedFile); + CHECK_EQUAL(line, reporter.lastFailedLine); + } + + TEST_FIXTURE(CheckArrayCloseFixture, CheckFailsOnException) + { + PerformCheckWithNonStdThrow(); + + CHECK(testResults.GetFailureCount() > 0); + } + + TEST_FIXTURE(CheckArrayCloseFixture, CheckFailsOnStdException) + { + PerformCheckWithStdThrow(); + + CHECK(testResults.GetFailureCount() > 0); + } + + TEST_FIXTURE(CheckArrayCloseFixture, CheckFailureOnExceptionIncludesCheckContents) + { + PerformCheckWithNonStdThrow(); + + CHECK(strstr(reporter.lastFailedMessage, "data")); + CHECK(strstr(reporter.lastFailedMessage, "ThrowingObject()")); + } + + TEST_FIXTURE(CheckArrayCloseFixture, CheckFailureOnStdExceptionIncludesCheckContents) + { + PerformCheckWithStdThrow(); + + CHECK(strstr(reporter.lastFailedMessage, "data")); + CHECK(strstr(reporter.lastFailedMessage, "StdThrowingObject()")); + } + + TEST_FIXTURE(CheckArrayCloseFixture, CheckFailureOnStdExceptionIncludesWhat) + { + PerformCheckWithStdThrow(); + + CHECK(strstr(reporter.lastFailedMessage, "exception (Test throw)")); + } + } + + SUITE(CheckArrayEqualExceptionTests) + { + struct CheckArrayEqualFixture + { + CheckArrayEqualFixture() + : reporter() + , testResults(&reporter) + , line(-1) + {} + + void PerformCheckWithNonStdThrow() + { UnitTest::TestDetails const testDetails("arrayEqualTest", "arrayEqualSuite", "filename", -1); ScopedCurrentTest scopedResults(testResults, &testDetails); int const data[4] = { 0, 1, 2, 3 }; CHECK_ARRAY_EQUAL(data, ThrowingObject(), 4); line = __LINE__; - } + } - void PerformCheckWithStdThrow() - { + void PerformCheckWithStdThrow() + { UnitTest::TestDetails const testDetails("arrayEqualTest", "arrayEqualSuite", "filename", -1); ScopedCurrentTest scopedResults(testResults, &testDetails); int const data[4] = { 0, 1, 2, 3 }; CHECK_ARRAY_EQUAL(data, StdThrowingObject(), 4); line = __LINE__; - } - - RecordingReporter reporter; - UnitTest::TestResults testResults; - int line; - }; - - TEST_FIXTURE(CheckArrayEqualFixture, CheckFailureBecauseOfExceptionContainsCorrectDetails) - { - PerformCheckWithNonStdThrow(); - - CHECK_EQUAL("arrayEqualTest", reporter.lastFailedTest); - CHECK_EQUAL("arrayEqualSuite", reporter.lastFailedSuite); - CHECK_EQUAL("filename", reporter.lastFailedFile); - CHECK_EQUAL(line, reporter.lastFailedLine); - } - - TEST_FIXTURE(CheckArrayEqualFixture, CheckFailureBecauseOfStdExceptionContainsCorrectDetails) - { - PerformCheckWithStdThrow(); - - CHECK_EQUAL("arrayEqualTest", reporter.lastFailedTest); - CHECK_EQUAL("arrayEqualSuite", reporter.lastFailedSuite); - CHECK_EQUAL("filename", reporter.lastFailedFile); - CHECK_EQUAL(line, reporter.lastFailedLine); - } - - TEST_FIXTURE(CheckArrayEqualFixture, CheckFailsOnException) - { - PerformCheckWithNonStdThrow(); - - CHECK(testResults.GetFailureCount() > 0); - } - - TEST_FIXTURE(CheckArrayEqualFixture, CheckFailsOnStdException) - { - PerformCheckWithStdThrow(); - - CHECK(testResults.GetFailureCount() > 0); - } - - TEST_FIXTURE(CheckArrayEqualFixture, CheckFailureOnExceptionIncludesCheckContents) - { - PerformCheckWithNonStdThrow(); - - CHECK(strstr(reporter.lastFailedMessage, "data")); - CHECK(strstr(reporter.lastFailedMessage, "ThrowingObject()")); - } - - TEST_FIXTURE(CheckArrayEqualFixture, CheckFailureOnStdExceptionIncludesCheckContents) - { - PerformCheckWithStdThrow(); - - CHECK(strstr(reporter.lastFailedMessage, "data")); - CHECK(strstr(reporter.lastFailedMessage, "StdThrowingObject()")); - } - - TEST_FIXTURE(CheckArrayEqualFixture, CheckFailureOnStdExceptionIncludesWhat) - { - PerformCheckWithStdThrow(); - - CHECK(strstr(reporter.lastFailedMessage, "exception (Test throw)")); - } -} - -SUITE(CheckArray2DExceptionTests) -{ - class ThrowingObject2D - { - public: - float* operator[](int) const - { + } + + RecordingReporter reporter; + UnitTest::TestResults testResults; + int line; + }; + + TEST_FIXTURE(CheckArrayEqualFixture, CheckFailureBecauseOfExceptionContainsCorrectDetails) + { + PerformCheckWithNonStdThrow(); + + CHECK_EQUAL("arrayEqualTest", reporter.lastFailedTest); + CHECK_EQUAL("arrayEqualSuite", reporter.lastFailedSuite); + CHECK_EQUAL("filename", reporter.lastFailedFile); + CHECK_EQUAL(line, reporter.lastFailedLine); + } + + TEST_FIXTURE(CheckArrayEqualFixture, CheckFailureBecauseOfStdExceptionContainsCorrectDetails) + { + PerformCheckWithStdThrow(); + + CHECK_EQUAL("arrayEqualTest", reporter.lastFailedTest); + CHECK_EQUAL("arrayEqualSuite", reporter.lastFailedSuite); + CHECK_EQUAL("filename", reporter.lastFailedFile); + CHECK_EQUAL(line, reporter.lastFailedLine); + } + + TEST_FIXTURE(CheckArrayEqualFixture, CheckFailsOnException) + { + PerformCheckWithNonStdThrow(); + + CHECK(testResults.GetFailureCount() > 0); + } + + TEST_FIXTURE(CheckArrayEqualFixture, CheckFailsOnStdException) + { + PerformCheckWithStdThrow(); + + CHECK(testResults.GetFailureCount() > 0); + } + + TEST_FIXTURE(CheckArrayEqualFixture, CheckFailureOnExceptionIncludesCheckContents) + { + PerformCheckWithNonStdThrow(); + + CHECK(strstr(reporter.lastFailedMessage, "data")); + CHECK(strstr(reporter.lastFailedMessage, "ThrowingObject()")); + } + + TEST_FIXTURE(CheckArrayEqualFixture, CheckFailureOnStdExceptionIncludesCheckContents) + { + PerformCheckWithStdThrow(); + + CHECK(strstr(reporter.lastFailedMessage, "data")); + CHECK(strstr(reporter.lastFailedMessage, "StdThrowingObject()")); + } + + TEST_FIXTURE(CheckArrayEqualFixture, CheckFailureOnStdExceptionIncludesWhat) + { + PerformCheckWithStdThrow(); + + CHECK(strstr(reporter.lastFailedMessage, "exception (Test throw)")); + } + } + + SUITE(CheckArray2DExceptionTests) + { + class ThrowingObject2D + { + public: + float* operator[](int) const + { throw "Test throw"; - } - }; - - class StdThrowingObject2D - { - public: - float* operator[](int) const - { + } + }; + + class StdThrowingObject2D + { + public: + float* operator[](int) const + { throw std::runtime_error("Test throw"); - } - }; - - struct CheckArray2DCloseFixture - { - CheckArray2DCloseFixture() - : reporter() - , testResults(&reporter) - , line(-1) - { - } - - void PerformCheckWithNonStdThrow() - { + } + }; + + struct CheckArray2DCloseFixture + { + CheckArray2DCloseFixture() + : reporter() + , testResults(&reporter) + , line(-1) + {} + + void PerformCheckWithNonStdThrow() + { UnitTest::TestDetails const testDetails("array2DCloseTest", "array2DCloseSuite", "filename", -1); ScopedCurrentTest scopedResults(testResults, &testDetails); const float data[2][2] = { {0, 1}, {2, 3} }; CHECK_ARRAY2D_CLOSE(data, ThrowingObject2D(), 2, 2, 0.01f); line = __LINE__; - } + } - void PerformCheckWithStdThrow() - { + void PerformCheckWithStdThrow() + { UnitTest::TestDetails const testDetails("array2DCloseTest", "array2DCloseSuite", "filename", -1); ScopedCurrentTest scopedResults(testResults, &testDetails); const float data[2][2] = { {0, 1}, {2, 3} }; CHECK_ARRAY2D_CLOSE(data, StdThrowingObject2D(), 2, 2, 0.01f); line = __LINE__; - } - - RecordingReporter reporter; - UnitTest::TestResults testResults; - int line; - }; - - TEST_FIXTURE(CheckArray2DCloseFixture, CheckFailureBecauseOfExceptionContainsCorrectDetails) - { - PerformCheckWithNonStdThrow(); - - CHECK_EQUAL("array2DCloseTest", reporter.lastFailedTest); - CHECK_EQUAL("array2DCloseSuite", reporter.lastFailedSuite); - CHECK_EQUAL("filename", reporter.lastFailedFile); - CHECK_EQUAL(line, reporter.lastFailedLine); - } - - TEST_FIXTURE(CheckArray2DCloseFixture, CheckFailureBecauseOfStdExceptionContainsCorrectDetails) - { - PerformCheckWithStdThrow(); - - CHECK_EQUAL("array2DCloseTest", reporter.lastFailedTest); - CHECK_EQUAL("array2DCloseSuite", reporter.lastFailedSuite); - CHECK_EQUAL("filename", reporter.lastFailedFile); - CHECK_EQUAL(line, reporter.lastFailedLine); - } - - TEST_FIXTURE(CheckArray2DCloseFixture, CheckFailsOnException) - { - PerformCheckWithNonStdThrow(); - - CHECK(testResults.GetFailureCount() > 0); - } - - TEST_FIXTURE(CheckArray2DCloseFixture, CheckFailsOnStdException) - { - PerformCheckWithStdThrow(); - - CHECK(testResults.GetFailureCount() > 0); - } - - TEST_FIXTURE(CheckArray2DCloseFixture, CheckFailureOnExceptionIncludesCheckContents) - { - PerformCheckWithNonStdThrow(); - - CHECK(strstr(reporter.lastFailedMessage, "data")); - CHECK(strstr(reporter.lastFailedMessage, "ThrowingObject2D()")); - } - - TEST_FIXTURE(CheckArray2DCloseFixture, CheckFailureOnStdExceptionIncludesCheckContents) - { - PerformCheckWithStdThrow(); - - CHECK(strstr(reporter.lastFailedMessage, "data")); - CHECK(strstr(reporter.lastFailedMessage, "StdThrowingObject2D()")); - } - - TEST_FIXTURE(CheckArray2DCloseFixture, CheckFailureOnStdExceptionIncludesWhat) - { - PerformCheckWithStdThrow(); - - CHECK(strstr(reporter.lastFailedMessage, "exception (Test throw)")); - } -} + } + + RecordingReporter reporter; + UnitTest::TestResults testResults; + int line; + }; + + TEST_FIXTURE(CheckArray2DCloseFixture, CheckFailureBecauseOfExceptionContainsCorrectDetails) + { + PerformCheckWithNonStdThrow(); + + CHECK_EQUAL("array2DCloseTest", reporter.lastFailedTest); + CHECK_EQUAL("array2DCloseSuite", reporter.lastFailedSuite); + CHECK_EQUAL("filename", reporter.lastFailedFile); + CHECK_EQUAL(line, reporter.lastFailedLine); + } + + TEST_FIXTURE(CheckArray2DCloseFixture, CheckFailureBecauseOfStdExceptionContainsCorrectDetails) + { + PerformCheckWithStdThrow(); + + CHECK_EQUAL("array2DCloseTest", reporter.lastFailedTest); + CHECK_EQUAL("array2DCloseSuite", reporter.lastFailedSuite); + CHECK_EQUAL("filename", reporter.lastFailedFile); + CHECK_EQUAL(line, reporter.lastFailedLine); + } + + TEST_FIXTURE(CheckArray2DCloseFixture, CheckFailsOnException) + { + PerformCheckWithNonStdThrow(); + + CHECK(testResults.GetFailureCount() > 0); + } + + TEST_FIXTURE(CheckArray2DCloseFixture, CheckFailsOnStdException) + { + PerformCheckWithStdThrow(); + + CHECK(testResults.GetFailureCount() > 0); + } + + TEST_FIXTURE(CheckArray2DCloseFixture, CheckFailureOnExceptionIncludesCheckContents) + { + PerformCheckWithNonStdThrow(); + + CHECK(strstr(reporter.lastFailedMessage, "data")); + CHECK(strstr(reporter.lastFailedMessage, "ThrowingObject2D()")); + } + + TEST_FIXTURE(CheckArray2DCloseFixture, CheckFailureOnStdExceptionIncludesCheckContents) + { + PerformCheckWithStdThrow(); + + CHECK(strstr(reporter.lastFailedMessage, "data")); + CHECK(strstr(reporter.lastFailedMessage, "StdThrowingObject2D()")); + } + + TEST_FIXTURE(CheckArray2DCloseFixture, CheckFailureOnStdExceptionIncludesWhat) + { + PerformCheckWithStdThrow(); + + CHECK(strstr(reporter.lastFailedMessage, "exception (Test throw)")); + } + } } #endif diff --git a/tests/TestMemoryOutStream.cpp b/tests/TestMemoryOutStream.cpp index ff1e920..7c1ffbc 100644 --- a/tests/TestMemoryOutStream.cpp +++ b/tests/TestMemoryOutStream.cpp @@ -11,325 +11,325 @@ using namespace std; namespace { -const char* const maxSignedIntegralStr(size_t nBytes) -{ - switch(nBytes) - { - case 8: - return "9223372036854775807"; - case 4: - return "2147483647"; - case 2: - return "32767"; - case 1: - return "127"; - default: - return "Unsupported signed integral size"; - } -} - -const char* const minSignedIntegralStr(size_t nBytes) -{ - switch(nBytes) - { - case 8: - return "-9223372036854775808"; - case 4: - return "-2147483648"; - case 2: - return "-32768"; - case 1: - return "-128"; - default: - return "Unsupported signed integral size"; - } -} - -const char* const maxUnsignedIntegralStr(size_t nBytes) -{ - switch(nBytes) - { - case 8: - return "18446744073709551615"; - case 4: - return "4294967295"; - case 2: - return "65535"; - case 1: - return "255"; - default: - return "Unsupported signed integral size"; - } -} - -TEST(DefaultIsEmptyString) -{ - MemoryOutStream const stream; - CHECK(stream.GetText() != 0); - CHECK_EQUAL("", stream.GetText()); -} - -TEST(StreamingTextCopiesCharacters) -{ - MemoryOutStream stream; - stream << "Lalala"; - CHECK_EQUAL("Lalala", stream.GetText()); -} - -TEST(StreamingMultipleTimesConcatenatesResult) -{ - MemoryOutStream stream; - stream << "Bork" << "Foo" << "Bar"; - CHECK_EQUAL("BorkFooBar", stream.GetText()); -} - -TEST(StreamingIntWritesCorrectCharacters) -{ - MemoryOutStream stream; - stream << (int)123; - CHECK_EQUAL("123", stream.GetText()); -} - -TEST(StreaminMaxIntWritesCorrectCharacters) -{ - MemoryOutStream stream; - stream << INT_MAX; - CHECK_EQUAL(maxSignedIntegralStr(sizeof(int)), stream.GetText()); -} - -TEST(StreamingMinIntWritesCorrectCharacters) -{ - MemoryOutStream stream; - stream << INT_MIN; - CHECK_EQUAL(minSignedIntegralStr(sizeof(int)), stream.GetText()); -} - -TEST(StreamingUnsignedIntWritesCorrectCharacters) -{ - MemoryOutStream stream; - stream << (unsigned int)123; - CHECK_EQUAL("123", stream.GetText()); -} - -TEST(StreamingMaxUnsignedIntWritesCorrectCharacters) -{ - MemoryOutStream stream; - stream << (unsigned int)UINT_MAX; - CHECK_EQUAL(maxUnsignedIntegralStr(sizeof(unsigned int)), stream.GetText()); -} - -TEST(StreamingMinUnsignedIntWritesCorrectCharacters) -{ - MemoryOutStream stream; - stream << (unsigned int)0; - CHECK_EQUAL("0", stream.GetText()); -} - -TEST(StreamingLongWritesCorrectCharacters) -{ - MemoryOutStream stream; - stream << (long)(-123); - CHECK_EQUAL("-123", stream.GetText()); -} - -TEST(StreamingMaxLongWritesCorrectCharacters) -{ - MemoryOutStream stream; - stream << (long)(LONG_MAX); - CHECK_EQUAL(maxSignedIntegralStr(sizeof(long)), stream.GetText()); -} - -TEST(StreamingMinLongWritesCorrectCharacters) -{ - MemoryOutStream stream; - stream << (long)(LONG_MIN); - CHECK_EQUAL(minSignedIntegralStr(sizeof(long)), stream.GetText()); -} - -TEST(StreamingUnsignedLongWritesCorrectCharacters) -{ - MemoryOutStream stream; - stream << (unsigned long)123; - CHECK_EQUAL("123", stream.GetText()); -} - -TEST(StreamingMaxUnsignedLongWritesCorrectCharacters) -{ - MemoryOutStream stream; - stream << (unsigned long)ULONG_MAX; - CHECK_EQUAL(maxUnsignedIntegralStr(sizeof(unsigned long)), stream.GetText()); -} - -TEST(StreamingMinUnsignedLongWritesCorrectCharacters) -{ - MemoryOutStream stream; - stream << (unsigned long)0ul; - CHECK_EQUAL("0", stream.GetText()); -} - -TEST(StreamingLongLongWritesCorrectCharacters) -{ - MemoryOutStream stream; + const char* const maxSignedIntegralStr(size_t nBytes) + { + switch(nBytes) + { + case 8: + return "9223372036854775807"; + case 4: + return "2147483647"; + case 2: + return "32767"; + case 1: + return "127"; + default: + return "Unsupported signed integral size"; + } + } + + const char* const minSignedIntegralStr(size_t nBytes) + { + switch(nBytes) + { + case 8: + return "-9223372036854775808"; + case 4: + return "-2147483648"; + case 2: + return "-32768"; + case 1: + return "-128"; + default: + return "Unsupported signed integral size"; + } + } + + const char* const maxUnsignedIntegralStr(size_t nBytes) + { + switch(nBytes) + { + case 8: + return "18446744073709551615"; + case 4: + return "4294967295"; + case 2: + return "65535"; + case 1: + return "255"; + default: + return "Unsupported signed integral size"; + } + } + + TEST(DefaultIsEmptyString) + { + MemoryOutStream const stream; + CHECK(stream.GetText() != 0); + CHECK_EQUAL("", stream.GetText()); + } + + TEST(StreamingTextCopiesCharacters) + { + MemoryOutStream stream; + stream << "Lalala"; + CHECK_EQUAL("Lalala", stream.GetText()); + } + + TEST(StreamingMultipleTimesConcatenatesResult) + { + MemoryOutStream stream; + stream << "Bork" << "Foo" << "Bar"; + CHECK_EQUAL("BorkFooBar", stream.GetText()); + } + + TEST(StreamingIntWritesCorrectCharacters) + { + MemoryOutStream stream; + stream << (int)123; + CHECK_EQUAL("123", stream.GetText()); + } + + TEST(StreaminMaxIntWritesCorrectCharacters) + { + MemoryOutStream stream; + stream << INT_MAX; + CHECK_EQUAL(maxSignedIntegralStr(sizeof(int)), stream.GetText()); + } + + TEST(StreamingMinIntWritesCorrectCharacters) + { + MemoryOutStream stream; + stream << INT_MIN; + CHECK_EQUAL(minSignedIntegralStr(sizeof(int)), stream.GetText()); + } + + TEST(StreamingUnsignedIntWritesCorrectCharacters) + { + MemoryOutStream stream; + stream << (unsigned int)123; + CHECK_EQUAL("123", stream.GetText()); + } + + TEST(StreamingMaxUnsignedIntWritesCorrectCharacters) + { + MemoryOutStream stream; + stream << (unsigned int)UINT_MAX; + CHECK_EQUAL(maxUnsignedIntegralStr(sizeof(unsigned int)), stream.GetText()); + } + + TEST(StreamingMinUnsignedIntWritesCorrectCharacters) + { + MemoryOutStream stream; + stream << (unsigned int)0; + CHECK_EQUAL("0", stream.GetText()); + } + + TEST(StreamingLongWritesCorrectCharacters) + { + MemoryOutStream stream; + stream << (long)(-123); + CHECK_EQUAL("-123", stream.GetText()); + } + + TEST(StreamingMaxLongWritesCorrectCharacters) + { + MemoryOutStream stream; + stream << (long)(LONG_MAX); + CHECK_EQUAL(maxSignedIntegralStr(sizeof(long)), stream.GetText()); + } + + TEST(StreamingMinLongWritesCorrectCharacters) + { + MemoryOutStream stream; + stream << (long)(LONG_MIN); + CHECK_EQUAL(minSignedIntegralStr(sizeof(long)), stream.GetText()); + } + + TEST(StreamingUnsignedLongWritesCorrectCharacters) + { + MemoryOutStream stream; + stream << (unsigned long)123; + CHECK_EQUAL("123", stream.GetText()); + } + + TEST(StreamingMaxUnsignedLongWritesCorrectCharacters) + { + MemoryOutStream stream; + stream << (unsigned long)ULONG_MAX; + CHECK_EQUAL(maxUnsignedIntegralStr(sizeof(unsigned long)), stream.GetText()); + } + + TEST(StreamingMinUnsignedLongWritesCorrectCharacters) + { + MemoryOutStream stream; + stream << (unsigned long)0ul; + CHECK_EQUAL("0", stream.GetText()); + } + + TEST(StreamingLongLongWritesCorrectCharacters) + { + MemoryOutStream stream; #ifdef UNITTEST_COMPILER_IS_MSVC6 - stream << (__int64)-12345i64; + stream << (__int64)-12345i64; #else - stream << (long long)-12345ll; + stream << (long long)-12345ll; #endif - CHECK_EQUAL("-12345", stream.GetText()); -} + CHECK_EQUAL("-12345", stream.GetText()); + } #ifdef LLONG_MAX -TEST(StreamingMaxLongLongWritesCorrectCharacters) -{ - MemoryOutStream stream; - stream << (long long)LLONG_MAX; - CHECK_EQUAL(maxSignedIntegralStr(sizeof(long long)), stream.GetText()); -} + TEST(StreamingMaxLongLongWritesCorrectCharacters) + { + MemoryOutStream stream; + stream << (long long)LLONG_MAX; + CHECK_EQUAL(maxSignedIntegralStr(sizeof(long long)), stream.GetText()); + } #endif #ifdef LLONG_MIN -TEST(StreamingMinLongLongWritesCorrectCharacters) -{ - MemoryOutStream stream; - stream << (long long)LLONG_MIN; - CHECK_EQUAL(minSignedIntegralStr(sizeof(long long)), stream.GetText()); -} + TEST(StreamingMinLongLongWritesCorrectCharacters) + { + MemoryOutStream stream; + stream << (long long)LLONG_MIN; + CHECK_EQUAL(minSignedIntegralStr(sizeof(long long)), stream.GetText()); + } #endif -TEST(StreamingUnsignedLongLongWritesCorrectCharacters) -{ - MemoryOutStream stream; + TEST(StreamingUnsignedLongLongWritesCorrectCharacters) + { + MemoryOutStream stream; #ifdef UNITTEST_COMPILER_IS_MSVC6 - stream << (unsigned __int64)85899ui64; + stream << (unsigned __int64)85899ui64; #else - stream << (unsigned long long)85899ull; + stream << (unsigned long long)85899ull; #endif - CHECK_EQUAL("85899", stream.GetText()); -} + CHECK_EQUAL("85899", stream.GetText()); + } #ifdef ULLONG_MAX -TEST(StreamingMaxUnsignedLongLongWritesCorrectCharacters) -{ - MemoryOutStream stream; - stream << (unsigned long long)ULLONG_MAX; - CHECK_EQUAL(maxUnsignedIntegralStr(sizeof(unsigned long long)), stream.GetText()); -} + TEST(StreamingMaxUnsignedLongLongWritesCorrectCharacters) + { + MemoryOutStream stream; + stream << (unsigned long long)ULLONG_MAX; + CHECK_EQUAL(maxUnsignedIntegralStr(sizeof(unsigned long long)), stream.GetText()); + } #endif -TEST(StreamingMinUnsignedLongLongWritesCorrectCharacters) -{ - MemoryOutStream stream; + TEST(StreamingMinUnsignedLongLongWritesCorrectCharacters) + { + MemoryOutStream stream; #ifdef UNITTEST_COMPILER_IS_MSVC6 - stream << (unsigned __int64)0ui64; + stream << (unsigned __int64)0ui64; #else - stream << (unsigned long long)0ull; + stream << (unsigned long long)0ull; #endif - CHECK_EQUAL("0", stream.GetText()); -} - -TEST(StreamingFloatWritesCorrectCharacters) -{ - MemoryOutStream stream; - stream << 3.1415f; - CHECK(strstr(stream.GetText(), "3.1415")); -} - -TEST(StreamingDoubleWritesCorrectCharacters) -{ - MemoryOutStream stream; - stream << 3.1415; - CHECK(strstr(stream.GetText(), "3.1415")); -} - -TEST(StreamingPointerWritesCorrectCharacters) -{ - MemoryOutStream stream; - int* p = (int*)0x1234; - stream << p; - CHECK(strstr(stream.GetText(), "1234")); -} - -TEST(StreamingSizeTWritesCorrectCharacters) -{ - MemoryOutStream stream; - size_t const s = 53124; - stream << s; - CHECK_EQUAL("53124", stream.GetText()); -} - -TEST(ClearEmptiesMemoryOutStreamContents) -{ - MemoryOutStream stream; - stream << "Hello world"; - stream.Clear(); - CHECK_EQUAL("", stream.GetText()); -} + CHECK_EQUAL("0", stream.GetText()); + } + + TEST(StreamingFloatWritesCorrectCharacters) + { + MemoryOutStream stream; + stream << 3.1415f; + CHECK(strstr(stream.GetText(), "3.1415")); + } + + TEST(StreamingDoubleWritesCorrectCharacters) + { + MemoryOutStream stream; + stream << 3.1415; + CHECK(strstr(stream.GetText(), "3.1415")); + } + + TEST(StreamingPointerWritesCorrectCharacters) + { + MemoryOutStream stream; + int* p = (int*)0x1234; + stream << p; + CHECK(strstr(stream.GetText(), "1234")); + } + + TEST(StreamingSizeTWritesCorrectCharacters) + { + MemoryOutStream stream; + size_t const s = 53124; + stream << s; + CHECK_EQUAL("53124", stream.GetText()); + } + + TEST(ClearEmptiesMemoryOutStreamContents) + { + MemoryOutStream stream; + stream << "Hello world"; + stream.Clear(); + CHECK_EQUAL("", stream.GetText()); + } #ifndef UNITTEST_MEMORYOUTSTREAM_IS_STD_OSTRINGSTREAM -TEST(StreamInitialCapacityIsCorrect) -{ - MemoryOutStream stream(MemoryOutStream::GROW_CHUNK_SIZE); - CHECK_EQUAL((int)MemoryOutStream::GROW_CHUNK_SIZE, stream.GetCapacity()); -} - -TEST(StreamInitialCapacityIsMultipleOfGrowChunkSize) -{ - MemoryOutStream stream(MemoryOutStream::GROW_CHUNK_SIZE + 1); - CHECK_EQUAL((int)MemoryOutStream::GROW_CHUNK_SIZE * 2, stream.GetCapacity()); -} - - -TEST(ExceedingCapacityGrowsBuffer) -{ - MemoryOutStream stream(MemoryOutStream::GROW_CHUNK_SIZE); - stream << "012345678901234567890123456789"; - char const* const oldBuffer = stream.GetText(); - stream << "0123456789"; - CHECK(oldBuffer != stream.GetText()); -} - -TEST(ExceedingCapacityGrowsBufferByGrowChunk) -{ - MemoryOutStream stream(MemoryOutStream::GROW_CHUNK_SIZE); - stream << "0123456789012345678901234567890123456789"; - CHECK_EQUAL(MemoryOutStream::GROW_CHUNK_SIZE * 2, stream.GetCapacity()); -} - -TEST(WritingStringLongerThanCapacityFitsInNewBuffer) -{ - MemoryOutStream stream(8); - stream << "0123456789ABCDEF"; - CHECK_EQUAL("0123456789ABCDEF", stream.GetText()); -} - -TEST(WritingIntLongerThanCapacityFitsInNewBuffer) -{ - MemoryOutStream stream(8); - stream << "aaaa" << 123456; - CHECK_EQUAL("aaaa123456", stream.GetText()); -} - -TEST(WritingFloatLongerThanCapacityFitsInNewBuffer) -{ - MemoryOutStream stream(8); - stream << "aaaa" << 123456.0f; - CHECK_EQUAL("aaaa123456.000000", stream.GetText()); -} - -TEST(WritingSizeTLongerThanCapacityFitsInNewBuffer) -{ - MemoryOutStream stream(8); - stream << "aaaa" << size_t(32145); - CHECK_EQUAL("aaaa32145", stream.GetText()); -} - -TEST(VerifyLargeDoubleCanBeStreamedWithoutCrashing) -{ - MemoryOutStream stream(8); - stream << DBL_MAX; - CHECK(true); -} + TEST(StreamInitialCapacityIsCorrect) + { + MemoryOutStream stream(MemoryOutStream::GROW_CHUNK_SIZE); + CHECK_EQUAL((int)MemoryOutStream::GROW_CHUNK_SIZE, stream.GetCapacity()); + } + + TEST(StreamInitialCapacityIsMultipleOfGrowChunkSize) + { + MemoryOutStream stream(MemoryOutStream::GROW_CHUNK_SIZE + 1); + CHECK_EQUAL((int)MemoryOutStream::GROW_CHUNK_SIZE * 2, stream.GetCapacity()); + } + + + TEST(ExceedingCapacityGrowsBuffer) + { + MemoryOutStream stream(MemoryOutStream::GROW_CHUNK_SIZE); + stream << "012345678901234567890123456789"; + char const* const oldBuffer = stream.GetText(); + stream << "0123456789"; + CHECK(oldBuffer != stream.GetText()); + } + + TEST(ExceedingCapacityGrowsBufferByGrowChunk) + { + MemoryOutStream stream(MemoryOutStream::GROW_CHUNK_SIZE); + stream << "0123456789012345678901234567890123456789"; + CHECK_EQUAL(MemoryOutStream::GROW_CHUNK_SIZE * 2, stream.GetCapacity()); + } + + TEST(WritingStringLongerThanCapacityFitsInNewBuffer) + { + MemoryOutStream stream(8); + stream << "0123456789ABCDEF"; + CHECK_EQUAL("0123456789ABCDEF", stream.GetText()); + } + + TEST(WritingIntLongerThanCapacityFitsInNewBuffer) + { + MemoryOutStream stream(8); + stream << "aaaa" << 123456; + CHECK_EQUAL("aaaa123456", stream.GetText()); + } + + TEST(WritingFloatLongerThanCapacityFitsInNewBuffer) + { + MemoryOutStream stream(8); + stream << "aaaa" << 123456.0f; + CHECK_EQUAL("aaaa123456.000000", stream.GetText()); + } + + TEST(WritingSizeTLongerThanCapacityFitsInNewBuffer) + { + MemoryOutStream stream(8); + stream << "aaaa" << size_t(32145); + CHECK_EQUAL("aaaa32145", stream.GetText()); + } + + TEST(VerifyLargeDoubleCanBeStreamedWithoutCrashing) + { + MemoryOutStream stream(8); + stream << DBL_MAX; + CHECK(true); + } #endif diff --git a/tests/TestTest.cpp b/tests/TestTest.cpp index 49529eb..5a4e1ca 100644 --- a/tests/TestTest.cpp +++ b/tests/TestTest.cpp @@ -7,127 +7,127 @@ using namespace UnitTest; namespace { -TEST(PassingTestHasNoFailures) -{ - class PassingTest : public Test - { - public: - PassingTest() : Test("passing") {} - virtual void RunImpl() const - { + TEST(PassingTestHasNoFailures) + { + class PassingTest : public Test + { + public: + PassingTest() : Test("passing") {} + virtual void RunImpl() const + { CHECK(true); - } - }; - - TestResults results; - { - ScopedCurrentTest scopedResults(results); - PassingTest().Run(); - } - - CHECK_EQUAL(0, results.GetFailureCount()); -} - - -TEST(FailingTestHasFailures) -{ - class FailingTest : public Test - { - public: - FailingTest() : Test("failing") {} - virtual void RunImpl() const - { + } + }; + + TestResults results; + { + ScopedCurrentTest scopedResults(results); + PassingTest().Run(); + } + + CHECK_EQUAL(0, results.GetFailureCount()); + } + + + TEST(FailingTestHasFailures) + { + class FailingTest : public Test + { + public: + FailingTest() : Test("failing") {} + virtual void RunImpl() const + { CHECK(false); - } - }; + } + }; - TestResults results; - { - ScopedCurrentTest scopedResults(results); - FailingTest().Run(); - } + TestResults results; + { + ScopedCurrentTest scopedResults(results); + FailingTest().Run(); + } - CHECK_EQUAL(1, results.GetFailureCount()); -} + CHECK_EQUAL(1, results.GetFailureCount()); + } #ifndef UNITTEST_NO_EXCEPTIONS -TEST(ThrowingTestsAreReportedAsFailures) -{ - class CrashingTest : public Test - { - public: - CrashingTest() : Test("throwing") {} - virtual void RunImpl() const - { + TEST(ThrowingTestsAreReportedAsFailures) + { + class CrashingTest : public Test + { + public: + CrashingTest() : Test("throwing") {} + virtual void RunImpl() const + { throw "Blah"; - } - }; - - TestResults results; - { - ScopedCurrentTest scopedResult(results); - CrashingTest().Run(); - } - - CHECK_EQUAL(1, results.GetFailureCount()); -} + } + }; + + TestResults results; + { + ScopedCurrentTest scopedResult(results); + CrashingTest().Run(); + } + + CHECK_EQUAL(1, results.GetFailureCount()); + } #if !defined(UNITTEST_MINGW) && !defined(UNITTEST_WIN32) // Skip this test in debug because some debuggers don't like it. #if defined(NDEBUG) -TEST(CrashingTestsAreReportedAsFailures) -{ - class CrashingTest : public Test - { - public: - CrashingTest() : Test("crashing") {} - virtual void RunImpl() const - { + TEST(CrashingTestsAreReportedAsFailures) + { + class CrashingTest : public Test + { + public: + CrashingTest() : Test("crashing") {} + virtual void RunImpl() const + { reinterpret_cast< void (*)() >(0)(); - } - }; + } + }; - TestResults results; - { - ScopedCurrentTest scopedResult(results); - CrashingTest().Run(); - } + TestResults results; + { + ScopedCurrentTest scopedResult(results); + CrashingTest().Run(); + } - CHECK_EQUAL(1, results.GetFailureCount()); -} + CHECK_EQUAL(1, results.GetFailureCount()); + } #endif #endif #endif -TEST(TestWithUnspecifiedSuiteGetsDefaultSuite) -{ - Test test("test"); - CHECK(test.m_details.suiteName != NULL); - CHECK_EQUAL("DefaultSuite", test.m_details.suiteName); -} - -TEST(TestReflectsSpecifiedSuiteName) -{ - Test test("test", "testSuite"); - CHECK(test.m_details.suiteName != NULL); - CHECK_EQUAL("testSuite", test.m_details.suiteName); -} - -void Fail() -{ - CHECK(false); -} - -TEST(OutOfCoreCHECKMacrosCanFailTests) -{ - TestResults results; - { - ScopedCurrentTest scopedResult(results); - Fail(); - } - - CHECK_EQUAL(1, results.GetFailureCount()); -} + TEST(TestWithUnspecifiedSuiteGetsDefaultSuite) + { + Test test("test"); + CHECK(test.m_details.suiteName != NULL); + CHECK_EQUAL("DefaultSuite", test.m_details.suiteName); + } + + TEST(TestReflectsSpecifiedSuiteName) + { + Test test("test", "testSuite"); + CHECK(test.m_details.suiteName != NULL); + CHECK_EQUAL("testSuite", test.m_details.suiteName); + } + + void Fail() + { + CHECK(false); + } + + TEST(OutOfCoreCHECKMacrosCanFailTests) + { + TestResults results; + { + ScopedCurrentTest scopedResult(results); + Fail(); + } + + CHECK_EQUAL(1, results.GetFailureCount()); + } } diff --git a/tests/TestTestList.cpp b/tests/TestTestList.cpp index 73732c5..4a0bdcb 100644 --- a/tests/TestTestList.cpp +++ b/tests/TestTestList.cpp @@ -6,45 +6,45 @@ using namespace UnitTest; namespace { -TEST(TestListIsEmptyByDefault) -{ - TestList list; - CHECK(list.GetHead() == 0); -} - -TEST(AddingTestSetsHeadToTest) -{ - Test test("test"); - TestList list; - list.Add(&test); - - CHECK(list.GetHead() == &test); - CHECK(test.m_nextTest == 0); -} - -TEST(AddingSecondTestAddsItToEndOfList) -{ - Test test1("test1"); - Test test2("test2"); - - TestList list; - list.Add(&test1); - list.Add(&test2); - - CHECK(list.GetHead() == &test1); - CHECK(test1.m_nextTest == &test2); - CHECK(test2.m_nextTest == 0); -} - -TEST(ListAdderAddsTestToList) -{ - TestList list; - - Test test(""); - ListAdder adder(list, &test); - - CHECK(list.GetHead() == &test); - CHECK(test.m_nextTest == 0); -} + TEST(TestListIsEmptyByDefault) + { + TestList list; + CHECK(list.GetHead() == 0); + } + + TEST(AddingTestSetsHeadToTest) + { + Test test("test"); + TestList list; + list.Add(&test); + + CHECK(list.GetHead() == &test); + CHECK(test.m_nextTest == 0); + } + + TEST(AddingSecondTestAddsItToEndOfList) + { + Test test1("test1"); + Test test2("test2"); + + TestList list; + list.Add(&test1); + list.Add(&test2); + + CHECK(list.GetHead() == &test1); + CHECK(test1.m_nextTest == &test2); + CHECK(test2.m_nextTest == 0); + } + + TEST(ListAdderAddsTestToList) + { + TestList list; + + Test test(""); + ListAdder adder(list, &test); + + CHECK(list.GetHead() == &test); + CHECK(test.m_nextTest == 0); + } } diff --git a/tests/TestTestMacros.cpp b/tests/TestTestMacros.cpp index 9bf7b91..4a13316 100644 --- a/tests/TestTestMacros.cpp +++ b/tests/TestTestMacros.cpp @@ -13,217 +13,210 @@ using namespace std; /* test for c++11 support */ #ifndef _MSC_VER - /* Test for clang >= 3.3 */ - #ifdef __clang__ - #if (__clang__ == 1) && (__clang_major__ > 3 || (__clang_major__ == 3 && (__clang_minor__ > 2 ))) - #define _NOEXCEPT_OP(x) noexcept(x) - #else - #define _NOEXCEPT_OP(x) - #endif - #endif - - #ifndef __clang__ - /* Test for GCC >= 4.8.0 */ - #ifdef __GNUC__ - #if (__GNUC__ > 4) || (__GNUC__ == 4 && (__GNUC_MINOR__ > 7 )) - #define _NOEXCEPT_OP(x) noexcept(x) - #else + /* Test for clang >= 3.3 */ + #ifdef __clang__ + #if (__clang__ == 1) && (__clang_major__ > 3 || (__clang_major__ == 3 && (__clang_minor__ > 2 ))) + #define _NOEXCEPT_OP(x) noexcept(x) + #else + #define _NOEXCEPT_OP(x) + #endif + #endif + + #ifndef __clang__ + /* Test for GCC >= 4.8.0 */ + #ifdef __GNUC__ + #if (__GNUC__ > 4) || (__GNUC__ == 4 && (__GNUC_MINOR__ > 7 )) + #define _NOEXCEPT_OP(x) noexcept(x) + #else #define _NOEXCEPT_OP(x) - #endif - #endif - #endif + #endif + #endif + #endif #elif _MSC_VER - #if (_MSC_VER > 1800) - #define _NOEXCEPT_OP(x) noexcept(x) - #else - #define _NOEXCEPT_OP(x) - #endif + #if (_MSC_VER > 1800) + #define _NOEXCEPT_OP(x) noexcept(x) + #else + #define _NOEXCEPT_OP(x) + #endif #endif namespace { -TestList list1; -TEST_EX(DummyTest, list1) -{ -} + TestList list1; + TEST_EX(DummyTest, list1) + {} -TEST (TestsAreAddedToTheListThroughMacro) -{ - CHECK(list1.GetHead() != 0); - CHECK(list1.GetHead()->m_nextTest == 0); -} + TEST (TestsAreAddedToTheListThroughMacro) + { + CHECK(list1.GetHead() != 0); + CHECK(list1.GetHead()->m_nextTest == 0); + } #ifndef UNITTEST_NO_EXCEPTIONS -struct ThrowingThingie -{ - ThrowingThingie() : dummy(false) - { - if (!dummy) + struct ThrowingThingie + { + ThrowingThingie() : dummy(false) + { + if (!dummy) throw "Oops"; - } + } - bool dummy; -}; + bool dummy; + }; -TestList list2; -TEST_FIXTURE_EX(ThrowingThingie, DummyTestName, list2) -{ -} + TestList list2; + TEST_FIXTURE_EX(ThrowingThingie, DummyTestName, list2) + {} -TEST (ExceptionsInFixtureAreReportedAsHappeningInTheFixture) -{ - RecordingReporter reporter; - TestResults result(&reporter); - { - ScopedCurrentTest scopedResults(result); - list2.GetHead()->Run(); - } - - CHECK(strstr(reporter.lastFailedMessage, "xception")); - CHECK(strstr(reporter.lastFailedMessage, "fixture")); - CHECK(strstr(reporter.lastFailedMessage, "ThrowingThingie")); -} + TEST (ExceptionsInFixtureAreReportedAsHappeningInTheFixture) + { + RecordingReporter reporter; + TestResults result(&reporter); + { + ScopedCurrentTest scopedResults(result); + list2.GetHead()->Run(); + } + + CHECK(strstr(reporter.lastFailedMessage, "xception")); + CHECK(strstr(reporter.lastFailedMessage, "fixture")); + CHECK(strstr(reporter.lastFailedMessage, "ThrowingThingie")); + } #endif -struct DummyFixture -{ - int x; -}; + struct DummyFixture + { + int x; + }; // We're really testing the macros so we just want them to compile and link -SUITE(TestSuite1) -{ - TEST(SimilarlyNamedTestsInDifferentSuitesWork) - { - } - - TEST_FIXTURE(DummyFixture, SimilarlyNamedFixtureTestsInDifferentSuitesWork) - { - } -} - -SUITE(TestSuite2) -{ - TEST(SimilarlyNamedTestsInDifferentSuitesWork) - { - } - - TEST_FIXTURE(DummyFixture,SimilarlyNamedFixtureTestsInDifferentSuitesWork) - { - } -} - -TestList macroTestList1; -TEST_EX(MacroTestHelper1, macroTestList1) -{ -} - -TEST(TestAddedWithTEST_EXMacroGetsDefaultSuite) -{ - CHECK(macroTestList1.GetHead() != NULL); - CHECK_EQUAL ("MacroTestHelper1", macroTestList1.GetHead()->m_details.testName); - CHECK_EQUAL ("DefaultSuite", macroTestList1.GetHead()->m_details.suiteName); -} - -TestList macroTestList2; -TEST_FIXTURE_EX(DummyFixture, MacroTestHelper2, macroTestList2) -{ -} - -TEST(TestAddedWithTEST_FIXTURE_EXMacroGetsDefaultSuite) -{ - CHECK(macroTestList2.GetHead() != NULL); - CHECK_EQUAL ("MacroTestHelper2", macroTestList2.GetHead()->m_details.testName); - CHECK_EQUAL ("DefaultSuite", macroTestList2.GetHead()->m_details.suiteName); -} + SUITE(TestSuite1) + { + TEST(SimilarlyNamedTestsInDifferentSuitesWork) + {} + + TEST_FIXTURE(DummyFixture, SimilarlyNamedFixtureTestsInDifferentSuitesWork) + {} + } + + SUITE(TestSuite2) + { + TEST(SimilarlyNamedTestsInDifferentSuitesWork) + {} + + TEST_FIXTURE(DummyFixture,SimilarlyNamedFixtureTestsInDifferentSuitesWork) + {} + } + + TestList macroTestList1; + TEST_EX(MacroTestHelper1, macroTestList1) + {} + + TEST(TestAddedWithTEST_EXMacroGetsDefaultSuite) + { + CHECK(macroTestList1.GetHead() != NULL); + CHECK_EQUAL ("MacroTestHelper1", macroTestList1.GetHead()->m_details.testName); + CHECK_EQUAL ("DefaultSuite", macroTestList1.GetHead()->m_details.suiteName); + } + + TestList macroTestList2; + TEST_FIXTURE_EX(DummyFixture, MacroTestHelper2, macroTestList2) + {} + + TEST(TestAddedWithTEST_FIXTURE_EXMacroGetsDefaultSuite) + { + CHECK(macroTestList2.GetHead() != NULL); + CHECK_EQUAL ("MacroTestHelper2", macroTestList2.GetHead()->m_details.testName); + CHECK_EQUAL ("DefaultSuite", macroTestList2.GetHead()->m_details.suiteName); + } #ifndef UNITTEST_NO_EXCEPTIONS -struct FixtureCtorThrows -{ - FixtureCtorThrows() { throw "exception"; } -}; - -TestList throwingFixtureTestList1; -TEST_FIXTURE_EX(FixtureCtorThrows, FixtureCtorThrowsTestName, throwingFixtureTestList1) -{ -} - -TEST(FixturesWithThrowingCtorsAreFailures) -{ - CHECK(throwingFixtureTestList1.GetHead() != NULL); - RecordingReporter reporter; - TestResults result(&reporter); - { - ScopedCurrentTest scopedResult(result); - throwingFixtureTestList1.GetHead()->Run(); - } - - int const failureCount = result.GetFailedTestCount(); - CHECK_EQUAL(1, failureCount); - CHECK(strstr(reporter.lastFailedMessage, "while constructing fixture")); -} - -struct FixtureDtorThrows -{ - ~FixtureDtorThrows() _NOEXCEPT_OP(false) { throw "exception"; } -}; - -TestList throwingFixtureTestList2; -TEST_FIXTURE_EX(FixtureDtorThrows, FixtureDtorThrowsTestName, throwingFixtureTestList2) -{ -} - -TEST(FixturesWithThrowingDtorsAreFailures) -{ - CHECK(throwingFixtureTestList2.GetHead() != NULL); - - RecordingReporter reporter; - TestResults result(&reporter); - { - ScopedCurrentTest scopedResult(result); - throwingFixtureTestList2.GetHead()->Run(); - } - - int const failureCount = result.GetFailedTestCount(); - CHECK_EQUAL(1, failureCount); - CHECK(strstr(reporter.lastFailedMessage, "while destroying fixture")); -} - -const int FailingLine = 123; - -struct FixtureCtorAsserts -{ - FixtureCtorAsserts() - { - UnitTest::ReportAssert("assert failure", "file", FailingLine); - } -}; - -TestList ctorAssertFixtureTestList; -TEST_FIXTURE_EX(FixtureCtorAsserts, CorrectlyReportsAssertFailureInCtor, ctorAssertFixtureTestList) -{ -} - -TEST(CorrectlyReportsFixturesWithCtorsThatAssert) -{ - RecordingReporter reporter; - TestResults result(&reporter); - { - ScopedCurrentTest scopedResults(result); - ctorAssertFixtureTestList.GetHead()->Run(); - } - - const int failureCount = result.GetFailedTestCount(); - CHECK_EQUAL(1, failureCount); - CHECK_EQUAL(FailingLine, reporter.lastFailedLine); - CHECK(strstr(reporter.lastFailedMessage, "assert failure")); -} + struct FixtureCtorThrows + { + FixtureCtorThrows() { + throw "exception"; + } + }; + + TestList throwingFixtureTestList1; + TEST_FIXTURE_EX(FixtureCtorThrows, FixtureCtorThrowsTestName, throwingFixtureTestList1) + {} + + TEST(FixturesWithThrowingCtorsAreFailures) + { + CHECK(throwingFixtureTestList1.GetHead() != NULL); + RecordingReporter reporter; + TestResults result(&reporter); + { + ScopedCurrentTest scopedResult(result); + throwingFixtureTestList1.GetHead()->Run(); + } + + int const failureCount = result.GetFailedTestCount(); + CHECK_EQUAL(1, failureCount); + CHECK(strstr(reporter.lastFailedMessage, "while constructing fixture")); + } + + struct FixtureDtorThrows + { + ~FixtureDtorThrows() _NOEXCEPT_OP(false) { + throw "exception"; + } + }; + + TestList throwingFixtureTestList2; + TEST_FIXTURE_EX(FixtureDtorThrows, FixtureDtorThrowsTestName, throwingFixtureTestList2) + {} + + TEST(FixturesWithThrowingDtorsAreFailures) + { + CHECK(throwingFixtureTestList2.GetHead() != NULL); + + RecordingReporter reporter; + TestResults result(&reporter); + { + ScopedCurrentTest scopedResult(result); + throwingFixtureTestList2.GetHead()->Run(); + } + + int const failureCount = result.GetFailedTestCount(); + CHECK_EQUAL(1, failureCount); + CHECK(strstr(reporter.lastFailedMessage, "while destroying fixture")); + } + + const int FailingLine = 123; + + struct FixtureCtorAsserts + { + FixtureCtorAsserts() + { + UnitTest::ReportAssert("assert failure", "file", FailingLine); + } + }; + + TestList ctorAssertFixtureTestList; + TEST_FIXTURE_EX(FixtureCtorAsserts, CorrectlyReportsAssertFailureInCtor, ctorAssertFixtureTestList) + {} + + TEST(CorrectlyReportsFixturesWithCtorsThatAssert) + { + RecordingReporter reporter; + TestResults result(&reporter); + { + ScopedCurrentTest scopedResults(result); + ctorAssertFixtureTestList.GetHead()->Run(); + } + + const int failureCount = result.GetFailedTestCount(); + CHECK_EQUAL(1, failureCount); + CHECK_EQUAL(FailingLine, reporter.lastFailedLine); + CHECK(strstr(reporter.lastFailedMessage, "assert failure")); + } #endif @@ -234,9 +227,8 @@ TEST(CorrectlyReportsFixturesWithCtorsThatAssert) // Note: we are outside of the anonymous namespace SUITE(SameTestSuite) { - TEST(DummyTest1) - { - } + TEST(DummyTest1) + {} } #define CUR_TEST_NAME CurrentTestDetailsContainCurrentTestInfo @@ -245,8 +237,8 @@ SUITE(SameTestSuite) TEST(CUR_TEST_NAME) { - const UnitTest::TestDetails* details = CurrentTest::Details(); - CHECK_EQUAL(STRINGIFY(CUR_TEST_NAME), details->testName); + const UnitTest::TestDetails* details = CurrentTest::Details(); + CHECK_EQUAL(STRINGIFY(CUR_TEST_NAME), details->testName); } #undef CUR_TEST_NAME diff --git a/tests/TestTestResults.cpp b/tests/TestTestResults.cpp index a115687..1f66744 100644 --- a/tests/TestTestResults.cpp +++ b/tests/TestTestResults.cpp @@ -6,106 +6,106 @@ using namespace UnitTest; namespace { -TestDetails const details("testname", "suitename", "filename", 123); - - -TEST(StartsWithNoTestsRun) -{ - TestResults results; - CHECK_EQUAL (0, results.GetTotalTestCount()); -} - -TEST(RecordsNumbersOfTests) -{ - TestResults results; - results.OnTestStart(details); - results.OnTestStart(details); - results.OnTestStart(details); - CHECK_EQUAL(3, results.GetTotalTestCount()); -} - -TEST(StartsWithNoTestsFailing) -{ - TestResults results; - CHECK_EQUAL (0, results.GetFailureCount()); -} - -TEST(RecordsNumberOfFailures) -{ - TestResults results; - results.OnTestFailure(details, ""); - results.OnTestFailure(details, ""); - CHECK_EQUAL(2, results.GetFailureCount()); -} - -TEST(RecordsNumberOfFailedTests) -{ - TestResults results; - - results.OnTestStart(details); - results.OnTestFailure(details, ""); - results.OnTestFinish(details, 0); - - results.OnTestStart(details); - results.OnTestFailure(details, ""); - results.OnTestFailure(details, ""); - results.OnTestFailure(details, ""); - results.OnTestFinish(details, 0); - - CHECK_EQUAL (2, results.GetFailedTestCount()); -} - -TEST(NotifiesReporterOfTestStartWithCorrectInfo) -{ - RecordingReporter reporter; - TestResults results(&reporter); - results.OnTestStart(details); - - CHECK_EQUAL (1, reporter.testRunCount); - CHECK_EQUAL ("suitename", reporter.lastStartedSuite); - CHECK_EQUAL ("testname", reporter.lastStartedTest); -} - -TEST(NotifiesReporterOfTestFailureWithCorrectInfo) -{ - RecordingReporter reporter; - TestResults results(&reporter); - - results.OnTestFailure(details, "failurestring"); - CHECK_EQUAL (1, reporter.testFailedCount); - CHECK_EQUAL ("filename", reporter.lastFailedFile); - CHECK_EQUAL (123, reporter.lastFailedLine); - CHECK_EQUAL ("suitename", reporter.lastFailedSuite); - CHECK_EQUAL ("testname", reporter.lastFailedTest); - CHECK_EQUAL ("failurestring", reporter.lastFailedMessage); -} - -TEST(NotifiesReporterOfCheckFailureWithCorrectInfo) -{ - RecordingReporter reporter; - TestResults results(&reporter); - - results.OnTestFailure(details, "failurestring"); - CHECK_EQUAL (1, reporter.testFailedCount); - - CHECK_EQUAL ("filename", reporter.lastFailedFile); - CHECK_EQUAL (123, reporter.lastFailedLine); - CHECK_EQUAL ("testname", reporter.lastFailedTest); - CHECK_EQUAL ("suitename", reporter.lastFailedSuite); - CHECK_EQUAL ("failurestring", reporter.lastFailedMessage); -} - -TEST(NotifiesReporterOfTestEnd) -{ - RecordingReporter reporter; - TestResults results(&reporter); - - results.OnTestFinish(details, 0.1234f); - CHECK_EQUAL (1, reporter.testFinishedCount); - CHECK_EQUAL ("testname", reporter.lastFinishedTest); - CHECK_EQUAL ("suitename", reporter.lastFinishedSuite); - CHECK_CLOSE (0.1234f, reporter.lastFinishedTestTime, 0.0001f); -} + TestDetails const details("testname", "suitename", "filename", 123); + + + TEST(StartsWithNoTestsRun) + { + TestResults results; + CHECK_EQUAL (0, results.GetTotalTestCount()); + } + + TEST(RecordsNumbersOfTests) + { + TestResults results; + results.OnTestStart(details); + results.OnTestStart(details); + results.OnTestStart(details); + CHECK_EQUAL(3, results.GetTotalTestCount()); + } + + TEST(StartsWithNoTestsFailing) + { + TestResults results; + CHECK_EQUAL (0, results.GetFailureCount()); + } + + TEST(RecordsNumberOfFailures) + { + TestResults results; + results.OnTestFailure(details, ""); + results.OnTestFailure(details, ""); + CHECK_EQUAL(2, results.GetFailureCount()); + } + + TEST(RecordsNumberOfFailedTests) + { + TestResults results; + + results.OnTestStart(details); + results.OnTestFailure(details, ""); + results.OnTestFinish(details, 0); + + results.OnTestStart(details); + results.OnTestFailure(details, ""); + results.OnTestFailure(details, ""); + results.OnTestFailure(details, ""); + results.OnTestFinish(details, 0); + + CHECK_EQUAL (2, results.GetFailedTestCount()); + } + + TEST(NotifiesReporterOfTestStartWithCorrectInfo) + { + RecordingReporter reporter; + TestResults results(&reporter); + results.OnTestStart(details); + + CHECK_EQUAL (1, reporter.testRunCount); + CHECK_EQUAL ("suitename", reporter.lastStartedSuite); + CHECK_EQUAL ("testname", reporter.lastStartedTest); + } + + TEST(NotifiesReporterOfTestFailureWithCorrectInfo) + { + RecordingReporter reporter; + TestResults results(&reporter); + + results.OnTestFailure(details, "failurestring"); + CHECK_EQUAL (1, reporter.testFailedCount); + CHECK_EQUAL ("filename", reporter.lastFailedFile); + CHECK_EQUAL (123, reporter.lastFailedLine); + CHECK_EQUAL ("suitename", reporter.lastFailedSuite); + CHECK_EQUAL ("testname", reporter.lastFailedTest); + CHECK_EQUAL ("failurestring", reporter.lastFailedMessage); + } + + TEST(NotifiesReporterOfCheckFailureWithCorrectInfo) + { + RecordingReporter reporter; + TestResults results(&reporter); + + results.OnTestFailure(details, "failurestring"); + CHECK_EQUAL (1, reporter.testFailedCount); + + CHECK_EQUAL ("filename", reporter.lastFailedFile); + CHECK_EQUAL (123, reporter.lastFailedLine); + CHECK_EQUAL ("testname", reporter.lastFailedTest); + CHECK_EQUAL ("suitename", reporter.lastFailedSuite); + CHECK_EQUAL ("failurestring", reporter.lastFailedMessage); + } + + TEST(NotifiesReporterOfTestEnd) + { + RecordingReporter reporter; + TestResults results(&reporter); + + results.OnTestFinish(details, 0.1234f); + CHECK_EQUAL (1, reporter.testFinishedCount); + CHECK_EQUAL ("testname", reporter.lastFinishedTest); + CHECK_EQUAL ("suitename", reporter.lastFinishedSuite); + CHECK_CLOSE (0.1234f, reporter.lastFinishedTestTime, 0.0001f); + } } diff --git a/tests/TestTestRunner.cpp b/tests/TestTestRunner.cpp index d4982a4..86c672d 100644 --- a/tests/TestTestRunner.cpp +++ b/tests/TestTestRunner.cpp @@ -10,328 +10,324 @@ using namespace UnitTest; namespace { - - struct MockTest : public Test - { - MockTest(char const* testName, bool const success_, bool const assert_, int const count_ = 1) - : Test(testName) - , success(success_) - , asserted(assert_) - , count(count_) - { - } - - virtual void RunImpl() const - { - TestResults& testResults_ = *CurrentTest::Results(); - - for (int i=0; i < count; ++i) + + struct MockTest : public Test + { + MockTest(char const* testName, bool const success_, bool const assert_, int const count_ = 1) + : Test(testName) + , success(success_) + , asserted(assert_) + , count(count_) + {} + + virtual void RunImpl() const + { + TestResults& testResults_ = *CurrentTest::Results(); + + for (int i=0; i < count; ++i) + { + if (asserted) { - if (asserted) - { - ReportAssert("desc", "file", 0); - } - else if (!success) - { - testResults_.OnTestFailure(m_details, "message"); - } + ReportAssert("desc", "file", 0); } - } - - bool const success; - bool const asserted; - int const count; - }; - - struct FixtureBase - { - FixtureBase() - : runner(reporter) - { - } - - template - int RunTestsIf(TestList const& list, char const* suiteName, - const Predicate& predicate, int maxTestTimeInMs) - { - TestResults* oldResults = CurrentTest::Results(); - const TestDetails* oldDetails = CurrentTest::Details(); - int result = runner.RunTestsIf(list, suiteName, predicate, maxTestTimeInMs); - CurrentTest::Results() = oldResults; - CurrentTest::Details() = oldDetails; - return result; - } - - TestRunner runner; - RecordingReporter reporter; - }; - - struct TestRunnerFixture : public FixtureBase - { - TestList list; - }; - - TEST_FIXTURE(TestRunnerFixture, TestStartIsReportedCorrectly) - { - MockTest test("goodtest", true, false); - list.Add(&test); - - RunTestsIf(list, NULL, True(), 0); - CHECK_EQUAL(1, reporter.testRunCount); - CHECK_EQUAL("goodtest", reporter.lastStartedTest); - } - - TEST_FIXTURE(TestRunnerFixture, TestFinishIsReportedCorrectly) - { - MockTest test("goodtest", true, false); - list.Add(&test); - - RunTestsIf(list, NULL, True(), 0); - CHECK_EQUAL(1, reporter.testFinishedCount); - CHECK_EQUAL("goodtest", reporter.lastFinishedTest); - } - - class SlowTest : public Test - { - public: - SlowTest() - : Test("slow", "somesuite", "filename", 123) - { - } - - virtual void RunImpl() const - { - TimeHelpers::SleepMs(20); - } - }; - - TEST_FIXTURE(TestRunnerFixture, TestFinishIsCalledWithCorrectTime) - { - SlowTest test; - list.Add(&test); - - // Using UnitTest::Timer here is arguably a bit hokey and self-referential, but - // it should guarantee that the test time recorded is less than that plus the - // overhead of RunTestsIf -- the only thing we can reliably assert without - // reworking the test to not use sleeps at all - Timer actual; - actual.Start(); - RunTestsIf(list, NULL, True(), 0); - - CHECK(reporter.lastFinishedTestTime >= 0.005f && reporter.lastFinishedTestTime <= actual.GetTimeInMs()); - } - - TEST_FIXTURE(TestRunnerFixture, FailureCountIsZeroWhenNoTestsAreRun) - { - CHECK_EQUAL(0, RunTestsIf(list, NULL, True(), 0)); - CHECK_EQUAL(0, reporter.testRunCount); - CHECK_EQUAL(0, reporter.testFailedCount); - } - - TEST_FIXTURE(TestRunnerFixture, CallsReportFailureOncePerFailingTest) - { - MockTest test1("test", false, false); - list.Add(&test1); - MockTest test2("test", true, false); - list.Add(&test2); - MockTest test3("test", false, false); - list.Add(&test3); - - CHECK_EQUAL(2, RunTestsIf(list, NULL, True(), 0)); - CHECK_EQUAL(2, reporter.testFailedCount); - } - - TEST_FIXTURE(TestRunnerFixture, TestsThatAssertAreReportedAsFailing) - { - MockTest test("test", true, true); - list.Add(&test); - - RunTestsIf(list, NULL, True(), 0); - CHECK_EQUAL(1, reporter.testFailedCount); - } - - - TEST_FIXTURE(TestRunnerFixture, ReporterNotifiedOfTestCount) - { - MockTest test1("test", true, false); - MockTest test2("test", true, false); - MockTest test3("test", true, false); - list.Add(&test1); - list.Add(&test2); - list.Add(&test3); - - RunTestsIf(list, NULL, True(), 0); - CHECK_EQUAL(3, reporter.summaryTotalTestCount); - } - - TEST_FIXTURE(TestRunnerFixture, ReporterNotifiedOfFailedTests) - { - MockTest test1("test", false, false, 2); - MockTest test2("test", true, false); - MockTest test3("test", false, false, 3); - list.Add(&test1); - list.Add(&test2); - list.Add(&test3); - - RunTestsIf(list, NULL, True(), 0); - CHECK_EQUAL(2, reporter.summaryFailedTestCount); - } - - TEST_FIXTURE(TestRunnerFixture, ReporterNotifiedOfFailures) - { - MockTest test1("test", false, false, 2); - MockTest test2("test", true, false); - MockTest test3("test", false, false, 3); - list.Add(&test1); - list.Add(&test2); - list.Add(&test3); - - RunTestsIf(list, NULL, True(), 0); - CHECK_EQUAL(5, reporter.summaryFailureCount); - } - - TEST_FIXTURE(TestRunnerFixture, SlowTestPassesForHighTimeThreshold) - { - SlowTest test; - list.Add(&test); - - RunTestsIf(list, NULL, True(), 0); - CHECK_EQUAL(0, reporter.testFailedCount); - } - - TEST_FIXTURE(TestRunnerFixture, SlowTestFailsForLowTimeThreshold) - { - SlowTest test; - list.Add(&test); - - RunTestsIf(list, NULL, True(), 3); - CHECK_EQUAL(1, reporter.testFailedCount); - } - - TEST_FIXTURE(TestRunnerFixture, SlowTestHasCorrectFailureInformation) - { - SlowTest test; - list.Add(&test); - - RunTestsIf(list, NULL, True(), 3); - - using namespace std; - - CHECK_EQUAL(test.m_details.testName, reporter.lastFailedTest); - CHECK(strstr(test.m_details.filename, reporter.lastFailedFile)); - CHECK_EQUAL(test.m_details.lineNumber, reporter.lastFailedLine); - - CHECK(strstr(reporter.lastFailedMessage, "Global time constraint failed")); - CHECK(strstr(reporter.lastFailedMessage, "3ms")); - } - - TEST_FIXTURE(TestRunnerFixture, SlowTestWithTimeExemptionPasses) - { - class SlowExemptedTest : public Test - { - public: - SlowExemptedTest() : Test("slowexempted", "", 0) {} - virtual void RunImpl() const + else if (!success) { - UNITTEST_TIME_CONSTRAINT_EXEMPT(); - TimeHelpers::SleepMs(20); + testResults_.OnTestFailure(m_details, "message"); } - }; - - SlowExemptedTest test; - list.Add(&test); - - RunTestsIf(list, NULL, True(), 3); - CHECK_EQUAL(0, reporter.testFailedCount); - } - - struct TestSuiteFixture : FixtureBase - { - TestSuiteFixture() - : test1("TestInDefaultSuite") - , test2("TestInOtherSuite", "OtherSuite") - , test3("SecondTestInDefaultSuite") - { - list.Add(&test1); - list.Add(&test2); - } - - Test test1; - Test test2; - Test test3; - TestList list; - }; - - TEST_FIXTURE(TestSuiteFixture, TestRunnerRunsAllSuitesIfNullSuiteIsPassed) - { - RunTestsIf(list, NULL, True(), 0); - CHECK_EQUAL(2, reporter.summaryTotalTestCount); - } - - TEST_FIXTURE(TestSuiteFixture,TestRunnerRunsOnlySpecifiedSuite) - { - RunTestsIf(list, "OtherSuite", True(), 0); - CHECK_EQUAL(1, reporter.summaryTotalTestCount); - CHECK_EQUAL("TestInOtherSuite", reporter.lastFinishedTest); - } - - struct RunTestIfNameIs - { - RunTestIfNameIs(char const* name_) - : name(name_) - { - } - - bool operator()(const Test* const test) const - { - using namespace std; - return (0 == strcmp(test->m_details.testName, name)); - } - - char const* name; - }; - - TEST(TestMockPredicateBehavesCorrectly) - { - RunTestIfNameIs predicate("pass"); - - Test pass("pass"); - Test fail("fail"); - - CHECK(predicate(&pass)); - CHECK(!predicate(&fail)); - } - - TEST_FIXTURE(TestRunnerFixture, TestRunnerRunsTestsThatPassPredicate) - { - Test should_run("goodtest"); - list.Add(&should_run); - - Test should_not_run("badtest"); - list.Add(&should_not_run); - - RunTestsIf(list, NULL, RunTestIfNameIs("goodtest"), 0); - CHECK_EQUAL(1, reporter.testRunCount); - CHECK_EQUAL("goodtest", reporter.lastStartedTest); - } - - TEST_FIXTURE(TestRunnerFixture, TestRunnerOnlyRunsTestsInSpecifiedSuiteAndThatPassPredicate) - { - Test runningTest1("goodtest", "suite"); - Test skippedTest2("goodtest"); - Test skippedTest3("badtest", "suite"); - Test skippedTest4("badtest"); - - list.Add(&runningTest1); - list.Add(&skippedTest2); - list.Add(&skippedTest3); - list.Add(&skippedTest4); - - RunTestsIf(list, "suite", RunTestIfNameIs("goodtest"), 0); - - CHECK_EQUAL(1, reporter.testRunCount); - CHECK_EQUAL("goodtest", reporter.lastStartedTest); - CHECK_EQUAL("suite", reporter.lastStartedSuite); - } - + } + } + + bool const success; + bool const asserted; + int const count; + }; + + struct FixtureBase + { + FixtureBase() + : runner(reporter) + {} + + template + int RunTestsIf(TestList const& list, char const* suiteName, + const Predicate& predicate, int maxTestTimeInMs) + { + TestResults* oldResults = CurrentTest::Results(); + const TestDetails* oldDetails = CurrentTest::Details(); + int result = runner.RunTestsIf(list, suiteName, predicate, maxTestTimeInMs); + CurrentTest::Results() = oldResults; + CurrentTest::Details() = oldDetails; + return result; + } + + TestRunner runner; + RecordingReporter reporter; + }; + + struct TestRunnerFixture : public FixtureBase + { + TestList list; + }; + + TEST_FIXTURE(TestRunnerFixture, TestStartIsReportedCorrectly) + { + MockTest test("goodtest", true, false); + list.Add(&test); + + RunTestsIf(list, NULL, True(), 0); + CHECK_EQUAL(1, reporter.testRunCount); + CHECK_EQUAL("goodtest", reporter.lastStartedTest); + } + + TEST_FIXTURE(TestRunnerFixture, TestFinishIsReportedCorrectly) + { + MockTest test("goodtest", true, false); + list.Add(&test); + + RunTestsIf(list, NULL, True(), 0); + CHECK_EQUAL(1, reporter.testFinishedCount); + CHECK_EQUAL("goodtest", reporter.lastFinishedTest); + } + + class SlowTest : public Test + { + public: + SlowTest() + : Test("slow", "somesuite", "filename", 123) + {} + + virtual void RunImpl() const + { + TimeHelpers::SleepMs(20); + } + }; + + TEST_FIXTURE(TestRunnerFixture, TestFinishIsCalledWithCorrectTime) + { + SlowTest test; + list.Add(&test); + + // Using UnitTest::Timer here is arguably a bit hokey and self-referential, but + // it should guarantee that the test time recorded is less than that plus the + // overhead of RunTestsIf -- the only thing we can reliably assert without + // reworking the test to not use sleeps at all + Timer actual; + actual.Start(); + RunTestsIf(list, NULL, True(), 0); + + CHECK(reporter.lastFinishedTestTime >= 0.005f && reporter.lastFinishedTestTime <= actual.GetTimeInMs()); + } + + TEST_FIXTURE(TestRunnerFixture, FailureCountIsZeroWhenNoTestsAreRun) + { + CHECK_EQUAL(0, RunTestsIf(list, NULL, True(), 0)); + CHECK_EQUAL(0, reporter.testRunCount); + CHECK_EQUAL(0, reporter.testFailedCount); + } + + TEST_FIXTURE(TestRunnerFixture, CallsReportFailureOncePerFailingTest) + { + MockTest test1("test", false, false); + list.Add(&test1); + MockTest test2("test", true, false); + list.Add(&test2); + MockTest test3("test", false, false); + list.Add(&test3); + + CHECK_EQUAL(2, RunTestsIf(list, NULL, True(), 0)); + CHECK_EQUAL(2, reporter.testFailedCount); + } + + TEST_FIXTURE(TestRunnerFixture, TestsThatAssertAreReportedAsFailing) + { + MockTest test("test", true, true); + list.Add(&test); + + RunTestsIf(list, NULL, True(), 0); + CHECK_EQUAL(1, reporter.testFailedCount); + } + + + TEST_FIXTURE(TestRunnerFixture, ReporterNotifiedOfTestCount) + { + MockTest test1("test", true, false); + MockTest test2("test", true, false); + MockTest test3("test", true, false); + list.Add(&test1); + list.Add(&test2); + list.Add(&test3); + + RunTestsIf(list, NULL, True(), 0); + CHECK_EQUAL(3, reporter.summaryTotalTestCount); + } + + TEST_FIXTURE(TestRunnerFixture, ReporterNotifiedOfFailedTests) + { + MockTest test1("test", false, false, 2); + MockTest test2("test", true, false); + MockTest test3("test", false, false, 3); + list.Add(&test1); + list.Add(&test2); + list.Add(&test3); + + RunTestsIf(list, NULL, True(), 0); + CHECK_EQUAL(2, reporter.summaryFailedTestCount); + } + + TEST_FIXTURE(TestRunnerFixture, ReporterNotifiedOfFailures) + { + MockTest test1("test", false, false, 2); + MockTest test2("test", true, false); + MockTest test3("test", false, false, 3); + list.Add(&test1); + list.Add(&test2); + list.Add(&test3); + + RunTestsIf(list, NULL, True(), 0); + CHECK_EQUAL(5, reporter.summaryFailureCount); + } + + TEST_FIXTURE(TestRunnerFixture, SlowTestPassesForHighTimeThreshold) + { + SlowTest test; + list.Add(&test); + + RunTestsIf(list, NULL, True(), 0); + CHECK_EQUAL(0, reporter.testFailedCount); + } + + TEST_FIXTURE(TestRunnerFixture, SlowTestFailsForLowTimeThreshold) + { + SlowTest test; + list.Add(&test); + + RunTestsIf(list, NULL, True(), 3); + CHECK_EQUAL(1, reporter.testFailedCount); + } + + TEST_FIXTURE(TestRunnerFixture, SlowTestHasCorrectFailureInformation) + { + SlowTest test; + list.Add(&test); + + RunTestsIf(list, NULL, True(), 3); + + using namespace std; + + CHECK_EQUAL(test.m_details.testName, reporter.lastFailedTest); + CHECK(strstr(test.m_details.filename, reporter.lastFailedFile)); + CHECK_EQUAL(test.m_details.lineNumber, reporter.lastFailedLine); + + CHECK(strstr(reporter.lastFailedMessage, "Global time constraint failed")); + CHECK(strstr(reporter.lastFailedMessage, "3ms")); + } + + TEST_FIXTURE(TestRunnerFixture, SlowTestWithTimeExemptionPasses) + { + class SlowExemptedTest : public Test + { + public: + SlowExemptedTest() : Test("slowexempted", "", 0) {} + virtual void RunImpl() const + { + UNITTEST_TIME_CONSTRAINT_EXEMPT(); + TimeHelpers::SleepMs(20); + } + }; + + SlowExemptedTest test; + list.Add(&test); + + RunTestsIf(list, NULL, True(), 3); + CHECK_EQUAL(0, reporter.testFailedCount); + } + + struct TestSuiteFixture : FixtureBase + { + TestSuiteFixture() + : test1("TestInDefaultSuite") + , test2("TestInOtherSuite", "OtherSuite") + , test3("SecondTestInDefaultSuite") + { + list.Add(&test1); + list.Add(&test2); + } + + Test test1; + Test test2; + Test test3; + TestList list; + }; + + TEST_FIXTURE(TestSuiteFixture, TestRunnerRunsAllSuitesIfNullSuiteIsPassed) + { + RunTestsIf(list, NULL, True(), 0); + CHECK_EQUAL(2, reporter.summaryTotalTestCount); + } + + TEST_FIXTURE(TestSuiteFixture,TestRunnerRunsOnlySpecifiedSuite) + { + RunTestsIf(list, "OtherSuite", True(), 0); + CHECK_EQUAL(1, reporter.summaryTotalTestCount); + CHECK_EQUAL("TestInOtherSuite", reporter.lastFinishedTest); + } + + struct RunTestIfNameIs + { + RunTestIfNameIs(char const* name_) + : name(name_) + {} + + bool operator()(const Test* const test) const + { + using namespace std; + return (0 == strcmp(test->m_details.testName, name)); + } + + char const* name; + }; + + TEST(TestMockPredicateBehavesCorrectly) + { + RunTestIfNameIs predicate("pass"); + + Test pass("pass"); + Test fail("fail"); + + CHECK(predicate(&pass)); + CHECK(!predicate(&fail)); + } + + TEST_FIXTURE(TestRunnerFixture, TestRunnerRunsTestsThatPassPredicate) + { + Test should_run("goodtest"); + list.Add(&should_run); + + Test should_not_run("badtest"); + list.Add(&should_not_run); + + RunTestsIf(list, NULL, RunTestIfNameIs("goodtest"), 0); + CHECK_EQUAL(1, reporter.testRunCount); + CHECK_EQUAL("goodtest", reporter.lastStartedTest); + } + + TEST_FIXTURE(TestRunnerFixture, TestRunnerOnlyRunsTestsInSpecifiedSuiteAndThatPassPredicate) + { + Test runningTest1("goodtest", "suite"); + Test skippedTest2("goodtest"); + Test skippedTest3("badtest", "suite"); + Test skippedTest4("badtest"); + + list.Add(&runningTest1); + list.Add(&skippedTest2); + list.Add(&skippedTest3); + list.Add(&skippedTest4); + + RunTestsIf(list, "suite", RunTestIfNameIs("goodtest"), 0); + + CHECK_EQUAL(1, reporter.testRunCount); + CHECK_EQUAL("goodtest", reporter.lastStartedTest); + CHECK_EQUAL("suite", reporter.lastStartedSuite); + } + } diff --git a/tests/TestTestSuite.cpp b/tests/TestTestSuite.cpp index ff56089..e0bbacf 100644 --- a/tests/TestTestSuite.cpp +++ b/tests/TestTestSuite.cpp @@ -5,8 +5,7 @@ // Note: we are outside of the anonymous namespace SUITE(SameTestSuite) { - TEST(DummyTest2) - { - } + TEST(DummyTest2) + {} } diff --git a/tests/TestTimeConstraint.cpp b/tests/TestTimeConstraint.cpp index ab93a05..976b8ce 100644 --- a/tests/TestTimeConstraint.cpp +++ b/tests/TestTimeConstraint.cpp @@ -9,61 +9,61 @@ using namespace UnitTest; namespace { -TEST(TimeConstraintSucceedsWithFastTest) -{ - TestResults result; - { - ScopedCurrentTest scopedResult(result); - TimeConstraint t(200, TestDetails("", "", "", 0), 0); - TimeHelpers::SleepMs(5); - } - CHECK_EQUAL(0, result.GetFailureCount()); -} + TEST(TimeConstraintSucceedsWithFastTest) + { + TestResults result; + { + ScopedCurrentTest scopedResult(result); + TimeConstraint t(200, TestDetails("", "", "", 0), 0); + TimeHelpers::SleepMs(5); + } + CHECK_EQUAL(0, result.GetFailureCount()); + } -TEST(TimeConstraintFailsWithSlowTest) -{ - TestResults result; - { - ScopedCurrentTest scopedResult(result); - TimeConstraint t(10, TestDetails("", "", "", 0),0); - TimeHelpers::SleepMs(20); - } - CHECK_EQUAL(1, result.GetFailureCount()); -} + TEST(TimeConstraintFailsWithSlowTest) + { + TestResults result; + { + ScopedCurrentTest scopedResult(result); + TimeConstraint t(10, TestDetails("", "", "", 0),0); + TimeHelpers::SleepMs(20); + } + CHECK_EQUAL(1, result.GetFailureCount()); + } -TEST(TimeConstraintFailureIncludesCorrectData) -{ - RecordingReporter reporter; - TestResults result(&reporter); - { - ScopedCurrentTest scopedResult(result); + TEST(TimeConstraintFailureIncludesCorrectData) + { + RecordingReporter reporter; + TestResults result(&reporter); + { + ScopedCurrentTest scopedResult(result); - TestDetails const details("testname", "suitename", "filename", 10); - TimeConstraint t(10, details,10); - TimeHelpers::SleepMs(20); - } + TestDetails const details("testname", "suitename", "filename", 10); + TimeConstraint t(10, details,10); + TimeHelpers::SleepMs(20); + } - using namespace std; + using namespace std; - CHECK(strstr(reporter.lastFailedFile, "filename")); - CHECK_EQUAL(10, reporter.lastFailedLine); - CHECK(strstr(reporter.lastFailedTest, "testname")); -} + CHECK(strstr(reporter.lastFailedFile, "filename")); + CHECK_EQUAL(10, reporter.lastFailedLine); + CHECK(strstr(reporter.lastFailedTest, "testname")); + } -TEST(TimeConstraintFailureIncludesTimeoutInformation) -{ - RecordingReporter reporter; - TestResults result(&reporter); - { - ScopedCurrentTest scopedResult(result); - TimeConstraint t(10, TestDetails("", "", "", 0),0); - TimeHelpers::SleepMs(20); - } + TEST(TimeConstraintFailureIncludesTimeoutInformation) + { + RecordingReporter reporter; + TestResults result(&reporter); + { + ScopedCurrentTest scopedResult(result); + TimeConstraint t(10, TestDetails("", "", "", 0),0); + TimeHelpers::SleepMs(20); + } - using namespace std; + using namespace std; - CHECK(strstr(reporter.lastFailedMessage, "ime constraint")); - CHECK(strstr(reporter.lastFailedMessage, "under 10ms")); -} + CHECK(strstr(reporter.lastFailedMessage, "ime constraint")); + CHECK(strstr(reporter.lastFailedMessage, "under 10ms")); + } } diff --git a/tests/TestTimeConstraintMacro.cpp b/tests/TestTimeConstraintMacro.cpp index 3caf15d..a8d0c79 100644 --- a/tests/TestTimeConstraintMacro.cpp +++ b/tests/TestTimeConstraintMacro.cpp @@ -6,83 +6,83 @@ namespace { -TEST(TimeConstraintMacroQualifiesNamespace) -{ - // If this compiles without a "using namespace UnitTest;", all is well. - UNITTEST_TIME_CONSTRAINT(1); -} - -TEST(TimeConstraintMacroUsesCorrectInfo) -{ - int testLine = 0; - RecordingReporter reporter; - - { - UnitTest::TestResults testResults(&reporter); - ScopedCurrentTest scopedResults(testResults); - - UNITTEST_TIME_CONSTRAINT(10); testLine = __LINE__; - UnitTest::TimeHelpers::SleepMs(20); - } - - using namespace std; - - CHECK_EQUAL(1, reporter.testFailedCount); - CHECK(strstr(reporter.lastFailedFile, __FILE__)); - CHECK_EQUAL(testLine, reporter.lastFailedLine); - CHECK(strstr(reporter.lastFailedTest, "TimeConstraintMacroUsesCorrectInfo")); -} - -TEST(TimeConstraintMacroComparesAgainstPreciseActual) -{ - int testLine = 0; - RecordingReporter reporter; - - { - UnitTest::TestResults testResults(&reporter); - ScopedCurrentTest scopedResults(testResults); - - UNITTEST_TIME_CONSTRAINT(1); testLine = __LINE__; - - // start a new timer and run until we're as little over the 1 msec - // threshold as we can achieve; this should guarantee that the "test" - // runs in some very small amount of time > 1 msec - UnitTest::Timer myTimer; - myTimer.Start(); - - while (myTimer.GetTimeInMs() < 1.001) - UnitTest::TimeHelpers::SleepMs(0); - } - - using namespace std; - - CHECK_EQUAL(1, reporter.testFailedCount); - CHECK(strstr(reporter.lastFailedFile, __FILE__)); - CHECK_EQUAL(testLine, reporter.lastFailedLine); - CHECK(strstr(reporter.lastFailedTest, "TimeConstraintMacroComparesAgainstPreciseActual")); -} - -struct EmptyFixture {}; - -TEST_FIXTURE(EmptyFixture, TimeConstraintMacroWorksInFixtures) -{ - int testLine = 0; - RecordingReporter reporter; - - { - UnitTest::TestResults testResults(&reporter); - ScopedCurrentTest scopedResults(testResults); - - UNITTEST_TIME_CONSTRAINT(10); testLine = __LINE__; - UnitTest::TimeHelpers::SleepMs(20); - } - - using namespace std; - - CHECK_EQUAL(1, reporter.testFailedCount); - CHECK(strstr(reporter.lastFailedFile, __FILE__)); - CHECK_EQUAL(testLine, reporter.lastFailedLine); - CHECK(strstr(reporter.lastFailedTest, "TimeConstraintMacroWorksInFixtures")); -} + TEST(TimeConstraintMacroQualifiesNamespace) + { + // If this compiles without a "using namespace UnitTest;", all is well. + UNITTEST_TIME_CONSTRAINT(1); + } + + TEST(TimeConstraintMacroUsesCorrectInfo) + { + int testLine = 0; + RecordingReporter reporter; + + { + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + UNITTEST_TIME_CONSTRAINT(10); testLine = __LINE__; + UnitTest::TimeHelpers::SleepMs(20); + } + + using namespace std; + + CHECK_EQUAL(1, reporter.testFailedCount); + CHECK(strstr(reporter.lastFailedFile, __FILE__)); + CHECK_EQUAL(testLine, reporter.lastFailedLine); + CHECK(strstr(reporter.lastFailedTest, "TimeConstraintMacroUsesCorrectInfo")); + } + + TEST(TimeConstraintMacroComparesAgainstPreciseActual) + { + int testLine = 0; + RecordingReporter reporter; + + { + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + UNITTEST_TIME_CONSTRAINT(1); testLine = __LINE__; + + // start a new timer and run until we're as little over the 1 msec + // threshold as we can achieve; this should guarantee that the "test" + // runs in some very small amount of time > 1 msec + UnitTest::Timer myTimer; + myTimer.Start(); + + while (myTimer.GetTimeInMs() < 1.001) + UnitTest::TimeHelpers::SleepMs(0); + } + + using namespace std; + + CHECK_EQUAL(1, reporter.testFailedCount); + CHECK(strstr(reporter.lastFailedFile, __FILE__)); + CHECK_EQUAL(testLine, reporter.lastFailedLine); + CHECK(strstr(reporter.lastFailedTest, "TimeConstraintMacroComparesAgainstPreciseActual")); + } + + struct EmptyFixture {}; + + TEST_FIXTURE(EmptyFixture, TimeConstraintMacroWorksInFixtures) + { + int testLine = 0; + RecordingReporter reporter; + + { + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + UNITTEST_TIME_CONSTRAINT(10); testLine = __LINE__; + UnitTest::TimeHelpers::SleepMs(20); + } + + using namespace std; + + CHECK_EQUAL(1, reporter.testFailedCount); + CHECK(strstr(reporter.lastFailedFile, __FILE__)); + CHECK_EQUAL(testLine, reporter.lastFailedLine); + CHECK(strstr(reporter.lastFailedTest, "TimeConstraintMacroWorksInFixtures")); + } } diff --git a/tests/TestUnitTestPP.cpp b/tests/TestUnitTestPP.cpp index 3bd6ab7..831b360 100644 --- a/tests/TestUnitTestPP.cpp +++ b/tests/TestUnitTestPP.cpp @@ -5,144 +5,143 @@ namespace { -TEST(ValidCheckSucceeds) -{ - bool const b = true; - CHECK(b); -} - -TEST(CheckWorksWithPointers) -{ - void* p = (void *)0x100; - CHECK(p); - CHECK(p != 0); -} - -TEST(ValidCheckEqualSucceeds) -{ - int const x = 3; - int const y = 3; - CHECK_EQUAL(x, y); -} - -TEST(CheckEqualWorksWithPointers) -{ - void* p = (void *)0; - CHECK_EQUAL((void*)0, p); -} - -TEST(ValidCheckCloseSucceeds) -{ - CHECK_CLOSE(2.0f, 2.001f, 0.01f); - CHECK_CLOSE(2.001f, 2.0f, 0.01f); -} - -TEST(ArrayCloseSucceeds) -{ - float const a1[] = {1, 2, 3}; - float const a2[] = {1, 2.01f, 3}; - CHECK_ARRAY_CLOSE(a1, a2, 3, 0.1f); -} + TEST(ValidCheckSucceeds) + { + bool const b = true; + CHECK(b); + } + + TEST(CheckWorksWithPointers) + { + void* p = (void *)0x100; + CHECK(p); + CHECK(p != 0); + } + + TEST(ValidCheckEqualSucceeds) + { + int const x = 3; + int const y = 3; + CHECK_EQUAL(x, y); + } + + TEST(CheckEqualWorksWithPointers) + { + void* p = (void *)0; + CHECK_EQUAL((void*)0, p); + } + + TEST(ValidCheckCloseSucceeds) + { + CHECK_CLOSE(2.0f, 2.001f, 0.01f); + CHECK_CLOSE(2.001f, 2.0f, 0.01f); + } + + TEST(ArrayCloseSucceeds) + { + float const a1[] = {1, 2, 3}; + float const a2[] = {1, 2.01f, 3}; + CHECK_ARRAY_CLOSE(a1, a2, 3, 0.1f); + } #ifndef UNITTEST_NO_EXCEPTIONS -TEST(CheckThrowMacroSucceedsOnCorrectException) -{ - struct TestException {}; - CHECK_THROW(throw TestException(), TestException); -} - -TEST(CheckAssertSucceeds) -{ - CHECK_ASSERT(UnitTest::ReportAssert("desc", "file", 0)); -} - -TEST(CheckThrowMacroFailsOnMissingException) -{ - class NoThrowTest : public UnitTest::Test - { - public: - NoThrowTest() : Test("nothrow") {} - void DontThrow() const - { - } - - virtual void RunImpl() const - { + TEST(CheckThrowMacroSucceedsOnCorrectException) + { + struct TestException {}; + CHECK_THROW(throw TestException(), TestException); + } + + TEST(CheckAssertSucceeds) + { + CHECK_ASSERT(UnitTest::ReportAssert("desc", "file", 0)); + } + + TEST(CheckThrowMacroFailsOnMissingException) + { + class NoThrowTest : public UnitTest::Test + { + public: + NoThrowTest() : Test("nothrow") {} + void DontThrow() const + {} + + virtual void RunImpl() const + { CHECK_THROW(DontThrow(), int); - } - }; - - UnitTest::TestResults results; - { - ScopedCurrentTest scopedResults(results); - - NoThrowTest test; - test.Run(); - } - - CHECK_EQUAL(1, results.GetFailureCount()); -} - -TEST(CheckThrowMacroFailsOnWrongException) -{ - class WrongThrowTest : public UnitTest::Test - { - public: - WrongThrowTest() : Test("wrongthrow") {} - virtual void RunImpl() const - { + } + }; + + UnitTest::TestResults results; + { + ScopedCurrentTest scopedResults(results); + + NoThrowTest test; + test.Run(); + } + + CHECK_EQUAL(1, results.GetFailureCount()); + } + + TEST(CheckThrowMacroFailsOnWrongException) + { + class WrongThrowTest : public UnitTest::Test + { + public: + WrongThrowTest() : Test("wrongthrow") {} + virtual void RunImpl() const + { CHECK_THROW(throw "oops", int); - } - }; + } + }; - UnitTest::TestResults results; - { - ScopedCurrentTest scopedResults(results); + UnitTest::TestResults results; + { + ScopedCurrentTest scopedResults(results); - WrongThrowTest test; - test.Run(); - } + WrongThrowTest test; + test.Run(); + } - CHECK_EQUAL(1, results.GetFailureCount()); -} + CHECK_EQUAL(1, results.GetFailureCount()); + } #endif -struct SimpleFixture -{ - SimpleFixture() - { - ++instanceCount; - } - ~SimpleFixture() - { - --instanceCount; - } - - static int instanceCount; -}; - -int SimpleFixture::instanceCount = 0; - -TEST_FIXTURE(SimpleFixture, DefaultFixtureCtorIsCalled) -{ - CHECK(SimpleFixture::instanceCount > 0); -} - -TEST_FIXTURE(SimpleFixture, OnlyOneFixtureAliveAtATime) -{ - CHECK_EQUAL(1, SimpleFixture::instanceCount); -} - -void CheckBool(const bool b) -{ - CHECK(b); -} - -TEST(CanCallCHECKOutsideOfTestFunction) -{ - CheckBool(true); -} + struct SimpleFixture + { + SimpleFixture() + { + ++instanceCount; + } + ~SimpleFixture() + { + --instanceCount; + } + + static int instanceCount; + }; + + int SimpleFixture::instanceCount = 0; + + TEST_FIXTURE(SimpleFixture, DefaultFixtureCtorIsCalled) + { + CHECK(SimpleFixture::instanceCount > 0); + } + + TEST_FIXTURE(SimpleFixture, OnlyOneFixtureAliveAtATime) + { + CHECK_EQUAL(1, SimpleFixture::instanceCount); + } + + void CheckBool(const bool b) + { + CHECK(b); + } + + TEST(CanCallCHECKOutsideOfTestFunction) + { + CheckBool(true); + } } diff --git a/tests/TestXmlTestReporter.cpp b/tests/TestXmlTestReporter.cpp index 154ada8..a3edde2 100644 --- a/tests/TestXmlTestReporter.cpp +++ b/tests/TestXmlTestReporter.cpp @@ -15,173 +15,172 @@ namespace #ifndef UNITTEST_MEMORYOUTSTREAM_IS_STD_OSTRINGSTREAM // Overload to let MemoryOutStream accept std::string -MemoryOutStream& operator<<(MemoryOutStream& s, const std::string& value) -{ - s << value.c_str(); - return s; -} + MemoryOutStream& operator<<(MemoryOutStream& s, const std::string& value) + { + s << value.c_str(); + return s; + } #endif -struct XmlTestReporterFixture -{ - XmlTestReporterFixture() - : reporter(output) - { - } - - ostringstream output; - XmlTestReporter reporter; -}; - -TEST_FIXTURE(XmlTestReporterFixture, MultipleCharactersAreEscaped) -{ - TestDetails const details("TestName", "suite", "filename.h", 4321); - - reporter.ReportTestStart(details); - reporter.ReportFailure(details, "\"\"\'\'&&<<>>"); - reporter.ReportTestFinish(details, 0.1f); - reporter.ReportSummary(1, 2, 3, 0.1f); - - char const* expected = - "" - "" - "" - "" - "" - ""; - - CHECK_EQUAL(expected, output.str().c_str()); -} - -TEST_FIXTURE(XmlTestReporterFixture, OutputIsCachedUntilReportSummaryIsCalled) -{ - TestDetails const details("", "", "", 0); - - reporter.ReportTestStart(details); - reporter.ReportFailure(details, "message"); - reporter.ReportTestFinish(details, 1.0F); - CHECK(output.str().empty()); - - reporter.ReportSummary(1, 1, 1, 1.0f); - CHECK(!output.str().empty()); -} - -TEST_FIXTURE(XmlTestReporterFixture, EmptyReportSummaryFormat) -{ - reporter.ReportSummary(0, 0, 0, 0.1f); - - const char *expected = - "" - "" - ""; - - CHECK_EQUAL(expected, output.str().c_str()); -} - -TEST_FIXTURE(XmlTestReporterFixture, SingleSuccessfulTestReportSummaryFormat) -{ - TestDetails const details("TestName", "DefaultSuite", "", 0); - - reporter.ReportTestStart(details); - reporter.ReportSummary(1, 0, 0, 0.1f); - - const char *expected = - "" - "" - "" - ""; - - CHECK_EQUAL(expected, output.str().c_str()); -} - -TEST_FIXTURE(XmlTestReporterFixture, SingleFailedTestReportSummaryFormat) -{ - TestDetails const details("A Test", "suite", "A File", 4321); - - reporter.ReportTestStart(details); - reporter.ReportFailure(details, "A Failure"); - reporter.ReportSummary(1, 1, 1, 0.1f); - - const char *expected = - "" - "" - "" - "" - "" - ""; - - CHECK_EQUAL(expected, output.str().c_str()); -} - -TEST_FIXTURE(XmlTestReporterFixture, FailureMessageIsXMLEscaped) -{ - TestDetails const details("TestName", "suite", "filename.h", 4321); - - reporter.ReportTestStart(details); - reporter.ReportFailure(details, "\"\'&<>"); - reporter.ReportTestFinish(details, 0.1f); - reporter.ReportSummary(1, 1, 1, 0.1f); - - char const* expected = - "" - "" - "" - "" - "" - ""; - - CHECK_EQUAL(expected, output.str().c_str()); -} - -TEST_FIXTURE(XmlTestReporterFixture, OneFailureAndOneSuccess) -{ - TestDetails const failedDetails("FailedTest", "suite", "fail.h", 1); - reporter.ReportTestStart(failedDetails); - reporter.ReportFailure(failedDetails, "expected 1 but was 2"); - reporter.ReportTestFinish(failedDetails, 0.1f); - - TestDetails const succeededDetails("SucceededTest", "suite", "", 0); - reporter.ReportTestStart(succeededDetails); - reporter.ReportTestFinish(succeededDetails, 1.0f); - reporter.ReportSummary(2, 1, 1, 1.1f); - - char const* expected = - "" - "" - "" - "" - "" - "" - ""; - - CHECK_EQUAL(expected, output.str().c_str()); -} - -TEST_FIXTURE(XmlTestReporterFixture, MultipleFailures) -{ - TestDetails const failedDetails1("FailedTest", "suite", "fail.h", 1); - TestDetails const failedDetails2("FailedTest", "suite", "fail.h", 31); - - reporter.ReportTestStart(failedDetails1); - reporter.ReportFailure(failedDetails1, "expected 1 but was 2"); - reporter.ReportFailure(failedDetails2, "expected one but was two"); - reporter.ReportTestFinish(failedDetails1, 0.1f); - - reporter.ReportSummary(1, 1, 2, 1.1f); - - char const* expected = - "" - "" - "" - "" - "" - "" - ""; - - CHECK_EQUAL(expected, output.str().c_str()); -} + struct XmlTestReporterFixture + { + XmlTestReporterFixture() + : reporter(output) + {} + + ostringstream output; + XmlTestReporter reporter; + }; + + TEST_FIXTURE(XmlTestReporterFixture, MultipleCharactersAreEscaped) + { + TestDetails const details("TestName", "suite", "filename.h", 4321); + + reporter.ReportTestStart(details); + reporter.ReportFailure(details, "\"\"\'\'&&<<>>"); + reporter.ReportTestFinish(details, 0.1f); + reporter.ReportSummary(1, 2, 3, 0.1f); + + char const* expected = + "" + "" + "" + "" + "" + ""; + + CHECK_EQUAL(expected, output.str().c_str()); + } + + TEST_FIXTURE(XmlTestReporterFixture, OutputIsCachedUntilReportSummaryIsCalled) + { + TestDetails const details("", "", "", 0); + + reporter.ReportTestStart(details); + reporter.ReportFailure(details, "message"); + reporter.ReportTestFinish(details, 1.0F); + CHECK(output.str().empty()); + + reporter.ReportSummary(1, 1, 1, 1.0f); + CHECK(!output.str().empty()); + } + + TEST_FIXTURE(XmlTestReporterFixture, EmptyReportSummaryFormat) + { + reporter.ReportSummary(0, 0, 0, 0.1f); + + const char *expected = + "" + "" + ""; + + CHECK_EQUAL(expected, output.str().c_str()); + } + + TEST_FIXTURE(XmlTestReporterFixture, SingleSuccessfulTestReportSummaryFormat) + { + TestDetails const details("TestName", "DefaultSuite", "", 0); + + reporter.ReportTestStart(details); + reporter.ReportSummary(1, 0, 0, 0.1f); + + const char *expected = + "" + "" + "" + ""; + + CHECK_EQUAL(expected, output.str().c_str()); + } + + TEST_FIXTURE(XmlTestReporterFixture, SingleFailedTestReportSummaryFormat) + { + TestDetails const details("A Test", "suite", "A File", 4321); + + reporter.ReportTestStart(details); + reporter.ReportFailure(details, "A Failure"); + reporter.ReportSummary(1, 1, 1, 0.1f); + + const char *expected = + "" + "" + "" + "" + "" + ""; + + CHECK_EQUAL(expected, output.str().c_str()); + } + + TEST_FIXTURE(XmlTestReporterFixture, FailureMessageIsXMLEscaped) + { + TestDetails const details("TestName", "suite", "filename.h", 4321); + + reporter.ReportTestStart(details); + reporter.ReportFailure(details, "\"\'&<>"); + reporter.ReportTestFinish(details, 0.1f); + reporter.ReportSummary(1, 1, 1, 0.1f); + + char const* expected = + "" + "" + "" + "" + "" + ""; + + CHECK_EQUAL(expected, output.str().c_str()); + } + + TEST_FIXTURE(XmlTestReporterFixture, OneFailureAndOneSuccess) + { + TestDetails const failedDetails("FailedTest", "suite", "fail.h", 1); + reporter.ReportTestStart(failedDetails); + reporter.ReportFailure(failedDetails, "expected 1 but was 2"); + reporter.ReportTestFinish(failedDetails, 0.1f); + + TestDetails const succeededDetails("SucceededTest", "suite", "", 0); + reporter.ReportTestStart(succeededDetails); + reporter.ReportTestFinish(succeededDetails, 1.0f); + reporter.ReportSummary(2, 1, 1, 1.1f); + + char const* expected = + "" + "" + "" + "" + "" + "" + ""; + + CHECK_EQUAL(expected, output.str().c_str()); + } + + TEST_FIXTURE(XmlTestReporterFixture, MultipleFailures) + { + TestDetails const failedDetails1("FailedTest", "suite", "fail.h", 1); + TestDetails const failedDetails2("FailedTest", "suite", "fail.h", 31); + + reporter.ReportTestStart(failedDetails1); + reporter.ReportFailure(failedDetails1, "expected 1 but was 2"); + reporter.ReportFailure(failedDetails2, "expected one but was two"); + reporter.ReportTestFinish(failedDetails1, 0.1f); + + reporter.ReportSummary(1, 1, 2, 1.1f); + + char const* expected = + "" + "" + "" + "" + "" + "" + ""; + + CHECK_EQUAL(expected, output.str().c_str()); + } }