Remove dependency on goconvey

This commit is contained in:
kortschak 2014-07-03 18:59:20 +09:30
parent 102402a13b
commit 03fb6e367a

View file

@ -15,117 +15,112 @@
package nquads package nquads
import ( import (
"reflect"
"testing" "testing"
. "github.com/smartystreets/goconvey/convey"
"github.com/google/cayley/graph" "github.com/google/cayley/graph"
) )
func TestParsingNTriples(t *testing.T) { var testNTriples = []struct {
Convey("When parsing", t, func() { message string
Convey("It should not parse invalid triples", func() { input string
x := Parse("invalid") expect *graph.Triple
So(x, ShouldBeNil) }{
}) // NTriple tests.
Convey("It should not parse comments", func() { {
x := Parse("# nominally valid triple .") message: "not parse invalid triples",
So(x, ShouldBeNil) input: "invalid",
}) expect: nil,
Convey("It should parse simple triples", func() { },
x := Parse("this is valid .") {
So(x, ShouldNotBeNil) message: "not parse comments",
So(x.Subject, ShouldEqual, "this") input: "# nominally valid triple .",
}) expect: nil,
Convey("It should parse quoted triples", func() { },
x := Parse("this is \"valid too\" .") {
So(x, ShouldNotBeNil) message: "parse simple triples",
So(x.Object, ShouldEqual, "valid too") input: "this is valid .",
So(x.Provenance, ShouldEqual, "") expect: &graph.Triple{"this", "is", "valid", ""},
}) },
Convey("It should parse escaped quoted triples", func() { {
x := Parse("he said \"\\\"That's all folks\\\"\" .") message: "parse quoted triples",
So(x, ShouldNotBeNil) input: `this is "valid too" .`,
So(x.Object, ShouldEqual, "\"That's all folks\"") expect: &graph.Triple{"this", "is", "valid too", ""},
So(x.Provenance, ShouldEqual, "") },
}) {
message: "parse escaped quoted triples",
input: `he said "\"That's all folks\"" .`,
expect: &graph.Triple{"he", "said", `"That's all folks"`, ""},
},
{
message: "parse an example real triple",
input: `":/guid/9202a8c04000641f80000000010c843c" "name" "George Morris" .`,
expect: &graph.Triple{":/guid/9202a8c04000641f80000000010c843c", "name", "George Morris", ""},
},
{
message: "parse a pathologically spaced triple",
input: "foo is \"\\tA big tough\\r\\nDeal\\\\\" .",
expect: &graph.Triple{"foo", "is", "\tA big tough\r\nDeal\\", ""},
},
Convey("It should parse an example real triple", func() { // NQuad tests.
x := Parse("\":/guid/9202a8c04000641f80000000010c843c\" \"name\" \"George Morris\" .") {
So(x, ShouldNotBeNil) message: "parse a simple quad",
So(x.Object, ShouldEqual, "George Morris") input: "this is valid quad .",
So(x.Provenance, ShouldEqual, "") expect: &graph.Triple{"this", "is", "valid", "quad"},
}) },
{
message: "parse a quoted quad",
input: `this is valid "quad thing" .`,
expect: &graph.Triple{"this", "is", "valid", "quad thing"},
},
{
message: "parse crazy escaped quads",
input: `"\"this" "\"is" "\"valid" "\"quad thing".`,
expect: &graph.Triple{`"this`, `"is`, `"valid`, `"quad thing`},
},
Convey("It should parse a pathologically spaced triple", func() { // NTriple official tests.
x := Parse("foo is \"\\tA big tough\\r\\nDeal\\\\\" .") {
So(x, ShouldNotBeNil) message: "handle simple case with comments",
So(x.Object, ShouldEqual, "\tA big tough\r\nDeal\\") input: "<http://example/s> <http://example/p> <http://example/o> . # comment",
So(x.Provenance, ShouldEqual, "") expect: &graph.Triple{"http://example/s", "http://example/p", "http://example/o", ""},
}) },
{
Convey("It should parse a simple quad", func() { message: "handle simple case with comments",
x := Parse("this is valid quad .") input: "<http://example/s> <http://example/p> _:o . # comment",
So(x, ShouldNotBeNil) expect: &graph.Triple{"http://example/s", "http://example/p", "_:o", ""},
So(x.Object, ShouldEqual, "valid") },
So(x.Provenance, ShouldEqual, "quad") {
}) message: "handle simple case with comments",
input: "<http://example/s> <http://example/p> \"o\" . # comment",
Convey("It should parse a quoted quad", func() { expect: &graph.Triple{"http://example/s", "http://example/p", "o", ""},
x := Parse("this is valid \"quad thing\" .") },
So(x, ShouldNotBeNil) {
So(x.Object, ShouldEqual, "valid") message: "handle simple case with comments",
So(x.Provenance, ShouldEqual, "quad thing") input: "<http://example/s> <http://example/p> \"o\"^^<http://example/dt> . # comment",
}) expect: &graph.Triple{"http://example/s", "http://example/p", "o", ""},
},
Convey("It should parse crazy escaped quads", func() { {
x := Parse("\"\\\"this\" \"\\\"is\" \"\\\"valid\" \"\\\"quad thing\".") message: "handle simple case with comments",
So(x, ShouldNotBeNil) input: "<http://example/s> <http://example/p> \"o\"@en . # comment",
So(x.Subject, ShouldEqual, "\"this") expect: &graph.Triple{"http://example/s", "http://example/p", "o", ""},
So(x.Predicate, ShouldEqual, "\"is") },
So(x.Object, ShouldEqual, "\"valid")
So(x.Provenance, ShouldEqual, "\"quad thing")
})
})
} }
func TestParsingNTriplesOfficial(t *testing.T) { func TestParse(t *testing.T) {
Convey("When using some public test cases...", t, func() { for _, test := range testNTriples {
Convey("It should handle some simple cases with comments", func() { got := Parse(test.input)
var x *graph.Triple if !reflect.DeepEqual(got, test.expect) {
x = Parse("<http://example/s> <http://example/p> <http://example/o> . # comment") t.Errorf("Failed to %s, %q, got:%q expect:%q", test.message, test.input, got, test.expect)
So(x, ShouldNotBeNil) }
So(x.Subject, ShouldEqual, "http://example/s") }
So(x.Predicate, ShouldEqual, "http://example/p")
So(x.Object, ShouldEqual, "http://example/o")
So(x.Provenance, ShouldEqual, "")
x = Parse("<http://example/s> <http://example/p> _:o . # comment")
So(x, ShouldNotBeNil)
So(x.Subject, ShouldEqual, "http://example/s")
So(x.Predicate, ShouldEqual, "http://example/p")
So(x.Object, ShouldEqual, "_:o")
So(x.Provenance, ShouldEqual, "")
x = Parse("<http://example/s> <http://example/p> \"o\" . # comment")
So(x, ShouldNotBeNil)
So(x.Object, ShouldEqual, "o")
So(x.Provenance, ShouldEqual, "")
x = Parse("<http://example/s> <http://example/p> \"o\"^^<http://example/dt> . # comment")
So(x, ShouldNotBeNil)
So(x.Object, ShouldEqual, "o")
So(x.Provenance, ShouldEqual, "")
x = Parse("<http://example/s> <http://example/p> \"o\"@en . # comment")
So(x, ShouldNotBeNil)
So(x.Object, ShouldEqual, "o")
So(x.Provenance, ShouldEqual, "")
})
})
} }
var result *graph.Triple
func BenchmarkParser(b *testing.B) { func BenchmarkParser(b *testing.B) {
for n := 0; n < b.N; n++ { for n := 0; n < b.N; n++ {
x := Parse("<http://example/s> <http://example/p> \"object of some real\\tlength\"@en . # comment") result = Parse("<http://example/s> <http://example/p> \"object of some real\\tlength\"@en . # comment")
if x.Object != "object of some real\tlength" {
b.Fail()
}
} }
} }