Adds and makes pass W3C test suite for NTriples syntax

pull/10/head
Tpt 7 years ago
parent d1be3b1854
commit 9994ffe645
  1. 12
      src/rio/ntriples/ntriples_grammar.rustpeg
  2. 66
      tests/rdf_test_cases.rs

@ -37,7 +37,7 @@ literal -> Literal =
//[144s] //[144s]
LANGTAG -> &'input str = "@" l: $([a-zA-Z]+ ("-" [a-zA-Z0-9]+)*) { LANGTAG -> &'input str = "@" l:$([a-zA-Z]+ ("-" [a-zA-Z0-9]+)*) {
l l
} }
@ -45,23 +45,23 @@ LANGTAG -> &'input str = "@" l: $([a-zA-Z]+ ("-" [a-zA-Z0-9]+)*) {
EOL = [\r\n]+ EOL = [\r\n]+
//[8] //[8]
IRIREF -> Url = "<" _ i: ((_IRIREF_simple_char / UCHAR)*) _ ">" {? IRIREF -> Url = "<" _ i:((_IRIREF_simple_char / UCHAR)*) _ ">" {?
let s = String::from_iter(i.into_iter()); let s = String::from_iter(i.into_iter());
match Url::parse(&s) { match Url::parse(&s) {
Ok(url) => Ok(url), Ok(url) => Ok(url),
Err(error) => Err("IRI parsing failed") Err(error) => Err("IRI parsing failed")
} }
} }
_IRIREF_simple_char -> char = c: $([^\u{00}-\u{20}<>"{}|^\u{60}\u{5c}]) { c.chars().next().unwrap() } _IRIREF_simple_char -> char = c:$([^\u{00}-\u{20}<>"{}|^\u{60}\u{5c}]) { c.chars().next().unwrap() }
//[9] //[9]
STRING_LITERAL_QUOTE -> String = "\"" l: ((STRING_LITERAL_QUOTE_simple_char / ECHAR / UCHAR)*) "\"" { STRING_LITERAL_QUOTE -> String = "\"" l:((STRING_LITERAL_QUOTE_simple_char / ECHAR / UCHAR)*) "\"" {
l.into_iter().collect() l.into_iter().collect()
} }
STRING_LITERAL_QUOTE_simple_char -> char = c: $([^\u{0022}\u{005c}\u{000a}\u{000d}]) { c.chars().next().unwrap() } STRING_LITERAL_QUOTE_simple_char -> char = c: $([^\u{0022}\u{005c}\u{000a}\u{000d}]) { c.chars().next().unwrap() }
//[141s] //[141s]
BLANK_NODE_LABEL -> &'input str = "_:" b: $((PN_CHARS_U / [0-9]) ((PN_CHARS / ".")* PN_CHARS)?) { BLANK_NODE_LABEL -> &'input str = "_:" b:$(([0-9] / PN_CHARS_U) PN_CHARS* ("."+ PN_CHARS+)*) {
b b
} }
@ -73,7 +73,7 @@ UCHAR -> char = "\\u" h: $(HEX HEX HEX HEX) {
} }
//[153s] //[153s]
ECHAR -> char = '\\' c: $([tbnrf"'\\]) { ECHAR -> char = '\\' c:$([tbnrf"'\\]) {
match c { match c {
"t" => '\u{0009}', "t" => '\u{0009}',
"b" => '\u{0008}', "b" => '\u{0008}',

@ -12,6 +12,7 @@ use rudf::rio::turtle::read_turtle;
use std::collections::HashSet; use std::collections::HashSet;
use std::iter::FromIterator; use std::iter::FromIterator;
use url::Url; use url::Url;
use rudf::rio::ntriples::read_ntriples;
struct RDFClient { struct RDFClient {
client: Client, client: Client,
@ -38,6 +39,16 @@ impl RDFClient {
Err(error) => Err(RioError::new(error)), Err(error) => Err(RioError::new(error)),
} }
} }
fn load_ntriples(&self, uri: Url) -> RioResult<HashSet<Triple>> {
match self.client.get(uri).send() {
Ok(response) => read_ntriples(
response,
&self.data_factory
).collect(),
Err(error) => Err(RioError::new(error)),
}
}
} }
fn objects_for_subject_predicate<'a>( fn objects_for_subject_predicate<'a>(
@ -131,3 +142,58 @@ fn turtle_w3c_testsuite() {
} }
}); });
} }
#[test]
fn ntriples_w3c_testsuite() {
let client = RDFClient::default();
let data_factory = &client.data_factory;
let manifest = client
.load_turtle(Url::parse("https://www.w3.org/2013/N-TriplesTests/manifest.ttl").unwrap())
.unwrap();
let rdf_type = data_factory
.named_node(Url::parse("http://www.w3.org/1999/02/22-rdf-syntax-ns#type").unwrap());
let mf_action = data_factory.named_node(
Url::parse("http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#action").unwrap(),
);
let rdfs_comment = data_factory
.named_node(Url::parse("http://www.w3.org/2000/01/rdf-schema#comment").unwrap());
let rdft_test_turtle_positive_syntax =
Term::from(data_factory.named_node(
Url::parse("http://www.w3.org/ns/rdftest#TestNTriplesPositiveSyntax").unwrap(),
));
let rdft_test_turtle_negative_syntax =
Term::from(data_factory.named_node(
Url::parse("http://www.w3.org/ns/rdftest#TestNTriplesNegativeSyntax").unwrap(),
));
subjects_for_predicate_object(&manifest, &rdf_type, &rdft_test_turtle_positive_syntax)
.for_each(|test| {
let comment = object_for_subject_predicate(&manifest, test, &rdfs_comment).unwrap();
if let Some(Term::NamedNode(file)) =
object_for_subject_predicate(&manifest, test, &mf_action)
{
if let Err(error) = client.load_ntriples(file.url().clone()) {
assert!(
false,
"Failure on positive syntax file {} about {} with error: {}",
file, comment, error
)
}
}
});
subjects_for_predicate_object(&manifest, &rdf_type, &rdft_test_turtle_negative_syntax)
.for_each(|test| {
let comment = object_for_subject_predicate(&manifest, test, &rdfs_comment).unwrap();
if let Some(Term::NamedNode(file)) =
object_for_subject_predicate(&manifest, test, &mf_action)
{
assert!(
client.load_ntriples(file.url().clone()).is_err(),
"Failure on negative syntax test file {} about {}",
file,
comment
);
}
});
}

Loading…
Cancel
Save