-
Notifications
You must be signed in to change notification settings - Fork 183
/
ut_be_within.tpb
186 lines (164 loc) · 8.07 KB
/
ut_be_within.tpb
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
176
177
178
179
180
181
182
183
184
185
186
create or replace type body ut_be_within as
/*
utPLSQL - Version 3
Copyright 2016 - 2019 utPLSQL Project
Licensed under the Apache License, Version 2.0 (the "License"):
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
constructor function ut_be_within(self in out nocopy ut_be_within, a_distance_from_expected number) return self as result is
begin
self.init(ut_data_value_number(a_distance_from_expected), $$plsql_unit);
return;
end;
constructor function ut_be_within(self in out nocopy ut_be_within, a_distance_from_expected dsinterval_unconstrained) return self as result is
begin
self.init(ut_data_value_dsinterval(a_distance_from_expected), $$plsql_unit);
return;
end;
constructor function ut_be_within(self in out nocopy ut_be_within, a_distance_from_expected yminterval_unconstrained) return self as result is
begin
self.init(ut_data_value_yminterval(a_distance_from_expected), $$plsql_unit);
return;
end;
member procedure of_(self in ut_be_within, a_expected date) is
l_result ut_be_within := self;
begin
l_result.expected := ut_data_value_date(a_expected);
if l_result.is_negated_flag = 1 then
l_result.expectation.not_to(l_result );
else
l_result.expectation.to_(l_result );
end if;
end;
member function of_(self in ut_be_within, a_expected date) return ut_be_within is
l_result ut_be_within := self;
begin
l_result.expected := ut_data_value_date(a_expected);
return l_result;
end;
member procedure of_(self in ut_be_within, a_expected timestamp) is
l_result ut_be_within := self;
begin
l_result.expected := ut_data_value_timestamp(a_expected);
if l_result.is_negated_flag = 1 then
l_result.expectation.not_to(l_result );
else
l_result.expectation.to_(l_result );
end if;
end;
member function of_(self in ut_be_within, a_expected timestamp) return ut_be_within is
l_result ut_be_within := self;
begin
l_result.expected := ut_data_value_timestamp(a_expected);
return l_result;
end;
member procedure of_(self in ut_be_within, a_expected timestamp_tz_unconstrained) is
l_result ut_be_within := self;
begin
l_result.expected := ut_data_value_timestamp_tz(a_expected);
if l_result.is_negated_flag = 1 then
l_result.expectation.not_to(l_result );
else
l_result.expectation.to_(l_result );
end if;
end;
member function of_(self in ut_be_within, a_expected timestamp_tz_unconstrained) return ut_be_within is
l_result ut_be_within := self;
begin
l_result.expected := ut_data_value_timestamp_tz(a_expected);
return l_result;
end;
member procedure of_(self in ut_be_within, a_expected timestamp_ltz_unconstrained) is
l_result ut_be_within := self;
begin
l_result.expected := ut_data_value_timestamp_ltz(a_expected);
if l_result.is_negated_flag = 1 then
l_result.expectation.not_to(l_result );
else
l_result.expectation.to_(l_result );
end if;
end;
member function of_(self in ut_be_within, a_expected timestamp_ltz_unconstrained) return ut_be_within is
l_result ut_be_within := self;
begin
l_result.expected := ut_data_value_timestamp_ltz(a_expected);
return l_result;
end;
overriding member function run_matcher(self in out nocopy ut_be_within, a_actual ut_data_value) return boolean is
l_result boolean;
function l_result_from_number return boolean is
l_expected number := treat(self.expected as ut_data_value_number).data_value;
l_actual number := treat(a_actual as ut_data_value_number).data_value;
l_distance number := treat(self.distance_from_expected as ut_data_value_number).data_value;
begin
return abs(l_expected - l_actual) <= l_distance;
end;
function l_result_from_date(a_distance ut_data_value) return boolean is
l_expected date := treat(self.expected as ut_data_value_date).data_value;
l_actual date := treat(a_actual as ut_data_value_date).data_value;
l_distance_ym yminterval_unconstrained := case when self.distance_from_expected is of ( ut_data_value_yminterval)
then treat(self.distance_from_expected as ut_data_value_yminterval).data_value
end;
l_distance_ds dsinterval_unconstrained := case when self.distance_from_expected is of ( ut_data_value_dsinterval)
then treat(self.distance_from_expected as ut_data_value_dsinterval).data_value
end;
begin
return case when l_distance_ym is not null
then l_actual between l_expected - l_distance_ym and l_expected + l_distance_ym
else l_actual between l_expected - l_distance_ds and l_expected + l_distance_ds
end;
end;
function l_result_from_timestamp (a_distance ut_data_value) return boolean is
l_expected timestamp := case when self.expected is of ( ut_data_value_timestamp) then
treat(self.expected as ut_data_value_timestamp).data_value
when self.expected is of ( ut_data_value_timestamp_tz) then
treat(self.expected as ut_data_value_timestamp_tz).data_value
when self.expected is of ( ut_data_value_timestamp_ltz) then
treat(self.expected as ut_data_value_timestamp_ltz).data_value
end;
l_actual timestamp with time zone := treat(a_actual as ut_data_value_date).data_value;
l_distance_ym yminterval_unconstrained := case when self.distance_from_expected is of ( ut_data_value_yminterval)
then treat(self.distance_from_expected as ut_data_value_yminterval).data_value
end;
l_distance_ds dsinterval_unconstrained := case when self.distance_from_expected is of ( ut_data_value_dsinterval)
then treat(self.distance_from_expected as ut_data_value_dsinterval).data_value
end;
begin
return case when l_distance_ym is not null
then l_actual between l_expected - l_distance_ym and l_expected + l_distance_ym
else l_actual between l_expected - l_distance_ds and l_expected + l_distance_ds
end;
end;
begin
if self.expected.data_type = a_actual.data_type then
if self.expected is of (ut_data_value_number) then
l_result := l_result_from_number;
elsif self.expected is of (ut_data_value_date)then
l_result := l_result_from_date(self.distance_from_expected);
elsif self.expected is of (ut_data_value_timestamp_tz)then
l_result := l_result_from_date(self.distance_from_expected);
elsif self.expected is of (ut_data_value_timestamp_ltz)then
l_result := l_result_from_date(self.distance_from_expected);
end if;
else
l_result := (self as ut_matcher).run_matcher(a_actual);
end if;
return l_result;
end;
overriding member function failure_message(a_actual ut_data_value) return varchar2 is
begin
return (self as ut_matcher).failure_message(a_actual) || ' '||self.distance_from_expected.to_string ||' of '|| expected.to_string_report();
end;
overriding member function failure_message_when_negated(a_actual ut_data_value) return varchar2 is
begin
return (self as ut_matcher).failure_message_when_negated(a_actual) || ' '||self.distance_from_expected.to_string ||' of '|| expected.to_string_report();
end;
end;
/