-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
123 lines (106 loc) · 3.25 KB
/
Copy patherrors_test.go
File metadata and controls
123 lines (106 loc) · 3.25 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package publiccode
import (
"encoding/json"
"testing"
)
func TestParseErrorError(t *testing.T) {
e := ParseError{Reason: "something went wrong"}
if e.Error() != "something went wrong" {
t.Errorf("unexpected: %q", e.Error())
}
}
func TestValidationErrorErrorEmptyKey(t *testing.T) {
e := ValidationError{Key: "", Description: "bad field", Line: 1, Column: 2}
got := e.Error()
want := "publiccode.yml:1:2: error: bad field"
if got != want {
t.Errorf("got %q, want %q", got, want)
}
}
func TestValidationErrorErrorWithKey(t *testing.T) {
e := ValidationError{Key: "foo", Description: "bad field", Line: 3, Column: 4}
got := e.Error()
want := "publiccode.yml:3:4: error: foo: bad field"
if got != want {
t.Errorf("got %q, want %q", got, want)
}
}
func TestValidationErrorErrorWithFile(t *testing.T) {
e := ValidationError{File: "my-amazing-code.yaml", Key: "foo", Description: "bad field", Line: 3, Column: 4}
got := e.Error()
want := "my-amazing-code.yaml:3:4: error: foo: bad field"
if got != want {
t.Errorf("got %q, want %q", got, want)
}
}
func TestValidationWarningErrorWithFile(t *testing.T) {
e := ValidationWarning{File: "my-amazing-code.yaml", Key: "foo", Description: "minor issue", Line: 5, Column: 6}
got := e.Error()
want := "my-amazing-code.yaml:5:6: warning: foo: minor issue"
if got != want {
t.Errorf("got %q, want %q", got, want)
}
}
func TestValidationErrorMarshalJSON(t *testing.T) {
e := ValidationError{Key: "foo", Description: "bad", Line: 1, Column: 1}
b, err := e.MarshalJSON()
if err != nil {
t.Fatal(err)
}
var m map[string]interface{}
if err := json.Unmarshal(b, &m); err != nil {
t.Fatal(err)
}
if m["type"] != "error" {
t.Errorf("expected type=error, got %v", m["type"])
}
if m["key"] != "foo" {
t.Errorf("expected key=foo, got %v", m["key"])
}
}
func TestValidationWarningMarshalJSON(t *testing.T) {
e := ValidationWarning{Key: "foo", Description: "minor", Line: 2, Column: 3}
b, err := e.MarshalJSON()
if err != nil {
t.Fatal(err)
}
var m map[string]interface{}
if err := json.Unmarshal(b, &m); err != nil {
t.Fatal(err)
}
if m["type"] != "warning" {
t.Errorf("expected type=warning, got %v", m["type"])
}
}
func TestValidationErrorMarshalJSONFile(t *testing.T) {
e := ValidationError{File: "my-amazing-code.yaml", Key: "foo", Description: "bad", Line: 1, Column: 2}
b, err := e.MarshalJSON()
if err != nil {
t.Fatal(err)
}
got := string(b)
want := `{"file":"my-amazing-code.yaml","key":"foo","description":"bad","line":1,"column":2,"type":"error"}`
if got != want {
t.Errorf("got %s, want %s", got, want)
}
}
func TestValidationWarningMarshalJSONFile(t *testing.T) {
e := ValidationWarning{File: "my-amazing-code.yaml", Key: "foo", Description: "minor", Line: 1, Column: 2}
b, err := e.MarshalJSON()
if err != nil {
t.Fatal(err)
}
got := string(b)
want := `{"file":"my-amazing-code.yaml","key":"foo","description":"minor","line":1,"column":2,"type":"warning"}`
if got != want {
t.Errorf("got %s, want %s", got, want)
}
}
func TestValidationWarningErrorEmptyKey(t *testing.T) {
e := ValidationWarning{Key: "", Description: "minor issue", Line: 5, Column: 6}
got := e.Error()
want := "publiccode.yml:5:6: warning: minor issue"
if got != want {
t.Errorf("got %q, want %q", got, want)
}
}