Skip to content

Commit 8c3a403

Browse files
committed
more documentation and one more example
1 parent b724ca8 commit 8c3a403

File tree

8 files changed

+83
-1
lines changed

8 files changed

+83
-1
lines changed

README.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,33 @@
1-
# clean-test-code-showcase
1+
# golang-clean-test-code-showcase
2+
3+
A showcase, of how unit test can be written.
4+
No claim to be complete here.
5+
6+
### Business requirement - for this demo purpose
7+
8+
This showcase/demo library has some acceptance criteria
9+
10+
* as a developer I would like to extract meaningful information from a condensed firmware string literal
11+
* the information extraction must work on a single line string
12+
* the information extraction must work on multi-line strings, separated by newline (\\n)
13+
* these fields must be available in the meaningful extracted information (struct)
14+
* FirmwareName,TargetName,TargetDetail,Version,ReleaseDateStr,GitHash,ReleaseTime
15+
16+
Example firmware String: \
17+
```Betaflight / SPRACINGF3EVO (SPEV) 3.4.0 Apr 17 2018 / 14:00:13 (b2c247d34) MSP API: 1.39```
18+
19+
### Implementations
20+
21+
1. Using native/vanilla GO language and library
22+
2. Using famous [stretchr/testify](https://github.com/stretchr/testify) library
23+
3. Using broken and outdated [rdrdr/hamcrest](https://github.com/corbym/gocrest) library
24+
4. Using almost unknown [corbym/gocrest](https://github.com/corbym/gocrest) library
25+
26+
### What is / Why "HAMCREST"?
27+
28+
[Hamcrest](http://hamcrest.org/) is a famous Java Library, that was developed to make
29+
unit tests in Java more readable and follow [Clean Code principles](https://de.wikipedia.org/wiki/Clean_Code)
30+
31+
Cite from hamcrest.org ... \
32+
*Matchers that can be combined to create flexible expressions of intent* \
33+
*Born in Java, Hamcrest now has implementations in a number of languages*

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ go 1.14
44

55
require (
66
github.com/corbym/gocrest v1.0.3
7+
github.com/rdrdr/hamcrest v0.0.0-20110214224206-601bfe136b59
78
github.com/stretchr/testify v1.6.1
89
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
44
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
55
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
66
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
7+
github.com/rdrdr/hamcrest v0.0.0-20110214224206-601bfe136b59 h1:89mA0FGOUc6g/K1lNBI50bm73w2YQY0jtWEpF4BSj4c=
8+
github.com/rdrdr/hamcrest v0.0.0-20110214224206-601bfe136b59/go.mod h1:/KOFTlW27+voCV5Icl9s18JDpulqhqVBu2iIIAK50ps=
79
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
810
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
911
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=

parser_1_golang_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
)
66

77
// See https://golang.org/pkg/testing/
8+
// The quasi standard ;)
89

910
func Test_betaflight_message_can_be_parsed__with_GOLANG(t *testing.T) {
1011
info := ParseFirmwareInformation("# Betaflight / SPRACINGF3EVO (SPEV) 3.4.0 Apr 17 2018 / 14:00:13 (b2c247d34) MSP API: 1.39")

parser_2_testify_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import (
55
"testing"
66
)
77

8+
// See https://github.com/stretchr/testify
9+
// (ca. 12k Stars on Github)
10+
811
func Test_betaflight_message_can_be_parsed__with_TESTIFY(t *testing.T) {
912
info := ParseFirmwareInformation("# Betaflight / SPRACINGF3EVO (SPEV) 3.4.0 Apr 17 2018 / 14:00:13 (b2c247d34) MSP API: 1.39")
1013

parser_3_hamcrest_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package parser
2+
3+
import (
4+
"github.com/rdrdr/hamcrest/asserter"
5+
"github.com/rdrdr/hamcrest/core"
6+
"testing"
7+
)
8+
9+
// See https://github.com/corbym/gocrest
10+
// (exactly 27 Stars on Github)
11+
12+
// BROKEN ?!?! Last Release from 2011?
13+
14+
func Test_betaflight_message_can_be_parsed__with_HAMCREST(t *testing.T) {
15+
info := ParseFirmwareInformation("# Betaflight / SPRACINGF3EVO (SPEV) 3.4.0 Apr 17 2018 / 14:00:13 (b2c247d34) MSP API: 1.39")
16+
17+
we := asserter.Using(t)
18+
we.AssertThat(info.FirmwareName, core.EqualTo("Betaflight").Comment("FirmwareName field"))
19+
we.AssertThat(info.TargetName, core.EqualTo("SPRACINGF3EVO"))
20+
we.AssertThat(info.TargetDetail, core.EqualTo("SPEV"))
21+
we.AssertThat(info.Version, core.GreaterThanOrEqualTo(int64(3)))
22+
we.AssertThat(info.ReleaseDateStr, core.EqualTo("Apr 17 2018"))
23+
we.AssertThat(info.ReleaseTime, core.EqualTo("14:00:13"))
24+
we.AssertThat(info.GitHash, core.EqualTo("b2c247d34"))
25+
}
26+
27+
func Test_parsing_multiple_lines_returns_first_hit__with_HAMCREST(t *testing.T) {
28+
info := ParseFirmwareInformation(
29+
"# bla blubber\n" +
30+
"# Betaflight1 / SPRACINGF3EVO1 (SPEV) 3.4.0 Apr 17 2018 / 14:00:13 (b2c247d34) MSP API: 1.39\n" +
31+
"# Betaflight2 / SPRACINGF3EVO2 (SPEV) 3.4.0 Apr 17 2018 / 14:00:13 (b2c247d34) MSP API: 1.39",
32+
)
33+
34+
we := asserter.Using(t)
35+
we.AssertThat(info.FirmwareName, core.EqualTo("Betaflight1").Comment("FirmwareName"))
36+
we.AssertThat(info.TargetName, core.EqualTo("SPRACINGF3EVO1"))
37+
}

parser_3_gocrest_test.go renamed to parser_4_gocrest_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import (
66
"testing"
77
)
88

9+
// See https://github.com/corbym/gocrest
10+
// (exactly 11 Stars on Github)
11+
912
func Test_betaflight_message_can_be_parsed__with_GOCREST(t *testing.T) {
1013
info := ParseFirmwareInformation("# Betaflight / SPRACINGF3EVO (SPEV) 3.4.0 Apr 17 2018 / 14:00:13 (b2c247d34) MSP API: 1.39")
1114

parser_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import (
55
"testing"
66
)
77

8+
// AUTO GENERATED code by IntelliJ
9+
// TODO implement ;)
10+
811
func TestParseFirmwareInformation(t *testing.T) {
912
type args struct {
1013
text string

0 commit comments

Comments
 (0)