-
-
Save ilyakaznacheev/f71dcf483e169307401b5607f700d7e5 to your computer and use it in GitHub Desktop.
Taxi calculator class test example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CLASS ltc_taxi_calc DEFINITION FOR TESTING | |
DURATION SHORT | |
RISK LEVEL HARMLESS | |
FINAL. | |
PRIVATE SECTION. | |
TYPES: | |
BEGIN OF ts_ride_points, | |
a TYPE geo_point, | |
b TYPE geo_point, | |
END OF ts_ride_points, | |
tt_rides TYPE TABLE OF ts_ride_points WITH EMPTY KEY. | |
METHODS check_short_ride FOR TESTING RAISING cx_static_check. | |
METHODS check_long_ride FOR TESTING RAISING cx_static_check. | |
METHODS check_wrong_ride FOR TESTING RAISING cx_static_check. | |
ENDCLASS. | |
CLASS ltc_taxi_calc IMPLEMENTATION. | |
METHOD check_long_ride. | |
DATA(lt_rides) = VALUE tt_rides( | |
( a = VALUE #( x = 1 y = 1 ) b = VALUE #( x = 11 y = 11 ) ) | |
( a = VALUE #( x = 2 y = 2 ) b = VALUE #( x = 12 y = 12 ) ) | |
). | |
DATA(lv_min_price) = CONV paymnt( 5 ). | |
DATA(lo_geo_double) = CAST zif_test_taxi_geo_service( | |
cl_abap_testdouble=>create( 'zif_test_taxi_geo_service' ) | |
). | |
cl_abap_testdouble=>configure_call( | |
lo_geo_double | |
)->returning( 1 ). | |
lo_geo_double->get_path_length( | |
iv_from = lt_rides[ 1 ]-a | |
iv_to = lt_rides[ 1 ]-b | |
). | |
cl_abap_testdouble=>configure_call( | |
lo_geo_double | |
)->returning( 2 ). | |
lo_geo_double->get_path_length( | |
iv_from = lt_rides[ 2 ]-a | |
iv_to = lt_rides[ 2 ]-b | |
). | |
cl_abap_testdouble=>configure_call( | |
lo_geo_double | |
)->ignore_all_parameters( | |
)->returning( abap_true ). | |
lo_geo_double->is_path_available( | |
iv_from = lt_rides[ 1 ]-a | |
iv_to = lt_rides[ 1 ]-b | |
). | |
DATA(lo_cut) = NEW zcl_test_taxi_ride_calculator( | |
iv_minimun_price = lv_min_price | |
iv_meter_cost = '1.5' | |
io_geo_service = lo_geo_double | |
). | |
DATA(lv_act1) = lo_cut->get_ride_price( | |
iv_from = lt_rides[ 1 ]-a | |
iv_to = lt_rides[ 1 ]-b | |
). | |
DATA(lv_act2) = lo_cut->get_ride_price( | |
iv_from = lt_rides[ 2 ]-a | |
iv_to = lt_rides[ 2 ]-b | |
). | |
cl_abap_unit_assert=>assert_equals( | |
act = lv_act1 | |
exp = lv_min_price | |
msg = |Wrong ride price { lv_act1 } but expected { lv_min_price }| | |
). | |
cl_abap_unit_assert=>assert_equals( | |
act = lv_act2 | |
exp = lv_min_price | |
msg = |Wrong ride price { lv_act1 } but expected { lv_min_price }| | |
). | |
ENDMETHOD. | |
METHOD check_short_ride. | |
DATA(lt_rides) = VALUE tt_rides( | |
( a = VALUE #( x = 1 y = 1 ) b = VALUE #( x = 111 y = 111 ) ) | |
( a = VALUE #( x = 2 y = 2 ) b = VALUE #( x = 122 y = 122 ) ) | |
). | |
DATA(lv_min_price) = CONV paymnt( 5 ). | |
DATA(lo_geo_double) = CAST zif_test_taxi_geo_service( | |
cl_abap_testdouble=>create( 'zif_test_taxi_geo_service' ) | |
). | |
cl_abap_testdouble=>configure_call( | |
lo_geo_double | |
)->returning( 10 ). | |
lo_geo_double->get_path_length( | |
iv_from = lt_rides[ 1 ]-a | |
iv_to = lt_rides[ 1 ]-b | |
). | |
cl_abap_testdouble=>configure_call( | |
lo_geo_double | |
)->returning( 20 ). | |
lo_geo_double->get_path_length( | |
iv_from = lt_rides[ 2 ]-a | |
iv_to = lt_rides[ 2 ]-b | |
). | |
cl_abap_testdouble=>configure_call( | |
lo_geo_double | |
)->ignore_all_parameters( | |
)->returning( abap_true ). | |
lo_geo_double->is_path_available( | |
iv_from = lt_rides[ 1 ]-a | |
iv_to = lt_rides[ 1 ]-b | |
). | |
DATA(lo_cut) = NEW zcl_test_taxi_ride_calculator( | |
iv_minimun_price = lv_min_price | |
iv_meter_cost = '1.5' | |
io_geo_service = lo_geo_double | |
). | |
DATA(lv_act1) = lo_cut->get_ride_price( | |
iv_from = lt_rides[ 1 ]-a | |
iv_to = lt_rides[ 1 ]-b | |
). | |
DATA(lv_act2) = lo_cut->get_ride_price( | |
iv_from = lt_rides[ 2 ]-a | |
iv_to = lt_rides[ 2 ]-b | |
). | |
cl_abap_unit_assert=>assert_equals( | |
act = lv_act1 | |
exp = 15 | |
msg = |Wrong ride price { lv_act1 } but expected { lv_min_price }| | |
). | |
cl_abap_unit_assert=>assert_equals( | |
act = lv_act2 | |
exp = 30 | |
msg = |Wrong ride price { lv_act1 } but expected { lv_min_price }| | |
). | |
ENDMETHOD. | |
METHOD check_wrong_ride. | |
DATA: | |
lv_error_ok TYPE abap_bool. | |
DATA(lt_rides) = VALUE tt_rides( | |
( a = VALUE #( x = 1 y = 1 ) b = VALUE #( x = 111 y = 111 ) ) | |
). | |
DATA(lo_geo_double) = CAST zif_test_taxi_geo_service( | |
cl_abap_testdouble=>create( 'zif_test_taxi_geo_service' ) | |
). | |
cl_abap_testdouble=>configure_call( | |
lo_geo_double | |
)->returning( 10 ). | |
lo_geo_double->get_path_length( | |
iv_from = lt_rides[ 1 ]-a | |
iv_to = lt_rides[ 1 ]-b | |
). | |
cl_abap_testdouble=>configure_call( | |
lo_geo_double | |
)->ignore_all_parameters( | |
)->returning( abap_false ). | |
lo_geo_double->is_path_available( | |
iv_from = lt_rides[ 1 ]-a | |
iv_to = lt_rides[ 1 ]-b | |
). | |
DATA(lo_cut) = NEW zcl_test_taxi_ride_calculator( | |
iv_minimun_price = '5' | |
iv_meter_cost = '1.5' | |
io_geo_service = lo_geo_double | |
). | |
TRY. | |
lo_cut->get_ride_price( | |
iv_from = lt_rides[ 1 ]-a | |
iv_to = lt_rides[ 1 ]-b | |
). | |
CATCH zcx_taxi_ride_not_possible . | |
lv_error_ok = abap_true. | |
ENDTRY. | |
cl_abap_unit_assert=>assert_true( | |
act = lv_error_ok | |
msg = |Error at wrong path was not raised| | |
). | |
ENDMETHOD. | |
ENDCLASS. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CLASS zcl_test_taxi_ride_calculator DEFINITION | |
PUBLIC | |
FINAL | |
CREATE PUBLIC . | |
PUBLIC SECTION. | |
METHODS constructor | |
IMPORTING | |
iv_minimun_price TYPE paymnt | |
iv_meter_cost TYPE paymnt | |
io_geo_service TYPE REF TO zif_test_taxi_geo_service. | |
METHODS get_ride_price | |
IMPORTING | |
iv_from TYPE geo_point | |
iv_to TYPE geo_point | |
RETURNING | |
VALUE(rv_ride_cost) TYPE paymnt | |
RAISING | |
zcx_taxi_ride_not_possible. | |
PRIVATE SECTION. | |
DATA: | |
gv_minimun_price TYPE paymnt, | |
gv_meter_cost TYPE paymnt, | |
go_geo_service TYPE REF TO zif_test_taxi_geo_service. | |
ENDCLASS. | |
CLASS zcl_test_taxi_ride_calculator IMPLEMENTATION. | |
METHOD constructor. | |
gv_minimun_price = iv_minimun_price. | |
gv_meter_cost = iv_meter_cost. | |
go_geo_service = io_geo_service. | |
ENDMETHOD. | |
METHOD get_ride_price. | |
IF NOT go_geo_service->is_path_available( | |
iv_from = iv_from | |
iv_to = iv_to | |
). | |
RAISE EXCEPTION TYPE zcx_taxi_ride_not_possible. | |
ENDIF. | |
DATA(lv_ride_cost) = gv_meter_cost * go_geo_service->get_path_length( | |
iv_from = iv_from | |
iv_to = iv_to | |
). | |
IF lv_ride_cost < gv_minimun_price. | |
rv_ride_cost = gv_minimun_price. | |
ELSE. | |
rv_ride_cost = lv_ride_cost. | |
ENDIF. | |
ENDMETHOD. | |
ENDCLASS. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
INTERFACE zif_test_taxi_geo_service | |
PUBLIC . | |
METHODS get_path_length | |
IMPORTING | |
iv_from TYPE geo_point | |
iv_to TYPE geo_point | |
RETURNING | |
VALUE(rv_distance) TYPE int4. | |
METHODS is_path_available | |
IMPORTING | |
iv_from TYPE geo_point | |
iv_to TYPE geo_point | |
RETURNING | |
VALUE(rv_available) TYPE abap_bool. | |
ENDINTERFACE. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment