-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiagnostic_helper_contract_test.go
More file actions
149 lines (139 loc) · 4.65 KB
/
Copy pathdiagnostic_helper_contract_test.go
File metadata and controls
149 lines (139 loc) · 4.65 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package goav
import (
"errors"
"reflect"
"strings"
"testing"
"github.com/thesyncim/goav/av"
"github.com/thesyncim/goav/errcode"
"github.com/thesyncim/goav/filter"
"github.com/thesyncim/goav/format"
"github.com/thesyncim/goav/shape"
)
func TestRecipeDiagnosticHelperContracts(t *testing.T) {
tests := []struct {
name string
err error
code errcode.Code
}{
{
name: "nil recipe",
err: nilRecipeError("compile job", "nil job"),
code: errcode.JobInvalid,
},
{
name: "runtime missing",
err: runtimeMissingError("compile job"),
code: errcode.RuntimeMissing,
},
{
name: "unconstructed job",
err: unconstructedJobError(),
code: errcode.JobInvalid,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assertBuildErrorCode(t, tt.err, tt.code)
if !errors.Is(tt.err, errUnsupportedBuild) {
t.Fatalf("err = %v, want errUnsupportedBuild cause", tt.err)
}
})
}
if got := resizeModesFromStrings(nil); got != nil {
t.Fatalf("nil resize modes = %#v, want nil", got)
}
modes := resizeModesFromStrings([]string{"exact", "", "fit", "fill"})
wantModes := []filter.ResizeMode{filter.ResizeExact, filter.ResizeFit, filter.ResizeFill}
if !reflect.DeepEqual(modes, wantModes) {
t.Fatalf("resize modes = %#v, want %#v", modes, wantModes)
}
if got := joinCodecIDs(nil); got != "" {
t.Fatalf("empty codec ids = %q, want empty", got)
}
if got := joinCodecIDs([]av.CodecID{av.CodecVP8, "", av.CodecOpus, av.CodecID("x_custom")}); got != "vp8,opus,x_custom" {
t.Fatalf("codec ids = %q", got)
}
}
func TestOutputFormatProbeErrorContract(t *testing.T) {
output := format.Output{
Name: "archive",
URI: "file://archive.custom",
Protocol: av.ProtocolFile,
MIMEType: "video/custom",
}
err := outputFormatProbeError(output, 3, format.ErrNotFound)
assertBuildErrorCode(t, err, outputFormatUnknownCode)
if !errors.Is(err, format.ErrNotFound) {
t.Fatalf("err = %v, want format.ErrNotFound cause", err)
}
var buildErr *BuildError
if !errors.As(err, &buildErr) {
t.Fatalf("err = %T, want *BuildError", err)
}
wantDetails := []string{
"name=archive",
"uri=file://archive.custom",
"protocol=file",
"mime=video/custom",
}
if !reflect.DeepEqual(buildErr.DetailLines(), wantDetails) {
t.Fatalf("details = %#v, want %#v", buildErr.DetailLines(), wantDetails)
}
if got, ok := buildErr.Detail("protocol"); !ok || got != av.ProtocolFile {
t.Fatalf("protocol detail = %#v, %v; want %q, true", got, ok, av.ProtocolFile)
}
if got, ok := buildErr.Detail("mime"); !ok || got != "video/custom" {
t.Fatalf("mime detail = %#v, %v; want video/custom, true", got, ok)
}
if len(buildErr.fields) != len(wantDetails) {
t.Fatalf("typed fields = %#v, want one field per rendered detail", buildErr.fields)
}
if len(buildErr.fixes) == 0 {
t.Fatal("typed fixes are empty")
}
cause := errors.New("probe crashed")
if got := outputFormatProbeError(output, 3, cause); got != cause {
t.Fatalf("non-not-found cause = %v, want original cause", got)
}
}
func TestCustomStreamPredicateAndPreferenceDropReasons(t *testing.T) {
match := MatchStream(func(stream av.Stream) bool {
return strings.HasPrefix(string(stream.ID), "external-") && stream.Codec.ID == av.CodecOpus
})
if !match.Matches(av.Stream{ID: "external-audio", Codec: av.CodecParameters{ID: av.CodecOpus}}) {
t.Fatal("custom stream predicate did not match")
}
if match.Matches(av.Stream{ID: "internal-audio", Codec: av.CodecParameters{ID: av.CodecOpus}}) {
t.Fatal("custom stream predicate matched the wrong stream")
}
if got := match.Description(); got != "custom" {
t.Fatalf("description = %q, want custom", got)
}
ambiguous := &shapeAdapterSelectionError{
media: av.MediaVideo,
needed: shape.AllowResize(),
candidates: []string{"resize-a", "resize-b"},
cause: errShapeAdapterAmbiguous,
}
if !errors.Is(ambiguous, errShapeAdapterAmbiguous) {
t.Fatalf("ambiguous error = %v, want errShapeAdapterAmbiguous", ambiguous)
}
if got := ambiguous.Error(); got != errShapeAdapterAmbiguous.Error() {
t.Fatalf("ambiguous Error() = %q", got)
}
if got := preferenceDropReason(ambiguous); !strings.Contains(got, "resize-a, resize-b") {
t.Fatalf("ambiguous preference reason = %q", got)
}
missing := &shapeAdapterSelectionError{
media: av.MediaAudio,
needed: shape.AllowResample(),
cause: errShapeAdapterMissing,
}
if got := preferenceDropReason(missing); got != "no registered adapter can perform the preferred conversion" {
t.Fatalf("missing preference reason = %q", got)
}
if got := preferenceDropReason(errors.New("plain")); got != "the preferred conversion cannot be planned" {
t.Fatalf("plain preference reason = %q", got)
}
}