From ff4e3d3a308f58dd83b84a079009c93e7964bc99 Mon Sep 17 00:00:00 2001 From: resoluteCoder Date: Tue, 15 Aug 2023 15:53:24 -0500 Subject: [PATCH] Test the rest of the framer.go methods --- .gitignore | 1 + pkg/framer/framer_test.go | 74 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/.gitignore b/.gitignore index 5beaf46c9..3ff8b4cfd 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,4 @@ kubectl .vagrant/ /docs/build /dist +coverage.txt \ No newline at end of file diff --git a/pkg/framer/framer_test.go b/pkg/framer/framer_test.go index c94550983..15a547894 100644 --- a/pkg/framer/framer_test.go +++ b/pkg/framer/framer_test.go @@ -41,3 +41,77 @@ func TestSendData(t *testing.T) { } } } + +func TestMessageReady(t *testing.T) { + buffer := []byte{1, 2, 3} + + messageReadyTestCases := []struct { + name string + inputBuffer []byte + expectedResult bool + }{ + { + name: "message ready", + inputBuffer: append([]byte{3, 0}, buffer...), + expectedResult: true, + }, + { + name: "message not ready", + inputBuffer: buffer, + expectedResult: false, + }, + } + + for _, testCase := range messageReadyTestCases { + f := framer.New() + f.RecvData(testCase.inputBuffer) + receivedResult := f.MessageReady() + + if testCase.expectedResult != receivedResult { + t.Errorf("%s - expected: %+v, received: %+v", testCase.name, testCase.expectedResult, receivedResult) + } + } +} + +func TestGetMessage(t *testing.T) { + buffer := []byte{1, 2, 3} + framedBuffer := append([]byte{3, 0}, buffer...) + + getMessageTestCases := []struct { + name string + inputBuffer []byte + expectedError bool + }{ + { + name: "message read", + inputBuffer: framedBuffer, + expectedError: false, + }, + { + name: "message not read with sending non framed buffer", + inputBuffer: buffer, + expectedError: true, + }, + { + name: "message not read with sending insufficient buffer length", + inputBuffer: []byte{1}, + expectedError: true, + }, + } + + for _, testCase := range getMessageTestCases { + t.Run(testCase.name, func(t *testing.T) { + f := framer.New() + f.RecvData(testCase.inputBuffer) + + receivedResult, err := f.GetMessage() + + if testCase.expectedError && err == nil { + t.Error(testCase.name) + } + if !testCase.expectedError && !bytes.Equal(buffer, receivedResult) { + t.Errorf("expected: %+v, received: %+v", buffer, receivedResult) + } + }) + } +}