-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformats_test.go
More file actions
77 lines (70 loc) · 2.73 KB
/
formats_test.go
File metadata and controls
77 lines (70 loc) · 2.73 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
//go:build unit
package srljrpc_test
import (
"fmt"
"testing"
"github.com/azyablov/srljrpc/formats"
)
func TestFormats(t *testing.T) {
// Verify default format is JSON
of := formats.OutputFormat{}
rof, err := of.GetFormat()
if err != nil || rof != formats.JSON {
t.Errorf("Default format should be JSON, but got %v", rof)
}
// Table driven tests
var testData = []struct {
testName string
format formats.EnumOutputFormats
expErrSet error
expErrGet error
errMsg string
}{
{testName: "Setting format to JSON", format: formats.JSON, expErrSet: nil, expErrGet: nil, errMsg: "format JSON isn't set properly: "},
{testName: "Setting format to TEXT", format: formats.TEXT, expErrSet: nil, expErrGet: nil, errMsg: "format TEXT isn't set properly: "},
{testName: "Setting format to TABLE", format: formats.TABLE, expErrSet: nil, expErrGet: nil, errMsg: "format TABLE isn't set properly: "},
{testName: "Setting format to non existent format bar", format: formats.EnumOutputFormats("bar"), expErrSet: fmt.Errorf(formats.SetErrMsg),
expErrGet: nil, errMsg: "fake format bar was handled incorrectly: "},
}
for _, td := range testData {
t.Run(td.testName, func(t *testing.T) {
of := formats.OutputFormat{}
err := of.SetFormat(td.format)
switch {
case err == nil && td.expErrSet == nil:
case err != nil && td.expErrSet != nil:
if err.Error() != td.expErrSet.Error() {
t.Errorf(td.errMsg+"got %s, while should be %s", err, td.expErrSet)
}
case err == nil && td.expErrSet != nil:
t.Errorf(td.errMsg+"got %s, while should be %s", err, td.expErrSet)
case err != nil && td.expErrSet == nil:
t.Errorf(td.errMsg+"got %s, while should be %s", err, td.expErrSet)
default:
t.Errorf(td.errMsg+"got %s, while should be %s", err, td.expErrSet)
}
rof, err := of.GetFormat()
switch {
case err == nil && td.expErrGet == nil:
// while SetFormat must failing, GetFormat must not get the same result
if rof == td.format && td.expErrSet != nil {
t.Errorf(td.errMsg+"got %v, while should be %v", rof, td.format)
}
// if SetFormat is ok, then GetFormat must return the same result
if rof != td.format && td.expErrSet == nil {
t.Errorf(td.errMsg+"got %v, while should be %v", rof, td.format)
}
case err != nil && td.expErrGet != nil:
if err.Error() != td.expErrGet.Error() {
t.Errorf(td.errMsg+"got %s, while should be %s", err, td.expErrGet)
}
case err == nil && td.expErrGet != nil:
t.Errorf(td.errMsg+"got %s, while should be %s", err, td.expErrGet)
case err != nil && td.expErrGet == nil:
t.Errorf(td.errMsg+"got %s, while should be %s", err, td.expErrGet)
default:
t.Errorf(td.errMsg+"got %s, while should be %s", err, td.expErrGet)
}
})
}
}