-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplay_test.go
72 lines (56 loc) · 2.33 KB
/
play_test.go
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
package play_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/wilhelm-murdoch/go-play"
)
const playUrlPattern = `^https?:\/\/(www\.)?go\.dev\/play\/p\/[a-zA-Z0-9-_]{11}$`
var (
goCodes = [][]byte{
[]byte(`package main
import "fmt"
func main() {
fmt.Println("Hello world!")
}`),
[]byte(`package main
import "fmt"
func main() {
fmt.Println("Hello from another world!")
}`),
[]byte(`abandon all hope ye who enter here`),
[]byte(""),
}
)
func TestFetch(t *testing.T) {
p := play.NewClient(play.ConfigDefault)
share, err := p.Fetch(goCodes[0])
assert.Nil(t, err, "expected to return with no error, but got %s instead", err)
assert.NotEmpty(t, share, "expected to return a valid url, but got nothing instead")
assert.Regexp(t, playUrlPattern, share, "expected to return a valid url, but got %s instead", share)
}
func TestFetchGroup(t *testing.T) {
p := play.NewClient(play.ConfigDefault)
shares, err := p.FetchGroup(goCodes)
assert.Equal(t, len(goCodes), len(shares), "expected to return %d valid urls, but got %d instead", len(goCodes), len(shares))
assert.Nil(t, err, "expected to return with no error, but got %s instead", err)
assert.NotEmpty(t, shares, "expected to return a valid url, but got nothing instead")
for _, share := range shares {
assert.Regexp(t, playUrlPattern, share, "expected to return a valid url, but got %s instead", share)
}
}
func TestFetchWithCustomConfig(t *testing.T) {
client := play.NewClient(play.NewConfig("https://does-not-work.io", "share", "play"))
share, err := client.Fetch(goCodes[0])
assert.NotNil(t, err, "expected to return with no error, but got %s instead", err)
assert.Empty(t, share, "expected to return an empty url, but got %s instead", share)
}
func TestFetchGroupWithCustomConfig(t *testing.T) {
client := play.NewClient(play.NewConfig("https://does-not-work.io", "share", "play"))
shares, err := client.FetchGroup(goCodes)
assert.NotEqual(t, len(goCodes), len(shares), "expected to return %d valid urls, but got %d instead", len(goCodes), len(shares))
assert.NotNil(t, err, "expected to return with no error, but got %s instead", err)
for _, share := range shares {
assert.Empty(t, share, "expected to return an empty url, but got %s instead", share)
assert.Regexp(t, playUrlPattern, share, "expected to return a valid url, but got %s instead", share)
}
}