git » gofer » commit 9f881d5

reqlog: Add tests for corner cases of the helper functions

author Alberto Bertogli
2022-10-06 00:54:59 UTC
committer Alberto Bertogli
2022-10-09 11:34:34 UTC
parent c66f2059d4a52dfc3db6138bfe0f69a9cd86047b

reqlog: Add tests for corner cases of the helper functions

reqlog/reqlog_test.go +59 -0

diff --git a/reqlog/reqlog_test.go b/reqlog/reqlog_test.go
new file mode 100644
index 0000000..6fe1f5a
--- /dev/null
+++ b/reqlog/reqlog_test.go
@@ -0,0 +1,59 @@
+package reqlog
+
+import (
+	"errors"
+	"io/fs"
+	"os"
+	"strings"
+	"testing"
+)
+
+func TestBadFormat(t *testing.T) {
+	l, err := New("/ignored", 10, "{{bad}}")
+	if !(l == nil && strings.Contains(err.Error(), `function "bad" not defined`)) {
+		t.Errorf("expected template error, got %v / %v", l, err)
+	}
+}
+
+func TestStdPaths(t *testing.T) {
+	l, err := New("<stdout>", 10, goferFormat)
+	if err != nil {
+		t.Errorf("unexpected error: %v", err)
+	}
+	if l.f != os.Stdout {
+		t.Errorf("expected log to os.Stdout, got %v", l.f)
+	}
+
+	l, err = New("<stderr>", 10, goferFormat)
+	if err != nil {
+		t.Errorf("unexpected error: %v", err)
+	}
+	if l.f != os.Stderr {
+		t.Errorf("expected log to os.Stderr, got %v", l.f)
+	}
+}
+
+func TestBadFile(t *testing.T) {
+	_, err := New("/bad/file", 10, goferFormat)
+	if !errors.Is(err, fs.ErrNotExist) {
+		t.Errorf("expected file does not exists error, got %v", err)
+	}
+}
+
+func TestQuoteString(t *testing.T) {
+	cases := []struct {
+		i interface{}
+		e string
+	}{
+		{nil, `""`},
+		{"hola", `"hola"`},
+		{[]string{"a", "b"}, `"a, b"`},
+		{1, "unknown-type-int"},
+	}
+	for _, c := range cases {
+		s := quoteString(c.i)
+		if s != c.e {
+			t.Errorf("%v: expected %q, got %q", c.i, c.e, s)
+		}
+	}
+}