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

Skip to content

Commit dd72803

Browse files
committed
Implemented be_like matcher. Refactored to_be_like matcher. Added tests for be_like.
1 parent 9fff9d0 commit dd72803

11 files changed

Lines changed: 142 additions & 23 deletions

File tree

examples/demo_expectations.pck

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@ create or replace package demo_expectations is
3333
procedure demo_to_match_failure;
3434

3535
-- %test(demo of success for to_match expectation)
36-
procedure demo_to_matchl_success;
36+
procedure demo_to_match_success;
37+
38+
-- %test(demo of failure for to_be_like expectation)
39+
procedure demo_to_be_like_failure;
40+
41+
-- %test(demo of success for to_be_like expectation)
42+
procedure demo_to_be_like_success;
3743

3844
end;
3945
/
@@ -415,12 +421,12 @@ create or replace package body demo_expectations is
415421
ut.expect( l_clob ).to_( match('^Stephen$') );
416422
ut.expect( l_clob ).to_( match('^Stephen$', 'i') ); --case insensitive
417423

418-
ut.expect( sysdate ).to_( match('^Stephen$', 'i') ); --case insensitive
419-
ut.expect( 12345 ).to_( match('^Stephen$', 'i') ); --case insensitive
424+
ut.expect( sysdate ).to_( match(sysdate, 'i') ); --case insensitive
425+
ut.expect( 12345 ).to_( match(12345, 'i') ); --case insensitive
420426

421427
end;
422428

423-
procedure demo_to_matchl_success is
429+
procedure demo_to_match_success is
424430
l_clob clob := rpad('a',32767,'a')||'STEPHEN';
425431
begin
426432
ut.expect( 'Hi, I am Stephen' ).to_match('Stephen$');
@@ -433,5 +439,35 @@ create or replace package body demo_expectations is
433439
ut.expect( l_clob ).to_( match('Stephen$', 'i') ); --case insensitive
434440
end;
435441

