forked from ariya/phantomjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs-spec-01.js
151 lines (128 loc) · 4.86 KB
/
fs-spec-01.js
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
describe("Basic Files API (read, write, remove, ...)", function() {
var FILENAME = "temp-01.test",
FILENAME_COPY = FILENAME + ".copy",
FILENAME_MOVED = FILENAME + ".moved",
FILENAME_EMPTY = FILENAME + ".empty",
FILENAME_ENC = FILENAME + ".enc",
FILENAME_BIN = FILENAME + ".bin",
ABSENT = "absent-01.test";
it("should be able to create and write a file", function() {
try{
var f = fs.open(FILENAME, "w");
f.write("hello");
f.writeLine("");
f.writeLine("world");
f.close();
} catch (e) { }
expect(fs.exists(FILENAME)).toBeTruthy();
});
it("should be able to create (touch) an empty file", function() {
expect(fs.exists(FILENAME_EMPTY)).toBeFalsy();
fs.touch(FILENAME_EMPTY);
expect(fs.exists(FILENAME_EMPTY)).toBeTruthy();
expect(fs.size(FILENAME_EMPTY)).toEqual(0);
});
it("should be able to read content from a file", function() {
var content = "";
try{
var f = fs.open(FILENAME, "r");
content = f.read();
f.close();
} catch (e) { }
expect(content).toEqual("hello\nworld\n");
});
it("should be able to read specific number of bytes from a specific position in a file", function() {
var content = "";
try{
var f = fs.open(FILENAME, "r");
f.seek(3);
content = f.read(5);
f.close();
} catch (e) { }
expect(content).toEqual("lo\nwo");
});
it("should be able to read/write/append content from a file", function() {
var content = "";
try{
var f = fs.open(FILENAME, "rw+");
f.writeLine("asdf");
content = f.read();
f.close();
} catch (e) { }
expect(content).toEqual("hello\nworld\nasdf\n");
});
it("should be able to copy a file", function() {
expect(fs.exists(FILENAME_COPY)).toBeFalsy();
fs.copy(FILENAME, FILENAME_COPY);
expect(fs.exists(FILENAME_COPY)).toBeTruthy();
expect(fs.read(FILENAME)).toEqual(fs.read(FILENAME_COPY));
});
it("should be able to move a file", function() {
expect(fs.exists(FILENAME)).toBeTruthy();
var contentBeforeMove = fs.read(FILENAME);
fs.move(FILENAME, FILENAME_MOVED);
expect(fs.exists(FILENAME)).toBeFalsy();
expect(fs.exists(FILENAME_MOVED)).toBeTruthy();
expect(fs.read(FILENAME_MOVED)).toEqual(contentBeforeMove);
});
it("should be able to remove a (moved) file", function() {
expect(fs.exists(FILENAME_MOVED)).toBeTruthy();
fs.remove(FILENAME_MOVED);
expect(fs.exists(FILENAME_MOVED)).toBeFalsy();
});
it("should be able to remove a (copied) file", function() {
expect(fs.exists(FILENAME_COPY)).toBeTruthy();
fs.remove(FILENAME_COPY);
expect(fs.exists(FILENAME_COPY)).toBeFalsy();
});
it("should be able to remove an empty file", function() {
expect(fs.exists(FILENAME_EMPTY)).toBeTruthy();
fs.remove(FILENAME_EMPTY);
expect(fs.exists(FILENAME_EMPTY)).toBeFalsy();
});
it("should throw an exception when trying to open for read a non existing file", function(){
expect(function(){
fs.open(ABSENT, "r");
}).toThrow("Unable to open file '"+ ABSENT +"'");
});
it("should throw an exception when trying to copy a non existing file", function() {
expect(function(){
fs.copy(ABSENT, FILENAME_COPY);
}).toThrow("Unable to copy file '" + ABSENT + "' at '" + FILENAME_COPY + "'");
});
it("should be read/write utf8 text by default", function() {
var content, output = "ÄABCÖ";
try {
var f = fs.open(FILENAME_ENC, "w");
f.write(output);
f.close();
f = fs.open(FILENAME_ENC, "r");
content = f.read();
f.close();
fs.remove(FILENAME_ENC);
} catch (e) { }
expect(content).toEqual(output);
});
it("should be read/write binary data", function() {
var content, output = String.fromCharCode(0, 1, 2, 3, 4, 5);
try {
var f = fs.open(FILENAME_BIN, "wb");
f.write(output);
f.close();
f = fs.open(FILENAME_BIN, "rb");
content = f.read();
f.close();
fs.remove(FILENAME_BIN);
} catch (e) { }
expect(content).toEqual(output);
});
it("should be read/write binary data (shortcuts)", function() {
var content, output = String.fromCharCode(0, 1, 2, 3, 4, 5);
try {
fs.write(FILENAME_BIN, output, "b");
content = fs.read(FILENAME_BIN, "b");
fs.remove(FILENAME_BIN);
} catch (e) { }
expect(content).toEqual(output);
});
});