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

Skip to content

Commit 97c887f

Browse files
committed
Added 'match' matcher for regexp matching of varchar2 and clob.
Added tests and examples for 'match' matcher.
1 parent bffcbab commit 97c887f

13 files changed

Lines changed: 126 additions & 14 deletions

examples/demo_expectations.pck

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ create or replace package demo_expectations is
2929
-- %test(demo of success for to_be_not_null expectation)
3030
procedure demo_to_be_not_null_success;
3131

32+
-- %test(demo of failure for to_match expectation)
33+
procedure demo_to_match_failure;
34+
35+
-- %test(demo of success for to_match expectation)
36+
procedure demo_to_matchl_success;
37+
3238
end;
3339
/
3440

@@ -393,5 +399,31 @@ create or replace package body demo_expectations is
393399
ut.expect( l_cursor ).to_( be_not_null );
394400
end;
395401

402+
procedure demo_to_match_failure is
403+
l_clob clob := rpad('a',32767,'a')||'Stephen';
404+
begin
405+
ut.expect( 'STEPHEN' ).to_match('^Stephen$');
406+
ut.expect( 'stephen ' ).to_match('^Stephen$', 'i'); --case insensitive
407+
ut.expect( 'stephen' ).to_( match('^Stephen$') );
408+
ut.expect( 'stephen ' ).to_( match('^Stephen$', 'i') ); --case insensitive
409+
ut.expect( l_clob ).to_match('^Stephen$');
410+
ut.expect( l_clob ).to_match('^Stephen$', 'i'); --case insensitive
411+
ut.expect( l_clob ).to_( match('^Stephen$') );
412+
ut.expect( l_clob ).to_( match('^Stephen$', 'i') ); --case insensitive
413+
end;
414+
415+
procedure demo_to_matchl_success is
416+
l_clob clob := rpad('a',32767,'a')||'STEPHEN';
417+
begin
418+
ut.expect( 'Hi, I am Stephen' ).to_match('Stephen$');
419+
ut.expect( 'stephen' ).to_match('^Stephen$', 'i'); --case insensitive
420+
ut.expect( 'Hi, I am Stephen' ).to_( match('Stephen$') );
421+
ut.expect( 'stephen' ).to_( match('^Stephen$', 'i') ); --case insensitive
422+
ut.expect( l_clob ).to_match('STEPHEN');
423+
ut.expect( l_clob ).to_match('Stephen$', 'i'); --case insensitive
424+
ut.expect( l_clob ).to_( match('STEPHEN$') );
425+
ut.expect( l_clob ).to_( match('Stephen$', 'i') ); --case insensitive
426+
end;
427+
396428
end;
397429
/

source/assertions/ut_assertion_clob.tpb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ create or replace type body ut_assertion_clob as
1818
self.add_assert_result(l_condition, 'to be like', ut_utils.to_string(a_mask)||l_escape_msg);
1919
end;
2020

