Skip to content

Commit

Permalink
Merge pull request #8 from sogko/master
Browse files Browse the repository at this point in the history
Merge latest changes from HEAD
  • Loading branch information
sogko committed Sep 30, 2015
2 parents c1b735a + b6ed6d3 commit 9854fc9
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 54 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ An *work in progress* implementation of GraphQL for Go.
#### Roadmap
- [x] Lexer
- [x] Parser
- [ ] Schema Parser
- [x] Schema Parser
- [ ] Printer
- [ ] Schema Printer
- [ ] Visitor
Expand Down
30 changes: 30 additions & 0 deletions errors/sortutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package graphqlerrors

import "bytes"

type GQLFormattedErrorSlice []GraphQLFormattedError

func (errs GQLFormattedErrorSlice) Len() int {
return len(errs)
}

func (errs GQLFormattedErrorSlice) Swap(i, j int) {
errs[i], errs[j] = errs[j], errs[i]
}

func (errs GQLFormattedErrorSlice) Less(i, j int) bool {
mCompare := bytes.Compare([]byte(errs[i].Message), []byte(errs[j].Message))
lesserLine := errs[i].Locations[0].Line < errs[j].Locations[0].Line
eqLine := errs[i].Locations[0].Line == errs[j].Locations[0].Line
lesserColumn := errs[i].Locations[0].Column < errs[j].Locations[0].Column
if mCompare < 0 {
return true
}
if mCompare == 0 && lesserLine {
return true
}
if mCompare == 0 && eqLine && lesserColumn {
return true
}
return false
}
45 changes: 23 additions & 22 deletions executor/nonnull_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package executor_test

import (
"reflect"
"sort"
"testing"

"github.com/chris-ramon/graphql-go/errors"
"github.com/chris-ramon/graphql-go/executor"
"github.com/chris-ramon/graphql-go/language/location"
"github.com/chris-ramon/graphql-go/testutil"
"github.com/chris-ramon/graphql-go/types"
"reflect"
"testing"
)

