Skip to content

Commit

Permalink
Began adding test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
oldgalileo committed Apr 21, 2017
1 parent e51905a commit e1823af
Show file tree
Hide file tree
Showing 6 changed files with 383 additions and 108 deletions.
244 changes: 188 additions & 56 deletions .idea/workspace.xml

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ func (cl *client) Initialize(nt NetworkTables) error {
return nil
}

func (cl *client) CreateEntry(message string) error
func (cl *client) CreateEntry(message string) error { return nil }

func (cl *client) UpdateEntry(message string) error
func (cl *client) UpdateEntry(message string) error { return nil }

func (cl *client) DeleteEntry(message string) error
func (cl *client) DeleteEntry(message string) error { return nil }

func (cl *client) GetEntry(message string) error
func (cl *client) GetEntry(message string) error { return nil }
37 changes: 7 additions & 30 deletions entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"github.com/imdario/mergo"
"io"
"github.com/howardstark/Abreuvoir/entry"
)

const (
Expand All @@ -16,6 +15,7 @@ const (
TypeDoubleArr = 0x11
TypeStringArr = 0x12
TypeRPCDef = 0x20
TypeUndef = 0xFF

FlagTemporary EntryFlag = 0x00
FlagPersistent = 0x01
Expand Down Expand Up @@ -55,21 +55,11 @@ type EntryValueArray interface {
EntryValue
}

func validateEntryValue() {
var bool EntryValue = BuildBoolean(true)
var string EntryValue = BuildString("meme")
var raw EntryValue = BuildRaw([]byte("kek"))
var boolarr EntryValueArray = BuildBooleanArray([]*ValueBoolean{})
var doublarr EntryValueArray = BuildDoubleArray([]*ValueDouble{})
var stringarr EntryValueArray = BuildStringArray([]*ValueString{})
_, _, _, _ ,_ ,_ = bool, string, raw, boolarr, doublarr, stringarr
}

func DecodeEntryType(r io.Reader) (EntryType, error) {
rawType := make([]byte, 1)
_, readErr := r.Read(rawType)
if readErr != nil {
return nil, readErr
return TypeUndef, readErr
}
return EntryType(rawType[0]), nil
}
Expand All @@ -80,22 +70,7 @@ func DecodeEntry(r io.Reader) (EntryValue, error) {
if readErr != nil {
return nil, readErr
}
switch entryType[0] {
case TypeBoolean:
return DecodeBoolean(r)
case TypeDouble:
return DecodeDouble(r)
case TypeString:
return DecodeString(r)
case TypeRawData:
return DecodeRaw(r)
case TypeBooleanArr:
return DecodeBooleanArray(r)
case TypeDoubleArr:
return DecodeDoubleArray(r)
case TypeStringArr:
return DecodeStringArray(r)
}
return DecodeEntryWithType(r, EntryType(entryType[0]))
}

func DecodeEntryWithType(r io.Reader, entryType EntryType) (EntryValue, error) {
Expand All @@ -114,6 +89,8 @@ func DecodeEntryWithType(r io.Reader, entryType EntryType) (EntryValue, error) {
return DecodeDoubleArray(r)
case TypeStringArr:
return DecodeStringArray(r)
default:
return nil, ErrEntryNoSuchType
}
}

Expand Down Expand Up @@ -443,7 +420,7 @@ func (array *ValueDoubleArray) Add(entry EntryValue) error {
}

func (array *ValueDoubleArray) GetRaw() []byte {
data := []byte(array.index)
data := []byte{byte(array.index)}
var i uint8 = 0
for ; i < array.index; i++ {
data = append(data, array.elements[i].RawValue...)
Expand Down Expand Up @@ -522,7 +499,7 @@ func (array *ValueStringArray) Add(entry EntryValue) error {
}

func (array *ValueStringArray) GetRaw() []byte {
data := []byte(array.index)
data := []byte{byte(array.index)}
var i uint8 = 0
for ; i < array.index; i++ {
data = append(data, array.elements[i].RawValue...)
Expand Down
163 changes: 163 additions & 0 deletions entry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package ntgo

import (
"testing"
"bytes"
"reflect"
"fmt"
)

func TestBuildBoolean(t *testing.T) {
result := BuildBoolean(true)
var expected = &ValueBoolean{
Value: true,
RawValue: []byte{BoolTrue},
}
if !reflect.DeepEqual(result, expected) {
t.Fatalf("Expected %s but got %s", expected, result)
}
}

func TestBuildDouble(t *testing.T) {
result := BuildDouble(0.5)
var expected = &ValueDouble{
Value: 0.5,
RawValue: Float64ToBytes(0.5),
}
fmt.Println(expected.RawValue)
if !reflect.DeepEqual(result, expected) {
t.Fatalf("Expected %s but got %s", expected, result)
}
}

func TestBuildString(t *testing.T) {
result := BuildString("test")
var expected = &ValueString{
Value: "test",
RawValue: []byte{0x04, 0x74, 0x65, 0x73, 0x74},
}
if !reflect.DeepEqual(result, expected) {
t.Fatalf("Expected %s but got %s", expected, result)
}
}

func TestBuildRaw(t *testing.T) {
result := BuildRaw([]byte{0x50, 0x21})
var expected = &ValueRaw{
Value: []byte{0x50, 0x21},
RawValue: []byte{0x02, 0x50, 0x21},
}
if !reflect.DeepEqual(result, expected) {
t.Fatalf("Expected %s but got %s", expected, result)
}
}

func TestBuildBooleanArray(t *testing.T) {
result := BuildBooleanArray([]*ValueBoolean{BuildBoolean(true)})
var expected = &ValueBooleanArray{
index: uint8(1),
elements: []*ValueBoolean{BuildBoolean(true)},
}
if !reflect.DeepEqual(result, expected) {
t.Fatalf("Expected %s but got %s", expected, result)
}
}

func TestBuildDoubleArray(t *testing.T) {
result := BuildDoubleArray([]*ValueDouble{BuildDouble(0.5)})
var expected = &ValueDoubleArray{
index: uint8(1),
elements: []*ValueDouble{BuildDouble(0.5)},
}
if !reflect.DeepEqual(result, expected) {
t.Fatalf("Expected %s but got %s", expected, result)
}
}

func TestDecodeEntry(t *testing.T) {

}

func TestDecodeEntryTypeBoolean(t *testing.T) {
result, err := DecodeEntryType(bytes.NewBuffer([]byte{byte(TypeBoolean)}))

if err != nil {
t.Fatal("Unexpected error: ", err)
}
var expected EntryType = TypeBoolean
if result != expected {
t.Fatalf("Expected %s but got %s", expected, result)
}
}

func TestDecodeEntryTypeDouble(t *testing.T) {
result, err := DecodeEntryType(bytes.NewBuffer([]byte{byte(TypeDouble)}))

if err != nil {
t.Fatal("Unexpected error: ", err)
}
var expected EntryType = TypeDouble
if result != expected {
t.Fatalf("Expected %s but got %s", expected, result)
}
}

func TestDecodeEntryTypeString(t *testing.T) {
result, err := DecodeEntryType(bytes.NewBuffer([]byte{byte(TypeString)}))

if err != nil {
t.Fatal("Unexpected error: ", err)
}
var expected EntryType = TypeString
if result != expected {
t.Fatalf("Expected %s but got %s", expected, result)
}
}

func TestDecodeEntryTypeRawData(t *testing.T) {
result, err := DecodeEntryType(bytes.NewBuffer([]byte{byte(TypeRawData)}))

if err != nil {
t.Fatal("Unexpected error: ", err)
}
var expected EntryType = TypeRawData
if result != expected {
t.Fatalf("Expected %s but got %s", expected, result)
}
}

func TestDecodeEntryTypeBooleanArray(t *testing.T) {
result, err := DecodeEntryType(bytes.NewBuffer([]byte{byte(TypeBooleanArr)}))

if err != nil {
t.Fatal("Unexpected error: ", err)
}
var expected EntryType = TypeBooleanArr
if result != expected {
t.Fatalf("Expected %s but got %s", expected, result)
}
}

func TestDecodeEntryTypeDoubleArray(t *testing.T) {
result, err := DecodeEntryType(bytes.NewBuffer([]byte{byte(TypeDoubleArr)}))

if err != nil {
t.Fatal("Unexpected error: ", err)
}
var expected EntryType = TypeDoubleArr
if result != expected {
t.Fatalf("Expected %s but got %s", expected, result)
}
}

func TestDecodeEntryTypeStringArray(t *testing.T) {
result, err := DecodeEntryType(bytes.NewBuffer([]byte{byte(TypeStringArr)}))

if err != nil {
t.Fatal("Unexpected error: ", err)
}
var expected EntryType = TypeStringArr
if result != expected {
t.Fatalf("Expected %s but got %s", expected, result)
}
}
33 changes: 18 additions & 15 deletions rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,24 +88,27 @@ func (rpc *ValueRPC) GetRaw() []byte {
return raw
}

func DecodeRPCParam(r io.Reader) (RPCParam, error) {
func DecodeRPCParam(r io.Reader) (rpcParam RPCParam, err error) {
rpcParam = RPCParam{}
entryType, typeErr := DecodeEntryType(r)
if typeErr != nil {
return nil, typeErr
err = typeErr
return
}
rpcParam.Type = entryType
name, nameErr := DecodeString(r)
if nameErr != nil {
return nil, nameErr
err = nameErr
return
}
rpcParam.Name = *name
val, valErr := DecodeEntryWithType(r, entryType)
if valErr != nil {
return nil, valErr
err = valErr
return
}
return RPCParam{
Type: entryType,
Name: *name,
DefaultVal: val,
}, nil
rpcParam.DefaultVal = val
return
}

func (param RPCParam) GetRaw() []byte {
Expand All @@ -116,18 +119,18 @@ func (param RPCParam) GetRaw() []byte {
}

func DecodeRPCOutput(r io.Reader) (RPCOutput, error) {
output := RPCOutput{}
entryType, typeErr := DecodeEntryType(r)
if typeErr != nil {
return nil, typeErr
return output, typeErr
}
output.Type = entryType
name, nameErr := DecodeString(r)
if nameErr != nil {
return nil, nameErr
return output, nameErr
}
return RPCOutput{
Type: entryType,
Name: *name,
}, nil
output.Name = *name
return output, nil
}

func (output RPCOutput) GetRaw() []byte {
Expand Down
6 changes: 3 additions & 3 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func BytesToFloat64(bytes []byte) float64 {

func Float64ToBytes(value float64) []byte {
bits := math.Float64bits(value)
bytes := []byte{}
binary.BigEndian.PutUint64(bytes, bits)
return bytes
floatBytes := make([]byte, 8)
binary.BigEndian.PutUint64(floatBytes, bits)
return floatBytes
}

0 comments on commit e1823af

Please sign in to comment.