From 0f944007d8713626d0d00aef2351ba292e82b2cd Mon Sep 17 00:00:00 2001 From: Alexander Tsybulsky Date: Thu, 25 May 2023 13:36:25 +0000 Subject: [PATCH] merge method --- src/zcl_abap_string_map.clas.abap | 18 +++++ src/zcl_abap_string_map.clas.testclasses.abap | 71 +++++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/src/zcl_abap_string_map.clas.abap b/src/zcl_abap_string_map.clas.abap index 3ac7aab..fb7a055 100644 --- a/src/zcl_abap_string_map.clas.abap +++ b/src/zcl_abap_string_map.clas.abap @@ -86,6 +86,11 @@ class zcl_abap_string_map definition !io_string_map type ref to zcl_abap_string_map returning value(ro_instance) type ref to zcl_abap_string_map. + methods merge + importing + !io_string_map type ref to zcl_abap_string_map + returning + value(ro_instance) type ref to zcl_abap_string_map. methods to_struc changing @@ -330,6 +335,19 @@ CLASS ZCL_ABAP_STRING_MAP IMPLEMENTATION. endmethod. + method merge. + + field-symbols like line of mt_entries. + + loop at io_string_map->mt_entries assigning . + set( + iv_key = -k + iv_val = -v ). + endloop. + + endmethod. + + method set. data ls_entry like line of mt_entries. diff --git a/src/zcl_abap_string_map.clas.testclasses.abap b/src/zcl_abap_string_map.clas.testclasses.abap index 83258f0..93f2b40 100644 --- a/src/zcl_abap_string_map.clas.testclasses.abap +++ b/src/zcl_abap_string_map.clas.testclasses.abap @@ -28,6 +28,7 @@ class ltcl_string_map definition methods from_entries for testing. methods from_string for testing. methods from_map for testing. + methods merge for testing. methods to_struc for testing. methods to_string for testing. @@ -766,4 +767,74 @@ class ltcl_string_map implementation. endmethod. + method merge. + + data lo_src type ref to zcl_abap_string_map. + data lo_cut type ref to zcl_abap_string_map. + + lo_cut = zcl_abap_string_map=>create( ). + lo_cut->set( + iv_key = 'a' + iv_val = '1' ). + lo_cut->set( + iv_key = 'b' + iv_val = '2' ). + + lo_src = zcl_abap_string_map=>create( ). + lo_src->set( + iv_key = 'b' + iv_val = '20' ). + lo_src->set( + iv_key = 'c' + iv_val = '30' ). + + lo_cut->merge( lo_src ). + + cl_abap_unit_assert=>assert_equals( + act = lo_cut->size( ) + exp = 3 ). + cl_abap_unit_assert=>assert_equals( + act = lo_cut->get( 'a' ) + exp = '1' ). + cl_abap_unit_assert=>assert_equals( + act = lo_cut->get( 'b' ) + exp = '20' ). + cl_abap_unit_assert=>assert_equals( + act = lo_cut->get( 'c' ) + exp = '30' ). + + " Case 2 + lo_cut = zcl_abap_string_map=>create( iv_case_insensitive = abap_true ). + lo_cut->set( + iv_key = 'a' + iv_val = '1' ). + lo_cut->set( + iv_key = 'b' + iv_val = '2' ). + + lo_src = zcl_abap_string_map=>create( ). + lo_src->set( + iv_key = 'B' + iv_val = '200' ). + lo_src->set( + iv_key = 'D' + iv_val = '400' ). + + lo_cut->merge( lo_src ). + + cl_abap_unit_assert=>assert_equals( + act = lo_cut->size( ) + exp = 3 ). + cl_abap_unit_assert=>assert_equals( + act = lo_cut->get( 'a' ) + exp = '1' ). + cl_abap_unit_assert=>assert_equals( + act = lo_cut->get( 'b' ) + exp = '200' ). + cl_abap_unit_assert=>assert_equals( + act = lo_cut->get( 'd' ) + exp = '400' ). + + endmethod. + endclass.