From 480279e2f54699cf3d2478cd394612de6dda70fa Mon Sep 17 00:00:00 2001 From: Jarno Rankinen Date: Sun, 12 Feb 2023 22:30:25 +0200 Subject: [PATCH] gh-14 Unit testing for pingvinKL.newCoil, test.sh for executing tests --- enervent-ctrl-go/pingvinKL/pingvinKL_test.go | 54 ++++++++++++++++++++ enervent-ctrl-go/test.sh | 16 ++++++ 2 files changed, 70 insertions(+) create mode 100644 enervent-ctrl-go/pingvinKL/pingvinKL_test.go create mode 100755 enervent-ctrl-go/test.sh diff --git a/enervent-ctrl-go/pingvinKL/pingvinKL_test.go b/enervent-ctrl-go/pingvinKL/pingvinKL_test.go new file mode 100644 index 0000000..66c1f1f --- /dev/null +++ b/enervent-ctrl-go/pingvinKL/pingvinKL_test.go @@ -0,0 +1,54 @@ +package pingvinKL + +import ( + "fmt" + "testing" + "strconv" +) + +func TestNewCoil(t *testing.T) { + data := readCsvLines("../coils.csv") + addr := data[1][0] + symbol := data[1][0] + description := data[1][0] + + coil := newCoil(addr, symbol, description) + typ := fmt.Sprintf("%T", coil) + // Assert newCoil returns pingvinKL.pingvinCoil + if typ != "pingvinKL.pingvinCoil" { + t.Errorf("newCoil returned %s, expecting pingvinCoil", typ) + } + // Assert Address is int and matches CSV + addrtype := fmt.Sprintf("%T", coil.Address) + if addrtype != "int" { + t.Errorf("newCoil.Address is of type %s, expecting int", addrtype) + } + iaddr, _ := strconv.Atoi(addr) + 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" { + t.Errorf("coil.Symbol is of type %s, expecting string", symboltype) + } + 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" { + t.Errorf("coil.Description is of type %s, expecting string", descriptiontype) + } + 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" { + t.Errorf("coil.Value is of type %s, expecting bool", valuetype) + } + if coil.Value != false { + t.Errorf("coil.Value is %t, expecting false", coil.Value) + } +} diff --git a/enervent-ctrl-go/test.sh b/enervent-ctrl-go/test.sh new file mode 100755 index 0000000..2d10adb --- /dev/null +++ b/enervent-ctrl-go/test.sh @@ -0,0 +1,16 @@ +#!/bin/bash -e + +SRCDIR="$PWD" +TMPTAR=/tmp/ec.tar +TESTDIR=/tmp/enervent-ctr-build + +rm -rf $TMPTAR $TESTDIR +tar chf $TMPTAR * +mkdir -p $TESTDIR +pushd $TESTDIR +tar xf $TMPTAR +pushd pingvinKL +go test -v . +popd +popd +rm -rf $TMPTAR