diff --git a/MANIFEST b/MANIFEST index 8e16ab84..153e9e0a 100644 --- a/MANIFEST +++ b/MANIFEST @@ -187,6 +187,7 @@ t/store-simpletriple.t t/term-map.t t/term.t t/treerewrite.t +t/types-general.t t/types-iri.t xt/dawg11-memory.t xt/eval-sparql-star-memory-simpleeval.t diff --git a/Makefile.PL b/Makefile.PL index 909bf48e..c15fa16c 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -42,6 +42,7 @@ requires 'Sub::Util' => 1.40; requires 'Test::Modern' => 0.012; requires 'Test::Moose' => 0; requires 'Test::Roo' => 0; +requires 'Test::TypeTiny' => 0; requires 'Text::CSV' => 0; requires 'Text::Table' => 0; requires 'Try::Tiny' => 0; diff --git a/lib/Types/Attean.pm b/lib/Types/Attean.pm index 82faaf7d..f3a00538 100644 --- a/lib/Types/Attean.pm +++ b/lib/Types/Attean.pm @@ -2,8 +2,16 @@ package Types::Attean; use strict; use warnings; -use Type::Library -base, -declare => qw( AtteanIRI ); -use Types::Standard qw( Str InstanceOf ScalarRef ); +use Type::Library -base, -declare => qw( + AtteanIRI + AtteanBlank + AtteanLiteral + AtteanSubject AtteanPredicate AtteanObject + AtteanGraph + AtteanTriple + AtteanQuad +); +use Types::Standard qw( Str InstanceOf ConsumerOf ScalarRef ); use Types::URI qw( Uri Iri ); use Types::Namespace qw( Namespace ); use Types::Path::Tiny qw( Path ); @@ -57,6 +65,46 @@ L and strings. Additionally, a C can be coerced into a C URI. +=item C<< AtteanBlank >> + +A role type for L. + +=item C<< AtteanLiteral >> + +A role type for L. + +=item C<< AtteanSubject >> + +A role type for a term that can be used as a subject +in a triple or quad +(i.e., L). + +=item C<< AtteanPredicate >> + +A role type for a term that can be used as a predicate +in a triple or quad +(i.e., L). + +=item C<< AtteanObject >> + +A role type for a term that can be used as an object +in a triple or quad +(i.e., L). + +=item C<< AtteanGraph >> + +A role type for a term that can be used as a graph +in a quad +(i.e., L). + +=item C<< AtteanTriple >> + +A role type for L. + +=item C<< AtteanQuad >> + +A role type for L. + =back =head1 OTHER COERCIONS @@ -90,5 +138,44 @@ AtteanIRI->coercion->add_type_coercions( require Attean::IRI; +__PACKAGE__->add_type( + name => AtteanBlank, + parent => ConsumerOf['Attean::API::Blank'] +); + +__PACKAGE__->add_type( + name => AtteanLiteral, + parent => ConsumerOf['Attean::API::Literal'] +); + +__PACKAGE__->add_type( + name => AtteanSubject, + parent => ConsumerOf['Attean::API::BlankOrIRI'] +); + +__PACKAGE__->add_type( + name => AtteanPredicate, + parent => ConsumerOf['Attean::API::IRI'] +); + +__PACKAGE__->add_type( + name => AtteanObject, + parent => ConsumerOf['Attean::API::Term'] +); + +__PACKAGE__->add_type( + name => AtteanGraph, + parent => ConsumerOf['Attean::API::BlankOrIRI'] +); + +__PACKAGE__->add_type( + name => AtteanTriple, + parent => ConsumerOf['Attean::API::Triple'] +); + +__PACKAGE__->add_type( + name => AtteanQuad, + parent => ConsumerOf['Attean::API::Quad'] +); 1; diff --git a/t/types-general.t b/t/types-general.t new file mode 100644 index 00000000..de3d4ffa --- /dev/null +++ b/t/types-general.t @@ -0,0 +1,59 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use Test::More; +use Test::TypeTiny; +use Attean; +use Test::Requires { 'Attean::IRI' => '0.023' }; +use Attean::RDF qw( + iri + blank + literal + triple + quad +); +use Types::Attean qw( + AtteanIRI + AtteanBlank + AtteanLiteral + AtteanSubject AtteanPredicate AtteanObject + AtteanGraph + AtteanTriple + AtteanQuad +); + +my $iri = iri('http://www.example.net/'); +my $blank = blank('b0'); +my $literal = literal('foo'); + +my $triple = triple( $blank, $iri, $literal ); +my $quad = quad( $blank, $iri, $literal, blank('g0') ); + +should_pass( $iri , AtteanIRI ); +should_pass( $blank , AtteanBlank ); +should_pass( $literal, AtteanLiteral ); + +note 'IRI can be in any position'; +should_pass( $iri , AtteanSubject ); +should_pass( $iri , AtteanPredicate ); +should_pass( $iri , AtteanObject ); +should_pass( $iri , AtteanGraph ); + +should_pass( $blank , AtteanSubject ); +should_fail( $blank , AtteanPredicate , 'blank can not be a predicate'); +should_pass( $blank , AtteanObject ); +should_pass( $blank , AtteanGraph ); + +should_fail( $literal, AtteanSubject ); +should_fail( $literal, AtteanPredicate ); +should_pass( $literal, AtteanObject , 'literal can only be an object'); +should_fail( $literal, AtteanGraph ); + +should_pass( $triple , AtteanTriple ); +should_fail( $triple , AtteanQuad , 'triple is not a quad'); + +should_pass( $quad , AtteanTriple , 'quad is also a triple'); +should_pass( $quad , AtteanQuad ); + +done_testing;