diff --git a/package.json b/package.json index 6494a221..2298d8db 100644 --- a/package.json +++ b/package.json @@ -5,8 +5,8 @@ "description": "test", "scripts": { "lint": "abaplint", - "unit": "rm -rf output && abap_transpile && echo RUNNING && node output/index.mjs && echo OK", - "integration": "rm -rf output_test && abap_transpile ./abap_transpile_test.json && node output_test/index.mjs", + "unit": "rm -rf output && abap_transpile && echo RUNNING && node --expose-gc output/index.mjs && echo OK", + "integration": "rm -rf output_test && abap_transpile ./abap_transpile_test.json && node --expose-gc output_test/index.mjs", "test": "npm run lint && npm run unit" }, "license": "MIT", diff --git a/src/abap/cl_abap_weak_reference.clas.abap b/src/abap/cl_abap_weak_reference.clas.abap new file mode 100644 index 00000000..4d413db7 --- /dev/null +++ b/src/abap/cl_abap_weak_reference.clas.abap @@ -0,0 +1,24 @@ +CLASS cl_abap_weak_reference DEFINITION PUBLIC. + PUBLIC SECTION. + METHODS constructor + IMPORTING + oref TYPE REF TO object. + + METHODS get + RETURNING + VALUE(oref) TYPE REF TO object. + PRIVATE SECTION. + DATA mv_ref TYPE x LENGTH 1. +ENDCLASS. + +CLASS cl_abap_weak_reference IMPLEMENTATION. + METHOD constructor. + ASSERT oref IS NOT INITIAL. +* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakRef + WRITE '@KERNEL this.mv_ref = new WeakRef(oref);'. + ENDMETHOD. + + METHOD get. + WRITE '@KERNEL oref.set(this.mv_ref.deref());'. + ENDMETHOD. +ENDCLASS. \ No newline at end of file diff --git a/src/abap/cl_abap_weak_reference.clas.testclasses.abap b/src/abap/cl_abap_weak_reference.clas.testclasses.abap new file mode 100644 index 00000000..99bfffc8 --- /dev/null +++ b/src/abap/cl_abap_weak_reference.clas.testclasses.abap @@ -0,0 +1,54 @@ +CLASS ltcl_dummy DEFINITION FOR TESTING. +ENDCLASS. + +CLASS ltcl_dummy IMPLEMENTATION. +ENDCLASS. + +****************************** + +CLASS ltcl_weak_reference DEFINITION FOR TESTING RISK LEVEL HARMLESS DURATION SHORT FINAL. + + PRIVATE SECTION. + METHODS basic FOR TESTING. + METHODS collected FOR TESTING. + + METHODS method RETURNING VALUE(weak) TYPE REF TO cl_abap_weak_reference. + +ENDCLASS. + +CLASS ltcl_weak_reference IMPLEMENTATION. + + METHOD basic. + + DATA ref TYPE REF TO ltcl_dummy. + DATA weak TYPE REF TO cl_abap_weak_reference. + + CREATE OBJECT ref. + CREATE OBJECT weak EXPORTING oref = ref. + + cl_abap_unit_assert=>assert_equals( + act = weak->get( ) + exp = ref ). + + ENDMETHOD. + + METHOD method. + DATA ref TYPE REF TO ltcl_dummy. + CREATE OBJECT ref. + CREATE OBJECT weak EXPORTING oref = ref. + ENDMETHOD. + + METHOD collected. + DATA weak TYPE REF TO cl_abap_weak_reference. + + weak = method( ). + +* https://github.com/orgs/nodejs/discussions/36467 + WRITE '@KERNEL await new Promise(resolve => setTimeout(resolve, 0));;'. +* works only on NodeJS, with --expose-gc + WRITE '@KERNEL global.gc();'. + + cl_abap_unit_assert=>assert_initial( weak->get( ) ). + ENDMETHOD. + +ENDCLASS. \ No newline at end of file diff --git a/src/rtti/cl_abap_typedescr.clas.testclasses.abap b/src/rtti/cl_abap_typedescr.clas.testclasses.abap index 088fe199..0723f547 100644 --- a/src/rtti/cl_abap_typedescr.clas.testclasses.abap +++ b/src/rtti/cl_abap_typedescr.clas.testclasses.abap @@ -67,6 +67,10 @@ CLASS ltcl_test DEFINITION FOR TESTING RISK LEVEL HARMLESS DURATION SHORT FINAL. METHODS structure_absolute FOR TESTING. METHODS tab_length FOR TESTING. + METHODS identical_refs1 FOR TESTING. + METHODS identical_refs2 FOR TESTING. + METHODS identical_refs3 FOR TESTING. + METHODS identical_refs4 FOR TESTING. ENDCLASS. @@ -623,4 +627,54 @@ CLASS ltcl_test IMPLEMENTATION. exp = 8 ). ENDMETHOD. + METHOD identical_refs1. + " DATA t TYPE timestamp. + " DATA ref1 TYPE REF TO cl_abap_typedescr. + " DATA ref2 TYPE REF TO cl_abap_typedescr. + " ref1 = cl_abap_typedescr=>describe_by_data( t ). + " ref2 = cl_abap_typedescr=>describe_by_data( t ). + " cl_abap_unit_assert=>assert_equals( + " act = ref1 + " exp = ref2 ). + ENDMETHOD. + + METHOD identical_refs2. + " TYPES ty TYPE c LENGTH 2. + " DATA t1 TYPE ty. + " DATA t2 TYPE ty. + " DATA ref1 TYPE REF TO cl_abap_typedescr. + " DATA ref2 TYPE REF TO cl_abap_typedescr. + " ref1 = cl_abap_typedescr=>describe_by_data( t1 ). + " ref2 = cl_abap_typedescr=>describe_by_data( t2 ). + " cl_abap_unit_assert=>assert_equals( + " act = ref1 + " exp = ref2 ). + ENDMETHOD. + + METHOD identical_refs3. + " DATA t1 TYPE c LENGTH 2. + " DATA t2 TYPE c LENGTH 2. + " DATA ref1 TYPE REF TO cl_abap_typedescr. + " DATA ref2 TYPE REF TO cl_abap_typedescr. + " ref1 = cl_abap_typedescr=>describe_by_data( t1 ). + " ref2 = cl_abap_typedescr=>describe_by_data( t2 ). + " cl_abap_unit_assert=>assert_equals( + " act = ref1 + " exp = ref2 ). + ENDMETHOD. + + METHOD identical_refs4. + " TYPES ty1 TYPE c LENGTH 2. + " TYPES ty2 TYPE c LENGTH 2. + " DATA t1 TYPE ty1. + " DATA t2 TYPE ty2. + " DATA ref1 TYPE REF TO cl_abap_typedescr. + " DATA ref2 TYPE REF TO cl_abap_typedescr. + " ref1 = cl_abap_typedescr=>describe_by_data( t1 ). + " ref2 = cl_abap_typedescr=>describe_by_data( t2 ). + " IF ref1 = ref2. + " cl_abap_unit_assert=>fail( ). + " ENDIF. + ENDMETHOD. + ENDCLASS. \ No newline at end of file diff --git a/src/unit/cl_abap_unit_assert.clas.abap b/src/unit/cl_abap_unit_assert.clas.abap index 7a336076..8fd38ba7 100644 --- a/src/unit/cl_abap_unit_assert.clas.abap +++ b/src/unit/cl_abap_unit_assert.clas.abap @@ -19,8 +19,8 @@ CLASS cl_abap_unit_assert DEFINITION PUBLIC. CLASS-METHODS assert_differs IMPORTING - act TYPE any - exp TYPE any + act TYPE simple + exp TYPE simple msg TYPE csequence OPTIONAL quit TYPE i OPTIONAL level TYPE i OPTIONAL. diff --git a/src/unit/cl_abap_unit_assert.clas.locals_imp.abap b/src/unit/cl_abap_unit_assert.clas.locals_imp.abap index 57042679..eb068f35 100644 --- a/src/unit/cl_abap_unit_assert.clas.locals_imp.abap +++ b/src/unit/cl_abap_unit_assert.clas.locals_imp.abap @@ -1,5 +1,4 @@ -* Note: dont reuse RTTI here, would like to keep it -* possible to run unit tests without RTTI if possible +* Note: dont reuse RTTI here, would like to keep it possible to run unit tests without RTTI CLASS lcl_dump DEFINITION. PUBLIC SECTION. CLASS-METHODS to_string @@ -14,6 +13,7 @@ ENDCLASS. CLASS lcl_dump IMPLEMENTATION. METHOD to_string. DATA lv_type TYPE c LENGTH 1. + DATA lv_name TYPE string. DESCRIBE FIELD iv_val TYPE lv_type. CASE lv_type. @@ -22,6 +22,9 @@ CLASS lcl_dump IMPLEMENTATION. rv_str = dump_structure( iv_val ). WHEN 'h'. rv_str = |[itab]|. + WHEN 'r'. + WRITE '@KERNEL lv_name.set(iv_val.get().constructor.name);'. + rv_str = |[object, { lv_name }]|. WHEN OTHERS. rv_str = |{ iv_val }|. ENDCASE. diff --git a/src/unit/osql/cl_osql_test_environment.clas.abap b/src/unit/osql/cl_osql_test_environment.clas.abap index 5642492c..95fb576c 100644 --- a/src/unit/osql/cl_osql_test_environment.clas.abap +++ b/src/unit/osql/cl_osql_test_environment.clas.abap @@ -15,6 +15,7 @@ CLASS cl_osql_test_environment DEFINITION PUBLIC. DATA mo_sql TYPE REF TO cl_sql_statement. METHODS initialize. + METHODS set_runtime_prefix. ENDCLASS. CLASS cl_osql_test_environment IMPLEMENTATION. @@ -39,7 +40,6 @@ CLASS cl_osql_test_environment IMPLEMENTATION. DATA lv_sql TYPE string. DATA lo_result TYPE REF TO cl_sql_result_set. DATA lr_ref TYPE REF TO data. - DATA lv_extra TYPE string. WRITE '@KERNEL if (abap.dbo.schemaPrefix !== "") throw new Error("already prefixed");'. @@ -63,6 +63,13 @@ CLASS cl_osql_test_environment IMPLEMENTATION. mo_sql->execute_update( lv_sql ). ENDLOOP. + set_runtime_prefix( ). + + ENDMETHOD. + + METHOD set_runtime_prefix. + + DATA lv_extra TYPE string. lv_extra = |'.'|. WRITE '@KERNEL abap.dbo.schemaPrefix = this.mv_schema.get() + lv_extra.get();'. @@ -91,6 +98,8 @@ CLASS cl_osql_test_environment IMPLEMENTATION. DATA lo_struct_descr TYPE REF TO cl_abap_structdescr. DATA lv_table TYPE string. + set_runtime_prefix( ). + lo_table_descr ?= cl_abap_typedescr=>describe_by_data( i_data ). lo_struct_descr ?= lo_table_descr->get_table_line_type( ). lv_table = lo_struct_descr->get_relative_name( ).