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

Skip to content

Commit 8ff4f6b

Browse files
committed
Adding code and tests,.
1 parent caeeb30 commit 8ff4f6b

18 files changed

Lines changed: 501 additions & 40 deletions

source/core/ut_utils.pkb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,5 +877,30 @@ create or replace package body ut_utils is
877877
end;
878878
end;
879879

880+
function interval_to_text(a_interval dsinterval_unconstrained) return varchar2 is
881+
l_day varchar2(100) := extract(day from a_interval);
882+
l_hour varchar2(100) := extract(hour from a_interval);
883+
l_minute varchar2(100) := extract(minute from a_interval);
884+
l_second varchar2(100) := extract(second from a_interval);
885+
l_result varchar2(32767);
886+
begin
887+
l_result := case when l_day > 0 then l_day ||' day' else null end;
888+
l_result := l_result || case when l_hour > 0 then ' '|| l_hour ||' hour' else null end;
889+
l_result := l_result || case when l_minute> 0 then ' '||l_minute ||' minute' else null end;
890+
l_result := l_result || case when l_second > 0 then ' '||l_second ||' second' else null end;
891+
return trim(leading ' ' from l_result);
892+
end;
893+
894+
function interval_to_text(a_interval yminterval_unconstrained) return varchar2 is
895+
l_year varchar2(4) := extract(year from a_interval);
896+
l_month varchar2(20) := extract(month from a_interval);
897+
l_result varchar2(32767);
898+
begin
899+
l_result := case when l_year > 0 then l_year ||' year' else null end;
900+
l_result := l_result || case when l_month > 0 then ' '||l_month ||' month' else null end;
901+
return trim(leading ' ' from l_result);
902+
end;
903+
904+
880905
end ut_utils;
881906
/

source/core/ut_utils.pks

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,13 @@ create or replace package ut_utils authid definer is
434434
* If null value passed returns null
435435
*/
436436
function qualified_sql_name(a_name varchar2) return varchar2;
437-
437+
438+
/*
439+
* Return value of interval in plain english
440+
*/
441+
function interval_to_text(a_interval dsinterval_unconstrained) return varchar2;
442+
443+
function interval_to_text(a_interval yminterval_unconstrained) return varchar2;
444+
438445
end ut_utils;
439446
/

source/expectations/data_values/ut_data_value_dsinterval.tpb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ create or replace type body ut_data_value_dsinterval as
3131

3232
overriding member function to_string return varchar2 is
3333
begin
34-
return ut_utils.to_string(self.data_value);
34+
return ut_utils.interval_to_text(self.data_value);
3535
end;
3636

3737
overriding member function compare_implementation(a_other ut_data_value) return integer is

source/expectations/data_values/ut_data_value_yminterval.tpb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ create or replace type body ut_data_value_yminterval as
3131

3232
overriding member function to_string return varchar2 is
3333
begin
34-
return ut_utils.to_string(self.data_value);
34+
return ut_utils.interval_to_text(self.data_value);
3535
end;
3636

