z_mirror/luapls

git clone https://git.raiguard.me/z_mirror/luapls

Caleb HeuerRename ast.Error to ast.Diagnostic442023c

main
1.2 KiB57 linesraw
1package parser
2
3import (
4	"encoding/json"
5	"os"
6	"path/filepath"
7	"strings"
8	"testing"
9
10	"github.com/stretchr/testify/assert"
11	"github.com/stretchr/testify/require"
12)
13
14type TestSpec struct {
15	Label  string
16	Input  string `json:"input"`
17	AST    json.RawMessage
18	Errors json.RawMessage `json:",omitempty"`
19}
20
21func TestParser(t *testing.T) {
22	dir, err := os.ReadDir("test_specs")
23	require.NoError(t, err)
24	for _, entry := range dir {
25		bytes, err := os.ReadFile(filepath.Join("test_specs", entry.Name()))
26		var specs []TestSpec
27		err = json.Unmarshal(bytes, &specs)
28		if !assert.NoError(t, err) {
29			continue
30		}
31		for _, spec := range specs {
32			specLabel := strings.TrimSuffix(entry.Name(), ".json") + "/" + spec.Label
33			t.Run(specLabel, func(t *testing.T) { testSpec(t, &spec) })
34		}
35	}
36}
37
38func testSpec(t *testing.T, spec *TestSpec) {
39	p := New(spec.Input)
40	file := p.ParseFile()
41	ast, err := json.Marshal(&file.Block)
42	if !assert.NoError(t, err) {
43		return
44	}
45	assert.JSONEq(t, string(spec.AST), string(ast))
46
47	if len(spec.Errors) == 0 {
48		assert.Empty(t, p.Errors())
49		return
50	}
51
52	errors, err := json.Marshal(&file.Diagnostics)
53	if !assert.NoError(t, err) {
54		return
55	}
56	assert.JSONEq(t, string(spec.Errors), string(errors))
57}