Conditions | 6 |
Total Lines | 33 |
Code Lines | 25 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | package devto |
||
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 | } |
||
50 |