diff options
| author | Jeff Halter <868228+jhalter@users.noreply.github.com> | 2024-06-15 11:13:16 -0700 |
|---|---|---|
| committer | Jeff Halter <868228+jhalter@users.noreply.github.com> | 2024-06-15 11:13:16 -0700 |
| commit | 95159e5585762c06c654945070ba54262b7dcec9 (patch) | |
| tree | 23609018c1460b056ce22067290ea12ee851d483 /hotline/field_test.go | |
| parent | a6216dd89252fa01dc176f98f1e4ecfd3f637566 (diff) | |
Refactoring and cleanup
* Split CLI client into separate project
* Convert more functions to follow common Golang idioms e.g io.Reader, io.Writer
* Use ldflags for versioning
* Misc cleanup and simplification
Diffstat (limited to 'hotline/field_test.go')
| -rw-r--r-- | hotline/field_test.go | 96 |
1 files changed, 95 insertions, 1 deletions
diff --git a/hotline/field_test.go b/hotline/field_test.go index fb686dc..317c2d9 100644 --- a/hotline/field_test.go +++ b/hotline/field_test.go @@ -1,7 +1,101 @@ package hotline -import "testing" +import ( + "fmt" + "github.com/stretchr/testify/assert" + "testing" +) func TestHello(t *testing.T) { } + +func Test_fieldScanner(t *testing.T) { + type args struct { + data []byte + in1 bool + } + tests := []struct { + name string + args args + wantAdvance int + wantToken []byte + wantErr assert.ErrorAssertionFunc + }{ + { + name: "when too few bytes are provided to read the field size", + args: args{ + data: []byte{}, + in1: false, + }, + wantAdvance: 0, + wantToken: []byte(nil), + wantErr: assert.NoError, + }, + { + name: "when too few bytes are provided to read the full payload", + args: args{ + data: []byte{ + 0, 1, + 0, 4, + 0, 0, + }, + in1: false, + }, + wantAdvance: 0, + wantToken: []byte(nil), + wantErr: assert.NoError, + }, + { + name: "when a full field is provided", + args: args{ + data: []byte{ + 0, 1, + 0, 4, + 0, 0, + 0, 0, + }, + in1: false, + }, + wantAdvance: 8, + wantToken: []byte{ + 0, 1, + 0, 4, + 0, 0, + 0, 0, + }, + wantErr: assert.NoError, + }, + { + name: "when a full field plus extra bytes are provided", + args: args{ + data: []byte{ + 0, 1, + 0, 4, + 0, 0, + 0, 0, + 1, 1, + }, + in1: false, + }, + wantAdvance: 8, + wantToken: []byte{ + 0, 1, + 0, 4, + 0, 0, + 0, 0, + }, + wantErr: assert.NoError, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotAdvance, gotToken, err := fieldScanner(tt.args.data, tt.args.in1) + if !tt.wantErr(t, err, fmt.Sprintf("fieldScanner(%v, %v)", tt.args.data, tt.args.in1)) { + return + } + assert.Equalf(t, tt.wantAdvance, gotAdvance, "fieldScanner(%v, %v)", tt.args.data, tt.args.in1) + assert.Equalf(t, tt.wantToken, gotToken, "fieldScanner(%v, %v)", tt.args.data, tt.args.in1) + }) + } +} |