Move gist URL tests to its own file

This commit is contained in:
Cristian Dominguez 2020-09-21 21:35:07 -03:00
parent 55a8f3d8ca
commit a7a90b3a4b
2 changed files with 57 additions and 25 deletions

View file

@ -0,0 +1,52 @@
package shared
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test_GetGistIDFromURL(t *testing.T) {
tests := []struct {
name string
url string
want string
wantErr bool
}{
{
name: "url",
url: "https://gist.github.com/1234",
want: "1234",
},
{
name: "url with username",
url: "https://gist.github.com/octocat/1234",
want: "1234",
},
{
name: "url, specific file",
url: "https://gist.github.com/1234#file-test-md",
want: "1234",
},
{
name: "invalid url",
url: "https://gist.github.com",
wantErr: true,
want: "Invalid gist URL https://gist.github.com",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
id, err := GistIDFromURL(tt.url)
if tt.wantErr {
assert.Error(t, err)
assert.EqualError(t, err, tt.want)
return
}
assert.NoError(t, err)
assert.Equal(t, tt.want, id)
})
}
}

View file

@ -83,12 +83,11 @@ func TestNewCmdView(t *testing.T) {
func Test_viewRun(t *testing.T) {
tests := []struct {
name string
opts *ViewOptions
wantOut string
wantStderr string
gist *shared.Gist
wantErr bool
name string
opts *ViewOptions
wantOut string
gist *shared.Gist
wantErr bool
}{
{
name: "no such gist",
@ -192,22 +191,6 @@ func Test_viewRun(t *testing.T) {
},
wantOut: "some files\ncicada.txt\n\nbwhiizzzbwhuiiizzzz\n\nfoo.md\n\n- foo\n\n",
},
{
name: "url",
opts: &ViewOptions{
Selector: "https://gist.github.com/octocat/1234",
},
wantErr: true,
wantStderr: "HTTP 404 (https://api.github.com/gists/1234)",
},
{
name: "invalid url",
opts: &ViewOptions{
Selector: "https://gist.github.com/octocat",
},
wantErr: true,
wantStderr: "Invalid gist URL https://gist.github.com/octocat",
},
}
for _, tt := range tests {
@ -235,9 +218,6 @@ func Test_viewRun(t *testing.T) {
err := viewRun(tt.opts)
if tt.wantErr {
assert.Error(t, err)
if tt.wantStderr != "" {
assert.EqualError(t, err, tt.wantStderr)
}
return
}
assert.NoError(t, err)