Skip to content

Commit 5617eb7

Browse files
committed
refactoring of tests
1 parent 71c6145 commit 5617eb7

File tree

5 files changed

+101
-106
lines changed

5 files changed

+101
-106
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ debug
22
arduino
33
arduino-cli
44
main
5-
.vscode/settings.json
5+
.vscode/settings.json
6+
cmd/formatter/debug.test

cmd/formatter/examples_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ import (
66
"github.com/bcmi-labs/arduino-cli/cmd/formatter"
77
)
88

9+
type TestStruct struct {
10+
Value int `json:"value"`
11+
}
12+
13+
func (ts TestStruct) String() string {
14+
return fmt.Sprint("VALUE = ", ts.Value)
15+
}
16+
917
func ExampleJSONFormatter_Print() {
1018
var example struct {
1119
Field1 string `json:"field1"`
@@ -68,3 +76,66 @@ func ExampleJSONFormatter_Format() {
6876
// RESULT: {"field1":"test","field2":10,"field3":{"inner1":"inner test","inner2":10.432412}}
6977
// RESULT: <empty string>
7078
}
79+
80+
func ExampleJSONFormatter_Print_debug() {
81+
valid := TestStruct{20}
82+
invalid := "invalid"
83+
jf := formatter.JSONFormatter{
84+
Debug: false,
85+
}
86+
//using struct
87+
jf.Print(valid)
88+
89+
//using string (invalid sine it's not a struct or a map)
90+
jf.Print(invalid)
91+
92+
jf.Debug = true
93+
jf.Print(valid)
94+
jf.Print(invalid)
95+
96+
//using map
97+
newValue := make(map[string]int)
98+
newValue["value2"] = 10
99+
100+
jf.Print(newValue)
101+
// Output:
102+
// {"value":20}
103+
//
104+
// {"value":20}
105+
// Only structs and maps values are accepted
106+
// {"value2":10}
107+
}
108+
109+
func ExampleFormat() {
110+
formatter.SetFormatter("text")
111+
result, err := formatter.Format(TestStruct{5})
112+
if err == nil {
113+
fmt.Println("RESULT: ", result)
114+
} else {
115+
fmt.Println("ERROR: ", err)
116+
}
117+
118+
formatter.SetFormatter("json")
119+
result, err = formatter.Format(TestStruct{10})
120+
if err == nil {
121+
fmt.Println("RESULT: ", result)
122+
} else {
123+
fmt.Println("ERROR: ", err)
124+
}
125+
result, err = formatter.Format(5)
126+
if err == nil {
127+
if result == "" {
128+
result = "<empty string>"
129+
fmt.Println("RESULT: ", result)
130+
} else {
131+
fmt.Println("unexpected RESULT: ", result)
132+
}
133+
} else {
134+
fmt.Sprintln("ERROR: ", err)
135+
}
136+
137+
// Output:
138+
// RESULT: VALUE = 5
139+
// RESULT: {"value":10}
140+
// RESULT: <empty string>
141+
}

cmd/formatter/formatter_test.go

Lines changed: 1 addition & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1 @@
1-
package formatter
2-
3-
import (
4-
"errors"
5-
"fmt"
6-
"testing"
7-
)
8-
9-
type TestStruct struct {
10-
Value int `json:"value"`
11-
}
12-
13-
func (ts TestStruct) String() string {
14-
return fmt.Sprint("VALUE = ", ts.Value)
15-
}
16-
func TestFormat(test *testing.T) {
17-
SetFormatter("text")
18-
result, err := Format(TestStruct{5})
19-
if err == nil {
20-
fmt.Println("RESULT: ", result)
21-
} else {
22-
fmt.Sprintln("ERROR: ", err)
23-
test.Fail()
24-
}
25-
26-
SetFormatter("json")
27-
result, err = Format(TestStruct{10})
28-
if err == nil {
29-
fmt.Println("RESULT: ", result)
30-
} else {
31-
fmt.Sprintln("ERROR: ", err)
32-
test.Fail()
33-
}
34-
result, err = Format(5)
35-
if err == nil {
36-
if result == "" {
37-
result = "<empty string>"
38-
fmt.Println("RESULT: ", result)
39-
} else {
40-
fmt.Println("unexpected RESULT: ", result)
41-
test.Fail()
42-
}
43-
} else {
44-
fmt.Sprintln("ERROR: ", err)
45-
}
46-
47-
// Output:
48-
// RESULT: VALUE = 5
49-
// RESULT: {value:10}
50-
}
51-
52-
func TestPrint(test *testing.T) {
53-
SetFormatter("text")
54-
Print(TestStruct{5})
55-
56-
SetFormatter("json")
57-
Print(TestStruct{10})
58-
59-
// Output:
60-
// RESULT: VALUE = 5
61-
// RESULT: {value:10}
62-
}
63-
64-
func TestJSONFormatterPrintDebug(test *testing.T) {
65-
valid := TestStruct{20}
66-
invalid := "invalid"
67-
jf := JSONFormatter{
68-
debug: false,
69-
}
70-
//using struct
71-
jf.Print(valid)
72-
73-
//using string (invalid sine it's not a struct or a map)
74-
jf.Print(invalid)
75-
76-
jf.debug = true
77-
jf.Print(valid)
78-
jf.Print(invalid)
79-
80-
//using map
81-
newValue := make(map[string]int)
82-
newValue["value2"] = 10
83-
84-
jf.Print(newValue)
85-
// Output:
86-
// {value:20}
87-
//
88-
// {value:20}
89-
// Only structs and maps values are accepted
90-
// {value2:10}
91-
}
92-
93-
func TestPrintError(test *testing.T) {
94-
SetFormatter("text")
95-
PrintError(errors.New("text error"))
96-
SetFormatter("json")
97-
PrintError(errors.New("json error"))
98-
99-
// Output:
100-
// text error
101-
// {error:"json error"}
102-
}
1+
package formatter_test

cmd/formatter/json.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import "errors"
3535

3636
//JSONFormatter represents a Printer and Formatter of JSON objects.
3737
type JSONFormatter struct {
38-
debug bool //if false, errors are not shown. Unparsable inputs are skipped. Otherwise an error message is shown.
38+
Debug bool //if false, errors are not shown. Unparsable inputs are skipped. Otherwise an error message is shown.
3939
}
4040

4141
// Format formaats a message into a JSON object.
@@ -44,10 +44,10 @@ type JSONFormatter struct {
4444
func (jf JSONFormatter) Format(msg interface{}) (string, error) {
4545
msgType := reflect.TypeOf(msg).Kind().String()
4646
if msgType == "struct" ||
47-
msgType == "map" { // TODO : optimize if possible
47+
msgType == "map" {
4848
ret, err := json.Marshal(msg)
4949
return string(ret), err
50-
} else if jf.debug {
50+
} else if jf.Debug {
5151
return "", errors.New("Only structs and maps values are accepted")
5252
}
5353
return "", nil

cmd/formatter/print_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package formatter_test
22

33
import (
4+
"errors"
45
"fmt"
56

67
"github.com/bcmi-labs/arduino-cli/cmd/formatter"
@@ -43,3 +44,26 @@ func ExamplePrint() {
4344
// Field3.Inner1: inner test
4445
// Field3.Inner2: 10.432412
4546
}
47+
48+
func ExamplePrint_alternative() {
49+
formatter.SetFormatter("text")
50+
formatter.Print(TestStruct{5})
51+
52+
formatter.SetFormatter("json")
53+
formatter.Print(TestStruct{10})
54+
55+
// Output:
56+
// VALUE = 5
57+
// {"value":10}
58+
}
59+
60+
func ExamplePrintError() {
61+
formatter.SetFormatter("text")
62+
formatter.PrintError(errors.New("text error"))
63+
formatter.SetFormatter("json")
64+
formatter.PrintError(errors.New("json error"))
65+
66+
// Output:
67+
// text error
68+
// {"error":"json error"}
69+
}

0 commit comments

Comments
 (0)