File: main_test.go

package info (click to toggle)
golang-golang-x-tools 1%3A0.25.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 22,724 kB
  • sloc: javascript: 2,027; asm: 1,645; sh: 166; yacc: 155; makefile: 49; ansic: 8
file content (116 lines) | stat: -rw-r--r-- 2,943 bytes parent folder | download | duplicates (2)
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
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"encoding/json"
	"fmt"
	"log"
	"os"
	"testing"
)

// These tests require the result of
//"git clone https://github.com/microsoft/vscode-languageserver-node" in the HOME directory

// this is not a test, but a way to get code coverage,
// (in vscode, just run the test with  "go.coverOnSingleTest": true)
func TestAll(t *testing.T) {
	t.Skip("needs vscode-languageserver-node repository")
	*lineNumbers = true
	log.SetFlags(log.Lshortfile)
	main()
}

// check that the parsed file includes all the information
// from the json file. This test will fail if the spec
// introduces new fields. (one can test this test by
// commenting out the version field in Model.)
func TestParseContents(t *testing.T) {
	t.Skip("needs vscode-languageserver-node repository")
	log.SetFlags(log.Lshortfile)

	// compute our parse of the specification
	dir := os.Getenv("HOME") + "/vscode-languageserver-node"
	fname := dir + "/protocol/metaModel.json"
	v := parse(fname)
	out, err := json.Marshal(v)
	if err != nil {
		t.Fatal(err)
	}
	var our interface{}
	if err := json.Unmarshal(out, &our); err != nil {
		t.Fatal(err)
	}

	// process the json file
	buf, err := os.ReadFile(fname)
	if err != nil {
		t.Fatalf("could not read metaModel.json: %v", err)
	}
	var raw interface{}
	if err := json.Unmarshal(buf, &raw); err != nil {
		t.Fatal(err)
	}

	// convert to strings showing the fields
	them := flatten(raw)
	us := flatten(our)

	// everything in them should be in us
	lesser := make(sortedMap[bool])
	for _, s := range them {
		lesser[s] = true
	}
	greater := make(sortedMap[bool]) // set of fields we have
	for _, s := range us {
		greater[s] = true
	}
	for _, k := range lesser.keys() { // set if fields they have
		if !greater[k] {
			t.Errorf("missing %s", k)
		}
	}
}

// flatten(nil) = "nil"
// flatten(v string) = fmt.Sprintf("%q", v)
// flatten(v float64)= fmt.Sprintf("%g", v)
// flatten(v bool) = fmt.Sprintf("%v", v)
// flatten(v []any) = []string{"[0]"flatten(v[0]), "[1]"flatten(v[1]), ...}
// flatten(v map[string]any) = {"key1": flatten(v["key1"]), "key2": flatten(v["key2"]), ...}
func flatten(x any) []string {
	switch v := x.(type) {
	case nil:
		return []string{"nil"}
	case string:
		return []string{fmt.Sprintf("%q", v)}
	case float64:
		return []string{fmt.Sprintf("%g", v)}
	case bool:
		return []string{fmt.Sprintf("%v", v)}
	case []any:
		var ans []string
		for i, x := range v {
			idx := fmt.Sprintf("[%.3d]", i)
			for _, s := range flatten(x) {
				ans = append(ans, idx+s)
			}
		}
		return ans
	case map[string]any:
		var ans []string
		for k, x := range v {
			idx := fmt.Sprintf("%q:", k)
			for _, s := range flatten(x) {
				ans = append(ans, idx+s)
			}
		}
		return ans
	default:
		log.Fatalf("unexpected type %T", x)
		return nil
	}
}