@@ -34,10 +34,20 @@ module Utils # rubocop:disable Metrics/ModuleLength
3434 expect ( klass . resource ) . to eq 'order'
3535 end
3636
37+ it 'for single ID' do
38+ klass = URI . build ( schema : 'com.example' , context : 'billing' , resource : 'invoice' , id : 'final' )
39+ expect ( klass . id ) . to eq 'final'
40+ end
41+
3742 it 'for a list of IDs' do
3843 klass = URI . build ( schema : 'com.example' , context : 'billing' , resource : 'invoice' , id : %w[ final past_due ] )
3944 expect ( klass . id ) . to eq %w[ final past_due ]
4045 end
46+
47+ it 'for regex' do
48+ klass = URI . build ( schema : 'com.example' , context : 'billing' , resource : 'invoice' , id : /I-\d {3}/ )
49+ expect ( klass . id ) . to eq ( /I-\d {3}/ )
50+ end
4151 end
4252
4353 describe 'URI regex' do
@@ -73,6 +83,18 @@ module Utils # rubocop:disable Metrics/ModuleLength
7383 it { is_expected . to match 'com.example:billing:invoice:final' }
7484 it { is_expected . to_not match 'com.example:billing:invoice:pro_forma' }
7585 end
86+
87+ context 'custom regex' do
88+ subject ( :regex ) {
89+ URI . build ( schema : 'com.example' , context : 'billing' , resource : 'invoice' , id : /I-\d {3}/ ) ::REGEX
90+ }
91+
92+ it { is_expected . to match 'com.example:billing:invoice:I-123' }
93+ it { is_expected . to_not match 'com.example:billing:invoice:i-123' }
94+ it { is_expected . to_not match 'com.example:billing:invoice:123' }
95+ it { is_expected . to_not match 'com.example:billing:invoice:I-abc' }
96+ it { is_expected . to_not match 'com.example:billing:invoice:I-1234' }
97+ end
7698 end
7799
78100 describe 'URI instance' do
@@ -308,6 +330,44 @@ module Utils # rubocop:disable Metrics/ModuleLength
308330
309331 end
310332
333+ describe 'Fixed URI' do
334+ subject ( :klass ) {
335+ URI . build (
336+ schema : 'com.example' ,
337+ context : 'billing' ,
338+ resource : 'invoice' ,
339+ id : 'final'
340+ )
341+ }
342+
343+ it 'only accepts fixed URI' do
344+ uri = klass . new (
345+ schema : 'com.example' ,
346+ context : 'billing' ,
347+ resource : 'invoice' ,
348+ id : 'final'
349+ )
350+
351+ expect ( klass . new ( id : 'final' ) ) . to eq uri
352+
353+ expect {
354+ klass . new ( id : 'not_final' )
355+ } . to raise_error Dry ::Types ::ConstraintError
356+
357+ expect {
358+ klass . new ( resource : 'not_invoice' , id : 'final' )
359+ } . to raise_error Dry ::Types ::ConstraintError
360+
361+ expect {
362+ klass . new ( context : 'not_billing' , id : 'final' )
363+ } . to raise_error Dry ::Types ::ConstraintError
364+
365+ expect {
366+ klass . new ( schema : 'not_example.com' , id : 'final' )
367+ } . to raise_error Dry ::Types ::ConstraintError
368+ end
369+ end
370+
311371 describe 'Attribute Types' do
312372 subject ( :klass ) {
313373 URI . build (
@@ -349,6 +409,42 @@ module Utils # rubocop:disable Metrics/ModuleLength
349409 } . to raise_error Dry ::Types ::ConstraintError
350410 end
351411 end
412+
413+ describe 'Regexp' do
414+ let ( :invoice_klass ) { URI . build ( from : klass , resource : 'invoice' , id : /I-\d {3}/ ) }
415+
416+ it 'validates input' do
417+ expect {
418+ invoice_klass . new ( id : 'I-123' )
419+ } . to_not raise_error
420+
421+ expect {
422+ invoice_klass . new ( id : 'X-123' )
423+ } . to raise_error Dry ::Types ::ConstraintError
424+ end
425+
426+ it 'has uri type' do
427+ type = invoice_klass ::Types ::URI
428+ uri = invoice_klass . new ( id : 'I-123' )
429+
430+ expect ( type [ uri ] ) . to eq uri
431+ expect ( type [ 'com.example:billing:invoice:I-123' ] ) . to eq uri
432+ expect {
433+ type [ 'com.example:billing:invoice:<other>' ]
434+ } . to raise_error Dry ::Types ::CoercionError
435+ end
436+
437+ it 'has string type' do
438+ type = invoice_klass ::Types ::String
439+ uri = invoice_klass . new ( id : 'I-123' )
440+
441+ expect ( type [ uri ] ) . to eq uri . to_s
442+ expect ( type [ 'com.example:billing:invoice:I-123' ] ) . to eq uri . to_s
443+ expect {
444+ type [ 'com.example:billing:invoice:<other>' ]
445+ } . to raise_error Dry ::Types ::CoercionError
446+ end
447+ end
352448 end
353449 end
354450 end
0 commit comments