Skip to content

Commit

Permalink
merge method
Browse files Browse the repository at this point in the history
  • Loading branch information
sbcgua committed May 25, 2023
1 parent 7b98809 commit 0f94400
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/zcl_abap_string_map.clas.abap
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -330,6 +335,19 @@ CLASS ZCL_ABAP_STRING_MAP IMPLEMENTATION.
endmethod.


method merge.

field-symbols <entry> like line of mt_entries.

loop at io_string_map->mt_entries assigning <entry>.
set(
iv_key = <entry>-k
iv_val = <entry>-v ).
endloop.

endmethod.


method set.

data ls_entry like line of mt_entries.
Expand Down
71 changes: 71 additions & 0 deletions src/zcl_abap_string_map.clas.testclasses.abap
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.

0 comments on commit 0f94400

Please sign in to comment.