Skip to content

Commit 0743ec0

Browse files
authored
Merge pull request #27 from x1unix/dev
Add some unit tests
2 parents 5b5dbff + 8481373 commit 0743ec0

File tree

19 files changed

+750
-11
lines changed

19 files changed

+750
-11
lines changed

.coveralls.yml

Lines changed: 0 additions & 2 deletions
This file was deleted.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
node_modules
55
/public/
66
/target/
7+
*.out

.travis.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
language: go
2+
3+
go:
4+
- "1.14.x"
5+
6+
branches:
7+
only:
8+
# - master
9+
- dev
10+
env:
11+
- GO111MODULE=on
12+
install: true
13+
before_install:
14+
- go get golang.org/x/tools/cmd/cover
15+
- go get github.com/mattn/goveralls
16+
script:
17+
- go list ./... | grep -v worker | xargs go vet
18+
- cat tools/cover.txt | xargs go test -v -covermode=count -coverprofile=coverage.out
19+
- $HOME/gopath/bin/goveralls -coverprofile=coverage.out -service=travis-ci -repotoken $COVERALLS_TOKEN

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,7 @@ run:
1919
ui:
2020
@cd $(UI) && REACT_APP_LANG_SERVER=http://$(LISTEN_ADDR) REACT_APP_GTAG=$(GTAG) REACT_APP_VERSION=testing yarn start
2121

22+
.PHONY: cover
23+
cover:
24+
@cat tools/cover.txt | xargs go test -v -covermode=count -coverprofile=/tmp/cover.out && \
25+
go tool cover -html=/tmp/cover.out

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
<p style="text-align: center;">
2-
31
# Better Go Playground
42

