123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- /*
- * Public Domain Software
- *
- * I (Matthias Ladkau) am the author of the source code in this file.
- * I have placed the source code in this file in the public domain.
- *
- * For further information see: http://creativecommons.org/publicdomain/zero/1.0/
- */
- package parser
- import (
- "fmt"
- "testing"
- )
- func TestNextAndPeek(t *testing.T) {
- l := &lexer{"", "Test", 0, 0, 0, 0, 0, make(chan LexToken)}
- if res := fmt.Sprintf("%c", l.next(0)); res != "T" {
- t.Error("Unexpected result:", res)
- }
- if res := fmt.Sprintf("%c", l.next(1)); res != "e" {
- t.Error("Unexpected result:", res)
- }
- if res := fmt.Sprintf("%c", l.next(2)); res != "s" {
- t.Error("Unexpected result:", res)
- }
- if res := fmt.Sprintf("%c", l.next(3)); res != "t" {
- t.Error("Unexpected result:", res)
- }
- if l.pos != 0 {
- t.Error("Lexer moved forward when it shouldn't: ", l.pos)
- return
- }
- if res := fmt.Sprintf("%c", l.next(-1)); res != "T" {
- t.Error("Unexpected result:", res)
- }
- if l.pos != 1 {
- t.Error("Lexer moved forward when it shouldn't: ", l.pos)
- return
- }
- if res := fmt.Sprintf("%c", l.next(-1)); res != "e" {
- t.Error("Unexpected result:", res)
- }
- if l.pos != 2 {
- t.Error("Lexer moved forward when it shouldn't: ", l.pos)
- return
- }
- }
- func TestSimpleLexing(t *testing.T) {
- if res := fmt.Sprint(LexToList("test", "\ufeff1!23")); res != `[int(1) ! int(23) EOF]` {
- t.Error("Unexpected result:", res)
- return
- }
- if res := fmt.Sprint(LexToList("test", "1!23.4e+11 3E-5 11.1 .4$")); res !=
- `[int(1) ! flt(23.4e+11) flt(3e-5) flt(11.1) flt(.4) $ EOF]` {
- t.Error("Unexpected result:", res)
- return
- }
- if res := fmt.Sprint(LexToList("test", "12!foo...bar99")); res !=
- `[int(12) ! <foo> ... <bar99> EOF]` {
- t.Error("Unexpected result:", res)
- return
- }
- if res := fmt.Sprint(LexToList("test", "-0 0 1230 0123")); res !=
- `[int(-0) int(0) int(1230) Error: 0123 (Line 1, Pos 11) EOF]` {
- t.Error("Unexpected result:", res)
- return
- }
- }
- func TestLexingErrors(t *testing.T) {
- if res := fmt.Sprint(LexToList("test", `"te`)); res != `[Error: EOF inside quotes (Line 1, Pos 1) EOF]` {
- t.Error("Unexpected result:", res)
- return
- }
- }
- func TestMultilineLexing(t *testing.T) {
- if res := fmt.Sprint(LexToList("test", `1!23#...4e+11
- 123
- true
- `)); res != `[int(1) ! int(23) int(123) <true> EOF]` {
- t.Error("Unexpected result:", res)
- return
- }
- if res := fmt.Sprint(LexToList("test", `"""
- 123
- """
- "["
- [
- "123"
- "123\u2318"
- """123\u2318"""
- """
- bla
- """
- `)); res != `["123" "[" [ "123" "123⌘" "123\u2318" "bla" EOF]` {
- t.Error("Unexpected result:", res)
- return
- }
- if res := fmt.Sprint(LexToList("test", `"""
- Hello,
- World!
- Yours,
- GraphQL.
- """
- `)); res != `["Hello,
- World!
- Yours,
- GraphQL." EOF]` {
- t.Error("Unexpected result:", res)
- return
- }
- if res := fmt.Sprint(LexToList("test", `"Hello,\n World!\n\nYours,\n GraphQL."
- `)); res != `["Hello,
- World!
- Yours,
- GraphQL." EOF]` {
- t.Error("Unexpected result:", res)
- return
- }
- ll := LexToList("test", `"Hello,\n World!\n\nYours,\n GraphQL."
- `)
- if res := ll[len(ll)-1].PosString(); res != "Line 2, Pos 1" {
- t.Error("Unexpected result:", res)
- return
- }
- }
- func TestIgnoredLexing(t *testing.T) {
- res := fmt.Sprint(LexToList("test", "1,2,3...abc\t\r\n#123\n"))
- if res != `[int(1) int(2) int(3) ... <abc> EOF]` {
- t.Error("Unexpected result:", res)
- return
- }
- res = fmt.Sprint(LexToList("test", "1,2,3 .. x abc\r\n#123\n"))
- if res != `[int(1) int(2) int(3) Error: .. (Line 1, Pos 7) <x> <abc> EOF]` {
- t.Error("Unexpected result:", res)
- return
- }
- res = fmt.Sprint(LexToList("test", "1,2,3 .. x abc\r\n#123"))
- if res != `[int(1) int(2) int(3) Error: .. (Line 1, Pos 7) <x> <abc> EOF]` {
- t.Error("Unexpected result:", res)
- return
- }
- }
- func TestSampleQueries(t *testing.T) {
- sampleQueries := [][]string{{`
- query StudentsNormal {
- allStudents(pagination: {offset: 0, limit: 10}, sort: {fields: [{field: "studentNumber", order: ASC}]},
- filter: {fields: [{op: NIN, value: "[Harry]", field: "name"}]}) {
- result {
- ...studentFields
- subjects {
- name
- classroom
- }
- }
- pagination {
- offset
- limit
- total
- }
- }
- }
- `, `[<query> <StudentsNormal> { <allStudents> ( <pagination> : { <offset> : int(0) <limit> : int(10) } <sort> : { <fields> : [ { <field> : "studentNumber" <order> : <ASC> } ] } <filter> : { <fields> : [ { <op> : <NIN> <value> : "[Harry]" <field> : "name" } ] } ) { <result> { ... <studentFields> <subjects> { <name> <classroom> } } <pagination> { <offset> <limit> <total> } } } EOF]`},
- {`
- query StudentsJPA {
- allStudentsJPA(pagination: {offset: 0, limit: 10}, sort: {fields: [{field: "studentNumber", order: ASC}]}, filter: {fields: [{op: NIN, value: "[Harry]", field: "name"}]}) {
- ... on PaginationWrapper_Student {
- result {
- name
- }
- }
- result {
- ...studentFields
- ... on Student {
- enrolled
- }
- subjects {
- name
- classroom
- }
- }
- pagination {
- offset
- limit
- total
- }
- }
- }
- `, `[<query> <StudentsJPA> { <allStudentsJPA> ( <pagination> : { <offset> : int(0) <limit> : int(10) } <sort> : { <fields> : [ { <field> : "studentNumber" <order> : <ASC> } ] } <filter> : { <fields> : [ { <op> : <NIN> <value> : "[Harry]" <field> : "name" } ] } ) { ... <on> <PaginationWrapper_Student> { <result> { <name> } } <result> { ... <studentFields> ... <on> <Student> { <enrolled> } <subjects> { <name> <classroom> } } <pagination> { <offset> <limit> <total> } } } EOF]`},
- {`
- # query variables
- {
- "st": {
- "studentNumber": 63170004,
- "studentLoan": 631700.04,
- "name": "Latest",
- "surname": "Greatest"
- }
- `, `[{ "st" : { "studentNumber" : int(63170004) "studentLoan" : flt(631700.04) "name" : "Latest" "surname" : "Greatest" } EOF]`}}
- for _, sampleQuery := range sampleQueries {
- if res := fmt.Sprint(LexToList("test", sampleQuery[0])); res != sampleQuery[1] {
- t.Error("Unexpected result\nGiven:\n", sampleQuery[0], "\nGot:\n", res, "\nExpected:\n", sampleQuery[1])
- return
- }
- }
- }
|