From 95159e5585762c06c654945070ba54262b7dcec9 Mon Sep 17 00:00:00 2001 From: Jeff Halter <868228+jhalter@users.noreply.github.com> Date: Sat, 15 Jun 2024 11:13:16 -0700 Subject: 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 --- hotline/field_test.go | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 95 insertions(+), 1 deletion(-) (limited to 'hotline/field_test.go') 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) + }) + } +} -- cgit