Skip to content

Commit b724ca8

Browse files
committed
add test showcases
1 parent 070b44f commit b724ca8

8 files changed

+155
-29
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea
2+
*.iml

go.mod

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module github.com/nitram509/golang-clean-test-code-showcase
2+
3+
go 1.14
4+
5+
require (
6+
github.com/corbym/gocrest v1.0.3
7+
github.com/stretchr/testify v1.6.1
8+
)

go.sum

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
github.com/corbym/gocrest v1.0.3 h1:gwEdq6RkTmq+09CTuM29DfKOCtZ7G7bcyxs3IZ6EVdU=
2+
github.com/corbym/gocrest v1.0.3/go.mod h1:maVFL5lbdS2PgfOQgGRWDYTeunSWQeiEgoNdTABShCs=
3+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
4+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
6+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
7+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
8+
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
9+
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
10+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
11+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
12+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
13+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

parser.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
package configurations
1+
package parser
22

33
import (
44
"regexp"
5+
"strconv"
56
"strings"
67
)
78

89
type FirmwareInformation struct {
910
FirmwareName string
1011
TargetName string
1112
TargetDetail string
12-
Version string
13+
Version int64
1314
ReleaseDateStr string
1415
GitHash string
1516
ReleaseTime string
16-
// extended
17-
ReleaseNotesUrl string
1817
}
1918

