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

Skip to content

Commit de9b5c1

Browse files
authored
Merge pull request #299 from Pazus/shortcuts
Added shortcuts to all the matchers
2 parents 5aa1e17 + 5a6e5f1 commit de9b5c1

83 files changed

Lines changed: 1176 additions & 288 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/userguide/expectations.md

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ To do that we use concept of expectation and a matcher to perform the check on t
66
Example of unit test procedure body with a single expectation.
77
```sql
88
begin
9+
ut.expect( 'the tested value' ).to_equal('the expected value');
910
ut.expect( 'the tested value' ).to_( equal('the expected value') );
1011
end;
1112
```
@@ -19,6 +20,11 @@ Pseudo-code:
1920
ut.expect( a_actual {data-type} ).not_to( {matcher} );
2021
```
2122

23+
All matchers have shortcuts like:
24+
```sql
25+
ut.expect( a_actual {data-type} ).to_{matcher};
26+
ut.expect( a_actual {data-type} ).not_to_{matcher};
27+
```
2228

2329
# Matchers
2430
utPLSQL provides following matchers to perform checks on the expected and actual values.
@@ -42,8 +48,11 @@ Validates that the actual value is between the lower and upper bound.
4248
Example:
4349
```sql
4450
begin
51+
ut.expect( a_actual => 3 ).to_be_between( a_lower_bound => 1, a_upper_bound => 3 );
52+
ut.expect( 3 ).to_be_between( 1, 3 );
53+
--or
4554
ut.expect( a_actual => 3 ).to_( be_between( a_lower_bound => 1, a_upper_bound => 3 ) );
46-
ut.expect( 3 ).to_( be_between( 1, 3 ) );
55+
ut.expect( 3 ).to_( be_between( 1, 3 ) );
4756
end;
4857
```
4958

@@ -56,6 +65,8 @@ procedure test_if_cursor_is_empty is
5665
l_cursor sys_refcursor;
5766
begin
5867
open l_cursor for select * from dual where 1 = 0;
68+
ut.expect( l_cursor ).to_be_empty();
69+
--or
5970
ut.expect( l_cursor ).to_( be_empty() );
6071
end;
6172
```
@@ -68,6 +79,8 @@ Unary matcher that validates if the provided value is false.
6879
Usage:
6980
```sql
7081
begin
82+
ut.expect( ( 1 = 0 ) ).to_be_false();
83+
--or
7184
ut.expect( ( 1 = 0 ) ).to_( be_false() );
7285
end;
7386
```
@@ -78,6 +91,8 @@ Allows to check if the actual value is greater or equal than the expected.
7891
Usage:
7992
```sql
8093
begin
94+
ut.expect( sysdate ).to_be_greater_or_equal( sysdate - 1 );
95+
--or
8196
ut.expect( sysdate ).to_( be_greater_or_equal( sysdate - 1 ) );
8297
end;
8398
```
@@ -88,6 +103,8 @@ Allows to check if the actual value is greater than the expected.
88103
Usage:
89104
```sql
90105
begin
106+
ut.expect( 2 ).to_be_greater_than( 1 );
107+
--or
91108
ut.expect( 2 ).to_( be_greater_than( 1 ) );
92109
end;
93110
```
@@ -98,6 +115,8 @@ Allows to check if the actual value is less or equal than the expected.
98115
Usage:
99116
```sql
100117
begin
118+
ut.expect( 3 ).to_be_less_or_equal( 3 );
119+
--or
101120
ut.expect( 3 ).to_( be_less_or_equal( 3 ) );
102121
end;
103122
```
@@ -108,6 +127,8 @@ Allows to check if the actual value is less than the expected.
108127
Usage:
109128
```sql
110129
begin
130+
ut.expect( 3 ).to_be_less_than( 2 );
131+
--or
111132
ut.expect( 3 ).to_( be_less_than( 2 ) );
112133
end;
113134
```
@@ -119,6 +140,9 @@ Validates that the actual value is like the expected expression.
119140
Usage:
120141
```sql
121142
begin
143+
ut.expect( 'Lorem_impsum' ).to_be_like( a_mask => '%rem\_%', a_escape_char => '\' );
144+
ut.expect( 'Lorem_impsum' ).to_be_like( '%rem\_%', '\' );
145+
--or
122146
ut.expect( 'Lorem_impsum' ).to_( be_like( a_mask => '%rem\_%', a_escape_char => '\' ) );
123147
ut.expect( 'Lorem_impsum' ).to_( be_like( '%rem\_%', '\' ) );
124148
end;
@@ -133,7 +157,11 @@ Unary matcher that validates if the actual value is not null.
133157
Usage:
134158
```sql
135159
begin
160+
ut.expect( to_clob('ABC') ).to_be_not_null();
161+
--or
136162
ut.expect( to_clob('ABC') ).to_( be_not_null() );
163+
--or
164+
ut.expect( to_clob('ABC') ).not_to( be_null() );
137165
end;
138166
```
139167
@@ -143,6 +171,8 @@ Unary matcher that validates if the actual value is null.
143171
Usage:
144172
```sql
145173
begin
174+
ut.expect( cast(null as varchar2(100)) ).to_be_null();
175+
--or
146176
ut.expect( cast(null as varchar2(100)) ).to_( be_null() );
147177
end;
148178
```
@@ -154,6 +184,8 @@ Unary matcher that validates if the provided value is false.
154184
Usage:
155185
```sql
156186
begin
187+
ut.expect( ( 1 = 1 ) ).to_be_true();
188+
--or
157189
ut.expect( ( 1 = 1 ) ).to_( be_true() );
158190
end;
159191
```
@@ -171,6 +203,9 @@ procedure check_if_cursors_are_equal is
171203
x sys_refcursor;
172204
y sys_refcursor;
173205
begin
206+
ut.expect( 'a dog' ).to_equal( 'a dog' );
207+
ut.expect( a_actual => y ).to_equal( a_expected => x, a_nulls_are_equal => true );
208+
--or
174209
ut.expect( 'a dog' ).to_( equal( 'a dog' ) );
175210
ut.expect( a_actual => y ).to_( equal( a_expected => x, a_nulls_are_equal => true ) );
176211
end;
@@ -193,7 +228,7 @@ procedure test_cursors_skip_columns is
193228
begin
194229
open x for select 'text' ignore_me, d.* from user_tables d;
195230
open y for select sysdate "ADate", d.* from user_tables d;
196-
ut.expect( a_actual => y ).to_( equal( a_expected => x, a_exclude => 'IGNORE_ME,ADate' ) );
231+
ut.expect( a_actual => y ).to_equal( a_expected => x, a_exclude => 'IGNORE_ME,ADate' );
197232
end;
198233
```
199234