var syncError = "sync"
Expand Down Expand Up @@ -148,7 +150,6 @@ func TestNonNull_NullsANullableFieldThatThrowsSynchronously(t *testing.T) {
}
}
func TestNonNull_NullsANullableFieldThatThrowsInAPromise(t *testing.T) {
t.Skipf("Promises not implemented")
doc := `
query Q {
promise
Expand Down Expand Up @@ -226,8 +227,7 @@ func TestNonNull_NullsASynchronouslyReturnedObjectThatContainsANullableFieldThat
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))
}
}
func TestNonNull_NullsASynchronouslyReturnedObjectThatContainsANullableFieldThatThrowsInAPromise(t *testing.T) {
t.Skipf("Promises not implemented")
func TestNonNull_NullsASynchronouslyReturnedObjectThatContainsANonNullableFieldThatThrowsInAPromise(t *testing.T) {
doc := `
query Q {
nest {
Expand Down Expand Up @@ -267,8 +267,7 @@ func TestNonNull_NullsASynchronouslyReturnedObjectThatContainsANullableFieldThat
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))
}
}
func TestNonNull_NullsAnObjectReturnedInAPromiseThatContainsANonNulableFieldThatThrowsSynchronously(t *testing.T) {
t.Skipf("Promises not implemented")
func TestNonNull_NullsAnObjectReturnedInAPromiseThatContainsANonNullableFieldThatThrowsSynchronously(t *testing.T) {
doc := `
query Q {
promiseNest {
Expand Down Expand Up @@ -308,8 +307,7 @@ func TestNonNull_NullsAnObjectReturnedInAPromiseThatContainsANonNulableFieldThat
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))
}
}
func TestNonNull_NullsAnObjectReturnedInAPromiseThatContainsANonNulableFieldThatThrowsInAPromise(t *testing.T) {
t.Skipf("Promises not implemented")
func TestNonNull_NullsAnObjectReturnedInAPromiseThatContainsANonNullableFieldThatThrowsInAPromise(t *testing.T) {
doc := `
query Q {
promiseNest {
Expand Down Expand Up @@ -497,8 +495,9 @@ func TestNonNull_NullsAComplexTreeOfNullableFieldsThatThrow(t *testing.T) {
if !reflect.DeepEqual(expected.Data, result.Data) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected.Data, result.Data))
}
t.Skipf("Testing equality for slice of errors in results")
if !testutil.EqualSet(expected.Errors, result.Errors) {
sort.Sort(graphqlerrors.GQLFormattedErrorSlice(expected.Errors))
sort.Sort(graphqlerrors.GQLFormattedErrorSlice(result.Errors))
if !reflect.DeepEqual(expected.Errors, result.Errors) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected.Errors, result.Errors))
}
}
Expand Down Expand Up @@ -560,25 +559,25 @@ func TestNonNull_NullsTheFirstNullableObjectAfterAFieldThrowsInALongChainOfField
},
Errors: []graphqlerrors.GraphQLFormattedError{
graphqlerrors.GraphQLFormattedError{
Message: `Cannot return null for non-nullable field DataType.nonNullSync.`,
Message: nonNullSyncError,
Locations: []location.SourceLocation{
location.SourceLocation{Line: 8, Column: 19},
},
},
graphqlerrors.GraphQLFormattedError{
Message: `Cannot return null for non-nullable field DataType.nonNullSync.`,
Message: nonNullSyncError,
Locations: []location.SourceLocation{
location.SourceLocation{Line: 19, Column: 19},
},
},
graphqlerrors.GraphQLFormattedError{
Message: `Cannot return null for non-nullable field DataType.nonNullPromise.`,
Message: nonNullPromiseError,
Locations: []location.SourceLocation{
location.SourceLocation{Line: 30, Column: 19},
},
},
graphqlerrors.GraphQLFormattedError{
Message: `Cannot return null for non-nullable field DataType.nonNullPromise.`,
Message: nonNullPromiseError,
Locations: []location.SourceLocation{
location.SourceLocation{Line: 41, Column: 19},
},
Expand All @@ -601,8 +600,9 @@ func TestNonNull_NullsTheFirstNullableObjectAfterAFieldThrowsInALongChainOfField
if !reflect.DeepEqual(expected.Data, result.Data) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected.Data, result.Data))
}
t.Skipf("Testing equality for slice of errors in results")
if !testutil.EqualSet(expected.Errors, result.Errors) {
sort.Sort(graphqlerrors.GQLFormattedErrorSlice(expected.Errors))
sort.Sort(graphqlerrors.GQLFormattedErrorSlice(result.Errors))
if !reflect.DeepEqual(expected.Errors, result.Errors) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected.Errors, result.Errors))
}

Expand Down Expand Up @@ -634,7 +634,7 @@ func TestNonNull_NullsANullableFieldThatSynchronouslyReturnsNull(t *testing.T) {
if !reflect.DeepEqual(expected.Data, result.Data) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected.Data, result.Data))
}
if !testutil.EqualSet(expected.Errors, result.Errors) {
if !reflect.DeepEqual(expected.Errors, result.Errors) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected.Errors, result.Errors))
}
}
Expand Down Expand Up @@ -665,7 +665,7 @@ func TestNonNull_NullsANullableFieldThatSynchronouslyReturnsNullInAPromise(t *te
if !reflect.DeepEqual(expected.Data, result.Data) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected.Data, result.Data))
}
if !testutil.EqualSet(expected.Errors, result.Errors) {
if !reflect.DeepEqual(expected.Errors, result.Errors) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected.Errors, result.Errors))
}
}
Expand Down Expand Up @@ -895,7 +895,7 @@ func TestNonNull_NullsAComplexTreeOfNullableFieldsThatReturnNull(t *testing.T) {
if !reflect.DeepEqual(expected.Data, result.Data) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected.Data, result.Data))
}
if !testutil.EqualSet(expected.Errors, result.Errors) {
if !reflect.DeepEqual(expected.Errors, result.Errors) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected.Errors, result.Errors))
}
}
Expand Down Expand Up @@ -998,8 +998,9 @@ func TestNonNull_NullsTheFirstNullableObjectAfterAFieldReturnsNullInALongChainOf
if !reflect.DeepEqual(expected.Data, result.Data) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected.Data, result.Data))
}
t.Skipf("Testing equality for slice of errors in results")
if !testutil.EqualSet(expected.Errors, result.Errors) {
sort.Sort(graphqlerrors.GQLFormattedErrorSlice(expected.Errors))
sort.Sort(graphqlerrors.GQLFormattedErrorSlice(result.Errors))
if !reflect.DeepEqual(expected.Errors, result.Errors) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected.Errors, result.Errors))
}
}
Expand Down
50 changes: 48 additions & 2 deletions language/parser/schema_parser_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package parser_test