3737
overriding member function compare_implementation(a_other ut_data_value) return integer is
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
create or replace type body ut_be_within as
2+
/*
3+
utPLSQL - Version 3
4+
Copyright 2016 - 2019 utPLSQL Project
5+
6+
Licensed under the Apache License, Version 2.0 (the "License"):
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
member procedure init(self in out nocopy ut_be_within, a_dist ut_data_value, a_is_pct number , a_self_type varchar2 := null) is
20+
begin
21+
self.dist := a_dist;
22+
self.is_pct := nvl(a_is_pct,0);
23+
self.self_type := nvl( a_self_type, $$plsql_unit );
24+
end;
25+
26+
constructor function ut_be_within(self in out nocopy ut_be_within, a_dist number, a_is_pct number) return self as result is
27+
begin
28+
init(ut_data_value_number(a_dist),a_is_pct);
29+
return;
30+
end;
31+
32+
constructor function ut_be_within(self in out nocopy ut_be_within, a_dist dsinterval_unconstrained, a_is_pct number) return self as result is
33+
begin
34+
init(ut_data_value_dsinterval(a_dist),a_is_pct);
35+
return;
36+
end;
37+
38+
constructor function ut_be_within(self in out nocopy ut_be_within, a_dist yminterval_unconstrained, a_is_pct number) return self as result is
39+
begin
40+
init(ut_data_value_yminterval(a_dist),a_is_pct);
41+
return;
42+
end;
43+
44+
member procedure of_(self in ut_be_within, a_expected number) is
45+
l_result ut_be_within := self;
46+
begin
47+
l_result.expected := ut_data_value_number(a_expected);
48+
l_result.expectation.to_(l_result );
49+
end;
50+
51+
member procedure of_(self in ut_be_within, a_expected date) is
52+
l_result ut_be_within := self;
53+
begin
54+
l_result.expected := ut_data_value_date(a_expected);
55+
l_result.expectation.to_(l_result );
56+
end;
57+
58+
overriding member function run_matcher(self in out nocopy ut_be_within, a_actual ut_data_value) return boolean is
59+
l_result boolean;
60+
begin
61+
if self.expected.data_type = a_actual.data_type then
62+
if self.expected is of (ut_data_value_number) and self.is_pct = 0 then
63+
l_result := abs((treat(self.expected as ut_data_value_number).data_value - treat(a_actual as ut_data_value_number).data_value)) <= treat(self.dist as ut_data_value_number).data_value;
64+
elsif self.expected is of (ut_data_value_number) and self.is_pct = 1 then
65+
l_result := treat(self.dist as ut_data_value_number).data_value >= ((treat(self.expected as ut_data_value_number).data_value - treat(a_actual as ut_data_value_number).data_value ) * 100 ) /
66+
(treat(self.expected as ut_data_value_number).data_value) ;
67+
elsif self.expected is of (ut_data_value_date) and self.dist is of ( ut_data_value_yminterval) then
68+
l_result := treat(a_actual as ut_data_value_date).data_value between (treat(self.expected as ut_data_value_date).data_value) - treat(self.dist as ut_data_value_yminterval).data_value
69+
and (treat(self.expected as ut_data_value_date).data_value) + treat(self.dist as ut_data_value_yminterval).data_value;
70+
elsif self.expected is of (ut_data_value_date) and self.dist is of ( ut_data_value_dsinterval) then
71+
l_result := treat(a_actual as ut_data_value_date).data_value between (treat(self.expected as ut_data_value_date).data_value) - treat(self.dist as ut_data_value_dsinterval).data_value
72+
and (treat(self.expected as ut_data_value_date).data_value) + treat(self.dist as ut_data_value_dsinterval).data_value;
73+
end if;
74+
else
75+
l_result := (self as ut_matcher).run_matcher(a_actual);
76+
end if;
77+
return l_result;
78+
end;
79+
80+
overriding member function failure_message(a_actual ut_data_value) return varchar2 is
81+
l_distance varchar2(32767);
82+
begin
83+
l_distance := case
84+
when self.dist is of (ut_data_value_number) then
85+
treat(self.dist as ut_data_value_number).to_string
86+
when self.dist is of (ut_data_value_yminterval) then
87+
treat(self.dist as ut_data_value_yminterval).to_string
88+
when self.dist is of (ut_data_value_dsinterval) then
89+
treat(self.dist as ut_data_value_dsinterval).to_string
90+
else
91+
null
92+
end;
93+
94+
return (self as ut_matcher).failure_message(a_actual) || ' '||l_distance ||' of '|| expected.to_string_report();
95+
end;
96+
97+
overriding member function failure_message_when_negated(a_actual ut_data_value) return varchar2 is
98+
l_result varchar2(32767);
99+
begin
100+
return (self as ut_matcher).failure_message_when_negated(a_actual) || ': '|| expected.to_string_report();
101+
end;
102+
103+
end;
104+
/
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
create or replace type ut_be_within under ut_comparison_matcher(
2+
/*
3+
utPLSQL - Version 3
4+
Copyright 2016 - 2019 utPLSQL Project
5+
6+
Licensed under the Apache License, Version 2.0 (the "License"):
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
20+
/**
21+
* Holds information about mather options
22+
*/
23+
dist ut_data_value,
24+
is_pct number(1,0),
25+
26+
member procedure init(self in out nocopy ut_be_within, a_dist ut_data_value, a_is_pct number , a_self_type varchar2 := null),
27+
constructor function ut_be_within(self in out nocopy ut_be_within, a_dist number, a_is_pct number) return self as result,
28+
constructor function ut_be_within(self in out nocopy ut_be_within, a_dist dsinterval_unconstrained, a_is_pct number) return self as result,
29+
constructor function ut_be_within(self in out nocopy ut_be_within, a_dist yminterval_unconstrained, a_is_pct number) return self as result,
30+
member procedure of_(self in ut_be_within, a_expected number),
31+
member procedure of_(self in ut_be_within, a_expected date),
32+
33+
overriding member function run_matcher(self in out nocopy ut_be_within, a_actual ut_data_value) return boolean,
34+
overriding member function failure_message(a_actual ut_data_value) return varchar2,
35+
overriding member function failure_message_when_negated(a_actual ut_data_value) return varchar2
36+
)
37+
not final
38+
/

