2 weeks ago
The ADT debugger feature "Copy as ABAP Value statement" can be used to get the ABAP code for initializing variables in ABAP Unit input or expectation. It's available since ADT 3.24 and ABAP 7.57 FPS 00 as said here.
How can it be done in other versions?
Thank you.
Sandra
Request clarification before answering.
Possibilities:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
One simple solution is to write the below class ZCL_ABAP_DATA and invoke it from your code which fills the concerned variable, to obtain VALUE #( ... ) as a string. Then copy the string via the debugger to the clipboard and paste it to your ABAP Unit class.
Usage (add this line and display temp via the debugger):
DATA(temp) = zcl_abap_data=>get_code_from_value( commitment_items ).
Example of temp value:
VALUE #( ( FIKRS = 'B301'
GJAHR = '2023'
FIPEX = '040281311'
FPART = 'H'
FIPUP = '04028131R'
)
( FIKRS = 'ZZ01'
GJAHR = '2023'
FIPEX = '040281532'
FPART = 'X'
FIPUP = '04028153'
) )
Class:
CLASS zcl_abap_data DEFINITION
PUBLIC FINAL
CREATE PRIVATE.
PUBLIC SECTION.
CLASS-METHODS get_code_from_value
IMPORTING !data TYPE any
RETURNING VALUE(result) TYPE string.
PRIVATE SECTION.
METHODS get_data
IMPORTING !data TYPE any
ref_to_parent_data TYPE REF TO data OPTIONAL
RETURNING VALUE(result) TYPE string.
METHODS get_elementary
IMPORTING !data TYPE any
RETURNING VALUE(result) TYPE string.
METHODS get_structure
IMPORTING !data TYPE any
ref_to_parent_data TYPE REF TO data OPTIONAL
RETURNING VALUE(result) TYPE string.
METHODS get_reference
IMPORTING !data TYPE any
RETURNING VALUE(result) TYPE string.
METHODS get_table
IMPORTING !data TYPE ANY TABLE
RETURNING VALUE(result) TYPE string.
ENDCLASS.
CLASS zcl_abap_data IMPLEMENTATION.
METHOD get_code_from_value.
DATA(code_generator) = NEW zcl_abap_data( ).
result = code_generator->get_data( data ).
ENDMETHOD.
METHOD get_data.
CASE cl_abap_typedescr=>describe_by_data( data )->kind.
WHEN cl_abap_typedescr=>kind_elem.
result = get_elementary( data ).
WHEN cl_abap_typedescr=>kind_ref.
result = get_reference( data ).
WHEN cl_abap_typedescr=>kind_struct.
result = get_structure( data = data
ref_to_parent_data = ref_to_parent_data ).
WHEN cl_abap_typedescr=>kind_table.
result = get_table( data ).
ENDCASE.
ENDMETHOD.
METHOD get_elementary.
CASE cl_abap_typedescr=>describe_by_data( data )->type_kind.
WHEN cl_abap_typedescr=>typekind_char.
result = |'{ replace( val = |{ data }|
sub = |'|
with = |''|
occ = 0 ) }'|.
WHEN cl_abap_typedescr=>typekind_string.
result = |`{ replace( val = |{ data }|
sub = |`|
with = |``|
occ = 0 ) }`|.
WHEN cl_abap_typedescr=>typekind_date
OR cl_abap_typedescr=>typekind_hex
OR cl_abap_typedescr=>typekind_num
OR cl_abap_typedescr=>typekind_time
OR cl_abap_typedescr=>typekind_xstring.
result = |'{ data }'|.
WHEN cl_abap_typedescr=>typekind_int
OR cl_abap_typedescr=>typekind_int1
OR cl_abap_typedescr=>typekind_int2.
result = |{ data }|.
WHEN OTHERS.
IF frac( data ) = 0.
result = |{ CONV decfloat34( data ) DECIMALS = 0 }|.
ELSE.
result = |'{ data }'|.
ENDIF.
ENDCASE.
ENDMETHOD.
METHOD get_reference.
ENDMETHOD.
METHOD get_structure.
DATA(structdescr) = CAST cl_abap_structdescr( cl_abap_typedescr=>describe_by_data( data ) ).
DATA(is_table_line) = xsdbool( ref_to_parent_data IS BOUND
AND cl_abap_typedescr=>describe_by_data_ref( ref_to_parent_data )->kind = cl_abap_typedescr=>kind_table ).
IF is_table_line = abap_false.
result = ` VALUE #(`.
ENDIF.
DATA(component_number) = 0.
LOOP AT structdescr->components REFERENCE INTO DATA(component_type).
component_number = component_number + 1.
ASSIGN COMPONENT component_type->name OF STRUCTURE data TO FIELD-SYMBOL(<component>).
IF <component> IS NOT INITIAL.
result = result && | { component_type->name } = { get_data( data = <component> )
}{ COND #( WHEN component_number < lines( structdescr->components )
THEN |\n| ) }|.
ENDIF.
ENDLOOP.
IF is_table_line = abap_false.
result = result && ` )`.
ENDIF.
ENDMETHOD.
METHOD get_table.
FIELD-SYMBOLS <table> TYPE ANY TABLE.
result = ` VALUE #(`.
ASSIGN data TO <table>.
DATA(line_number) = 0.
LOOP AT <table> ASSIGNING FIELD-SYMBOL(<line>).
line_number = line_number + 1.
result = result && | ({ get_data( data = <line>
ref_to_parent_data = REF #( data ) )
} ){ COND #( WHEN line_number < lines( <table> )
THEN |\n| ) }|.
ENDLOOP.
result = result && ` )`.
ENDMETHOD.
ENDCLASS.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
73 | |
18 | |
10 | |
7 | |
7 | |
4 | |
4 | |
4 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.