Skip to content

Commit

Permalink
Test the rest of the framer.go methods
Browse files Browse the repository at this point in the history
  • Loading branch information
resoluteCoder committed Aug 18, 2023
1 parent ff0ff07 commit ff4e3d3
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ kubectl
.vagrant/
/docs/build
/dist
coverage.txt
74 changes: 74 additions & 0 deletions pkg/framer/framer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}

0 comments on commit ff4e3d3

Please sign in to comment.