-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadini_test.go
More file actions
103 lines (97 loc) · 3.75 KB
/
readini_test.go
File metadata and controls
103 lines (97 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
* @Author: FunctionSir
* @License: AGPLv3
* @Date: 2025-04-06 00:15:04
* @LastEditTime: 2025-06-20 09:13:22
* @LastEditors: FunctionSir
* @Description: -
* @FilePath: /readini/readini_test.go
*/
package readini
import (
"testing"
)
var testStrOK string = "KeyA=ValA\n [ SomeSection ]\n 第二个键🚩 = 初音 ミク/はつねミク Hatsune Miku 🚩 来自中文维基百科\n[ AnotherSection]\nWoW = \" 123\""
var testStrErr string = "KeyA = ValA\n [SomeSection \n KeyB=ValB"
var testFileOk string = "testFileOK.conf"
var testFileErr string = "testFileErr.conf"
var testFileNotExists string = "testFileNotExists.conf"
func TestLoadFromFile(t *testing.T) {
res, err := LoadFromFile(testFileOk)
if err != nil {
t.Errorf("Should not get an error, but error \"%s\" got",
err.Error())
}
if !res.HasSection("") {
t.Errorf("Should have section \"\", but it said it doesn't have")
}
if res.HasSection("NoSuchSection") {
t.Errorf("Should not have section \"NoSuchSection\", but it said it has")
}
if !res.HasSection("SomeSection") {
t.Errorf("Should have section \"SomeSection\", but it said it doesn't have")
}
if res[""]["KeyA"] != "ValA" {
t.Errorf("Val of \"KeyA\" be \"ValA\", but %s found",
res[""]["KeyA"])
}
if !res.HasKey("SomeSection", "第二个键🚩") {
t.Errorf("There has key \"第二个键🚩\" in section \"SomeSection\", but got false")
}
if res.HasKey("SomeSection", "第三个键🚩") {
t.Errorf("There has no key \"第三个键🚩\" in section \"SomeSection\", but got true")
}
if res.HasKey("NoSuchSection", "第四个键🚩") {
t.Errorf("There has no key \"第三个键🚩\" in section \"SomeSection\", but got true")
}
if res["SomeSection"]["第二个键🚩"] != "初音 ミク/はつねミク Hatsune Miku 🚩 来自中文维基百科" {
t.Errorf("Val of \"第二个键🚩\" in section \"SomeSection\" should be \"初音 ミク/はつねミク Hatsune Miku 🚩 来自中文维基百科\", but %s found",
res["SomeSection"]["第二个键🚩"])
}
sec := res["SomeSection"]
if !sec.HasKey("第二个键🚩") {
t.Errorf("There has key \"第二个键🚩\" in section \"SomeSection\", but got false")
}
if sec.HasKey("第三个键🚩") {
t.Errorf("There has no key \"第三个键🚩\" in section \"SomeSection\", but got true")
}
_, err = LoadFromFile(testFileErr)
if err == nil {
t.Errorf("Should get an error, but \"nil\" got")
}
_, err = LoadFromFile(testFileNotExists)
if err == nil {
t.Errorf("Should get an error, but \"nil\" got")
}
}
func TestLoadFromRunes(t *testing.T) {
res, err := LoadFromRunes([]rune(testStrOK))
if err != nil {
t.Errorf("Should not get an error, but error \"%s\" got",
err.Error())
}
if !res.HasSection("") {
t.Errorf("Should have section \"\", but it said it doesn't have")
}
if !res.HasSection("SomeSection") {
t.Errorf("Should have section \"SomeSection\", but it said it doesn't have")
}
if !res.HasSection("AnotherSection") {
t.Errorf("Should have section \"AnotherSection\", but it said it doesn't have")
}
if res[""]["KeyA"] != "ValA" {
t.Errorf("Val of \"KeyA\" be \"ValA\", but %s found",
res[""]["KeyA"])
}
if res["SomeSection"]["第二个键🚩"] != "初音 ミク/はつねミク Hatsune Miku 🚩 来自中文维基百科" {
t.Errorf("Val of \"第二个键🚩\" in section \"SomeSection\" should be \"初音 ミク/はつねミク Hatsune Miku 🚩 来自中文维基百科\", but %s found",
res["SomeSection"]["第二个键🚩"])
}
if res["AnotherSection"]["WoW"] != " 123" {
t.Errorf("Val of \"WoW\" in section \"AnotherSection\" should be \" 123\", but %s found", res["AnotherSection"]["WoW"])
}
_, err = LoadFromRunes([]rune(testStrErr))
if err == nil {
t.Errorf("Should get an error, but \"nil\" got")
}
}