-
Notifications
You must be signed in to change notification settings - Fork 87
/
project_test.go
156 lines (138 loc) · 3.84 KB
/
project_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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright 2016 The lime Authors.
// Use of this source code is governed by a 2-clause
// BSD-style license that can be found in the LICENSE file.
package backend
import (
"encoding/json"
"io/ioutil"
"os"
"testing"
"github.com/limetext/util"
)
func TestSaveAs(t *testing.T) {
w := GetEditor().NewWindow()
defer w.Close()
p := newProject(w)
p.AddFolder(".")
p.SaveAs("testdata/saved_project")
defer os.Remove("testdata/saved_project")
d1, err := json.Marshal(p)
if err != nil {
t.Fatalf("Error on marshaling project to json: %s", err)
}
d2, err := ioutil.ReadFile("testdata/saved_project")
if err != nil {
t.Fatalf("Error on reading 'testdata/saved_project': %s", err)
}
if diff := util.Diff(string(d1), string(d2)); diff != "" {
t.Errorf("Saved project doesn't match expected result\n%s", diff)
}
}
func TestAddFolder(t *testing.T) {
w := GetEditor().NewWindow()
defer w.Close()
p := newProject(w)
p.AddFolder("/test/path")
if got := len(p.Folders()); got != 1 {
t.Fatalf("Expected project folders len 1, but got %d", got)
}
if got, exp := p.Folders()[0], "/test/path"; got != exp {
t.Errorf("Expected %s in project folders, but got %s", exp, got)
}
}
func TestRemoveFolder(t *testing.T) {
w := GetEditor().NewWindow()
defer w.Close()
p := newProject(w)
p.AddFolder("/test/path")
p.RemoveFolder("/test/path")
if got := len(p.Folders()); got != 0 {
t.Errorf("Expected project folders empty, but got %d", got)
}
}
func TestUnmarshalJSON(t *testing.T) {
data, err := ioutil.ReadFile("testdata/project")
if err != nil {
t.Fatalf("Error reading project file 'testdata/project': %s", err)
}
w := GetEditor().NewWindow()
defer w.Close()
p := newProject(w)
if err = p.UnmarshalJSON(data); err != nil {
t.Fatalf("Error on unmarshaling data to project: %s", err)
}
if got, exp := p.Settings().Int("tab_size", 4), 8; got != exp {
t.Errorf("Expected project settings %d, but got %d", exp, got)
}
if got := len(p.folders); got != 2 {
t.Errorf("Expected 2 folders, but got %d", got)
}
f1 := p.Folder("src")
if f1 == nil {
t.Fatal("Returned src folder is nil")
}
if len(f1.ExcludePatterns) == 0 {
t.Fatal("src 'folder_exluce_patters' is empty")
}
if got, exp := f1.ExcludePatterns[0], "backup"; got != exp {
t.Errorf("Expected %s in src 'folder_exclude_patterns', but got %s", exp, got)
}
if !f1.FollowSymlinks {
t.Error("Expected src 'follow_symlinks' true but its false")
}
f2 := p.Folder("docs")
if f2 == nil {
t.Fatal("Returned docs folder is nil")
}
if got, exp := f2.Name, "Documentation"; got != exp {
t.Errorf("Expected %s in docs 'name', but got %s", exp, got)
}
if len(f2.FileExcludePatterns) == 0 {
t.Fatal("src 'file_exclude_patterns' is empty")
}
if got, exp := f2.FileExcludePatterns[0], "*.css"; got != exp {
t.Errorf("Expected %s in docs 'file_exclude_patterns', but got %s", exp, got)
}
}
func TestMarshalJSON(t *testing.T) {
w := GetEditor().NewWindow()
defer w.Close()
p := newProject(w)
p.Settings().Set("font_size", 12)
p.AddFolder("./testdata")
data, err := p.MarshalJSON()
if err != nil {
t.Fatalf("Error on marshaling project to json: %s", err)
}
exp := `{
"folders":
[
{
"path": "./testdata"
}
],
"settings":
{
"font_size": 12
}
}
`
if diff := util.Diff(exp, string(data)); diff != "" {
t.Logf("Expected:\n%s\nGot:\n%s", exp, string(data))
t.Errorf("Marshaled project to json doesn't match expected result\n%s", diff)
}
}
func TestClose(t *testing.T) {
w := GetEditor().NewWindow()
defer w.Close()
p := newProject(w)
p.Settings().Set("font_size", 14)
p.AddFolder("./testdata")
p.Close()
if got := len(p.Folders()); got != 0 {
t.Errorf("Expected project folders after close be empty, but got %d", got)
}
if got, exp := p.Settings().Int("font_size", 12), 12; got != exp {
t.Errorf("Expected project font_size settings after close %d, but got %d", exp, got)
}
}