-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added simple SPARQL parsing tests (#55).
- Loading branch information
Showing
1 changed file
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
use v5.14; | ||
use autodie; | ||
use utf8; | ||
use Test::Modern; | ||
use Test::Exception; | ||
use FindBin qw($Bin); | ||
use File::Glob qw(bsd_glob); | ||
use File::Spec; | ||
|
||
use Attean; | ||
use Attean::RDF; | ||
use AtteanX::SPARQL::Constants; | ||
use Type::Tiny::Role; | ||
|
||
{ | ||
my $parser = Attean->get_parser('SPARQL')->new(); | ||
isa_ok($parser, 'AtteanX::Parser::SPARQL'); | ||
my $type = $parser->handled_type; | ||
can_ok($type, 'role'); | ||
is($type->role, 'Attean::Algebra'); | ||
} | ||
|
||
{ | ||
my $data = "ASK {}"; | ||
open(my $fh, '<', \$data); | ||
my $parser = Attean->get_parser('SPARQL')->new(); | ||
my $iter = $parser->parse_iter_from_io($fh); | ||
does_ok($iter, 'Attean::API::Iterator'); | ||
my $a = $iter->next; | ||
does_ok($a, 'Attean::API::Algebra'); | ||
isa_ok($a, 'Attean::Algebra::Ask'); | ||
} | ||
|
||
{ | ||
my $map = URI::NamespaceMap->new(); | ||
my $parser = Attean->get_parser('SPARQL')->new( namespaces => $map ); | ||
my $content = <<'END'; | ||
PREFIX ex: <http://example.org/> | ||
PREFIX foaf: <http://xmlns.com/foaf/0.1/> | ||
SELECT * WHERE { | ||
?s a foaf:Person . | ||
} | ||
END | ||
my ($a) = $parser->parse_list_from_bytes($content); | ||
is_deeply([sort $map->list_prefixes], [qw(ex foaf)]); | ||
my $foaf = $map->namespace_uri('foaf'); | ||
isa_ok($foaf, 'URI::Namespace'); | ||
is($foaf->as_string, 'http://xmlns.com/foaf/0.1/'); | ||
isa_ok($a, 'Attean::Algebra::Project') | ||
} | ||
|
||
subtest 'escaping' => sub { | ||
my $turtle = q[ASK { <s> ex:p "\\"", '\\'', '\\u706b\\U0000661F' \\u007d]; | ||
open(my $fh, '<:encoding(UTF-8)', \$turtle); | ||
my $l = AtteanX::Parser::SPARQLLex->new(); | ||
my $iter = $l->parse_iter_from_io($fh); | ||
|
||
expect($iter->next, KEYWORD, ['ASK']); | ||
expect($iter->next, LBRACE, ['{'],); | ||
expect($iter->next, IRI, ['s'], 'subject'); | ||
expect($iter->next, PREFIXNAME, ['ex:', 'p'], 'predicate'); | ||
expect($iter->next, STRING1D, ['"'], 'double quote'); | ||
expect($iter->next, COMMA, [',']); | ||
expect($iter->next, STRING1S, ["'"], 'single quote'); | ||
expect($iter->next, COMMA, [',']); | ||
expect($iter->next, STRING1S, ["火星"], 'unicode \\u and \\U escapes'); | ||
expect($iter->next, RBRACE, ['}'], 'escaped closing brace'); | ||
}; | ||
|
||
|
||
done_testing(); | ||
|
||
sub expect { | ||
my $token = shift; | ||
my $type = shift; | ||
my $values = shift; | ||
my $name = shift // ''; | ||
if (length($name)) { | ||
$name = "${name}: "; | ||
} | ||
is($token->type, $type, "${name}token type"); | ||
is_deeply($token->args, $values, "${name}token values"); | ||
} |