From a4fcbdc2ed99a89ce7259ecbe91ee840de8dc6d1 Mon Sep 17 00:00:00 2001 From: Jarno Rankinen Date: Sun, 12 Feb 2023 22:55:58 +0200 Subject: [PATCH] gh-14 Tests for newRegister --- enervent-ctrl-go/pingvinKL/pingvinKL_test.go | 82 +++++++++++++++++++- 1 file changed, 79 insertions(+), 3 deletions(-) diff --git a/enervent-ctrl-go/pingvinKL/pingvinKL_test.go b/enervent-ctrl-go/pingvinKL/pingvinKL_test.go index 252d86c..8629034 100644 --- a/enervent-ctrl-go/pingvinKL/pingvinKL_test.go +++ b/enervent-ctrl-go/pingvinKL/pingvinKL_test.go @@ -16,8 +16,9 @@ func TestNewCoil(t *testing.T) { typ := fmt.Sprintf("%T", coil) // Assert newCoil returns pingvinKL.pingvinCoil if typ != "pingvinKL.pingvinCoil" { - t.Errorf("newCoil returned %s, expecting pingvinCoil", typ) + t.Errorf("newCoil returned %s, expecting pingvinKL.pingvinCoil", typ) } + // Assert Address is int and matches CSV addrtype := fmt.Sprintf("%T", coil.Address) if addrtype != "int" { @@ -27,6 +28,7 @@ func TestNewCoil(t *testing.T) { if coil.Address != iaddr { t.Errorf("coil.Address is %d, expecting %d", coil.Address, iaddr) } + // Assert Symbol is string and matches CSV symboltype := fmt.Sprintf("%T", coil.Symbol) if symboltype != "string" { @@ -35,6 +37,7 @@ func TestNewCoil(t *testing.T) { if coil.Symbol != symbol { t.Errorf("coil.Symbol is %s, expecting %s", coil.Symbol, symbol) } + // Assert Description is string and matches CSV descriptiontype := fmt.Sprintf("%T", coil.Description) if descriptiontype != "string" { @@ -43,6 +46,7 @@ func TestNewCoil(t *testing.T) { if coil.Description != description { t.Errorf("coil.Description is %s, expecting %s", coil.Description, description) } + // Assert Value is boolean and false valuetype := fmt.Sprintf("%T", coil.Value) if valuetype != "bool" { @@ -51,10 +55,11 @@ func TestNewCoil(t *testing.T) { if coil.Value != false { t.Errorf("coil.Value is %t, expecting false", coil.Value) } - // Assert Reserved is bool and true + + // Assert Reserved is bool and false reservedtype := fmt.Sprintf("%T", coil.Reserved) if reservedtype != "bool" { - t.Errorf("coil.Reserved is of type %s, expecting bool", typ) + t.Errorf("coil.Reserved is of type %s, expecting bool", reservedtype) } if coil.Reserved != false { t.Errorf("coil.Reserved is %t, expecting false", coil.Reserved) @@ -77,3 +82,74 @@ func TestNewReservedCoil(t *testing.T) { t.Errorf("coil.Reserved is %t, expecting true", coil.Reserved) } } + +func TestNewRegister(t *testing.T) { + data := readCsvLines("../registers.csv") + addr := data[4][0] + symbol := data[4][1] + regtype := data[4][2] + description := data[4][6] + + hreg := newRegister(addr, symbol, regtype, description) + + // Assert newRegister returns pingvinKL.pingvinRegister + typ := fmt.Sprintf("%T", hreg) + if typ != "pingvinKL.pingvinRegister" { + t.Errorf("newRegister returned %s, expecting pingvinKL.pingvinRegister", typ) + } + + // Assert Address is int and matches CSV + addrtype := fmt.Sprintf("%T", hreg.Address) + if addrtype != "int" { + t.Errorf("hreg.Address is of type %s, expecting int", addrtype) + } + iaddr, _ := strconv.Atoi(addr) + if hreg.Address != iaddr { + t.Errorf("hreg.Address is %d, expecting %d", hreg.Address, iaddr) + } + + // Assert Symbol is string and matches CSV + symboltype := fmt.Sprintf("%T", hreg.Symbol) + if symboltype != "string" { + t.Errorf("hreg.Symbol is of type %s, expecting string", symboltype) + } + if hreg.Symbol != symbol { + t.Errorf("hreg.Symbol is %s, expecting %s", hreg.Symbol, symbol) + } + + // Assert Description is string and matches CSV + descriptiontype := fmt.Sprintf("%T", hreg.Description) + if descriptiontype != "string" { + t.Errorf("hreg.Description is of type %s, expecting string", descriptiontype) + } + if hreg.Description != description { + t.Errorf("hreg.Description is %s, expecting %s", hreg.Description, description) + } + + // Assert Value is int and 0 + valuetype := fmt.Sprintf("%T", hreg.Value) + if valuetype != "int" { + t.Errorf("hreg.Value is of type %s, expecting int", valuetype) + } + if hreg.Value != 0 { + t.Errorf("hreg.Value is %d, expecting false", hreg.Value) + } + + // Assert Reserved is bool and false + reservedtype := fmt.Sprintf("%T", hreg.Reserved) + if reservedtype != "bool" { + t.Errorf("hreg.Reserved is of type %s, expecting bool", reservedtype) + } + if hreg.Reserved != false { + t.Errorf("hreg.Reserved is %t, expecting false", hreg.Reserved) + } + + // Assert Type is string and matches CSV + hregtype := fmt.Sprintf("%T", hreg.Type) + if hregtype != "string" { + t.Errorf("hreg.Type is of type %s, expecting string", hregtype) + } + if hreg.Type != regtype { + t.Errorf("hreg.Type is %s, expecting %s", hreg.Type, regtype) + } +}