-
Notifications
You must be signed in to change notification settings - Fork 1
/
gprops_test.go
119 lines (104 loc) · 2.65 KB
/
gprops_test.go
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package gprops_test
import (
"github.com/zbroju/gprops"
"io/ioutil"
"os"
"testing"
)
func TestNew(t *testing.T) {
properties := gprops.New()
properties.Set("key1", "value1")
}
func TestSet(t *testing.T) {
properties := gprops.New()
properties.Set("key1", "value1")
}
func TestGet(t *testing.T) {
properties := gprops.New()
properties.Set("key1", "value1")
if properties.Get("key1") != "value1" {
t.Errorf("Returned value is not as expected.\n")
}
}
func TestContainsKey(t *testing.T) {
properties := gprops.New()
properties.Set("key", "")
if properties.Contains("key") == false {
t.Errorf("Returns false for existing key.\n")
}
if properties.Contains("anotherkey") == true {
t.Errorf("Returns true for not existing key.\n")
}
}
func TestDelete(t *testing.T) {
properties := gprops.New()
properties.Set("key1", "value1")
properties.Delete("key1")
if properties.Contains("key1") == true {
t.Errorf("The key has not been deleted.\n")
}
}
func TestLoad(t *testing.T) {
// Create temporary data & file
tempFile := "tempRCData"
comment := "# Example config file"
key1 := "FILE"
val1 := "/home/user/myfile"
key2 := "VERBOSE"
val2 := "true"
byteStream := []byte(comment + "\n" + key1 + " = " + val1 + "\n" + key2 + " = " + val2 + "\n")
err := ioutil.WriteFile(tempFile, byteStream, 0644)
if err != nil {
t.Errorf("Problem with creating temporary file.")
}
defer os.Remove(tempFile)
// Open temporary file
file, err := os.Open(tempFile)
if err != nil {
t.Errorf(err.Error())
}
defer file.Close()
// Try to load properties and read the data
properties := gprops.New()
errLoad := properties.Load(file)
if errLoad != nil {
t.Errorf(errLoad.Error())
}
if properties.Get(key1) != val1 || properties.Get(key2) != val2 {
t.Errorf("Data loaded from file are not as expected.\n")
}
}
func TestStore(t *testing.T) {
// Prepare the properties to store
propsToStore := gprops.New()
key1 := "key1"
val1 := "val1"
key2 := "key2"
val2 := "val2"
propsToStore.Set(key1, val1)
propsToStore.Set(key2, val2)
// Store properties in file
tempFile := "temporaryPropsFile"
f, err := os.Create(tempFile)
if err != nil {
t.Errorf(err.Error())
}
propsToStore.Store(f, "Test properties")
f.Close()
// Load the properties from the file
propsLoaded := gprops.New()
f2, err := os.Open(tempFile)
if err != nil {
t.Errorf(err.Error())
}
defer f2.Close()
errLoad := propsLoaded.Load(f2)
if errLoad != nil {
t.Errorf(errLoad.Error())
}
// Compare the properties
if propsLoaded.Get(key1) != propsToStore.Get(key1) || propsLoaded.Get(key2) != propsToStore.Get(key2) {
t.Errorf("Data stored in a file are not as expected.\n")
}
os.Remove(tempFile)
}