Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9847364

Browse files
committed
Added support for warnings on --%throws annotation
1 parent 9e55e99 commit 9847364

4 files changed

Lines changed: 148 additions & 46 deletions

File tree

docs/userguide/annotations.md

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,20 +1180,23 @@ Keep in mind that when your test runs as autonomous transaction it will not see
11801180
### Throws
11811181

11821182
The `--%throws` annotation allows you to specify a list of exceptions as one of:
1183-
- number literals
1184-
- variables of type exception defined in a package specification
1185-
- variables of type number defined in a package specification
1186-
- [predefined oracle exceptions](https://docs.oracle.com/cd/E11882_01/timesten.112/e21639/exceptions.htm#CIHFIGFE)
1183+
- number literals - example `--%throws(-20134)`
1184+
- variables of type exception defined in a package specification - example `--%throws(exc_pkg.c_exception_No_variable)`
1185+
- variables of type number defined in a package specification - example `--%throws(exc_pkg.c_some_exception)`
1186+
- [predefined oracle exceptions](https://docs.oracle.com/cd/E11882_01/timesten.112/e21639/exceptions.htm#CIHFIGFE) - example `--%throws(no_data_found)`
11871187

1188-
If `--%throws(-20001,-20002)` is specified and no exception is raised or the exception raised is not on the list of provided exceptions, the test is marked as failed.
1188+
The annotation is ignored, when no valid arguments are provided. Examples of invalid annotations `--%throws()`,`--%throws`, `--%throws(abe, 723pf)`.
11891189

1190-
The framework ignores bad arguments. `--%throws(7894562, operaqk, -=1, -20496, pow74d, posdfk3)` will be interpreted as `--%throws(-20496)`.
1191-
The annotation is ignored, when no valid arguments are provided `--%throws()`,`--%throws`, `--%throws(abe, 723pf)`.
1190+
If `--%throws` annotation is specified with arguments and no exception is raised, the test is marked as failed.
11921191

1193-
The framework allows to pass exception defined in variables for example constants in other packages or as exceptions `--%throws(exc_pkg.c_exc_variable)`
1192+
If `--%throws` annotation is specified with arguments and exception raised is not on the list of provided exceptions, the test is marked as failed.
11941193

1195-
Please note that NO_DATA_FOUND is a special case and it will be translated into -1403.
1194+
The framework will raise a warning, when `--%throws` annotation has invalid arguments or when no arguments were provided.
11961195

1196+
Annotation `--%throws(7894562, operaqk, -=1, -20496, pow74d, posdfk3)` will be interpreted as `--%throws(-20496)`.
1197+
1198+
Please note that `NO_DATA_FOUND` exception is a special case in Oracle. To capture it use `NO_DATA_FOUND` named exception or `-1403` exception No.
1199+
11971200
Example:
11981201
```sql
11991202
create or replace package exc_pkg is
@@ -1212,7 +1215,7 @@ create or replace package example_pgk as
12121215
--%suite(Example Throws Annotation)
12131216

12141217
--%test(Throws one of the listed exceptions)
1215-
--%throws(-20145,-20146, -20189 ,-20563)
1218+
--%throws(-20145,bad,-20146, -20189 ,-20563)
12161219
procedure raised_one_listed_exception;
12171220

12181221
--%test(Throws different exception than expected)
@@ -1247,6 +1250,10 @@ create or replace package example_pgk as
12471250
--%throws(DUP_VAL_ON_INDEX)
12481251
procedure raise_named_exc;
12491252

1253+
--%test(Invalid throws annotation)
1254+
--%throws
1255+
procedure bad_throws_annotation;
1256+
12501257
end;
12511258
/
12521259
create or replace package body example_pgk is
@@ -1295,24 +1302,29 @@ create or replace package body example_pgk is
12951302
raise DUP_VAL_ON_INDEX;
12961303
end;
12971304

1305+
procedure bad_throws_annotation is
1306+
begin
1307+
null;
1308+
end;
12981309
end;
12991310
/
13001311

1301-
exec ut.run('example_pgk');
1312+
exec ut3.ut.run('example_pgk');
13021313
```
13031314

13041315
Running the test will give report:
13051316
```
13061317
Example Throws Annotation
1307-
Throws one of the listed exceptions [.003 sec]
1308-
Throws different exception than expected [.003 sec] (FAILED - 1)
1309-
Throws different exception than listed [.004 sec] (FAILED - 2)
1310-
Gives failure when an exception is expected and nothing is thrown [.004 sec] (FAILED - 3)
1318+
Throws one of the listed exceptions [.002 sec]
1319+
Throws different exception than expected [.002 sec] (FAILED - 1)
1320+
Throws different exception than listed [.003 sec] (FAILED - 2)
1321+
Gives failure when an exception is expected and nothing is thrown [.002 sec] (FAILED - 3)
13111322
Throws package exception option1 [.003 sec]
1312-
Throws package exception option2 [.003 sec]
1313-
Throws package exception option3 [.003 sec]
1314-
Throws package exception option4 [.003 sec]
1315-
Raise name exception [.003 sec]
1323+
Throws package exception option2 [.002 sec]
1324+
Throws package exception option3 [.002 sec]
1325+
Throws package exception option4 [.002 sec]
1326+
Raise name exception [.002 sec]
1327+
Invalid throws annotation [.002 sec]
13161328
13171329
Failures:
13181330
@@ -1333,9 +1345,18 @@ Failures:
13331345
3) nothing_thrown
13341346
Expected one of exceptions (-20459, -20136, -20145) but nothing was raised.
13351347
1336-
Finished in .033843 seconds
1337-
9 tests, 3 failed, 0 errored, 0 disabled, 0 warning(s)
1338-
1348+
1349+
Warnings:
1350+
1351+
1) example_pgk
1352+
Invalid parameter value "bad" for "--%throws" annotation. Parameter ignored.
1353+
at "UT3.EXAMPLE_PGK.RAISED_ONE_LISTED_EXCEPTION", line 6
1354+
2) example_pgk
1355+
"--%throws" annotation requires a parameter. Annotation ignored.
1356+
at "UT3.EXAMPLE_PGK.BAD_THROWS_ANNOTATION", line 42
1357+
1358+
Finished in .025784 seconds
1359+
10 tests, 3 failed, 0 errored, 0 disabled, 2 warning(s)
13391360
```
13401361

13411362
## Order of execution

source/core/ut_suite_builder.pkb

Lines changed: 63 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,20 @@ create or replace package body ut_suite_builder is
159159
-- Processing annotations
160160
-----------------------------------------------
161161

162+
function get_qualified_object_name(
163+
a_suite ut_suite_item, a_procedure_name t_object_name
164+
) return varchar2 is
165+
l_result varchar2(1000);
166+
begin
167+
if a_suite is not null then
168+
l_result := upper( a_suite.object_owner || '.' || a_suite.object_name );
169+
if a_procedure_name is not null then
170+
l_result := l_result || upper( '.' || a_procedure_name );
171+
end if;
172+
end if;
173+
return l_result;
174+
end;
175+
162176
procedure add_annotation_ignored_warning(
163177
a_suite in out nocopy ut_suite_item,
164178
a_annotation t_annotation_name,
@@ -168,13 +182,9 @@ create or replace package body ut_suite_builder is
168182
) is
169183
l_object_name varchar2(1000);
170184
begin
171-
l_object_name := upper( a_suite.object_owner || '.' || a_suite.object_name );
172-
if a_procedure_name is not null then
173-
l_object_name := l_object_name || upper( '.' || a_procedure_name );
174-
end if;
175185
a_suite.put_warning(
176186
replace(a_message,'%%%','"--%'||a_annotation||'"') || ' Annotation ignored.'
177-
|| chr( 10 ) || 'at "' || l_object_name || '", line ' || a_line_no
187+
|| chr( 10 ) || 'at "' || get_qualified_object_name(a_suite, a_procedure_name) || '", line ' || a_line_no
178188
);
179189
end;
180190

@@ -256,10 +266,16 @@ create or replace package body ut_suite_builder is
256266
return false;
257267
end;
258268

259-
function build_exception_numbers_list(a_annotation_text in varchar2) return ut_integer_list is
269+
function build_exception_numbers_list(
270+
a_suite in out nocopy ut_suite,
271+
a_procedure_name t_object_name,
272+
a_line_no integer,
273+
a_annotation_text in varchar2
274+
) return ut_integer_list is
260275
l_throws_list ut_varchar2_list;
276+
l_exception_number integer;
261277
l_exception_number_list ut_integer_list := ut_integer_list();
262-
l_regexp_for_exception_no varchar2(30) := '^-?[[:digit:]]{1,5}$';
278+
c_regexp_for_exception_no constant varchar2(30) := '^-?[[:digit:]]{1,5}$';
263279
begin
264280
/*the a_expected_error_codes is converted to a ut_varchar2_list after that is trimmed and filtered to left only valid exception numbers*/
265281
l_throws_list := ut_utils.trim_list_elements(ut_utils.string_to_table(a_annotation_text, ',', 'Y'));
@@ -269,39 +285,62 @@ create or replace package body ut_suite_builder is
269285
/**
270286
* Check if its a valid qualified name and if so try to resolve name to an exception number
271287
*/
272-
if is_valid_qualified_name(l_throws_list(i)) then
273-
l_throws_list(i) := get_exception_number(l_throws_list(i));
288+
if is_valid_qualified_name(l_throws_list(i)) then
289+
l_exception_number := get_exception_number(l_throws_list(i));
290+
elsif regexp_like(l_throws_list(i), c_regexp_for_exception_no) then
291+
l_exception_number := l_throws_list(i);
274292
end if;
275-
end loop;
276-
277-
l_throws_list := ut_utils.filter_list( ut_utils.trim_list_elements(l_throws_list), l_regexp_for_exception_no);
278293

279-
l_exception_number_list.extend(l_throws_list.count);
280-
for i in 1 .. l_throws_list.count loop
281-
l_exception_number_list(i) := l_throws_list(i);
294+
if l_exception_number is null then
295+
a_suite.put_warning(
296+
'Invalid parameter value "'||l_throws_list(i)||'" for "--%throws" annotation. Parameter ignored.'
297+
|| chr( 10 ) || 'at "' || get_qualified_object_name(a_suite, a_procedure_name) || '", line ' || a_line_no
298+
);
299+
else
300+
l_exception_number_list.extend;
301+
l_exception_number_list(l_exception_number_list.last) := l_exception_number;
302+
end if;
303+
l_exception_number := null;
282304
end loop;
305+
283306
return l_exception_number_list;
284307
end;
285308

286309
procedure add_to_throws_numbers_list(
287-
a_list in out nocopy ut_integer_list,
310+
a_suite in out nocopy ut_suite,
311+
a_list in out nocopy ut_integer_list,
312+
a_procedure_name t_object_name,
288313
a_throws_ann_text tt_annotation_texts
289314
) is
290315
l_annotation_pos binary_integer;
291316
begin
292317
a_list := ut_integer_list();
293318
l_annotation_pos := a_throws_ann_text.first;
294319
while l_annotation_pos is not null loop
295-
a_list := a_list multiset union build_exception_numbers_list( a_throws_ann_text(l_annotation_pos));
320+
if a_throws_ann_text(l_annotation_pos) is null then
321+
a_suite.put_warning(
322+
'"--%throws" annotation requires a parameter. Annotation ignored.'
323+
|| chr( 10 ) || 'at "' || get_qualified_object_name(a_suite, a_procedure_name) || '", line ' || l_annotation_pos
324+
);
325+
else
326+
a_list :=
327+
a_list multiset union
328+
build_exception_numbers_list(
329+
a_suite,
330+
a_procedure_name,
331+
l_annotation_pos,
332+
a_throws_ann_text(l_annotation_pos)
333+
);
334+
end if;
296335
l_annotation_pos := a_throws_ann_text.next(l_annotation_pos);
297336
end loop;
298337
end;
299338

300339
procedure add_to_list(
301340
a_executables in out nocopy ut_executables,
302-
a_owner varchar2,
303-
a_package_name varchar2,
304-
a_procedure_name varchar2,
341+
a_owner t_object_name,
342+
a_package_name t_object_name,
343+
a_procedure_name t_object_name,
305344
a_executable_type ut_utils.t_executable_type
306345
) is
307346
begin
@@ -314,8 +353,8 @@ create or replace package body ut_suite_builder is
314353

315354
procedure add_all_to_list(
316355
a_executables in out nocopy ut_executables,
317-
a_owner varchar2,
318-
a_package_name varchar2,
356+
a_owner t_object_name,
357+
a_package_name t_object_name,
319358
a_annotation_texts tt_annotation_texts,
320359
a_event_name ut_utils.t_event_name
321360
) is
@@ -398,7 +437,7 @@ create or replace package body ut_suite_builder is
398437

399438
procedure add_test(
400439
a_suite in out nocopy ut_suite,
401-
a_procedure_name varchar2,
440+
a_procedure_name t_object_name,
402441
a_annotations tt_procedure_annotations
403442
) is
404443
l_test ut_test;
@@ -435,7 +474,7 @@ create or replace package body ut_suite_builder is
435474
add_all_to_list( l_test.after_test_list, l_test.object_owner, l_test.object_name, a_annotations(gc_aftertest), ut_utils.gc_after_test );
436475
end if;
437476
if a_annotations.exists(gc_throws) then
438-
add_to_throws_numbers_list(l_test.expected_error_codes, a_annotations(gc_throws));
477+
add_to_throws_numbers_list(a_suite, l_test.expected_error_codes, a_procedure_name, a_annotations(gc_throws));
439478
end if;
440479
l_test.disabled_flag := ut_utils.boolean_to_int(a_annotations.exists(gc_disabled));
441480

test/core/test_suite_builder.pkb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,5 +705,41 @@ create or replace package body test_suite_builder is
705705
);
706706
end;
707707

708+
procedure throws_value_empty is
709+
l_actual clob;
710+
l_annotations ut3.ut_annotations;
711+
begin
712+
--Arrange
713+
l_annotations := ut3.ut_annotations(
714+
ut3.ut_annotation(1, 'suite','Cool', null),
715+
ut3.ut_annotation(3, 'test','A test with empty throws annotation', 'A_TEST_PROCEDURE'),
716+
ut3.ut_annotation(3, 'throws',null, 'A_TEST_PROCEDURE')
717+
);
718+
--Act
719+
l_actual := invoke_builder_for_annotations(l_annotations, 'SOME_PACKAGE');
720+
--Assert
721+
ut.expect(l_actual).to_be_like(
722+
'%<WARNINGS>%&quot;--%throws&quot; annotation requires a parameter. Annotation ignored.%</WARNINGS>%'
723+
);
724+
end;
725+
726+
procedure throws_value_invalid is
727+
l_actual clob;
728+
l_annotations ut3.ut_annotations;
729+
begin
730+
--Arrange
731+
l_annotations := ut3.ut_annotations(
732+
ut3.ut_annotation(1, 'suite','Cool', null),
733+
ut3.ut_annotation(3, 'test','A test with invalid throws annotation', 'A_TEST_PROCEDURE'),
734+
ut3.ut_annotation(3, 'throws',' -20145 , bad_variable_name ', 'A_TEST_PROCEDURE')
735+
);
736+
--Act
737+
l_actual := invoke_builder_for_annotations(l_annotations, 'SOME_PACKAGE');
738+
--Assert
739+
ut.expect(l_actual).to_be_like(
740+
'%<WARNINGS>%Invalid parameter value &quot;bad_variable_name&quot; for &quot;--%throws&quot; annotation. Parameter ignored.%</WARNINGS>%'
741+
);
742+
end;
743+
708744
end;
709745
/

test/core/test_suite_builder.pks

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,11 @@ create or replace package test_suite_builder is
8383
--%test(Gives warning if --%endcontext is missing a preceding --%context)
8484
procedure endcontext_without_context;
8585

86+
--%test(Gives warning if --%throws annotation has no value)
87+
procedure throws_value_empty;
88+
89+
--%test(Gives warning if --%throws annotation has invalid value)
90+
procedure throws_value_invalid;
91+
8692
end;
8793
/

0 commit comments

Comments
 (0)