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

Skip to content

Commit 6a061ed

Browse files
authored
Merge pull request #159 from jgebal/feature/color_reporter
Added colors to the documentation reporter.
2 parents eb3fe7c + 91cc456 commit 6a061ed

20 files changed

Lines changed: 352 additions & 139 deletions

client_source/sqlplus/ut_run.sql

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
Current limit of script parameters is 39
66
77
Scrip invocation:
8-
ut_run.sql user/password@database [-p=(ut_path|ut_paths)] [-f=format [-o=output] [-s] ...]
8+
ut_run.sql user/password@database [-p=(ut_path|ut_paths)] [-c] [-f=format [-o=output] [-s] ...]
99
1010
Parameters:
1111
user - username to connect as
@@ -34,6 +34,7 @@ Parameters:
3434
If not defined, then output will be displayed on screen, even if the parameter -s is not specified.
3535
If more than one -o parameter is specified for one -f parameter, the last one is taken into consideration.
3636
-s - Forces putting output to to screen for a given -f parameter.
37+
-c - If specified, enables printing of test results in colors as defined by ANSICONSOLE standards
3738
3839
Parameters -f, -o, -s are correlated. That is parameters -o and -s are defining outputs for -f.
3940
Examples of invocation using sqlplus from command line:
@@ -102,14 +103,13 @@ end;
102103
spool off
103104
set define &
104105

105-
106106
@@set_run_params.sql.tmp
107107

