89 lines
3.2 KiB
Plaintext
89 lines
3.2 KiB
Plaintext
package ini_config
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path"
|
|
"testing"
|
|
)
|
|
|
|
func Test_File(t *testing.T) {
|
|
foder, _ := os.Getwd()
|
|
filePath := path.Join(foder, "ini.conf")
|
|
|
|
kvmap, err := ParseFile(filePath)
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
|
|
if len(kvmap) != 12 {
|
|
t.Error("读取的内容数量不正确")
|
|
return
|
|
}
|
|
|
|
if v, exists := kvmap["log.es.enable"]; exists == false || v != "false" {
|
|
t.Error("log.es.enable读取的值不正确")
|
|
}
|
|
|
|
if v, exists := kvmap["log.es.url"]; exists == false || v != "" {
|
|
t.Error("log.es.url读取的值不正确")
|
|
}
|
|
}
|
|
|
|
func Test_String(t *testing.T) {
|
|
k, v, err := Parse("log.es.enable")
|
|
if err == nil {
|
|
t.Error(fmt.Errorf("解析格式错误"))
|
|
return
|
|
}
|
|
|
|
k, v, err = Parse("log.es.enable=false #(false,默认值,关闭es日志记录,后续配置可不填写; true 打开日志记录)")
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
if k != "log.es.enable" || v != "false" {
|
|
t.Error("解析的值不正确")
|
|
}
|
|
|
|
k, v, err = Parse("dbconnection=root:moqikaka3309!#@tcp(10.252.0.62:3309)/liangjian2_groupserver_auto_master?charset=utf8&parseTime=true&loc=Local&timeout=30s#数据库连接")
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
if k != "dbconnection" || v != "root:moqikaka3309!#@tcp(10.252.0.62:3309)/liangjian2_groupserver_auto_master?charset=utf8&parseTime=true&loc=Local&timeout=30s" {
|
|
t.Error("解析的值不正确")
|
|
}
|
|
|
|
}
|
|
|
|
func Test_Content(t *testing.T) {
|
|
content := "#配置项说明\nlog.es.enable #(false,默认值,关闭es日志记录,后续配置可不填写; true 打开日志记录)\nlog.es.url= #(es服务地址)\nlog.es.indexName=1 #(es服务中Index名)\nlog.es.level=info #(debug|info|warn|error|fatal等级,等于或高于配置项则记录)\n\nlog.file.enable=false #(默认false)\nlog.file.path=log #(运行目录下log目录,默认logs)\nlog.file.pre=log #(文件名前缀,默认log)\nlog.file.enableHour=true #(文件以小时划分,格式:yyyyMMddHH,默认true,false 一天一个文件,格式:yyyyMMdd)\nlog.file.level=info\n\nlog.console.enable=false #(默认false)\nlog.console.level=info"
|
|
kvmap, err := ParseMultipleLines(content)
|
|
if err == nil {
|
|
t.Error(fmt.Errorf("配置格式不正确"))
|
|
return
|
|
}
|
|
|
|
content = "#配置项说明\nlog.es.enable=false #(false,默认值,关闭es日志记录,后续配置可不填写; true 打开日志记录)\nlog.es.url= #(es服务地址)\nlog.es.indexName=1 #(es服务中Index名)\nlog.es.level=info #(debug|info|warn|error|fatal等级,等于或高于配置项则记录)\n\nlog.file.enable=false #(默认false)\nlog.file.path=log #(运行目录下log目录,默认logs)\nlog.file.pre=log #(文件名前缀,默认log)\nlog.file.enableHour=true #(文件以小时划分,格式:yyyyMMddHH,默认true,false 一天一个文件,格式:yyyyMMdd)\nlog.file.level=info\n\nlog.console.enable=false #(默认false)\nlog.console.level=info"
|
|
kvmap, err = ParseMultipleLines(content)
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
|
|
if len(kvmap) != 11 {
|
|
t.Error("读取的内容数量不正确")
|
|
return
|
|
}
|
|
|
|
if v, exists := kvmap["log.es.enable"]; exists == false || v != "false" {
|
|
t.Error("log.es.enable读取的值不正确")
|
|
}
|
|
|
|
if v, exists := kvmap["log.es.url"]; exists == false || v != "" {
|
|
t.Error("log.es.url读取的值不正确")
|
|
}
|
|
}
|