github.com/cellofellow/gopkg@v0.0.0-20140722061823-eec0544a62ad/encoding/ini/ini_test.go (about) 1 // Copyright 2012 <chaishushan{AT}gmail.com>. 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 "fmt" 9 "testing" 10 ) 11 12 var iniStr = ` 13 # Copyright 2011-2013 <chaishushan{AT}gmail.com>. All rights reserved. 14 # Use of this source code is governed by a BSD-style 15 # license that can be found in the LICENSE file. 16 17 [gmail] 18 LoginName = abc 19 WebSite = http://mail.google.com 20 Comments = 21 22 [taobao] 23 LoginName = 123 24 WebSite = http://www.taobao.com 25 Comments = 26 ` 27 28 var golden Dict = map[string]map[string]string{ 29 "gmail": map[string]string{ 30 "LoginName": "abc", 31 "WebSite": "http://mail.google.com", 32 "Comments": "", 33 "Bad": "", 34 }, 35 "taobao": map[string]string{ 36 "LoginName": "123", 37 "WebSite": "http://www.taobao.com", 38 "Comments": "", 39 }, 40 "Bad": map[string]string{ 41 "LoginName": "", 42 "WebSite": "", 43 "Comments": "", 44 }, 45 } 46 47 func TestDict(t *testing.T) { 48 dict, err := LoadString(iniStr) 49 if err != nil { 50 t.Errorf("LoadString(iniStr) fail: %v", err) 51 } 52 53 section_list := dict.GetSections() 54 for i := 0; i < len(section_list); i++ { 55 if _, ok := golden[section_list[i]]; !ok { 56 t.Errorf("section[%s] not found", section_list[i]) 57 } 58 } 59 60 for sec, sec_values := range golden { 61 for key, value := range sec_values { 62 if dict[sec][key] != value { 63 t.Errorf("dict([%s]:%s): Need=%v, Got=%v", sec, key, value, dict[sec][key]) 64 } 65 } 66 } 67 } 68 69 func ExampleDict() { 70 dict, _ := LoadString(` 71 # Copyright 2011-2013 <chaishushan{AT}gmail.com>. All rights reserved. 72 # Use of this source code is governed by a BSD-style 73 # license that can be found in the LICENSE file. 74 75 [gmail] 76 LoginName = abc 77 WebSite = http://mail.google.com 78 Comments = 79 80 [taobao] 81 LoginName = 123 82 WebSite = http://www.taobao.com 83 Comments = 84 `) 85 86 fmt.Println(dict["gmail"]["LoginName"]) 87 fmt.Println(dict["gmail"]["WebSite"]) 88 fmt.Println(dict["gmail"]["Comments"]) 89 fmt.Println(dict["taobao"]["LoginName"]) 90 fmt.Println(dict["taobao"]["WebSite"]) 91 fmt.Println(dict["taobao"]["Comments"]) 92 // Output: 93 // abc 94 // http://mail.google.com 95 // 96 // 123 97 // http://www.taobao.com 98 // 99 }