3+
[![Build Status](https://travis-ci.org/x1unix/go-playground.svg?branch=master)](https://travis-ci.org/x1unix/go-playground)
54
[![Coverage Status](https://coveralls.io/repos/github/x1unix/go-playground/badge.svg?branch=master)](https://coveralls.io/github/x1unix/go-playground?branch=master)
65
[![Goreportcard](https://goreportcard.com/badge/github.com/x1unix/go-playground)](https://goreportcard.com/report/github.com/x1unix/go-playground)
76

87
Improved Go Playground powered by Monaco Editor and React
98

109
![alt text](./docs/demo.gif)
11-
</p>
1210

1311
## Features
1412

@@ -23,7 +21,7 @@ And more
2321

2422
## Demo
2523

26-
[http://goplay.tools/](goplay.tools)
24+
[https://goplay.tools/](https://goplay.tools)
2725

2826
## Installation
2927

pkg/analyzer/analyzer_test.go

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package analyzer
2+
3+
import (
4+
"github.com/stretchr/testify/require"
5+
"github.com/x1unix/go-playground/pkg/testutil"
6+
"testing"
7+
)
8+
9+
func TestSetLogger(t *testing.T) {
10+
l := testutil.GetLogger(t).Desugar()
11+
SetLogger(l)
12+
}
13+
14+
func TestSetRoot(t *testing.T) {
15+
root := "/go"
16+
SetRoot(root)
17+
require.Equal(t, root, goRoot)
18+
}
19+
20+
func TestReadPackagesFile(t *testing.T) {
21+
cases := map[string]struct {
22+
file string
23+
want []*Package
24+
err string
25+
}{
26+
"valid package": {
27+
file: "pkg1.json",
28+
want: []*Package{
29+
{
30+
Name: "time",
31+
Synopsis: "Package time provides functionality for measuring and displaying time.\n\n[\"time\" on pkg.go.dev](https://pkg.go.dev/time/)",
32+
URL: "https://pkg.go.dev/time/",
33+
Path: "time",
34+
},
35+
{
36+
Name: "unicode",
37+
Synopsis: "Package unicode provides data and functions to test some properties of Unicode code points.\n\n[\"unicode\" on pkg.go.dev](https://pkg.go.dev/unicode/)",
38+
URL: "https://pkg.go.dev/unicode/",
39+
Path: "unicode",
40+
Children: []*Package{
41+
{
42+
Name: "utf16",
43+
Synopsis: "Package utf16 implements encoding and decoding of UTF-16 sequences.\n\n[\"unicode/utf16\" on pkg.go.dev](https://pkg.go.dev/unicode/utf16/)",
44+
URL: "https://pkg.go.dev/unicode/utf16/",
45+
Path: "unicode/utf16",
46+
},
47+
{
48+
Name: "utf8",
49+
Synopsis: "Package utf8 implements functions and constants to support text encoded in UTF-8.\n\n[\"unicode/utf8\" on pkg.go.dev](https://pkg.go.dev/unicode/utf8/)",
50+
URL: "https://pkg.go.dev/unicode/utf8/",
51+
Path: "unicode/utf8",
52+
},
53+
},
54+
},
55+
},
56+
},
57+
"bad JSON": {
58+
file: "bad.txt",
59+
err: "invalid character",
60+
},
61+
"invalid file": {
62+
file: "foobar",
63+
err: "no such file",
64+
},
65+
}
66+
67+
for n, c := range cases {
68+
t.Run(n, func(t *testing.T) {
69+
got, err := ReadPackagesFile(testutil.TestdataPath(c.file))
70+
if c.err != "" {
71+
testutil.ContainsError(t, err, c.err)
72+
return
73+
}
74+
require.NoError(t, err)
75+
require.Equal(t, c.want, got)
76+
})
77+
}
78+
}
79+
80+
func TestBuildPackageIndex(t *testing.T) {
81+
pkgs, err := ReadPackagesFile(testutil.TestdataPath("pkg2.json"))
82+
require.NoError(t, err)
83+
index := BuildPackageIndex(pkgs)
84+
require.Equal(t, pkgs, index.Packages)
85+
require.Equal(t, len(pkgs), index.Len())
86+
87+
expectSymbols := make(map[string][]*CompletionItem, len(pkgs))
88+
89+
for _, pkg := range pkgs {
90+
got, ok := index.PackageByName(pkg.Name)
91+
require.True(t, ok)
92+
require.Equal(t, pkg, got)
93+
94+
char := string(pkg.Name[0])
95+
expectSymbols[char] = append(expectSymbols[char], &CompletionItem{
96+
Label: pkg.Name,
97+
Kind: Module,
98+
Detail: pkg.Name,
99+
Documentation: pkg.documentation(),
100+
InsertText: pkg.Name,
101+
})
102+
103+
for _, child := range pkg.Children {
104+
got, ok := index.PackageByName(child.Name)
105+
require.True(t, ok)
106+
require.Equal(t, child, got)
107+
108+
char := string(pkg.Name[0])
109+
expectSymbols[char] = append(expectSymbols[char], &CompletionItem{
110+
Label: child.Name,
111+
Kind: Module,
112+
Detail: child.Name,
113+
Documentation: child.documentation(),
114+
InsertText: child.Name,
115+
})
116+
}
117+
}
118+
119+
for char, expect := range expectSymbols {
120+
match := index.Match(char)
121+
require.Equal(t, expect, match)
122+
}
123+
}

pkg/analyzer/package_test.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package analyzer
2+
3+
import (
4+
"github.com/stretchr/testify/require"
5+
"testing"
6+
)
7+
8+
func TestPackage_IsBuiltin(t *testing.T) {
9+
pkg1 := Package{Name: "foo"}
10+
pkg2 := Package{Name: "builtin"}
11+
require.False(t, pkg1.IsBuiltin())
12+
require.True(t, pkg2.IsBuiltin())
13+
}
14+
15+
func TestPackage_GetLocation(t *testing.T) {
16+
SetRoot("/")
17+
want := "/src/foo/bar"
18+
pkg := Package{Path: "foo/bar"}
19+
require.Equal(t, want, pkg.GetLocation())
20+
}
21+
22+
func TestPackage_SymbolByChar(t *testing.T) {
23+
funcs := &CompletionItem{Label: "bar", Kind: Function}
24+
syms := &CompletionItem{Label: "baz", Kind: Constant}
25+
26+
allFuncs := []*CompletionItem{{Label: "aaaa", Kind: Function}, funcs}
27+
allSyms := []*CompletionItem{{Label: "xxxx", Kind: Constant}, syms}
28+
pkg := Package{
29+
Synopsis: "test doc",
30+
PackageSummary: PackageSummary{
31+
Functions: newSymbolIndex(allFuncs),
32+
Values: newSymbolIndex(allSyms),
33+
},
34+
}
35+
36+
require.Equal(t, MarkdownString{Value: "test doc"}, pkg.documentation())
37+
wantByChar := []*CompletionItem{funcs, syms}
38+
gotChar := pkg.SymbolByChar("b")
39+
require.Equal(t, wantByChar, gotChar)
40+
41+
wantAll := make([]*CompletionItem, 0, len(allSyms)+len(allFuncs))
42+
wantAll = append(wantAll, allFuncs...)
43+
wantAll = append(wantAll, allSyms...)
44+
gotAll := pkg.AllSymbols()
45+
require.Equal(t, wantAll, gotAll)
46+
}
47+
48+
func TestPackages_GetCompletionItems(t *testing.T) {
49+
want := []*CompletionItem{
50+
{
51+
Label: "a",
52+
Kind: Module,
53+
Detail: "a",
54+
InsertText: "a",
55+
Documentation: MarkdownString{},
56+
},
57+
{
58+
Label: "b",
59+
Kind: Module,
60+
Detail: "b",
61+
InsertText: "b",
62+
Documentation: MarkdownString{},
63+
},
64+
}
65+
66+
pkgs := make(Packages, 0, len(want))
67+
for _, sym := range want {
68+
pkgs = append(pkgs, &Package{
69+
Name: sym.Label,
70+
})
71+
}
72+
73+
got := pkgs.GetCompletionItems()
74+
require.Equal(t, want, got)
75+
}
76+
77+
func TestPackage_HasChildren(t *testing.T) {
78+
pkg := Package{Children: make([]*Package, 2)}
79+
require.True(t, pkg.HasChildren())
80+
81+
pkg.Children = nil
82+
require.False(t, pkg.HasChildren())
83+
}
84+
85+
func TestPackage_AllSymbols(t *testing.T) {
86+
87+
}

pkg/analyzer/sym_index_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package analyzer
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func TestSymbolIndex(t *testing.T) {
10+
syms := []*CompletionItem{
11+
{Label: "foo"},
12+
{Label: "bar"},
13+
{Label: "baz"},
14+
}
15+
16+
index := newSymbolIndex(syms)
17+
require.Equal(t, syms, index.Symbols)
18+
require.Equal(t, len(syms), index.Len())
19+
20+
expectSymbols := make(map[string][]*CompletionItem, len(syms))
21+
22+
for _, s := range syms {
23+
got := index.SymbolByName(s.Label)
24+
require.NotNil(t, got)
25+
require.Equal(t, s, got)
26+
27+
char := string(s.Label[0])
28+
expectSymbols[char] = append(expectSymbols[char], s)
29+
}
30+
31+
for char, expect := range expectSymbols {
32+
match := index.Match(char)
33+
require.Equal(t, expect, match)
34+
}
35+
}
36+
37+
func TestSymbolIndex_Append(t *testing.T) {
38+
idx := emptySymbolIndex()
39+
sym := &CompletionItem{Label: "test"}
40+
idx.Append(sym)
41+
idx.Append(nil)
42+
require.Equal(t, 1, idx.Len())
43+
got := idx.SymbolByName(sym.Label)
44+
require.NotNil(t, got)
45+
require.Equal(t, sym, got)
46+
}

pkg/analyzer/testdata/bad.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
badval

pkg/analyzer/testdata/pkg1.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
[
2+
{
3+
"name": "time",
4+
"synopsis": "Package time provides functionality for measuring and displaying time.\n\n[\"time\" on pkg.go.dev](https://pkg.go.dev/time/)",
5+
"url": "https://pkg.go.dev/time/",
6+
"path": "time",
7+
"depth": 0
8+
},
9+
{
10+
"name": "unicode",
11+
"synopsis": "Package unicode provides data and functions to test some properties of Unicode code points.\n\n[\"unicode\" on pkg.go.dev](https://pkg.go.dev/unicode/)",
12+
"url": "https://pkg.go.dev/unicode/",
13+
"path": "unicode",
14+
"depth": 0,
15+
"children": [
16+
{
17+
"name": "utf16",
18+
"synopsis": "Package utf16 implements encoding and decoding of UTF-16 sequences.\n\n[\"unicode/utf16\" on pkg.go.dev](https://pkg.go.dev/unicode/utf16/)",
19+
"url": "https://pkg.go.dev/unicode/utf16/",
20+
"path": "unicode/utf16",
21+
"depth": 20
22+
},
23+
{
24+
"name": "utf8",
25+
"synopsis": "Package utf8 implements functions and constants to support text encoded in UTF-8.\n\n[\"unicode/utf8\" on pkg.go.dev](https://pkg.go.dev/unicode/utf8/)",
26+
"url": "https://pkg.go.dev/unicode/utf8/",
27+
"path": "unicode/utf8",
28+
"depth": 20
29+
}
30+
]
31+
}
32+
]

0 commit comments

Comments
 (0)