From 4f012b0950cfab7c4110767da17ca3f0e27680d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=BCnter?= Date: Tue, 10 Oct 2017 09:37:31 +0000 Subject: [PATCH] Fix Issue 980 and add test code --- src/zabapgit_object_oo_functions.prog.abap | 72 ++++---- src/zabapgit_unit_test.prog.abap | 191 ++++++++++++++++++++- 2 files changed, 232 insertions(+), 31 deletions(-) diff --git a/src/zabapgit_object_oo_functions.prog.abap b/src/zabapgit_object_oo_functions.prog.abap index 33acf7c06..b64207dd3 100644 --- a/src/zabapgit_object_oo_functions.prog.abap +++ b/src/zabapgit_object_oo_functions.prog.abap @@ -169,6 +169,11 @@ CLASS lcl_oo_serializer DEFINITION. IMPORTING is_clskey TYPE seoclskey RETURNING VALUE(rt_source) TYPE zif_abapgit_definitions=>ty_string_tt RAISING zcx_abapgit_exception. + METHODS calculate_skip_testclass + IMPORTING + it_source TYPE zif_abapgit_definitions=>ty_string_tt + RETURNING + VALUE(rv_skip_testclass) TYPE abap_bool. PRIVATE SECTION. DATA mv_skip_testclass TYPE abap_bool. METHODS serialize_abap_old @@ -341,39 +346,10 @@ CLASS lcl_oo_serializer IMPLEMENTATION. METHOD serialize_testclasses. - DATA: lv_line1 LIKE LINE OF rt_source, - lv_line2 LIKE LINE OF rt_source. - - rt_source = read_include( is_clskey = is_clskey iv_type = seop_ext_class_testclasses ). -* when creating classes in Eclipse it automatically generates the -* testclass include, but it is not needed, so skip to avoid -* creating an extra file in the repository. -* Also remove it if the content is manually removed, but -* the class still thinks it contains tests - "@TODO: Put under test - mv_skip_testclass = abap_false. - IF lines( rt_source ) = 2. - READ TABLE rt_source INDEX 1 INTO lv_line1. - ASSERT sy-subrc = 0. - READ TABLE rt_source INDEX 2 INTO lv_line2. - ASSERT sy-subrc = 0. - IF lv_line1(3) = '*"*' AND lv_line2 IS INITIAL. - mv_skip_testclass = abap_true. - ENDIF. - ELSEIF lines( rt_source ) = 1. - READ TABLE rt_source INDEX 1 INTO lv_line1. - ASSERT sy-subrc = 0. - IF lv_line1 IS INITIAL - OR ( strlen( lv_line1 ) >= 3 AND lv_line1(3) = '*"*' ) - OR ( strlen( lv_line1 ) = 1 AND lv_line1(1) = '*' ). - mv_skip_testclass = abap_true. - ENDIF. - ELSEIF lines( rt_source ) = 0. - mv_skip_testclass = abap_true. - ENDIF. + mv_skip_testclass = calculate_skip_testclass( rt_source ). ENDMETHOD. "serialize_test @@ -385,10 +361,46 @@ CLASS lcl_oo_serializer IMPLEMENTATION. reduce( CHANGING ct_source = rt_source ). ENDMETHOD. "serialize_macro + METHOD are_test_classes_skipped. rv_return = mv_skip_testclass. ENDMETHOD. + + METHOD calculate_skip_testclass. + + DATA: lv_line1 LIKE LINE OF it_source, + lv_line2 LIKE LINE OF it_source. + +* when creating classes in Eclipse it automatically generates the +* testclass include, but it is not needed, so skip to avoid +* creating an extra file in the repository. +* Also remove it if the content is manually removed, but +* the class still thinks it contains tests + + rv_skip_testclass = abap_false. + IF lines( it_source ) = 2. + READ TABLE it_source INDEX 1 INTO lv_line1. + ASSERT sy-subrc = 0. + READ TABLE it_source INDEX 2 INTO lv_line2. + ASSERT sy-subrc = 0. + IF strlen( lv_line1 ) >= 3 AND lv_line1(3) = '*"*' AND lv_line2 IS INITIAL. + rv_skip_testclass = abap_true. + ENDIF. + ELSEIF lines( it_source ) = 1. + READ TABLE it_source INDEX 1 INTO lv_line1. + ASSERT sy-subrc = 0. + IF lv_line1 IS INITIAL + OR ( strlen( lv_line1 ) >= 3 AND lv_line1(3) = '*"*' ) + OR ( strlen( lv_line1 ) = 1 AND lv_line1(1) = '*' ). + rv_skip_testclass = abap_true. + ENDIF. + ELSEIF lines( it_source ) = 0. + rv_skip_testclass = abap_true. + ENDIF. + + ENDMETHOD. + ENDCLASS. CLASS lcl_oo_base DEFINITION ABSTRACT. diff --git a/src/zabapgit_unit_test.prog.abap b/src/zabapgit_unit_test.prog.abap index 475784212..9e43a74cb 100644 --- a/src/zabapgit_unit_test.prog.abap +++ b/src/zabapgit_unit_test.prog.abap @@ -38,6 +38,11 @@ DEFINE _append_result. -filename = &8. END-OF-DEFINITION. +DEFINE _given_source_is. + APPEND INITIAL LINE TO mt_source ASSIGNING . + = &1. +END-OF-DEFINITION. + * todo, should the tests be in the same include as the classes * they are testing? @@ -816,7 +821,7 @@ CLASS ltcl_url IMPLEMENTATION. TRY. lcl_url=>host( 'not a real url' ). "#EC NOTEXT cl_abap_unit_assert=>fail( ). - CATCH zcx_abapgit_exception. "#EC NO_HANDLER + CATCH zcx_abapgit_exception. "#EC NO_HANDLER ENDTRY. ENDMETHOD. "repo_error @@ -2601,5 +2606,189 @@ CLASS ltcl_persistence_settings IMPLEMENTATION. ENDCLASS. +CLASS ltcl_oo_serialize DEFINITION FINAL FOR TESTING + DURATION SHORT + RISK LEVEL HARMLESS. + + PRIVATE SECTION. + METHODS: + setup, + empty_include FOR TESTING RAISING cx_static_check, + one_line_include FOR TESTING RAISING cx_static_check, + one_line_include_2 FOR TESTING RAISING cx_static_check, + one_line_include_3 FOR TESTING RAISING cx_static_check, + two_line_include FOR TESTING RAISING cx_static_check, + two_line_include_2 FOR TESTING RAISING cx_static_check, + two_line_include_3 FOR TESTING RAISING cx_static_check, + more_than_two_lines FOR TESTING RAISING cx_static_check, + + _given_empty_test_include, + _when_skip_is_calculated, + _then_should_be_skipped, + _then_should_not_be_skipped. + + DATA: mo_oo_serializer TYPE REF TO lcl_oo_serializer, + mt_source TYPE zif_abapgit_definitions=>ty_string_tt, + mv_skip_testclass TYPE abap_bool. + +ENDCLASS. + + +CLASS ltcl_oo_serialize IMPLEMENTATION. + + METHOD setup. + + CREATE OBJECT mo_oo_serializer. + + ENDMETHOD. + + METHOD empty_include. + + _given_empty_test_include( ). + + _when_skip_is_calculated( ). + + _then_should_be_skipped( ). + + ENDMETHOD. + + METHOD one_line_include. + + FIELD-SYMBOLS: LIKE LINE OF mt_source. + + _given_source_is: + `*"* use this source file for your ABAP unit test classes`. + + _when_skip_is_calculated( ). + + _then_should_be_skipped( ). + + ENDMETHOD. + + METHOD one_line_include_2. + + FIELD-SYMBOLS: LIKE LINE OF mt_source. + + _given_source_is: + `*`. + + _when_skip_is_calculated( ). + + _then_should_be_skipped( ). + + ENDMETHOD. + + METHOD one_line_include_3. + + FIELD-SYMBOLS: LIKE LINE OF mt_source. + + _given_source_is: + `write: 'This is ABAP'.`. + + _when_skip_is_calculated( ). + + _then_should_not_be_skipped( ). + + ENDMETHOD. + + METHOD two_line_include. + + FIELD-SYMBOLS: LIKE LINE OF mt_source. + + _given_source_is: + `*"* use this source file for your ABAP unit test classes`, + ``. + + _when_skip_is_calculated( ). + + _then_should_be_skipped( ). + + ENDMETHOD. + + METHOD two_line_include_2. + + FIELD-SYMBOLS: LIKE LINE OF mt_source. + + _given_source_is: + `*"* use this source file for your ABAP unit test classes`, + `write: 'This is ABAP'.`. + + _when_skip_is_calculated( ). + + _then_should_not_be_skipped( ). + + ENDMETHOD. + + METHOD two_line_include_3. + + FIELD-SYMBOLS: LIKE LINE OF mt_source. + + _given_source_is: + ` `, + `*"* use this source file for your ABAP unit test classes`. + + _when_skip_is_calculated( ). + + _then_should_not_be_skipped( ). + + ENDMETHOD. + + METHOD more_than_two_lines. + + FIELD-SYMBOLS: LIKE LINE OF mt_source. + + _given_source_is: + '*"* use this source file for your ABAP unit test classes', + `CLASS ltcl_test DEFINITION FINAL FOR TESTING`, + ` DURATION SHORT`, + ` RISK LEVEL HARMLESS.`, + + ` PRIVATE SECTION.`, + ` METHODS:`, + ` first_test FOR TESTING RAISING cx_static_check.`, + `ENDCLASS.`, + ` `, + `CLASS ltcl_test IMPLEMENTATION.`, + + ` METHOD first_test.`, + ` cl_abap_unit_assert=>fail( 'This is a real test' ).`, + ` ENDMETHOD.`, + + `ENDCLASS.`. + + _when_skip_is_calculated( ). + + _then_should_not_be_skipped( ). + + ENDMETHOD. + + + METHOD _given_empty_test_include. + + ENDMETHOD. + + METHOD _when_skip_is_calculated. + + mv_skip_testclass = mo_oo_serializer->calculate_skip_testclass( mt_source ). + + ENDMETHOD. + + METHOD _then_should_be_skipped. + + cl_abap_unit_assert=>assert_true( act = mv_skip_testclass + msg = |Testclass should be skipped| ). + + ENDMETHOD. + + + METHOD _then_should_not_be_skipped. + + cl_abap_unit_assert=>assert_false( act = mv_skip_testclass + msg = |Testclass should not be skipped| ). + + ENDMETHOD. + +ENDCLASS. + INCLUDE zabapgit_unit_test_clas_intf. INCLUDE zabapgit_unit_test_transport.