-
Notifications
You must be signed in to change notification settings - Fork 12
/
zcl_max_line_length_pp.clas.testclasses.abap
175 lines (145 loc) · 7.29 KB
/
zcl_max_line_length_pp.clas.testclasses.abap
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
*"* use this source file for your ABAP unit test classes
CLASS ltcl_pretty_printer_should DEFINITION DEFERRED.
CLASS zcl_max_line_length_pp DEFINITION LOCAL FRIENDS ltcl_pretty_printer_should.
CLASS ltcl_pretty_printer_should DEFINITION FINAL FOR TESTING
DURATION SHORT
RISK LEVEL HARMLESS.
PRIVATE SECTION.
DATA cut TYPE REF TO zcl_max_line_length_pp.
METHODS:
setup,
"! test escaped apostrophes in the beginning, middle and end of string.
escape_apostrophes FOR TESTING RAISING cx_static_check,
indent_and_add_line_breaks FOR TESTING RAISING cx_static_check,
"! ABAP Editor does not support more than 255 lines.
"! Issue: https://github.com/objective-partner/abap_debugger_data_view_extension/issues/9
break_line_if_length_less_20 FOR TESTING RAISING cx_static_check,
max_line_length_less_128 FOR TESTING RAISING cx_static_check,
"!calls to pretty printer should be idempotent
clear_last_result FOR TESTING RAISING cx_static_check.
ENDCLASS.
CLASS ltcl_pretty_printer_should IMPLEMENTATION.
METHOD setup.
"as we are here in a private unit test class so we don't need to use the factory or injector for instantiating
cut = NEW #( indent_size = 2 max_line_length = 128 ).
ENDMETHOD.
METHOD escape_apostrophes.
"GIVEN
cut = NEW #( indent_size = 2 max_line_length = 128 ).
DATA(input) = |D = VALUE #( COL_1 = 'It''s easy (isn''t it?)' COL_2 = 'keep on doin''' COL_3 = '''ne Weile' COL_4 = '''' )|.
DATA(expected) = |D = VALUE #(\r COL_1 = 'It''s easy (isn''t it?)'\r COL_2 = 'keep on doin'''\r COL_3 = '''ne Weile'\r COL_4 = ''''\r)|.
"WHEN
DATA(formatted_string) = cut->format( input ).
"THEN
cl_abap_unit_assert=>assert_equals(
EXPORTING
act = formatted_string
exp = expected
msg = |Apostrophes or brackets were not handled correctly.| ).
ENDMETHOD.
METHOD indent_and_add_line_breaks.
"GIVEN
cut = NEW #( indent_size = 2 max_line_length = 128 ).
DATA(input) = |D = VALUE #( COL1 = '1' COL2 = VALUE #( COL1 = '1' COL2 = '2' ) COL3 = '3' COL4 = VALUE #( COL1 = '1' COL2 = '2' ) )|.
DATA(expected) = |D = VALUE #(\r COL1 = '1'\r COL2 = VALUE #(\r COL1 = '1'\r COL2 = '2'\r )\r COL3 = '3'\r COL4 = VALUE #(\r COL1 = '1'\r COL2 = '2'\r )\r)|.
"D = VALUE #(
" COL1 = '1'
" COL2 = VALUE #(
" COL1 = '1'
" COL2 = '2'
" )
" COL3 = '3'
" COL4 = VALUE #(
" COL1 = '1'
" COL2 = '2'
" )
").
"WHEN
DATA(formatted_string) = cut->format( input ).
"THEN
cl_abap_unit_assert=>assert_equals(
EXPORTING
act = formatted_string
exp = expected
msg = |Indent or line breaks were not added correctly| ).
ENDMETHOD.
METHOD break_line_if_length_less_20.
"GIVEN
cut = NEW #( indent_size = 2 max_line_length = 20 ).
DATA(input) = |D = VALUE #( C1 = 'A123456789B123456789C123456789D123456789E123456789' )|.
DATA(expected) = |D = VALUE #(\r C1 = 'A123456789' &&\r 'B123456789C1234' &&\r '56789D123456789' &&\r 'E123456789'\r)|.
"WHEN
DATA(formatted_string) = cut->format( input ).
"THEN
cl_abap_unit_assert=>assert_equals(
EXPORTING
act = formatted_string
exp = expected
msg = |Values exceeding maximum line length are not handled correctly.| ).
ENDMETHOD.
METHOD max_line_length_less_128.
DATA lines_actual TYPE TABLE OF string.
DATA lines_expected TYPE TABLE OF string.
"GIVEN
cut = NEW #( indent_size = 2 max_line_length = 128 ).
DATA(input) = |EDIDD = VALUE #( ( SEGNAM = 'E1EDK01' SDATA = 'EUREUR1.00000 0021 DE12345678901 DE12345678901 INVO1234561234| &&
| 2.000 2.000 ABCDE 1112223344 | &&
| ABCDEDFGHIJKLMNOPQRSTUVWXYZ AND STILL SOME MORE CHARACTERS - THERE WILL BE NO END | &&
| - MAYBE THERE IS AN END IN SIGHT' ) ).|.
DATA(expected) = |EDIDD = VALUE #(\r| &&
| (\r SEGNAM = 'E1EDK01'\r| &&
| SDATA = 'EUREUR1.00000 0021 DE12345678901 DE12345678901 INVO1234561234 ' &&\r| &&
| ' 2.000 2.000 ABCDE 1112223344 ' &&\r| &&
| 'ABCDEDFGHIJKLMNOPQRSTUVWXYZ AND STILL SOME MORE CHARACTERS - THERE WILL BE NO END ' &&\r| &&
| ' - MAYBE THERE IS AN END IN SIGHT'\r| &&
| )\r)|.
lines_expected = VALUE #(
( |EDIDD = VALUE #(| )
( | (| )
( | SEGNAM = 'E1EDK01'| )
( | SDATA = 'EUREUR1.00000 0021 DE12345678901 DE12345678901 INVO1234561234 ' &&| )
( | ' 2.000 2.000 ABCDE 1112223344 ' &&| )
( | 'ABCDEDFGHIJKLMNOPQRSTUVWXYZ AND STILL SOME MORE CHARACTERS - THERE WILL BE NO END ' &&| )
( | ' - MAYBE THERE IS AN END IN SIGHT'| )
( | )| )
( |)| )
).
"EDIDD = VALUE #(
" (
" SEGNAM = 'E1EDK01'
" SDATA = 'EUREUR1.00000 0021 DE12345678901 DE12345678901 INVO1234561234 ' &&
" ' 2.000 2.000 ABCDE 1112223344 ' &&
" 'ABCDEDFGHIJKLMNOPQRSTUVWXYZ AND STILL SOME MORE CHARACTERS - THERE WILL BE NO END ' &&
" ' - MAYBE THERE IS AN END IN SIGHT'
" )
")
"WHEN
DATA(formated_string) = cut->format( input ).
SPLIT formated_string AT zcl_max_line_length_pp=>c_newline INTO TABLE lines_actual.
DATA(max_length) = 0.
LOOP AT lines_actual INTO DATA(line).
DATA(length) = strlen( line ).
max_length = COND i( WHEN length >= max_length THEN length ELSE max_length ).
ENDLOOP.
"THEN
cl_abap_unit_assert=>assert_number_between( lower = 2 upper = 128 number = max_length ).
cl_abap_unit_assert=>assert_equals( act = lines_actual exp = lines_expected ).
cl_abap_unit_assert=>assert_equals( act = formated_string exp = expected ).
ENDMETHOD.
METHOD clear_last_result.
"GIVEN
cut = NEW #( indent_size = 2 max_line_length = 20 ).
DATA(input) = |D = VALUE #( C1 = 'A123456789B123456789C123456789D123456789E123456789' )|.
DATA(expected) = |D = VALUE #(\r C1 = 'A123456789' &&\r 'B123456789C1234' &&\r '56789D123456789' &&\r 'E123456789'\r)|.
"WHEN
DATA(formatted_string) = cut->format( input ).
"we need to call it twice here, to see if output is still the right one
formatted_string = cut->format( input ).
"THEN
cl_abap_unit_assert=>assert_equals(
EXPORTING
act = formatted_string
exp = expected
msg = |Values exceeding maximum line length are not handled correctly.| ).
ENDMETHOD.
ENDCLASS.