442+
procedure demo_to_be_like_failure is
443+
l_clob clob := rpad('a',32767,'a')||'Stephen';
444+
begin
445+
ut.expect( 'STEPHEN' ).to_be_like('Stephen');
446+
ut.expect( 'Stephen ' ).to_be_like('Stephen\_', '\'); --escape wildcards with '\'
447+
ut.expect( 'stephen' ).to_( be_like('%Stephen%') );
448+
ut.expect( 'Stephen ' ).to_( be_like('Stephen^_', '^') ); --escape wildcards with '^'
449+
ut.expect( l_clob ).to_be_like('%stephen');
450+
ut.expect( l_clob ).to_be_like('%Stephe\_', '\'); --escape wildcards with '\'
451+
ut.expect( l_clob ).to_( be_like('%stephen') );
452+
ut.expect( l_clob ).to_( be_like('%Stephe\_', '\') ); --escape wildcards with '\'
453+
454+
ut.expect( sysdate ).to_( be_like(sysdate) ); --case insensitive
455+
ut.expect( 12345 ).to_( be_like(12345) ); --case insensitive
456+
457+
end;
458+
459+
procedure demo_to_be_like_success is
460+
l_clob clob := rpad('a',32767,'a')||'STEPHEN_';
461+
begin
462+
ut.expect( 'Hi, I am Stephen' ).to_be_like('%Stephen');
463+
ut.expect( 'stephen_' ).to_be_like('_tephen\_', '\'); --escape wildcards with '\'
464+
ut.expect( 'Hi, I am Stephen' ).to_( be_like('%Stephen') );
465+
ut.expect( 'stephen_' ).to_( be_like('_tephen^_', '^')); --escape wildcards with '^'
466+
ut.expect( l_clob ).to_be_like('a%a_TE%');
467+
ut.expect( l_clob ).to_be_like('a%a_TE%\_', '\'); --escape wildcards with '\'
468+
ut.expect( l_clob ).to_( be_like('a%a_TE%') );
469+
ut.expect( l_clob ).to_( be_like('a%a_TE%\_', '\') ); --escape wildcards with '\'
470+
end;
471+
436472
end;
437473
/

source/assertions/ut_assertion_clob.tpb

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,9 @@ create or replace type body ut_assertion_clob as
77
end;
88

99
member procedure to_be_like(self in ut_assertion_clob, a_mask in varchar2, a_escape_char in varchar2 := null) is
10-
l_condition boolean;
11-
l_escape_msg varchar2(100) := case when a_escape_char is not null then ' using escape '''||a_escape_char||'''' end;
1210
begin
13-
if a_escape_char is not null then
14-
l_condition := treat(self.actual_data as ut_data_value_clob).value like a_mask escape a_escape_char;
15-
else
16-
l_condition := treat(self.actual_data as ut_data_value_clob).value like a_mask;
17-
end if;
18-
self.add_assert_result(l_condition, 'to be like', ut_utils.to_string(a_mask)||l_escape_msg);
11+
ut_utils.debug_log('ut_assertion_clob.to_be_like(self in ut_assertion, a_mask in varchar2, a_escape_char in varchar2 default null)');
12+
self.to_( be_like(a_mask, a_escape_char) );
1913
end;
2014

2115
member procedure to_match(self in ut_assertion_clob, a_pattern in varchar2, a_modifiers in varchar2 default null) is

source/assertions/ut_assertion_varchar2.tpb

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,9 @@ create or replace type body ut_assertion_varchar2 as
77
end;
88

99
member procedure to_be_like(self in ut_assertion_varchar2, a_mask in varchar2, a_escape_char in varchar2 := null) is
10-
l_condition boolean;
11-
l_escape_msg varchar2(100) := case when a_escape_char is not null then ' using escape '''||a_escape_char||'''' end;
1210
begin
13-
if a_escape_char is not null then
14-
l_condition := treat(self.actual_data as ut_data_value_varchar2).value like a_mask escape a_escape_char;
15-
else
16-
l_condition := treat(self.actual_data as ut_data_value_varchar2).value like a_mask;
17-
end if;
18-
self.add_assert_result(l_condition, 'to be like', ut_utils.to_string(a_mask)||l_escape_msg);
11+
ut_utils.debug_log('ut_assertion_varchar2.to_be_like(self in ut_assertion, a_mask in varchar2, a_escape_char in varchar2 default null)');
12+
self.to_( be_like(a_mask, a_escape_char) );
1913
end;
2014

2115
member procedure to_match(self in ut_assertion_varchar2, a_pattern in varchar2, a_modifiers in varchar2 default null) is

source/expectations/be_like.tpb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
create or replace type body be_like as
2+
3+
constructor function be_like(self in out nocopy be_like, a_mask in varchar2, a_escape_char in varchar2 := null) return self as result is
4+
begin
5+
if a_mask is not null then
6+
self.additional_info := ''''||a_mask||'''';
7+
if a_escape_char is not null then
8+
self.additional_info := self.additional_info ||' escape '''||a_escape_char||'''';
9+
end if;
10+
end if;
11+
self.name := lower($$plsql_unit);
12+
self.mask := a_mask;
13+
self.escape_char := a_escape_char;
14+
return;
15+
end;
16+
17+
overriding member function run_expectation(self in out nocopy be_like, a_actual ut_data_value) return boolean is
18+
l_value clob;
19+
begin
20+
if a_actual is of (ut_data_value_varchar2) then
21+
l_value := treat(a_actual as ut_data_value_varchar2).value;
22+
elsif a_actual is of (ut_data_value_clob) then
23+
l_value := treat(a_actual as ut_data_value_clob).value;
24+
end if;
25+
26+
return
27+
case
28+
when a_actual is of (ut_data_value_varchar2, ut_data_value_clob)
29+
then
30+
case
31+
when escape_char is not null
32+
then l_value like mask escape escape_char
33+
else l_value like mask
34+
end
35+
else (self as ut_expectation).run_expectation(a_actual)
36+
end;
37+
end;
38+
39+
end;
40+
/

source/expectations/be_like.tps

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

source/install.sql

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ whenever oserror exit failure rollback
3333
@@expectation_data_values/ut_data_value_varchar2.tps
3434
@@expectations/ut_expectation.tps
3535
@@expectations/be_false.tps
36+
@@expectations/be_like.tps
3637
@@expectations/be_not_null.tps
3738
@@expectations/be_null.tps
3839
@@expectations/be_true.tps
@@ -82,6 +83,7 @@ whenever oserror exit failure rollback
8283
@@expectation_data_values/ut_data_value_varchar2.tpb
8384
@@expectations/ut_expectation.tpb
8485
@@expectations/be_false.tpb
86+
@@expectations/be_like.tpb
8587
@@expectations/be_not_null.tpb
8688
@@expectations/be_null.tpb
8789
@@expectations/be_true.tpb
@@ -107,14 +109,14 @@ whenever oserror exit failure rollback
107109

108110

109111
prompt Validating installation
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'));
112+
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','BE_LIKE'));
111113

112114
declare
113115
l_cnt integer;
114116
begin
115117
select count(1)
116118
into l_cnt
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'));
119+
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','BE_LIKE'));
118120
if l_cnt > 0 then
119121
raise_application_error(-20000, 'Not all sources were successfully installed.');
120122
end if;

source/uninstall.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ drop type be_null;
4040

4141
drop type be_not_null;
4242

43+
drop type be_like;
44+
4345
drop type be_false;
4446

4547
drop type ut_expectation;

tests/RunAll.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ create table ut$test_table (val varchar2(1));
2121
@@lib/RunTest.sql asssertions/ut.expect.to_be_false.GivesFailureWhenExpessionIsNull.sql
2222
@@lib/RunTest.sql asssertions/ut.expect.to_be_false.GivesFailureWhenExpessionIsTrue.sql
2323
@@lib/RunTest.sql asssertions/ut.expect.to_be_false.GivesSuccessWhenExpessionIsFalse.sql
24+
@@lib/RunTest.sql asssertions/ut.expect.to_be_like.sql
2425
@@lib/RunTest.sql asssertions/ut.expect.to_be_not_null.GivesFailureWhenActualIsNull.sql
2526
@@lib/RunTest.sql asssertions/ut.expect.to_be_not_null.GivesSuccessWhenActualIsNotNull.sql
2627
@@lib/RunTest.sql asssertions/ut.expect.to_be_null.anydata.GivesSuccessWhenAnydataIsNull.sql
@@ -48,7 +49,7 @@ create table ut$test_table (val varchar2(1));
4849
@@lib/RunTest.sql asssertions/ut.expect.to_equal.PutsNullIntoStringValueWhenActualIsNull.sql
4950
@@lib/RunTest.sql asssertions/ut.expect.to_equal.PutsNullIntoStringValueWhenExpectedIsNull.sql
5051
@@lib/RunTest.sql asssertions/ut.expect.to_equal.with_text.GivesTheProvidedTextAsMessage.sql
51-
@@lib/RunTest.sql asssertions/ut.expect.to_match.GivesSuccessWhenStringMatchesRegex.sql
52+
@@lib/RunTest.sql asssertions/ut.expect.to_match.sql
5253
@@lib/RunTest.sql asssertions/ut_assert_processor.nulls_are_equal.raisesExceptionWhenTryingToSetNullValue.sql
5354
@@lib/RunTest.sql ut_annotations/ut_annotations.parse_package_annotations.ParseAnnotationMixedWithWrongBeforeProcedure.sql
5455
@@lib/RunTest.sql ut_annotations/ut_annotations.parse_package_annotations.ParseAnnotationNotBeforeProcedure.sql
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
declare
2+
l_actual &1 := &2;
3+
l_pattern varchar2(32767) := '&3';
4+
l_escape_char varchar2(32767) := '&4';
5+
l_result integer;
6+
begin
7+
--Act
8+
ut.expect( l_actual ).to_( be_like(l_pattern, l_escape_char) );
9+
l_result := ut_assert_processor.get_aggregate_asserts_result();
10+
--Assert
11+
if l_result = &5 then
12+
:test_result := ut_utils.tr_success;
13+
else
14+
dbms_output.put_line('expected: '''||l_actual||''', to be like '''||l_pattern||''' escape'''||l_escape_char||'''' );
15+
end if;
16+
end;
17+
/
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
PROMPT Gives success when varchar2 is matching pattern without escape char
2+
@@asssertions/common/ut.expect.to_be_like.common.sql 'varchar2(100)' '''Stephen_King''' 'Ste__en%' '' 'ut_utils.tr_success'
3+
4+
PROMPT Gives success when varchar2 is matching pattern with escape char
5+
@@asssertions/common/ut.expect.to_be_like.common.sql 'varchar2(100)' '''Stephen_King''' 'Ste__en\_K%' '\' 'ut_utils.tr_success'
6+
7+
PROMPT Gives success when clob is matching pattern without escape char
8+
@@asssertions/common/ut.expect.to_be_like.common.sql 'clob' 'rpad(''a'',32767,''a'')||''Stephen_King''' 'a%Ste__en%' '' 'ut_utils.tr_success'
9+
10+
PROMPT Gives success when clob is matching pattern with escape char
11+
@@asssertions/common/ut.expect.to_be_like.common.sql 'clob' 'rpad(''a'',32767,''a'')||''Stephen_King''' 'a%Ste__en\_K%' '\' 'ut_utils.tr_success'
12+
13+
PROMPT Gives failure when varchar2 is not matching pattern without escape char
14+
@@asssertions/common/ut.expect.to_be_like.common.sql 'varchar2(100)' '''Stephen_King''' 'Ste_en%' '' 'ut_utils.tr_failure'
15+
16+
PROMPT Gives failure when varchar2 is not matching pattern with escape char
17+
@@asssertions/common/ut.expect.to_be_like.common.sql 'varchar2(100)' '''Stephen_King''' 'Stephe\__%' '\' 'ut_utils.tr_failure'
18+
19+
PROMPT Gives failure when clob is not matching pattern without escape char
20+
@@asssertions/common/ut.expect.to_be_like.common.sql 'clob' 'rpad(''a'',32767,''a'')||''Stephen_King''' 'a%Ste_en%' '' 'ut_utils.tr_failure'
21+
22+
PROMPT Gives failure when clob is not matching pattern with escape char
23+
@@asssertions/common/ut.expect.to_be_like.common.sql 'clob' 'rpad(''a'',32767,''a'')||''Stephen_King''' 'a%Stephe\__%' '\' 'ut_utils.tr_failure'
24+
25+
PROMPT Gives failure when trying to use matcher with number datatype
26+
@@asssertions/common/ut.expect.to_be_like.common.sql 'number' '12345' 'a%Stephe\__%' '\' 'ut_utils.tr_failure'

0 commit comments

Comments
 (0)