Passed
Push — master ( 7a190b...51e7b4 )
by Victor Hugo
01:04 queued 11s
created

devto.TestWebURL_UnmarshalJSON   B

Complexity

Conditions 6

Size

Total Lines 33
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 25
nop 1
dl 0
loc 33
rs 8.3466
c 0
b 0
f 0
1
package devto
2
3
import (
4
	"encoding/json"
5
	"errors"
6
	"io/ioutil"
7
	"reflect"
8
	"strings"
9
	"testing"
10
)
11
12
func TestWebURL_UnmarshalJSON(t *testing.T) {
13
	tests := []struct {
14
		name    string
15
		content string
16
		wantErr bool
17
		err     error
18
	}{
19
		{
20
			"Unmarshal works fine",
21
			`{"visit": "https://dev.to/victoravelar"}`,
22
			false,
23
			nil,
24
		},
25
		{
26
			"Unmarshal fails when parsing url",
27
			`{"visit": " http://localhost"}`,
28
			false,
29
			errors.New("parse  http://localhost: first path segment in URL cannot contain colon"),
30
		},
31
	}
32
33
	type tester struct {
34
		Visit WebURL `json:"visit"`
35
	}
36
37
	for _, tt := range tests {
38
		t.Run(tt.name, func(t *testing.T) {
39
			var wu tester
40
			c, err := ioutil.ReadAll(strings.NewReader(tt.content))
41
			json.Unmarshal(c, &wu)
42
			if tt.wantErr && err != nil {
43
				if !reflect.DeepEqual(tt.err, err) {
44
					t.Errorf("want: %v, got: %v", tt.err, err)
45
				}
46
			}
47
		})
48
	}
49
}
50