From f3200419baba452475654ba2cf2419d092263646 Mon Sep 17 00:00:00 2001 From: Drew Bednar Date: Sun, 23 Jun 2024 10:43:45 -0400 Subject: [PATCH] Add unit test structure for common --- go-web-app/gowiki/Makefile | 5 +++- go-web-app/gowiki/common/common_test.go | 40 +++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 go-web-app/gowiki/common/common_test.go diff --git a/go-web-app/gowiki/Makefile b/go-web-app/gowiki/Makefile index 2ff8074..8f8d4e9 100644 --- a/go-web-app/gowiki/Makefile +++ b/go-web-app/gowiki/Makefile @@ -2,4 +2,7 @@ build: go build -o ./bin/gowiki ./cmd/gowiki/main.go clean: - rm -rf ./bin/gowiki \ No newline at end of file + rm -rf ./bin/gowiki + +test: + go test ./... \ No newline at end of file diff --git a/go-web-app/gowiki/common/common_test.go b/go-web-app/gowiki/common/common_test.go new file mode 100644 index 0000000..419ee65 --- /dev/null +++ b/go-web-app/gowiki/common/common_test.go @@ -0,0 +1,40 @@ +package common + +import ( + "log" + "os" + "testing" +) + +func assertString(t *testing.T, got, want string) { + t.Helper() + if got != want { + log.Fatalf("Assertion failed. Got: %s want: %s", got, want) + } +} + +func TestGetDefaultEnv(t *testing.T) { + + t.Run("test key exists", func(t *testing.T) { + //Setup + want := "test value" + os.Setenv("TEST_KEY", want) + got := GetDefaultEnv("TEST_KEY", "") + assertString(t, got, want) + os.Unsetenv("TEST_KEY") + }) + + t.Run("key does not exists", func(t *testing.T) { + //Setup + want := "another test value" + got := GetDefaultEnv("TEST_KEY", want) + assertString(t, got, want) + }) + + t.Run("key maps to empty value", func(t *testing.T) { + //Setup + want := "" + got := GetDefaultEnv("TEST_EMPTY_KEY", want) + assertString(t, got, want) + }) +}