github.com/camlistore/go4@v0.0.0-20200104003542-c7e774b10ea0/jsonconfig/jsonconfig_test.go (about) 1 /* 2 Copyright 2011 The go4 Authors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package jsonconfig 18 19 import ( 20 "os" 21 "reflect" 22 "strings" 23 "testing" 24 ) 25 26 func testIncludes(configFile string, t *testing.T) { 27 var c ConfigParser 28 c.IncludeDirs = []string{"testdata"} 29 obj, err := c.ReadFile(configFile) 30 if err != nil { 31 t.Fatal(err) 32 } 33 two := obj.RequiredObject("two") 34 if err := obj.Validate(); err != nil { 35 t.Error(err) 36 } 37 if g, e := two.RequiredString("key"), "value"; g != e { 38 t.Errorf("sub object key = %q; want %q", g, e) 39 } 40 } 41 42 func TestIncludesCWD(t *testing.T) { 43 testIncludes("testdata/include1.json", t) 44 } 45 46 func TestIncludesIncludeDirs(t *testing.T) { 47 testIncludes("testdata/include1bis.json", t) 48 } 49 50 func TestIncludeLoop(t *testing.T) { 51 _, err := ReadFile("testdata/loop1.json") 52 if err == nil { 53 t.Fatal("expected an error about import cycles.") 54 } 55 if !strings.Contains(err.Error(), "include cycle detected") { 56 t.Fatalf("expected an error about import cycles; got: %v", err) 57 } 58 } 59 60 func TestBoolEnvs(t *testing.T) { 61 os.Setenv("TEST_EMPTY", "") 62 os.Setenv("TEST_TRUE", "true") 63 os.Setenv("TEST_ONE", "1") 64 os.Setenv("TEST_ZERO", "0") 65 os.Setenv("TEST_FALSE", "false") 66 obj, err := ReadFile("testdata/boolenv.json") 67 if err != nil { 68 t.Fatal(err) 69 } 70 if str := obj.RequiredString("emptystr"); str != "" { 71 t.Errorf("str = %q, want empty", str) 72 } 73 tests := []struct { 74 key string 75 want bool 76 }{ 77 {"def_false", false}, 78 {"def_true", true}, 79 {"set_true_def_false", true}, 80 {"set_false_def_true", false}, 81 {"lit_true", true}, 82 {"lit_false", false}, 83 {"one", true}, 84 {"zero", false}, 85 } 86 for _, tt := range tests { 87 if v := obj.RequiredBool(tt.key); v != tt.want { 88 t.Errorf("key %q = %v; want %v", tt.key, v, tt.want) 89 } 90 } 91 if err := obj.Validate(); err != nil { 92 t.Error(err) 93 } 94 } 95 96 func TestListExpansion(t *testing.T) { 97 os.Setenv("TEST_BAR", "bar") 98 obj, err := ReadFile("testdata/listexpand.json") 99 if err != nil { 100 t.Fatal(err) 101 } 102 s := obj.RequiredString("str") 103 l := obj.RequiredList("list") 104 if err := obj.Validate(); err != nil { 105 t.Error(err) 106 } 107 want := []string{"foo", "bar"} 108 if !reflect.DeepEqual(l, want) { 109 t.Errorf("got = %#v\nwant = %#v", l, want) 110 } 111 if s != "bar" { 112 t.Errorf("str = %q, want %q", s, "bar") 113 } 114 }