@@ -445,11 +445,66 @@ module Utils # rubocop:disable Metrics/ModuleLength
445445 } . to raise_error Dry ::Types ::CoercionError
446446 end
447447 end
448+
449+ describe 'Type' do
450+ let ( :invoice_number_type ) { Dry . Types ::Coercible ::Integer . constrained ( gteq : 1000 ) }
451+ let ( :invoice_klass ) { URI . build ( from : klass , resource : 'invoice' , id : invoice_number_type ) }
452+
453+ it 'validates input' do
454+ expect {
455+ invoice_klass . new ( id : '1042' )
456+ } . to_not raise_error
457+
458+ expect {
459+ invoice_klass . new ( id : '23' )
460+ } . to raise_error Dry ::Types ::ConstraintError
461+ end
462+
463+ it 'has uri type' do
464+ type = invoice_klass ::Types ::URI
465+ uri = invoice_klass . new ( id : '1042' )
466+
467+ expect ( type [ uri ] ) . to eq uri
468+ expect ( type [ 'com.example:billing:invoice:1042' ] ) . to eq uri
469+ expect {
470+ type [ 'com.example:billing:invoice:0023' ]
471+ } . to raise_error Dry ::Types ::CoercionError
472+ end
473+
474+ it 'hase string type' do
475+ type = invoice_klass ::Types ::String
476+ uri = invoice_klass . new ( id : '1042' )
477+
478+ expect ( type [ uri ] ) . to eq uri . to_s
479+ expect ( type [ 'com.example:billing:invoice:1042' ] ) . to eq uri . to_s
480+ expect {
481+ type [ 'com.example:billing:invoice:0023' ]
482+ } . to raise_error Dry ::Types ::CoercionError
483+ end
484+
485+ it 'has fallback regex' do
486+ regex = invoice_klass ::REGEX
487+
488+ expect ( regex ) . to match 'com.example:billing:invoice:abc'
489+ expect ( regex ) . to match 'com.example:billing:invoice:123'
490+ expect ( regex ) . to match 'com.example:billing:invoice:123123'
491+ expect ( regex ) . to match 'com.example:billing:invoice:'
492+ expect ( regex ) . to_not match 'com.example:billing:invoicX:abc'
493+ end
494+
495+ it 'can be build from string' do
496+ uri = invoice_klass . new ( id : '1042' )
497+
498+ expect ( invoice_klass . build ( 'com.example:billing:invoice:1042' ) ) . to eq uri
499+
500+ expect {
501+ invoice_klass . build ( 'com.example:billing:invoice:23' )
502+ } . to raise_error Dry ::Types ::ConstraintError
503+ end
504+ end
448505 end
449506 end
450507 end
451508 end
452509 end
453510end
454-
455-
0 commit comments