import (
"reflect"
"testing"

"github.com/chris-ramon/graphql-go/errors"
"github.com/chris-ramon/graphql-go/language/ast"
"github.com/chris-ramon/graphql-go/language/location"
"github.com/chris-ramon/graphql-go/language/parser"
"github.com/chris-ramon/graphql-go/language/source"
"reflect"
"testing"
)

func parse(t *testing.T, query string) *ast.Document {
Expand Down Expand Up @@ -70,6 +71,51 @@ type Hello {
}
}

func TestSchemaParser_SimpleExtension(t *testing.T) {

body := `
extend type Hello {
world: String
}`
astDoc := parse(t, body)
expected := ast.NewDocument(&ast.Document{
Loc: loc(1, 38),
Definitions: []ast.Node{
ast.NewTypeExtensionDefinition(&ast.TypeExtensionDefinition{
Loc: loc(1, 38),
Definition: ast.NewObjectTypeDefinition(&ast.ObjectTypeDefinition{
Loc: loc(8, 38),
Name: ast.NewName(&ast.Name{
Value: "Hello",
Loc: loc(13, 18),
}),
Interfaces: []*ast.NamedType{},
Fields: []*ast.FieldDefinition{
ast.NewFieldDefinition(&ast.FieldDefinition{
Loc: loc(23, 36),
Name: ast.NewName(&ast.Name{
Value: "world",
Loc: loc(23, 28),
}),
Arguments: []*ast.InputValueDefinition{},
Type: ast.NewNamedType(&ast.NamedType{
Loc: loc(30, 36),
Name: ast.NewName(&ast.Name{
Value: "String",
Loc: loc(30, 36),
}),
}),
}),
},
}),
}),
},
})
if !reflect.DeepEqual(astDoc, expected) {
t.Fatalf("unexpected document, expected: %v, got: %v", expected, astDoc)
}
}

func TestSchemaParser_SimpleNonNullType(t *testing.T) {

body := `
Expand Down
32 changes: 3 additions & 29 deletions testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ package testutil

import (
"encoding/json"
"strconv"
"testing"

"github.com/chris-ramon/graphql-go/executor"
"github.com/chris-ramon/graphql-go/language/ast"
"github.com/chris-ramon/graphql-go/language/parser"
"github.com/chris-ramon/graphql-go/types"
"github.com/deckarep/golang-set"
"github.com/kr/pretty"
"reflect"
"strconv"
"testing"
)

var (
Expand Down Expand Up @@ -368,31 +367,6 @@ func Diff(a, b interface{}) []string {
return pretty.Diff(a, b)
}

// TODO: EqualSet
func EqualSet(a, b interface{}) bool {

aa := []interface{}{}
bb := []interface{}{}

aVal := reflect.ValueOf(a)
bVal := reflect.ValueOf(b)
if aVal.Type().Kind() == reflect.Slice {
for i := 0; i < aVal.Len(); i++ {
val := aVal.Index(i).Interface()
aa = append(aa, &val)
}
}
if bVal.Type().Kind() == reflect.Slice {
for i := 0; i < bVal.Len(); i++ {
val := bVal.Index(i).Interface()
bb = append(bb, &val)
}
}
xx := mapset.NewSetFromSlice(aa)
yy := mapset.NewSetFromSlice(bb)
return xx.Equal(yy)
}

func ASTToJSON(t *testing.T, a ast.Node) interface{} {
b, err := json.Marshal(a)
if err != nil {
Expand Down

0 comments on commit 9854fc9

Please sign in to comment.