source/expectations/matchers/ut_matcher.tps

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ create or replace type ut_matcher under ut_matcher_base(
1717
*/
1818
is_errored integer,
1919
is_negated_flag number(1,0),
20-
expectation ut_expectation,
20+
expectation ut_expectation_base,
2121
/*
2222
function: run_matcher
2323

source/expectations/ut_expectation.tpb

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,6 @@ create or replace type body ut_expectation as
1515
See the License for the specific language governing permissions and
1616
limitations under the License.
1717
*/
18-
member procedure to_(self in ut_expectation, a_matcher ut_matcher_base) is
19-
l_expectation_result boolean;
20-
l_matcher ut_matcher := treat(a_matcher as ut_matcher);
21-
l_message varchar2(32767);
22-
begin
23-
if l_matcher.is_negated() then
24-
self.not_to( a_matcher );
25-
else
26-
l_expectation_result := l_matcher.run_matcher( self.actual_data );
27-
l_expectation_result := coalesce(l_expectation_result,false);
28-
l_message := coalesce( l_matcher.error_message( self.actual_data ), l_matcher.failure_message( self.actual_data ) );
29-
ut_expectation_processor.add_expectation_result( ut_expectation_result( ut_utils.to_test_result( l_expectation_result ), self.description, l_message ) );
30-
end if;
31-
end;
32-
33-
member procedure not_to(self in ut_expectation, a_matcher ut_matcher_base) is
34-
l_expectation_result boolean;
35-
l_matcher ut_matcher := treat(a_matcher as ut_matcher);
36-
l_message varchar2(32767);
37-
begin
38-
l_expectation_result := coalesce( l_matcher.run_matcher_negated( self.actual_data ), false );
39-
40-
l_message := coalesce( l_matcher.error_message( self.actual_data ), l_matcher.failure_message_when_negated( self.actual_data ) );
41-
ut_expectation_processor.add_expectation_result( ut_expectation_result( ut_utils.to_test_result( l_expectation_result ), self.description, l_message ) );
42-
end;
4318

4419
member procedure to_be_null(self in ut_expectation) is
4520
begin
@@ -719,5 +694,37 @@ create or replace type body ut_expectation as
719694
self.not_to( ut_contain(a_expected).negated() );
720695
end;
721696

697+
member function to_be_within(a_dist natural) return ut_be_within is
698+
l_result ut_matcher;
699+
begin
700+
l_result := ut_be_within(a_dist,0);
701+
l_result.expectation := self;
702+
return treat(l_result as ut_be_within);
703+
end;
704+
705+
member function to_be_within(a_dist dsinterval_unconstrained) return ut_be_within is
706+
l_result ut_matcher;
707+
begin
708+
l_result := ut_be_within(a_dist,0);
709+
l_result.expectation := self;
710+
return treat(l_result as ut_be_within);
711+
end;
712+
713+
member function to_be_within(a_dist yminterval_unconstrained) return ut_be_within is
714+
l_result ut_matcher;
715+
begin
716+
l_result := ut_be_within(a_dist,0);
717+
l_result.expectation := self;
718+
return treat(l_result as ut_be_within);
719+
end;
720+
721+
member function to_be_within_pct(a_dist natural) return ut_be_within is
722+
l_result ut_matcher;
723+
begin
724+
l_result := ut_be_within(a_dist,1);
725+
l_result.expectation := self;
726+
return treat(l_result as ut_be_within);
727+
end;
728+
722729
end;
723730
/

source/expectations/ut_expectation.tps

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
create or replace type ut_expectation authid current_user as object(
1+
create or replace type ut_expectation under ut_expectation_base(
22
/*
33
utPLSQL - Version 3
44
Copyright 2016 - 2019 utPLSQL Project
@@ -15,13 +15,7 @@ create or replace type ut_expectation authid current_user as object(
1515
See the License for the specific language governing permissions and
1616
limitations under the License.
1717
*/
18-
actual_data ut_data_value,
19-
description varchar2(4000 char),
20-
21-
--base matcher executors
22-
member procedure to_(self in ut_expectation, a_matcher ut_matcher_base),
23-
member procedure not_to(self in ut_expectation, a_matcher ut_matcher_base),
24-
18+
2519
--shortcuts
2620
member procedure to_be_null(self in ut_expectation),
2721
member procedure to_be_not_null(self in ut_expectation),
@@ -168,8 +162,12 @@ create or replace type ut_expectation authid current_user as object(
168162
member procedure to_contain(self in ut_expectation, a_expected sys_refcursor),
169163
member procedure not_to_contain(self in ut_expectation, a_expected sys_refcursor),
170164
member procedure to_contain(self in ut_expectation, a_expected anydata),
171-
member procedure not_to_contain(self in ut_expectation, a_expected anydata)
165+
member procedure not_to_contain(self in ut_expectation, a_expected anydata),
172166

167+
member function to_be_within(a_dist natural) return ut_be_within,
168+
member function to_be_within(a_dist dsinterval_unconstrained) return ut_be_within,
169+
member function to_be_within(a_dist yminterval_unconstrained) return ut_be_within,
170+
member function to_be_within_pct(a_dist natural) return ut_be_within
173171
)
174172
not final
175173
/
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
create or replace type body ut_expectation_base as
2+
/*
3+
utPLSQL - Version 3
4+
Copyright 2016 - 2019 utPLSQL Project
5+
6+
Licensed under the Apache License, Version 2.0 (the "License"):
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
member procedure to_(self in ut_expectation_base, a_matcher ut_matcher_base) is
19+
l_expectation_result boolean;
20+
l_matcher ut_matcher := treat(a_matcher as ut_matcher);
21+
l_message varchar2(32767);
22+
begin
23+
if l_matcher.is_negated() then
24+
self.not_to( a_matcher );
25+
else
26+
l_expectation_result := l_matcher.run_matcher( self.actual_data );
27+
l_expectation_result := coalesce(l_expectation_result,false);
28+
l_message := coalesce( l_matcher.error_message( self.actual_data ), l_matcher.failure_message( self.actual_data ) );
29+
ut_expectation_processor.add_expectation_result( ut_expectation_result( ut_utils.to_test_result( l_expectation_result ), self.description, l_message ) );
30+
end if;
31+
end;
32+
33+
member procedure not_to(self in ut_expectation_base, a_matcher ut_matcher_base) is
34+
l_expectation_result boolean;
35+
l_matcher ut_matcher := treat(a_matcher as ut_matcher);
36+
l_message varchar2(32767);
37+
begin
38+
l_expectation_result := coalesce( l_matcher.run_matcher_negated( self.actual_data ), false );
39+
40+
l_message := coalesce( l_matcher.error_message( self.actual_data ), l_matcher.failure_message_when_negated( self.actual_data ) );
41+
ut_expectation_processor.add_expectation_result( ut_expectation_result( ut_utils.to_test_result( l_expectation_result ), self.description, l_message ) );
42+
end;
43+
end;
44+
/

0 commit comments

Comments
 (0)