@@ -261,8 +296,8 @@ create or replace package body test_get_events is
261296
l_actual := get_events(gc_event_date-1, gc_event_date+1);
262297
ut.reset_nls();
263298
264-
ut.expect(l_actual).to_( equal(l_expected) );
265-
ut.expect(l_actual).not_to( equal(l_expected_bad_date) );
299+
ut.expect(l_actual).to_equal(l_expected);
300+
ut.expect(l_actual).not_to_equal(l_expected_bad_date);
266301
end;
267302
268303
end;
@@ -309,7 +344,7 @@ create or replace package body demo_dept as
309344
begin
310345
v_expected := department('HR');
311346
v_actual := department('IT');
312-
ut.expect( anydata.convertObject(v_expected) ).to_( equal( anydata.convertObject(v_actual) ) );
347+
ut.expect( anydata.convertObject(v_expected) ).to_equal( anydata.convertObject(v_actual) );
313348
end;
314349
315350
procedure test_department is
@@ -318,7 +353,7 @@ create or replace package body demo_dept as
318353
begin
319354
v_expected := departments(department('HR'));
320355
v_actual := departments(department('IT'));
321-
ut.expect( anydata.convertCollection(v_expected) ).to_( equal( anydata.convertCollection(v_actual) ) );
356+
ut.expect( anydata.convertCollection(v_expected) ).to_equal( anydata.convertCollection(v_actual) );
322357
end;
323358
324359
end;
@@ -333,6 +368,9 @@ Validates that the actual value is matching the expected regular expression.
333368
Usage:
334369
```sql
335370
begin
371+
ut.expect( a_actual => '123-456-ABcd' ).to_match( a_pattern => '\d{3}-\d{3}-[a-z]', a_modifiers => 'i' );
372+
ut.expect( 'some value' ).to_match( '^some.*' );
373+
--or
336374
ut.expect( a_actual => '123-456-ABcd' ).to_( match( a_pattern => '\d{3}-\d{3}-[a-z]', a_modifiers => 'i' ) );
337375
ut.expect( 'some value' ).to_( match( '^some.*' ) );
338376
end;
@@ -370,13 +408,15 @@ Expectations provide a very convenient way to check for a negative of the expect
370408
Syntax of check for matcher evaluating to true:
371409
```sql
372410
begin
411+
ut.expect( a_actual {data-type} ).to_{matcher};
373412
ut.expect( a_actual {data-type} ).to_( {matcher} );
374413
end;
375414
```
376415

377416
Syntax of check for matcher evaluating to false:
378417
```sql
379-
begin
418+
begin
419+
ut.expect( a_actual {data-type} ).not_to_{matcher};
380420
ut.expect( a_actual {data-type} ).not_to( {matcher} );
381421
end;
382422
```

examples/RunWithDocumentationReporter.sql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@ create or replace package body demo_doc_reporter1 is
2828

2929
procedure test_without_name is
3030
begin
31-
ut.expect(1).to_(equal(1));
31+
ut.expect(1).to_equal(1);
3232
end;
3333

3434
procedure failing_test is
3535
begin
36-
ut.expect(1).to_(equal(2));
36+
ut.expect(1).to_equal(2);
3737
end;
3838
procedure failing_no_name is
3939
begin
40-
ut.expect(sysdate).to_(equal(to_char(sysdate)));
40+
ut.expect(sysdate).to_equal(to_char(sysdate));
4141
end;
4242
procedure failing_exception_raised is
4343
l_date date;
@@ -77,7 +77,7 @@ create or replace package body suite_package_without_name is
7777

7878
procedure passing_test2 is
7979
begin
80-
ut.expect(1).to_(equal(1));
80+
ut.expect(1).to_equal(1);
8181
end;
8282
end;
8383
/

examples/award_bonus/test_award_bonus.pkg

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,21 @@ create or replace package body test_award_bonus as
4747
select salary as new_salary
4848
from employees_test where employee_id = gc_test_employee;
4949

50-
ut.expect( results ).to_( equal( expected ) );
50+
ut.expect( results ).to_equal( expected );
5151

5252
open results for
5353
select * from employees_test where employee_id != gc_test_employee;
5454

55-
ut.expect( results ).to_( equal( not_affected ) );
55+
ut.expect( results ).to_equal( not_affected );
5656
end;
5757

5858
procedure fail_on_null_bonus is
5959
begin
6060
award_bonus(emp_id => gc_test_employee, sales_amt => null);
61-
ut.expect( sqlcode ).not_to( equal( 0 ) );
61+
ut.expect( sqlcode ).not_to_equal( 0 );
6262
exception
6363
when others then
64-
ut.expect( sqlcode ).not_to( equal( 0 ) );
64+
ut.expect( sqlcode ).not_to_equal( 0 );
6565
end;
6666

6767
procedure add_employee( emp_id number, comm_pct number, sal number ) is

examples/between_string/test_betwnstr.pkg

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,22 @@ create or replace package body test_betwnstr as
2929

3030
procedure zero_start_position is
3131
begin
32-
ut.expect( betwnstr( '1234567', 0, 5 ) ).to_( equal('12345') );
32+
ut.expect( betwnstr( '1234567', 0, 5 ) ).to_equal('12345');
3333
end;
3434

3535
procedure big_end_position is
3636
begin
37-
ut.expect( betwnstr( '1234567', 0, 500 ) ).to_( equal('1234567') );
37+
ut.expect( betwnstr( '1234567', 0, 500 ) ).to_equal('1234567');
3838
end;
3939

4040
procedure null_string is
4141
begin
42-
ut.expect( betwnstr( null, 2, 5 ) ).to_( be_null );
42+
ut.expect( betwnstr( null, 2, 5 ) ).to_be_null;
4343
end;
4444

4545
procedure disabled_test is
4646
begin
47-
ut.expect( betwnstr( null, null, null) ).not_to( be_null );
47+
ut.expect( betwnstr( null, null, null) ).not_to_be_null;
4848
end;
4949

5050
end;

examples/demo_expectations.pck

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,12 @@ create or replace package body demo_expectations is
546546
ut.expect(sysdate).not_to( be_between(sysdate+1,sysdate+2) );
547547
ut.expect( 1 ).not_to( equal( 2 ) );
548548
ut.expect( 'asd' ).not_to( be_like('z%q') );
549+
550+
ut.expect( sysdate ).not_to_be_null();
551+
ut.expect( to_char(null) ).not_to_be_not_null();
552+
ut.expect(sysdate).not_to_be_between(sysdate+1,sysdate+2);
553+
ut.expect( 1 ).not_to_equal( 2 );
554+
ut.expect( 'asd' ).not_to_be_like('z%q');
549555
end;
550556

551557
end;

examples/demo_of_expectations/demo_equal_matcher.sql

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ create or replace package body demo_equal_matcher as
6363
l_expected := demo_department('Sales');
6464
--get the actual data
6565
l_actual := demo_department('Sales');
66-
ut.expect(anydata.convertObject(l_actual)).to_(equal(anydata.convertObject(l_expected))
66+
ut.expect(anydata.convertObject(l_actual)).to_equal(anydata.convertObject(l_expected)
6767
);
6868
end;
6969

@@ -75,7 +75,7 @@ create or replace package body demo_equal_matcher as
7575
l_expected := demo_department('Sales');
7676
--get the actual data
7777
-- nothing done
78-
ut.expect(anydata.convertObject(l_actual)).to_(equal(anydata.convertObject(l_expected))
78+
ut.expect(anydata.convertObject(l_actual)).to_equal(anydata.convertObject(l_expected)
7979
);
8080
end;
8181

@@ -84,24 +84,21 @@ create or replace package body demo_equal_matcher as
8484
l_actual demo_department;
8585
begin
8686
l_actual := demo_department('Sales');
87-
ut.expect(anydata.convertObject(l_actual)).to_(equal(anydata.convertObject(l_expected))
88-
);
87+
ut.expect(anydata.convertObject(l_actual)).to_equal(anydata.convertObject(l_expected));
8988
end;
9089

9190
procedure object_compare_null_both_ok is
9291
l_expected demo_department;
9392
l_actual demo_department;
9493
begin
95-
ut.expect(anydata.convertObject(l_actual)).to_(equal(anydata.convertObject(l_expected))
96-
);
94+
ut.expect(anydata.convertObject(l_actual)).to_equal(anydata.convertObject(l_expected));
9795
end;
9896

9997
procedure object_compare_null_both_fail is
10098
l_expected demo_department;
10199
l_actual demo_department;
102100
begin
103-
ut.expect(anydata.convertObject(l_actual)).to_(equal(anydata.convertObject(l_expected),false)
104-
);
101+
ut.expect(anydata.convertObject(l_actual)).to_equal(anydata.convertObject(l_expected),false);
105102
end;
106103

107104

@@ -113,8 +110,7 @@ create or replace package body demo_equal_matcher as
113110
l_expected := demo_department('Sales');
114111
--get the actual data
115112
l_actual := demo_department('HR');
116-
ut.expect(anydata.convertObject(l_actual)).to_(equal(anydata.convertObject(l_expected))
117-
);
113+
ut.expect(anydata.convertObject(l_actual)).to_equal(anydata.convertObject(l_expected));
118114
end;
119115

120116
procedure object_compare_different_type is
@@ -125,8 +121,7 @@ create or replace package body demo_equal_matcher as
125121
l_expected := demo_department('Sales');
126122
--get the actual data
127123
l_actual := demo_department_new('Sales');
128-
ut.expect(anydata.convertObject(l_actual)).to_(equal(anydata.convertObject(l_expected))
129-
);
124+
ut.expect(anydata.convertObject(l_actual)).to_equal(anydata.convertObject(l_expected));
130125
end;
131126

132127
end;

0 commit comments

Comments
 (0)