2019
func ParseFirmwareInformation(text string) *FirmwareInformation {
@@ -34,7 +33,7 @@ func createSearchExpression() (*regexp.Regexp, error) {
3433
"(\\w+)?\\s/\\s" +
3534
"(\\w+)?\\s" +
3635
"\\((\\w+)\\)\\s" +
37-
"(\\d+\\.\\d+\\.\\d+)?\\s" +
36+
"(\\d+)\\.\\d+\\.\\d+\\s" +
3837
"(\\w+\\s\\d+\\s\\d{4})?\\s/\\s" +
3938
"(\\d+:\\d+:\\d+)?\\s" +
4039
"\\((\\w+)\\)?")
@@ -52,7 +51,7 @@ func parseLine(regex *regexp.Regexp, text string) *FirmwareInformation {
5251
info.TargetDetail = string(submatch[3])
5352
}
5453
if len(submatch) > 4 {
55-
info.Version = string(submatch[4])
54+
info.Version, _ = strconv.ParseInt(string(submatch[4]), 10, 64)
5655
}
5756
if len(submatch) > 5 {
5857
info.ReleaseDateStr = string(submatch[5])

parser_1_golang_test.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package parser
2+
3+
import (
4+
"testing"
5+
)
6+
7+
// See https://golang.org/pkg/testing/
8+
9+
func Test_betaflight_message_can_be_parsed__with_GOLANG(t *testing.T) {
10+
info := ParseFirmwareInformation("# Betaflight / SPRACINGF3EVO (SPEV) 3.4.0 Apr 17 2018 / 14:00:13 (b2c247d34) MSP API: 1.39")
11+
12+
if info.FirmwareName != "Betaflight" {
13+
t.Errorf("FirmwareName = %s; want Betaflight", info.FirmwareName)
14+
}
15+
if info.TargetName != "SPRACINGF3EVO" {
16+
t.Errorf("TargetName = %s; want SPRACINGF3EVO", info.TargetName)
17+
}
18+
if info.TargetDetail != "SPEV" {
19+
t.Errorf("TargetDetail = %s; want SPEV", info.TargetDetail)
20+
}
21+
if !(info.Version >= int64(3)) {
22+
t.Errorf("Version = %d; want less than 3", info.Version)
23+
}
24+
if info.ReleaseDateStr != "Apr 17 2018" {
25+
t.Errorf("ReleaseDateStr = %s; want Apr 17 2018", info.ReleaseDateStr)
26+
}
27+
if info.ReleaseTime != "14:00:13" {
28+
t.Errorf("ReleaseTime = %s; want 14:00:13", info.ReleaseTime)
29+
}
30+
if info.GitHash != "b2c247d34" {
31+
t.Errorf("GitHash = %s; want b2c247d34", info.GitHash)
32+
}
33+
}
34+
35+
func Test_xparsing_multiple_lines_returns_first_hit__with_GOLANG(t *testing.T) {
36+
info := ParseFirmwareInformation(
37+
"# bla blubber\n" +
38+
"# Betaflight1 / SPRACINGF3EVO1 (SPEV) 3.4.0 Apr 17 2018 / 14:00:13 (b2c247d34) MSP API: 1.39\n" +
39+
"# Betaflight2 / SPRACINGF3EVO2 (SPEV) 3.4.0 Apr 17 2018 / 14:00:13 (b2c247d34) MSP API: 1.39",
40+
)
41+
42+
if info.FirmwareName != "Betaflight1" {
43+
t.Errorf("FirmwareName = %s; want Betaflight1", info.FirmwareName)
44+
}
45+
if info.TargetName != "SPRACINGF3EVO1" {
46+
t.Errorf("TargetName = %s; want SPRACINGF3EVO1", info.TargetName)
47+
}
48+
}

parser_2_testify_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package parser
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func Test_betaflight_message_can_be_parsed__with_TESTIFY(t *testing.T) {
9+
info := ParseFirmwareInformation("# Betaflight / SPRACINGF3EVO (SPEV) 3.4.0 Apr 17 2018 / 14:00:13 (b2c247d34) MSP API: 1.39")
10+
11+
assert.Equal(t, info.FirmwareName, "Betaflight")
12+
assert.Equal(t, info.TargetName, "SPRACINGF3EVO")
13+
assert.Contains(t, info.TargetName, "RACING")
14+
assert.GreaterOrEqualf(t, info.Version, int64(3), "error message %s")
15+
assert.Equal(t, info.ReleaseDateStr, "Apr 17 2018")
16+
assert.Equal(t, info.ReleaseTime, "14:00:13")
17+
assert.Equal(t, info.GitHash, "b2c247d34")
18+
}
19+
20+
func Test_parsing_multiple_lines_returns_first_hit__with_TESTIFY(t *testing.T) {
21+
info := ParseFirmwareInformation(
22+
"# bla blubber\n" +
23+
"# Betaflight1 / SPRACINGF3EVO1 (SPEV) 3.4.0 Apr 17 2018 / 14:00:13 (b2c247d34) MSP API: 1.39\n" +
24+
"# Betaflight2 / SPRACINGF3EVO2 (SPEV) 3.4.0 Apr 17 2018 / 14:00:13 (b2c247d34) MSP API: 1.39",
25+
)
26+
27+
assert.Equal(t, "Betaflight1", info.FirmwareName)
28+
assert.Equal(t, "SPRACINGF3EVO1", info.TargetName)
29+
}

parser_3_gocrest_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package parser
2+
3+
import (
4+
"github.com/corbym/gocrest/is"
5+
"github.com/corbym/gocrest/then"
6+
"testing"
7+
)
8+
9+
func Test_betaflight_message_can_be_parsed__with_GOCREST(t *testing.T) {
10+
info := ParseFirmwareInformation("# Betaflight / SPRACINGF3EVO (SPEV) 3.4.0 Apr 17 2018 / 14:00:13 (b2c247d34) MSP API: 1.39")
11+
12+
then.AssertThat(t, info.FirmwareName, is.EqualTo("Betaflight"))
13+
then.AssertThat(t, info.TargetName, is.EqualTo("SPRACINGF3EVO"))
14+
then.AssertThat(t, info.TargetDetail, is.EqualTo("SPEV"))
15+
then.AssertThat(t, info.Version, is.GreaterThanOrEqualTo(int64(3)))
16+
then.AssertThat(t, info.ReleaseDateStr, is.EqualTo("Apr 17 2018"))
17+
then.AssertThat(t, info.ReleaseTime, is.EqualTo("14:00:13"))
18+
then.AssertThat(t, info.GitHash, is.EqualTo("b2c247d34"))
19+
}
20+
21+
func Test_parsing_multiple_lines_returns_first_hit__with_GOCREST(t *testing.T) {
22+
info := ParseFirmwareInformation(
23+
"# bla blubber\n" +
24+
"# Betaflight1 / SPRACINGF3EVO1 (SPEV) 3.4.0 Apr 17 2018 / 14:00:13 (b2c247d34) MSP API: 1.39\n" +
25+
"# Betaflight2 / SPRACINGF3EVO2 (SPEV) 3.4.0 Apr 17 2018 / 14:00:13 (b2c247d34) MSP API: 1.39",
26+
)
27+
28+
then.AssertThat(t, info.FirmwareName, is.EqualTo("Betaflight1"))
29+
then.AssertThat(t, info.TargetName, is.EqualTo("SPRACINGF3EVO1"))
30+
}

parser_test.go

+20-23
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,26 @@
1-
package configurations
1+
package parser
22

33
import (
4-
"github.com/corbym/gocrest/is"
5-
"github.com/corbym/gocrest/then"
4+
"reflect"
65
"testing"
76
)
87

9-
func Test_betaflight_message_can_be_parsed(t *testing.T) {
10-
info := ParseFirmwareInformation("# Betaflight / SPRACINGF3EVO (SPEV) 3.4.0 Apr 17 2018 / 14:00:13 (b2c247d34) MSP API: 1.39")
11-
then.AssertThat(t, info.FirmwareName, is.EqualTo("Betaflight"))
12-
then.AssertThat(t, info.TargetName, is.EqualTo("SPRACINGF3EVO"))
13-
then.AssertThat(t, info.TargetDetail, is.EqualTo("SPEV"))
14-
then.AssertThat(t, info.Version, is.EqualTo("3.4.0"))
15-
then.AssertThat(t, info.ReleaseDateStr, is.EqualTo("Apr 17 2018"))
16-
then.AssertThat(t, info.ReleaseTime, is.EqualTo("14:00:13"))
17-
then.AssertThat(t, info.GitHash, is.EqualTo("b2c247d34"))
18-
}
19-
20-
func Test_parsing_multiple_lines_returns_first_hit(t *testing.T) {
21-
info := ParseFirmwareInformation(
22-
"# bla blubber\n" +
23-
"# Betaflight1 / SPRACINGF3EVO1 (SPEV) 3.4.0 Apr 17 2018 / 14:00:13 (b2c247d34) MSP API: 1.39\n" +
24-
"# Betaflight2 / SPRACINGF3EVO2 (SPEV) 3.4.0 Apr 17 2018 / 14:00:13 (b2c247d34) MSP API: 1.39",
25-
)
26-
27-
then.AssertThat(t, info.FirmwareName, is.EqualTo("Betaflight1"))
28-
then.AssertThat(t, info.TargetName, is.EqualTo("SPRACINGF3EVO1"))
8+
func TestParseFirmwareInformation(t *testing.T) {
9+
type args struct {
10+
text string
11+
}
12+
tests := []struct {
13+
name string
14+
args args
15+
want *FirmwareInformation
16+
}{
17+
// TODO: Add test cases.
18+
}
19+
for _, tt := range tests {
20+
t.Run(tt.name, func(t *testing.T) {
21+
if got := ParseFirmwareInformation(tt.args.text); !reflect.DeepEqual(got, tt.want) {
22+
t.Errorf("ParseFirmwareInformation() = %v, want %v", got, tt.want)
23+
}
24+
})
25+
}
2926
}

0 commit comments

Comments
 (0)