github.com/searKing/golang/go@v1.2.117/encoding/ini/config_test.go (about) 1 // Copyright 2020 The searKing Author. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package ini 6 7 import ( 8 "bytes" 9 "fmt" 10 "testing" 11 ) 12 13 func testConfig(cfg *Config, t *testing.T) { 14 if v, err := cfg.GetBool("a"); err != nil { 15 t.Fatal(err) 16 } else if v != true { 17 t.Fatal(v) 18 } 19 20 checkInt := func(t *testing.T, cfg *Config, key string, check int) { 21 if v, err := cfg.GetInt(key); err != nil { 22 t.Fatal(err) 23 } else if v != check { 24 t.Fatal(fmt.Sprintf("%s %d != %d", key, v, check)) 25 } 26 } 27 28 checkInt(t, cfg, "b", 100) 29 checkInt(t, cfg, "kb", 1024) 30 checkInt(t, cfg, "k", 1000) 31 checkInt(t, cfg, "mb", 1024*1024) 32 checkInt(t, cfg, "m", 1000*1000) 33 checkInt(t, cfg, "gb", 1024*1024*1024) 34 checkInt(t, cfg, "g", 1000*1000*1000) 35 } 36 37 func TestGetConfig(t *testing.T) { 38 cfg := NewConfig() 39 cfg.Values["a"] = "true" 40 cfg.Values["b"] = "100" 41 cfg.Values["kb"] = "1kb" 42 cfg.Values["k"] = "1k" 43 cfg.Values["mb"] = "1mb" 44 cfg.Values["m"] = "1m" 45 cfg.Values["gb"] = "1gb" 46 cfg.Values["g"] = "1g" 47 48 testConfig(cfg, t) 49 } 50 51 func TestReadWriteConfig(t *testing.T) { 52 var b = []byte(` 53 # comment 54 a = true 55 b = 100 56 kb = 1kb 57 k = 1k 58 mb = 1mb 59 m = 1m 60 gb = 1gb 61 g = 1g 62 `) 63 64 cfg, err := ReadConfig(b) 65 if err != nil { 66 t.Fatal(err) 67 } 68 69 testConfig(cfg, t) 70 71 var buf bytes.Buffer 72 73 if err := cfg.Write(&buf); err != nil { 74 t.Fatal(err) 75 } 76 77 cfg.Values = make(map[string]string) 78 if err := cfg.Read(&buf); err != nil { 79 t.Fatal(err) 80 } 81 82 testConfig(cfg, t) 83 }