59 lines
1.1 KiB
Go
59 lines
1.1 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"log"
|
||
|
"os"
|
||
|
"testing"
|
||
|
"time"
|
||
|
|
||
|
"gopkg.in/ini.v1"
|
||
|
)
|
||
|
|
||
|
func TestIntCreate(t *testing.T) {
|
||
|
|
||
|
os.Remove("./my_test.cfg")
|
||
|
time.Sleep(time.Second * 1)
|
||
|
|
||
|
for i := 0; i < 3; i++ {
|
||
|
|
||
|
var portid string
|
||
|
var baud int
|
||
|
var rtimeout time.Duration
|
||
|
|
||
|
var cfg *ini.File
|
||
|
cfg, err := ini.Load("my_test.cfg")
|
||
|
if err != nil {
|
||
|
log.Println(err)
|
||
|
log.Println("加载配置my.cfg失败, 将使用默认值")
|
||
|
f, err := os.OpenFile("./my_test.cfg", os.O_CREATE|os.O_WRONLY, 0666)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
f.WriteString("[config]\nportid = COM1\nbaud = 9600\nreadtimeout = 5")
|
||
|
cfg, err = ini.Load("my_test.cfg")
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
section := cfg.Section("config")
|
||
|
portid = section.Key("portid").MustString("COM1")
|
||
|
baud = section.Key("baud").MustInt(9600)
|
||
|
rtimeout = time.Second * time.Duration(section.Key("readtimeout").MustInt64(5))
|
||
|
|
||
|
if portid != "COM1" {
|
||
|
t.Error(`portid != "COM1"`)
|
||
|
}
|
||
|
|
||
|
if baud != 9600 {
|
||
|
t.Error(`baud != 9600`)
|
||
|
}
|
||
|
|
||
|
if rtimeout != time.Second*5 {
|
||
|
t.Error(`rtimeout != time.Second * 5`)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
os.Remove("./my_test.cfg")
|
||
|
}
|