Skip to content

Commit 932607f

Browse files
committed
Add a has_type function
1 parent 8b4f0d4 commit 932607f

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

lib/Test/RDF.pm

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use RDF::Trine::Graph;
1212
use Scalar::Util qw/blessed/;
1313

1414
use base 'Test::Builder::Module';
15-
our @EXPORT = qw/are_subgraphs is_rdf is_valid_rdf isomorph_graphs has_subject has_predicate has_object_uri has_uri hasnt_uri has_literal hasnt_literal pattern_target pattern_ok pattern_fail/;
15+
our @EXPORT = qw/are_subgraphs is_rdf is_valid_rdf isomorph_graphs has_subject has_predicate has_object_uri has_type has_uri hasnt_uri has_literal hasnt_literal pattern_target pattern_ok pattern_fail/;
1616

1717

1818
=head1 NAME
@@ -41,6 +41,7 @@ our $VERSION = '1.20';
4141
has_subject($uri_string, $model, 'Subject URI is found');
4242
has_predicate($uri_string, $model, 'Predicate URI is found');
4343
has_object_uri($uri_string, $model, 'Object URI is found');
44+
has_type($uri_string, $model, 'Class URI is found');
4445
has_literal($string, $language, $datatype, $model, 'Literal is found');
4546
hasnt_literal($string, $language, $datatype, $model, 'Literal is not found');
4647
pattern_target($model);
@@ -226,6 +227,26 @@ sub has_object_uri {
226227
return _single_uri_tests($count, $name);
227228
}
228229

230+
231+
=head2 has_type
232+
233+
Check if the string URI passed as first argument is an RDF class
234+
instance in any of the statements given in the model given as second
235+
argument.
236+
237+
=cut
238+
239+
sub has_type {
240+
my ($uri, $model, $name) = @_;
241+
confess 'No valid model given in test' unless (blessed($model) && $model->isa('RDF::Trine::Model'));
242+
my $resource = _resource_uri_checked($uri, $name);
243+
return $resource unless ($resource);
244+
my $count = $model->count_statements(undef,
245+
RDF::Trine::Node::Resource->new('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
246+
$resource);
247+
return _single_uri_tests($count, $name);
248+
}
249+
229250
=head2 has_literal
230251
231252
Check if the string passed as first argument, with corresponding

t/has_uri.t

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use Test::Tester tests => 118;
1+
use Test::Tester tests => 125;
22

33
use Test::RDF;
44

@@ -85,6 +85,17 @@ check_test(
8585
}
8686
);
8787

88+
check_test(
89+
sub {
90+
has_type('http://example.com/Bar', $model, 'Has not correct type URI');
91+
},
92+
{
93+
ok => 0,
94+
name => 'Has not correct type URI',
95+
diag => 'No matching URIs found in model'
96+
}
97+
);
98+
8899
check_test(
89100
sub {
90101
has_object_uri('"This is a Another test"@en', $model, 'Has literal not URI');

0 commit comments

Comments
 (0)