aboutsummaryrefslogtreecommitdiffhomepage
path: root/config/parser_test.go
diff options
context:
space:
mode:
authorGravatar Frédéric Guillot <fred@miniflux.net>2019-06-01 18:18:09 -0700
committerGravatar fguillot <fred@miniflux.net>2019-06-02 06:30:08 -0700
commit228862fefaa645026caa483ffe9993bf8c00b22e (patch)
tree2b590dc6cda3e50928a31ce673641357805f75ce /config/parser_test.go
parent04d85b3c63afcf6c9539fc8dc7a91c4e36c2e8fb (diff)
Refactor config package
- Parse configuration only once during startup time - Store configuration values in a global variable
Diffstat (limited to 'config/parser_test.go')
-rw-r--r--config/parser_test.go79
1 files changed, 79 insertions, 0 deletions
diff --git a/config/parser_test.go b/config/parser_test.go
new file mode 100644
index 0000000..ba454b4
--- /dev/null
+++ b/config/parser_test.go
@@ -0,0 +1,79 @@
+// Copyright 2019 Frédéric Guillot. All rights reserved.
+// Use of this source code is governed by the Apache 2.0
+// license that can be found in the LICENSE file.
+
+package config // import "miniflux.app/config"
+
+import (
+ "os"
+ "testing"
+)
+
+func TestGetBooleanValueWithUnsetVariable(t *testing.T) {
+ os.Clearenv()
+ if getBooleanValue("MY_TEST_VARIABLE") {
+ t.Errorf(`Unset variables should returns false`)
+ }
+}
+
+func TestGetBooleanValue(t *testing.T) {
+ scenarios := map[string]bool{
+ "": false,
+ "1": true,
+ "Yes": true,
+ "yes": true,
+ "True": true,
+ "true": true,
+ "on": true,
+ "false": false,
+ "off": false,
+ "invalid": false,
+ }
+
+ for input, expected := range scenarios {
+ os.Clearenv()
+ os.Setenv("MY_TEST_VARIABLE", input)
+ result := getBooleanValue("MY_TEST_VARIABLE")
+ if result != expected {
+ t.Errorf(`Unexpected result for %q, got %v instead of %v`, input, result, expected)
+ }
+ }
+}
+
+func TestGetStringValueWithUnsetVariable(t *testing.T) {
+ os.Clearenv()
+ if getStringValue("MY_TEST_VARIABLE", "defaultValue") != "defaultValue" {
+ t.Errorf(`Unset variables should returns the default value`)
+ }
+}
+
+func TestGetStringValue(t *testing.T) {
+ os.Clearenv()
+ os.Setenv("MY_TEST_VARIABLE", "test")
+ if getStringValue("MY_TEST_VARIABLE", "defaultValue") != "test" {
+ t.Errorf(`Defined variables should returns the specified value`)
+ }
+}
+
+func TestGetIntValueWithUnsetVariable(t *testing.T) {
+ os.Clearenv()
+ if getIntValue("MY_TEST_VARIABLE", 42) != 42 {
+ t.Errorf(`Unset variables should returns the default value`)
+ }
+}
+
+func TestGetIntValueWithInvalidInput(t *testing.T) {
+ os.Clearenv()
+ os.Setenv("MY_TEST_VARIABLE", "invalid integer")
+ if getIntValue("MY_TEST_VARIABLE", 42) != 42 {
+ t.Errorf(`Invalid integer should returns the default value`)
+ }
+}
+
+func TestGetIntValue(t *testing.T) {
+ os.Clearenv()
+ os.Setenv("MY_TEST_VARIABLE", "2018")
+ if getIntValue("MY_TEST_VARIABLE", 42) != 2018 {
+ t.Errorf(`Defined variables should returns the specified value`)
+ }
+}