-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathapi_change_test.go
85 lines (71 loc) · 2.83 KB
/
api_change_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
73
74
75
76
77
78
79
80
81
82
83
84
85
package checker_test
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/tufin/oasdiff/checker"
"github.com/tufin/oasdiff/load"
)
var apiChange = checker.ApiChange{
Id: "change_id",
Args: []any{},
Comment: "comment_id",
Level: checker.ERR,
Operation: "GET",
OperationId: "123",
Path: "/test",
Source: load.NewSource("source"),
SourceFile: "sourceFile",
SourceLine: 1,
SourceLineEnd: 2,
SourceColumn: 3,
SourceColumnEnd: 4,
}
func TestApiChange(t *testing.T) {
require.Equal(t, "paths", apiChange.GetSection())
require.Equal(t, "change_id", apiChange.GetId())
require.Equal(t, "comment", apiChange.GetComment(MockLocalizer))
require.Equal(t, checker.ERR, apiChange.GetLevel())
require.Equal(t, "GET", apiChange.GetOperation())
require.Equal(t, "123", apiChange.GetOperationId())
require.Equal(t, "/test", apiChange.GetPath())
require.Equal(t, "source", apiChange.GetSource())
require.Equal(t, "sourceFile", apiChange.GetSourceFile())
require.Equal(t, 1, apiChange.GetSourceLine())
require.Equal(t, 2, apiChange.GetSourceLineEnd())
require.Equal(t, 3, apiChange.GetSourceColumn())
require.Equal(t, 4, apiChange.GetSourceColumnEnd())
require.Equal(t, "error at source, in API GET /test This is a breaking change. [change_id]. comment", apiChange.SingleLineError(MockLocalizer, checker.ColorNever))
}
func MockLocalizer(originalKey string, args ...interface{}) string {
switch originalKey {
case "change_id":
return "This is a breaking change."
case "comment_id":
return "comment"
default:
return originalKey
}
}
func TestApiChange_MatchIgnore(t *testing.T) {
require.True(t, apiChange.MatchIgnore("/test", "error at source, in api get /test this is a breaking change. [change_id]. comment", MockLocalizer))
}
func TestApiChange_MultiLineError(t *testing.T) {
require.Equal(t, "error\t[change_id] at source\t\n\tin API GET /test\n\t\tThis is a breaking change.\n\t\tcomment", apiChange.MultiLineError(MockLocalizer, checker.ColorNever))
}
func TestApiChange_MultiLineError_NoComment(t *testing.T) {
apiChangeNoComment := apiChange
apiChangeNoComment.Comment = ""
require.Equal(t, "error\t[change_id] at source\t\n\tin API GET /test\n\t\tThis is a breaking change.", apiChangeNoComment.MultiLineError(MockLocalizer, checker.ColorNever))
}
func TestApiChange_SourceFile(t *testing.T) {
apiChangeSourceFile := apiChange
apiChangeSourceFile.SourceFile = ""
apiChangeSourceFile.Source = load.NewSource("spec.yaml")
require.Equal(t, "spec.yaml", apiChangeSourceFile.GetSourceFile())
}
func TestApiChange_SourceUrl(t *testing.T) {
apiChangeSourceFile := apiChange
apiChangeSourceFile.SourceFile = ""
apiChangeSourceFile.Source = load.NewSource("http://google.com/spec.yaml")
require.Equal(t, "", apiChangeSourceFile.GetSourceFile())
}