// Copyright 2014 The Cayley Authors. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package nquads import ( "reflect" "testing" "github.com/google/cayley/graph" ) var testNTriples = []struct { message string input string expect *graph.Triple err error }{ // Tests taken from http://www.w3.org/TR/n-quads/ and http://www.w3.org/TR/n-triples/. // N-Triples example 1. { message: "parse triple with commment", input: ` . # comments here`, expect: &graph.Triple{ Subject: "", Predicate: "", Object: "", Provenance: "", }, err: nil, }, { message: "parse triple with blank subject node, literal object and no comment (1)", input: `_:subject1 "object1" .`, expect: &graph.Triple{ Subject: "_:subject1", Predicate: "", Object: `"object1"`, Provenance: "", }, err: nil, }, { message: "parse triple with blank subject node, literal object and no comment (2)", input: `_:subject2 "object2" .`, expect: &graph.Triple{ Subject: "_:subject2", Predicate: "", Object: `"object2"`, Provenance: "", }, err: nil, }, // N-Triples example 2. { message: "parse triple with three IRIREFs", input: ` .`, expect: &graph.Triple{ Subject: "", Predicate: "", Object: "", Provenance: "", }, err: nil, }, // N-Triples example 3. { message: "parse triple with blank node labelled subject and object and IRIREF predicate (1)", input: `_:alice _:bob .`, expect: &graph.Triple{ Subject: "_:alice", Predicate: "", Object: "_:bob", Provenance: "", }, err: nil, }, { message: "parse triple with blank node labelled subject and object and IRIREF predicate (2)", input: `_:bob _:alice .`, expect: &graph.Triple{ Subject: "_:bob", Predicate: "", Object: "_:alice", Provenance: "", }, err: nil, }, // N-Quads example 1. { message: "parse quad with commment", input: ` . # comments here`, expect: &graph.Triple{ Subject: "", Predicate: "", Object: "", Provenance: "", }, err: nil, }, { message: "parse quad with blank subject node, literal object, IRIREF predicate and label, and no comment (1)", input: `_:subject1 "object1" .`, expect: &graph.Triple{ Subject: "_:subject1", Predicate: "", Object: `"object1"`, Provenance: "", }, err: nil, }, { message: "parse quad with blank subject node, literal object, IRIREF predicate and label, and no comment (2)", input: `_:subject2 "object2" .`, expect: &graph.Triple{ Subject: "_:subject2", Predicate: "", Object: `"object2"`, Provenance: "", }, err: nil, }, // N-Quads example 2. { message: "parse quad with all IRIREF parts", input: ` .`, expect: &graph.Triple{ Subject: "", Predicate: "", Object: "", Provenance: "", }, err: nil, }, // N-Quads example 3. { message: "parse quad with blank node labelled subject and object and IRIREF predicate and label (1)", input: `_:alice _:bob .`, expect: &graph.Triple{ Subject: "_:alice", Predicate: "", Object: "_:bob", Provenance: "", }, err: nil, }, { message: "parse quad with blank node labelled subject and object and IRIREF predicate and label (2)", input: `_:bob _:alice .`, expect: &graph.Triple{ Subject: "_:bob", Predicate: "", Object: "_:alice", Provenance: "", }, err: nil, }, // N-Triples tests. { message: "parse triple with all IRIREF parts", input: ` .`, expect: &graph.Triple{ Subject: "", Predicate: "", Object: "", Provenance: "", }, err: nil, }, { message: "parse triple with all IRIREF parts", input: ` .`, expect: &graph.Triple{ Subject: "", Predicate: "", Object: "", Provenance: "", }, err: nil, }, { message: "parse triple with IRIREF schema on literal object", input: ` "1990-07-04"^^ .`, expect: &graph.Triple{ Subject: "", Predicate: "", Object: `"1990-07-04"^^`, Provenance: "", }, err: nil, }, { message: "parse commented IRIREF in triple", input: ` .`, expect: &graph.Triple{ Subject: "", Predicate: "", Object: "", Provenance: "", }, err: nil, }, { message: "parse triple with literal subject", input: ` "Mona Lisa" .`, expect: &graph.Triple{ Subject: "", Predicate: "", Object: `"Mona Lisa"`, Provenance: "", }, err: nil, }, { message: "parse triple with all IRIREF parts (1)", input: ` .`, expect: &graph.Triple{ Subject: "", Predicate: "", Object: "", Provenance: "", }, err: nil, }, { message: "parse triple with all IRIREF parts (2)", input: ` .`, expect: &graph.Triple{ Subject: "", Predicate: "", Object: "", Provenance: "", }, err: nil, }, // N-Quads tests. { message: "parse commented IRIREF in quad (1)", input: ` .`, expect: &graph.Triple{ Subject: "", Predicate: "", Object: "", Provenance: "", }, err: nil, }, { message: "parse quad with all IRIREF parts", input: ` .`, expect: &graph.Triple{ Subject: "", Predicate: "", Object: "", Provenance: "", }, err: nil, }, { message: "parse quad with IRIREF schema on literal object", input: ` "1990-07-04"^^ .`, expect: &graph.Triple{ Subject: "", Predicate: "", Object: `"1990-07-04"^^`, Provenance: "", }, err: nil, }, { message: "parse commented IRIREF in quad (2)", input: ` .`, expect: &graph.Triple{ Subject: "", Predicate: "", Object: "", Provenance: "", }, err: nil, }, { message: "parse literal object and colon qualified label in quad", input: ` "Mona Lisa" .`, expect: &graph.Triple{ Subject: "", Predicate: "", Object: `"Mona Lisa"`, Provenance: "", }, err: nil, }, { message: "parse all IRIREF parts with colon qualified label in quad (1)", input: ` .`, expect: &graph.Triple{ Subject: "", Predicate: "", Object: "", Provenance: "", }, err: nil, }, { message: "parse all IRIREF parts with colon qualified label in quad (2)", input: ` .`, expect: &graph.Triple{ Subject: "", Predicate: "", Object: "", Provenance: "", }, err: nil, }, { message: "parse all IRIREF parts (quad section - 1)", input: ` .`, expect: &graph.Triple{ Subject: "", Predicate: "", Object: "", Provenance: "", }, err: nil, }, { message: "parse all IRIREF parts (quad section - 2)", input: ` .`, expect: &graph.Triple{ Subject: "", Predicate: "", Object: "", Provenance: "", }, err: nil, }, } func TestParse(t *testing.T) { for _, test := range testNTriples { got, err := Parse(test.input) if err != test.err { t.Errorf("Unexpected error when %s: got:%v expect:%v", test.message, err, test.err) } if !reflect.DeepEqual(got, test.expect) { t.Errorf("Failed to %s, %q, got:%q expect:%q", test.message, test.input, got, test.expect) } } } var result *graph.Triple func BenchmarkParser(b *testing.B) { for n := 0; n < b.N; n++ { result, _ = Parse(" \"object of some real\\tlength\"@en . # comment") } }