-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmem_test.go
More file actions
91 lines (73 loc) · 1.85 KB
/
mem_test.go
File metadata and controls
91 lines (73 loc) · 1.85 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
package storage_test
import (
"io"
"io/ioutil"
"testing"
"github.com/sajari/storage"
)
func TestMemOpen(t *testing.T) {
m := storage.Mem()
_, err := m.Open(nil, "")
if err == nil {
t.Errorf("expected 'not found' error from m.Open()")
}
if !storage.IsNotExist(err) {
t.Errorf("IsNotExist(%v) = false, expected true", err)
}
}
func TestMemCreate(t *testing.T) {
testMemCreate(t, "testing")
}
func TestMemCreateEmpty(t *testing.T) {
testMemCreate(t, "")
}
func testMemCreate(t *testing.T, content string) {
path := "test.txt"
m := storage.Mem()
wc, err := m.Create(nil, path)
if err != nil {
t.Errorf("unexpected error from m.Create(): %v", err)
}
if _, err := io.WriteString(wc, content); err != nil {
t.Errorf("unexpected error from wc.Write(): %v", err)
}
if err := wc.Close(); err != nil {
t.Errorf("unexpected error from wc.Close(): %v", err)
}
f, err := m.Open(nil, path)
if err != nil {
t.Errorf("unexpected error from m.Open(): %v", err)
}
b, err := ioutil.ReadAll(f)
if err != nil {
t.Errorf("unexpected error from ioutil.ReadAll(): %v", err)
}
got := string(b)
if got != content {
t.Errorf("ioutil.ReadAll() = %q, expected %q", got, content)
}
if err := f.Close(); err != nil {
t.Errorf("unexpected error from f.Close(): %v", err)
}
}
func TestMemDelete(t *testing.T) {
path := "test.txt"
m := storage.Mem()
wc, err := m.Create(nil, path)
if err != nil {
t.Errorf("unexpected error from m.Create(): %v", err)
}
if err := wc.Close(); err != nil {
t.Errorf("unexpected error from wc.Close(): %v", err)
}
if err := m.Delete(nil, path); err != nil {
t.Errorf("unexpected error from m.Delete(%q): %v", path, err)
}
_, err = m.Open(nil, path)
if err == nil {
t.Error("expected 'not found' error from m.Open()")
}
if !storage.IsNotExist(err) {
t.Errorf("IsNotExist(%v) = false, expected true", err)
}
}