From 87dd8307a3803ee44af83a07a792cae80b5d582b Mon Sep 17 00:00:00 2001 From: "Bernhard E. Reiter" Date: Tue, 8 Mar 2022 16:25:17 +0100 Subject: [PATCH] Add first simple unit test with workflow (#81) * Adding first simple unit test * Add test run to workflow Co-authored-by: Sascha L. Teichmann --- .github/workflows/go.yml | 3 +++ util/file_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 util/file_test.go diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 083ca92..0d39a6d 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -29,3 +29,6 @@ jobs: - name: golint uses: Jerome1337/golint-action@v1.0.2 + + - name: Tests + run: go test -v ./.. diff --git a/util/file_test.go b/util/file_test.go new file mode 100644 index 0000000..9691975 --- /dev/null +++ b/util/file_test.go @@ -0,0 +1,30 @@ +package util + +import ( + "bytes" + "testing" +) + +func TestNWriter(t *testing.T) { + + msg := []byte("Gruß!\n") + + first, second := msg[:len(msg)/2], msg[len(msg)/2:] + + var buf bytes.Buffer + nw := NWriter{Writer: &buf, N: 0} + _, err1 := nw.Write(first) + _, err2 := nw.Write(second) + + if err1 != nil || err2 != nil { + t.Error("Calling NWriter failed") + } + + if n := int64(len(msg)); nw.N != n { + t.Errorf("Expected %d bytes, but counted %d", n, nw.N) + } + + if out := buf.Bytes(); !bytes.Equal(msg, out) { + t.Errorf("Expected %q, but got %q", msg, out) + } +}