21-
member procedure to_match(self in ut_assertion_clob, a_pattern in varchar2, a_modifier in varchar2 default null) is
22-
l_modifiers_msg varchar2(100) := case when a_modifier is not null then ' using modifiers '''||a_modifier||'''' end;
21+
member procedure to_match(self in ut_assertion_clob, a_pattern in varchar2, a_modifiers in varchar2 default null) is
2322
begin
24-
self.add_assert_result((regexp_like(treat(self.actual_data as ut_data_value_clob).value, a_pattern, a_modifier)), 'to match', ut_utils.to_string(a_pattern)||l_modifiers_msg);
23+
ut_utils.debug_log('ut_assertion_clob.to_match(self in ut_assertion, a_pattern in varchar2, a_modifiers in varchar2 default null)');
24+
self.to_( match(a_pattern, a_modifiers) );
2525
end;
2626

2727
end;

source/assertions/ut_assertion_clob.tps

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ create or replace type ut_assertion_clob under ut_assertion
22
(
33
overriding member procedure to_equal(self in ut_assertion_clob, a_expected clob, a_nulls_are_equal boolean := null),
44
member procedure to_be_like(self in ut_assertion_clob, a_mask in varchar2, a_escape_char in varchar2 := null),
5-
member procedure to_match(self in ut_assertion_clob, a_pattern in varchar2, a_modifier in varchar2 := null)
5+
member procedure to_match(self in ut_assertion_clob, a_pattern in varchar2, a_modifiers in varchar2 := null)
66
)
77
/

source/assertions/ut_assertion_varchar2.tpb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,10 @@ create or replace type body ut_assertion_varchar2 as
1818
self.add_assert_result(l_condition, 'to be like', ut_utils.to_string(a_mask)||l_escape_msg);
1919
end;
2020

21-
member procedure to_match(self in ut_assertion_varchar2, a_pattern in varchar2, a_modifier in varchar2 default null) is
22-
l_modifiers_msg varchar2(100) := case when a_modifier is not null then ' using modifiers '''||a_modifier||'''' end;
21+
member procedure to_match(self in ut_assertion_varchar2, a_pattern in varchar2, a_modifiers in varchar2 default null) is
2322
begin
24-
self.add_assert_result(
25-
(regexp_like(treat(self.actual_data as ut_data_value_varchar2).value, a_pattern, a_modifier))
26-
, 'to match', ut_utils.to_string(a_pattern)||l_modifiers_msg
27-
);
23+
ut_utils.debug_log('ut_assertion_varchar2.to_match(self in ut_assertion, a_pattern in varchar2, a_modifiers in varchar2 default null)');
24+
self.to_( match(a_pattern, a_modifiers) );
2825
end;
2926

3027
end;

source/assertions/ut_assertion_varchar2.tps

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ create or replace type ut_assertion_varchar2 under ut_assertion
22
(
33
overriding member procedure to_equal(self in ut_assertion_varchar2, a_expected varchar2, a_nulls_are_equal boolean := null),
44
member procedure to_be_like(self in ut_assertion_varchar2, a_mask in varchar2, a_escape_char in varchar2 := null),
5-
member procedure to_match(self in ut_assertion_varchar2, a_pattern in varchar2, a_modifier in varchar2 := null)
5+
member procedure to_match(self in ut_assertion_varchar2, a_pattern in varchar2, a_modifiers in varchar2 := null)
66
)
77
/

source/expectations/match.tpb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
create or replace type body match as
2+
3+
constructor function match(self in out nocopy match, a_pattern in varchar2, a_modifiers in varchar2 default null) return self as result is
4+
l_matcher_expression varchar2(4000);
5+
begin
6+
if a_pattern is not null then
7+
l_matcher_expression := ' pattern '''||a_pattern||'''';
8+
if a_modifiers is not null then
9+
l_matcher_expression := l_matcher_expression ||', modifiers '''||a_modifiers||'''';
10+
end if;
11+
end if;
12+
self.name := lower($$plsql_unit) || l_matcher_expression;
13+
self.pattern := a_pattern;
14+
self.modifiers := a_modifiers;
15+
return;
16+
end;
17+
18+
overriding member function run_expectation(a_actual ut_data_value) return boolean is
19+
begin
20+
return
21+
case
22+
when a_actual is of (ut_data_value_varchar2)
23+
then regexp_like(treat(a_actual as ut_data_value_varchar2).value, pattern, modifiers)
24+
when a_actual is of (ut_data_value_clob)
25+
then regexp_like(treat(a_actual as ut_data_value_clob).value, pattern, modifiers)
26+
else (self as ut_expectation).run_expectation(a_actual)
27+
end;
28+
end;
29+
30+
end;
31+
/

source/expectations/match.tps

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
create or replace type match under ut_expectation(
2+
pattern varchar2(4000),
3+
modifiers varchar2(4000),
4+
constructor function match(self in out nocopy match, a_pattern in varchar2, a_modifiers in varchar2 default null) return self as result,
5+
overriding member function run_expectation(a_actual ut_data_value) return boolean
6+
)
7+
/

source/expectations/ut_expectation.tps

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
create or replace type ut_expectation as object(
2-
name varchar2(128),
2+
name varchar2(4000),
33
expected ut_data_value,
44
member function run_expectation(a_actual ut_data_value) return boolean
55
) not final not instantiable

source/install.sql

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ whenever oserror exit failure rollback
3737
@@expectations/be_null.tps
3838
@@expectations/be_true.tps
3939
@@expectations/equal.tps
40+
@@expectations/match.tps
4041
@@assertions/ut_assert_processor.pks
4142
@@assertions/ut_assertion.tps
4243
@@assertions/ut_assertion_anydata.tps
@@ -85,6 +86,7 @@ whenever oserror exit failure rollback
8586
@@expectations/be_null.tpb
8687
@@expectations/be_true.tpb
8788
@@expectations/equal.tpb
89+
@@expectations/match.tpb
8890
@@assertions/ut_assert_processor.pkb
8991
@@assertions/ut_assertion.tpb
9092
@@assertions/ut_assertion_anydata.tpb
@@ -105,14 +107,14 @@ whenever oserror exit failure rollback
105107

106108

107109
prompt Validating installation
108-
select * from user_errors where name not like 'BIN$%' and (name like 'UT%' or name in ('EQUAL','BE_TRUE','BE_FALSE','BE_NULL','BE_NOT_NULL'));
110+
select * from user_errors where name not like 'BIN$%' and (name like 'UT%' or name in ('EQUAL','BE_TRUE','BE_FALSE','BE_NULL','BE_NOT_NULL','MATCH'));
109111

110112
declare
111113
l_cnt integer;
112114
begin
113115
select count(1)
114116
into l_cnt
115-
from user_errors where name not like 'BIN$%' and (name like 'UT%' or name in ('EQUAL','BE_TRUE','BE_FALSE','BE_NULL','BE_NOT_NULL'));
117+
from user_errors where name not like 'BIN$%' and (name like 'UT%' or name in ('EQUAL','BE_TRUE','BE_FALSE','BE_NULL','BE_NOT_NULL','MATCH'));
116118
if l_cnt > 0 then
117119
raise_application_error(-20000, 'Not all sources were successfully installed.');
118120
end if;

source/uninstall.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ drop type ut_assertion;
3030

3131
drop package ut_assert_processor;
3232

33+
drop type match;
34+
3335
drop type equal;
3436

3537
drop type be_true;

0 commit comments

Comments
 (0)