108-
109108
spool run_in_backgroung.sql.tmp
110109
declare
111110
l_output_type varchar2(256) := ut_runner.get_streamed_output_type_name();
112111
l_run_params ut_runner.t_run_params := ut_runner.get_run_params();
112+
l_color_enabled varchar2(5) := case when l_run_params.color_enabled then 'true' else 'false' end;
113113
procedure p(a_text varchar2) is
114114
begin
115115
dbms_output.put_line(a_text);
@@ -124,12 +124,14 @@ begin
124124
p( ' v_reporter ut_reporter_base;');
125125
p( ' v_reporters_list ut_reporters := ut_reporters();');
126126
p( 'begin');
127-
for i in 1 .. cardinality(l_run_params.call_params) loop
128-
p(' v_reporter := '||l_run_params.call_params(i).ut_reporter_name||'('||l_output_type||'());');
129-
p(' v_reporter.output.output_id := '''||l_run_params.call_params(i).output_id||''';');
130-
p(' v_reporters_list.extend; v_reporters_list(v_reporters_list.last) := v_reporter;');
131-
end loop;
132-
p( ' ut_runner.run( ut_varchar2_list('||l_run_params.ut_paths||'), v_reporters_list );');
127+
if l_run_params.call_params is not null then
128+
for i in 1 .. l_run_params.call_params.count loop
129+
p(' v_reporter := '||l_run_params.call_params(i).ut_reporter_name||'('||l_output_type||'());');
130+
p(' v_reporter.output.output_id := '''||l_run_params.call_params(i).output_id||''';');
131+
p(' v_reporters_list.extend; v_reporters_list(v_reporters_list.last) := v_reporter;');
132+
end loop;
133+
end if;
134+
p( ' ut_runner.run( ut_varchar2_list('||l_run_params.ut_paths||'), v_reporters_list, a_color_console => '||l_color_enabled||' );');
133135
p( 'end;');
134136
p( '/');
135137
p( 'spool off');
@@ -150,13 +152,15 @@ declare
150152
begin
151153
p('declare l_date date := sysdate; begin loop exit when l_date < sysdate; end loop; end;');
152154
p('/');
153-
for i in 1 .. cardinality(l_run_params.call_params) loop
154-
p('set termout '||l_run_params.call_params(i).output_to_screen);
155-
l_need_spool := (l_run_params.call_params(i).output_file_name is not null);
156-
p(case when l_need_spool then 'spool '||l_run_params.call_params(i).output_file_name||chr(10) end||
157-
'select * from table( '||l_output_type||'().get_lines('''||l_run_params.call_params(i).output_id||''') );'||
158-
case when l_need_spool then chr(10)||'spool off' end);
159-
end loop;
155+
if l_run_params.call_params is not null then
156+
for i in 1 .. l_run_params.call_params.count loop
157+
p('set termout '||l_run_params.call_params(i).output_to_screen);
158+
l_need_spool := (l_run_params.call_params(i).output_file_name is not null);
159+
p(case when l_need_spool then 'spool '||l_run_params.call_params(i).output_file_name||chr(10) end||
160+
'select * from table( '||l_output_type||'().get_lines('''||l_run_params.call_params(i).output_id||''') );'||
161+
case when l_need_spool then chr(10)||'spool off' end);
162+
end loop;
163+
end if;
160164
end;
161165
/
162166

examples/RunDeveloperExamples.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ set echo off
44
set feedback off
55
set linesize 1000
66

7+
exec ut_documentation_reporter.set_color_enabled(true);
78
--developer examples
89
prompt RunExampleComplexSuiteWithCustomReporter
910
@@developer_examples/RunExampleComplexSuiteWithCustomReporter.sql

examples/RunUserExamples.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ set linesize 1000
66

77
prompt Common examples from web
88

9+
exec ut_documentation_reporter.set_color_enabled(true);
910
@@award_bonus/run_award_bonus_test.sql
1011

1112
@@between_string/run_betwnstr_test.sql

source/api/ut_runner.pkb

Lines changed: 11 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
create or replace package body ut_runner is
22

3-
g_run_params t_run_params;
4-
5-
procedure run(a_paths ut_varchar2_list, a_reporters ut_reporters) is
3+
procedure run(a_paths ut_varchar2_list, a_reporters ut_reporters, a_color_console boolean := false) is
64
l_items_to_run ut_run;
75
l_listener ut_event_listener;
86
l_current_suite ut_logical_suite;
97
begin
8+
ut_console_reporter_base.set_color_enabled(a_color_console);
109
if a_reporters is null or a_reporters.count = 0 then
1110
l_listener := ut_event_listener(ut_reporters(ut_documentation_reporter()));
1211
else
@@ -29,109 +28,35 @@ create or replace package body ut_runner is
2928
raise;
3029
end;
3130

32-
procedure run(a_paths ut_varchar2_list, a_reporter ut_reporter_base := ut_documentation_reporter()) is
31+
procedure run(a_paths ut_varchar2_list, a_reporter ut_reporter_base := ut_documentation_reporter(), a_color_console boolean := false) is
3332
begin
34-
run(a_paths, ut_reporters(coalesce(a_reporter,ut_documentation_reporter())));
33+
run(a_paths, ut_reporters(coalesce(a_reporter,ut_documentation_reporter())), a_color_console);
3534
end;
3635

3736

38-
procedure run(a_path in varchar2, a_reporter ut_reporter_base := ut_documentation_reporter()) is
37+
procedure run(a_path in varchar2, a_reporter ut_reporter_base := ut_documentation_reporter(), a_color_console boolean := false) is
3938
begin
40-
run(ut_varchar2_list(coalesce(a_path, sys_context('userenv', 'current_schema'))), a_reporter);
39+
run(ut_varchar2_list(coalesce(a_path, sys_context('userenv', 'current_schema'))), a_reporter, a_color_console);
4140
end run;
4241

43-
procedure run(a_path in varchar2, a_reporters in ut_reporters) is
42+
procedure run(a_path in varchar2, a_reporters in ut_reporters, a_color_console boolean := false) is
4443
begin
45-
run(ut_varchar2_list(coalesce(a_path, sys_context('userenv', 'current_schema'))), a_reporters);
44+
run(ut_varchar2_list(coalesce(a_path, sys_context('userenv', 'current_schema'))), a_reporters, a_color_console);
4645
end run;
4746

48-
function parse_reporting_params(a_params ut_varchar2_list) return tt_call_params is
49-
l_default_call_param t_call_param;
50-
l_call_params tt_call_params := tt_call_params();
51-
l_force_out_to_screen boolean;
52-
begin
53-
for param in(
54-
with
55-
param_vals as(
56-
select regexp_substr(column_value,'-([fos])\=?(.*)',1,1,'c',1) param_type,
57-
regexp_substr(column_value,'-([fos])\=(.*)',1,1,'c',2) param_value
58-
from table(a_params)
59-
where column_value is not null)
60-
select param_type, param_value
61-
from param_vals
62-
where param_type is not null
63-
) loop
64-
if param.param_type = 'f' or l_call_params.last is null then
65-
l_call_params.extend;
66-
l_call_params(l_call_params.last) := l_default_call_param;
67-
if param.param_type = 'f' then
68-
l_call_params(l_call_params.last).ut_reporter_name := param.param_value;
69-
end if;
70-
l_force_out_to_screen := false;
71-
end if;
72-
if param.param_type = 'o' then
73-
l_call_params(l_call_params.last).output_file_name := param.param_value;
74-
if not l_force_out_to_screen then
75-
l_call_params(l_call_params.last).output_to_screen := 'off';
76-
end if;
77-
elsif param.param_type = 's' then
78-
l_call_params(l_call_params.last).output_to_screen := 'on';
79-
l_force_out_to_screen := true;
80-
end if;
81-
end loop;
82-
if l_call_params.count = 0 then
83-
l_call_params.extend;
84-
l_call_params(1) := l_default_call_param;
85-
end if;
86-
return l_call_params;
87-
end;
88-
89-
function parse_paths_param(a_params ut_varchar2_list) return varchar2 is
90-
l_paths varchar2(4000);
91-
begin
92-
begin
93-
select ''''||replace(ut_paths,',',''',''')||''''
94-
into l_paths
95-
from (select regexp_substr(column_value,'-p\=(.*)',1,1,'c',1) as ut_paths from table(a_params) )
96-
where ut_paths is not null;
97-
exception
98-
when no_data_found then
99-
l_paths := 'user';
100-
when too_many_rows then
101-
raise_application_error(-20000, 'Parameter "-p=ut_path(s)" defined more than once. Only one "-p=ut_path(s)" parameter can be used.');
102-
end;
103-
return l_paths;
104-
end;
105-
106-
procedure setup_reporting_output_ids(a_call_params in out nocopy tt_call_params) is
107-
begin
108-
for i in 1 .. cardinality(a_call_params) loop
109-
execute immediate 'begin :l_output_id := '||get_streamed_output_type_name()||'().generate_output_id(); end;'
110-
using out a_call_params(i).output_id;
111-
end loop;
112-
end;
113-
11447
procedure set_run_params(a_params ut_varchar2_list) is
115-
l_call_params tt_call_params := tt_call_params();
11648
begin
117-
l_call_params := parse_reporting_params(a_params);
118-
g_run_params.ut_paths := parse_paths_param(a_params);
119-
setup_reporting_output_ids(l_call_params);
120-
g_run_params.call_params := l_call_params;
49+
ut_runner_helper.set_run_params(a_params);
12150
end set_run_params;
12251

12352
function get_run_params return t_run_params is
12453
begin
125-
return g_run_params;
54+
return ut_runner_helper.get_run_params();
12655
end;
12756

12857
function get_streamed_output_type_name return varchar2 is
129-
l_result varchar2(255);
13058
begin
131-
select type_name
132-
into l_result
133-
from user_types where supertype_name = 'UT_OUTPUT_STREAM';
134-
return lower(l_result);
59+
return ut_runner_helper.get_streamed_output_type_name();
13560
end;
13661

13762
end ut_runner;

source/api/ut_runner.pks

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
create or replace package ut_runner authid current_user is
22

3-
type t_call_param is record (
4-
ut_reporter_name varchar2(4000) := 'ut_documentation_reporter',
5-
output_file_name varchar2(4000),
6-
output_to_screen varchar2(3) := 'on',
7-
output_id varchar2(4000)
8-
);
3+
subtype t_call_param is ut_runner_helper.t_call_param;
94

10-
type tt_call_params is table of t_call_param;
5+
subtype tt_call_params is ut_runner_helper.tt_call_params;
116

12-
type t_run_params is record(
13-
ut_paths varchar2(4000),
14-
call_params tt_call_params
15-
);
7+
subtype t_run_params is ut_runner_helper.t_run_params;
168

179
/**
1810
* Run suites/tests by path
@@ -28,15 +20,15 @@ create or replace package ut_runner authid current_user is
2820
* parent setup/teardown procedures
2921
*/
3022

31-
procedure run(a_path varchar2 := null, a_reporter ut_reporter_base := ut_documentation_reporter());
23+
procedure run(a_path varchar2 := null, a_reporter ut_reporter_base := ut_documentation_reporter(), a_color_console boolean := false);
3224

33-
procedure run(a_path varchar2, a_reporters ut_reporters);
25+
procedure run(a_path varchar2, a_reporters ut_reporters, a_color_console boolean := false);
3426

3527
-- TODO - implementation to be changed
36-
procedure run(a_paths ut_varchar2_list, a_reporter ut_reporter_base := ut_documentation_reporter());
28+
procedure run(a_paths ut_varchar2_list, a_reporter ut_reporter_base := ut_documentation_reporter(), a_color_console boolean := false);
3729

3830
-- TODO - implementation to be changed
39-
procedure run(a_paths ut_varchar2_list, a_reporters ut_reporters);
31+
procedure run(a_paths ut_varchar2_list, a_reporters ut_reporters, a_color_console boolean := false);
4032

4133

4234

source/core/types/ut_assert_result.tpb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,6 @@ create or replace type body ut_assert_result is
8282
add_text_line(l_result, ' error: '||ut_utils.indent_lines( self.error_message, length(' error: ') ) );
8383
end if;
8484

85-
if l_result is not null and self.caller_info is not null then
86-
add_text_line(l_result, self.caller_info);
87-
end if;
88-
if length(l_result) > 0 then
89-
add_text_line(l_result,' '||chr(10));
90-
end if;
9185
return l_result;
9286
end;
9387

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
create or replace type body ut_console_reporter_base is
2+
3+
static procedure set_color_enabled(a_flag boolean) is
4+
begin
5+
ut_ansiconsole_helper.color_enabled(a_flag);
6+
end;
7+
8+
member procedure print_red_text(self in out nocopy ut_console_reporter_base, a_text varchar2) is
9+
begin
10+
self.print_text(ut_ansiconsole_helper.red(a_text));
11+
end;
12+
13+
member procedure print_green_text(self in out nocopy ut_console_reporter_base, a_text varchar2) is
14+
begin
15+
self.print_text(ut_ansiconsole_helper.green(a_text));
16+
end;
17+
18+
member procedure print_yellow_text(self in out nocopy ut_console_reporter_base, a_text varchar2) is
19+
begin
20+
self.print_text(ut_ansiconsole_helper.yellow(a_text));
21+
end;
22+
23+
member procedure print_blue_text(self in out nocopy ut_console_reporter_base, a_text varchar2) is
24+
begin
25+
self.print_text(ut_ansiconsole_helper.red(a_text));
26+
end;
27+
28+
member procedure print_cyan_text(self in out nocopy ut_console_reporter_base, a_text varchar2) is
29+
begin
30+
self.print_text(ut_ansiconsole_helper.cyan(a_text));
31+
end;
32+
33+
member procedure print_magenta_text(self in out nocopy ut_console_reporter_base, a_text varchar2) is
34+
begin
35+
self.print_text(ut_ansiconsole_helper.magenta(a_text));
36+
end;
37+
38+
end;
39+
/
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
create or replace type ut_console_reporter_base under ut_reporter_base
2+
(
3+
static procedure set_color_enabled(a_flag boolean),
4+
5+
member procedure print_red_text(self in out nocopy ut_console_reporter_base, a_text varchar2),
6+
7+
member procedure print_green_text(self in out nocopy ut_console_reporter_base, a_text varchar2),
8+
9+
member procedure print_yellow_text(self in out nocopy ut_console_reporter_base, a_text varchar2),
10+
11+
member procedure print_blue_text(self in out nocopy ut_console_reporter_base, a_text varchar2),
12+
13+
member procedure print_cyan_text(self in out nocopy ut_console_reporter_base, a_text varchar2),
14+
15+
member procedure print_magenta_text(self in out nocopy ut_console_reporter_base, a_text varchar2)
16+
17+
) not final not instantiable
18+
/

source/core/types/ut_reporter_base.tps

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ create or replace type ut_reporter_base force authid current_user as object
55
constructor function ut_reporter_base(self in out nocopy ut_reporter_base, a_output ut_output default ut_output_dbms_output()) return self as result,
66

77
member procedure print_text(self in out nocopy ut_reporter_base, a_text varchar2),
8+
89
member procedure print_clob(self in out nocopy ut_reporter_base, a_text clob),
910

1011
-- run hooks

0 commit comments

Comments
 (0)