File: runner.go

package info (click to toggle)
golang-github-vektah-gqlparser 2.5.12-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,748 kB
  • sloc: javascript: 164; sh: 46; makefile: 10
file content (137 lines) | stat: -rw-r--r-- 3,938 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package testrunner

import (
	"os"
	"strconv"
	"strings"
	"testing"

	"github.com/andreyvit/diff"
	"github.com/vektah/gqlparser/v2/gqlerror"
	"gopkg.in/yaml.v2"
)

type Features map[string][]Spec

type Spec struct {
	Name   string
	Input  string
	Error  *gqlerror.Error
	Tokens []Token
	AST    string
}

type Token struct {
	Kind   string
	Value  string
	Start  int
	End    int
	Line   int
	Column int
	Src    string
}

func (t Token) String() string {
	return t.Kind + " " + strconv.Quote(t.Value)
}

func Test(t *testing.T, filename string, f func(t *testing.T, input string) Spec) {
	b, err := os.ReadFile(filename)
	if err != nil {
		panic(err)
	}
	var tests Features
	err = yaml.Unmarshal(b, &tests)
	if err != nil {
		t.Errorf("unable to load %s: %s", filename, err.Error())
		return
	}

	for name, specs := range tests {
		t.Run(name, func(t *testing.T) {
			for _, spec := range specs {
				t.Run(spec.Name, func(t *testing.T) {
					result := f(t, spec.Input)
					switch {
					case spec.Error == nil:
						if result.Error != nil {
							t.Errorf("unexpected error %s", result.Error.Message)
						}
					case result.Error == nil:
						t.Errorf("expected error but got none")
					default:
						if result.Error.Message != spec.Error.Message {
							t.Errorf("wrong error returned\nexpected: %s\ngot:      %s", spec.Error.Message, result.Error.Message)
						}

						if result.Error.Locations[0].Column != spec.Error.Locations[0].Column || result.Error.Locations[0].Line != spec.Error.Locations[0].Line {
							t.Errorf(
								"wrong error location:\nexpected: line %d column %d\ngot:      line %d column %d",
								spec.Error.Locations[0].Line,
								spec.Error.Locations[0].Column,
								result.Error.Locations[0].Line,
								result.Error.Locations[0].Column,
							)
						}
					}

					if len(spec.Tokens) != len(result.Tokens) {
						var tokensStr []string
						for _, t := range result.Tokens {
							tokensStr = append(tokensStr, t.String())
						}
						t.Errorf("token count mismatch, got: \n%s", strings.Join(tokensStr, "\n"))
					} else {
						for i, tok := range result.Tokens {
							expected := spec.Tokens[i]

							if !strings.EqualFold(strings.ReplaceAll(expected.Kind, "_", ""), tok.Kind) {
								t.Errorf("token[%d].kind should be %s, was %s", i, expected.Kind, tok.Kind)
							}
							if expected.Value != "undefined" && expected.Value != tok.Value {
								t.Errorf("token[%d].value incorrect\nexpected: %s\ngot:      %s", i, strconv.Quote(expected.Value), strconv.Quote(tok.Value))
							}
							if expected.Start != 0 && expected.Start != tok.Start {
								t.Errorf("token[%d].start should be %d, was %d", i, expected.Start, tok.Start)
							}
							if expected.End != 0 && expected.End != tok.End {
								t.Errorf("token[%d].end should be %d, was %d", i, expected.End, tok.End)
							}
							if expected.Line != 0 && expected.Line != tok.Line {
								t.Errorf("token[%d].line should be %d, was %d", i, expected.Line, tok.Line)
							}
							if expected.Column != 0 && expected.Column != tok.Column {
								t.Errorf("token[%d].column should be %d, was %d", i, expected.Column, tok.Column)
							}
							if tok.Src != "spec" {
								t.Errorf("token[%d].source.name should be spec, was %s", i, strconv.Quote(tok.Src))
							}
						}
					}

					spec.AST = strings.TrimSpace(spec.AST)
					result.AST = strings.TrimSpace(result.AST)

					if spec.AST != "" && spec.AST != result.AST {
						diffStr := diff.LineDiff(spec.AST, result.AST)
						if diffStr != "" {
							t.Errorf("AST mismatch:\n%s", diffStr)
						}
					}

					if t.Failed() {
						t.Logf("input: %s", strconv.Quote(spec.Input))
						if result.Error != nil {
							t.Logf("error: %s", result.Error.Message)
						}
						t.Log("tokens: ")
						for _, tok := range result.Tokens {
							t.Logf("  - %s", tok.String())
						}
						t.Logf("  - <EOF>")
					}
				})
			}
		})
	}
}