github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/utils/config/config_test.go (about) 1 // Copyright 2019 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package config 16 17 import ( 18 "strings" 19 "testing" 20 ) 21 22 func TestConfigGetters(t *testing.T) { 23 mc := NewMapConfig(map[string]string{ 24 "string": "this is a string", 25 "int": "-15", 26 "uint": "1234567", 27 "float": "3.1415", 28 "bad_int": "1a2b3c", 29 "bad_float": "1.2.3.4", 30 "bad_uint": "-123456", 31 }) 32 33 if _, err := GetString(mc, "missing"); err != ErrConfigParamNotFound { 34 t.Error("missing failure") 35 } 36 37 if v, err := GetString(mc, "string"); v != "this is a string" || err != nil { 38 t.Error("string failure") 39 } 40 41 if v, err := GetInt(mc, "int"); v != -15 || err != nil { 42 t.Error("int failure") 43 } 44 45 if v, err := GetFloat(mc, "float"); v != 3.1415 || err != nil { 46 t.Error("float failure") 47 } 48 49 if v, err := GetUint(mc, "uint"); v != 1234567 || err != nil { 50 t.Error("uint failure") 51 } 52 53 if _, err := GetInt(mc, "bad_int"); err == nil { 54 t.Error("bad_int failure") 55 } 56 57 if _, err := GetFloat(mc, "bad_float"); err == nil { 58 t.Error("baf_float failure") 59 } 60 61 if _, err := GetUint(mc, "bad_uint"); err == nil { 62 t.Error("bad_uint failure") 63 } 64 } 65 66 func TestConfigSetters(t *testing.T) { 67 mc := NewMapConfig(map[string]string{ 68 "string": "initial", 69 }) 70 71 err := SetStrings(mc, map[string]string{ 72 "string": "updated", 73 "new_string": "new_value", 74 }) 75 76 if err != nil { 77 t.Error("Error setting strings") 78 } 79 80 SetInt(mc, "int", -15) 81 SetFloat(mc, "float", 3.1415) 82 SetUint(mc, "uint", 1234567) 83 84 if str, err := mc.GetString("string"); err != nil || str != "updated" { 85 t.Error("string failure") 86 } 87 88 if str, err := mc.GetString("new_string"); err != nil || str != "new_value" { 89 t.Error("new_string failure") 90 } 91 92 if str, err := mc.GetString("int"); err != nil || str != "-15" { 93 t.Error("int failure") 94 } 95 96 if str, err := mc.GetString("float"); err != nil || !strings.HasPrefix(str, "3.1415") { 97 t.Error("float failure") 98 } 99 100 if str, err := mc.GetString("uint"); err != nil || str != "1234567" { 101 t.Error("uint failure") 102 } 103 } 104 105 func testIteration(t *testing.T, expected map[string]string, cfg ReadableConfig) { 106 cfg.Iter(func(name string, value string) (stop bool) { 107 if expectedVal, ok := expected[name]; ok { 108 if expectedVal == value { 109 delete(expected, name) 110 } else { 111 t.Error("When iterating value of " + name + " had an unexpected value.") 112 } 113 } else if !ok { 114 t.Error("Iterated over unexpected value " + name) 115 } 116 117 return false 118 }) 119 120 if len(expected) != 0 { 121 t.Error("Iteration did not iterate over all expected results.") 122 } 123 }