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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
import nimgen/fileops, common, regex, os
import unittest
let testFileContent = """
this is text
this is text
replace me
prepend me
end
"""
let prependMiddleExpected = """
this is text
this is text
replace me
prepended data
prepend me
end
"""
let prependEndExpected = """
this is text
this is text
replace me
prepend me
data
end
"""
let appendEndExpected = """
this is text
this is text
replace me
prepend me
end
data
"""
let appendMiddleExpected = """
this is data
text
this is text
replace me
prepend me
end
"""
let freplaceDefaultExpected = """
replace me
prepend me
end
"""
let freplaceWithExpected = """
this is text
this is text
foobar
prepend me
end
"""
let freplaceRegexExpected = """
foobar
foobar
replace me
prepend me
end
"""
let commentExpected = """
this is text
this is text
//replace me
//prepend me
//end
"""
let commentMiddleExpected = """
this //is text
//this is text
replace me
prepend me
end
"""
let dataDir = currentSourcePath().splitPath().head / "data"
let testfilename = dataDir / "testing.txt"
suite "test file ops":
if not dataDir.dirExists():
dataDir.createDir()
setup:
if testfilename.existsFile():
removeFile(testfilename)
writeFile(testfilename, testFileContent)
################### Prepend #######################
test "prepend at beginning of file":
prepend(testfilename, "data\n")
let expected = "data\n" & testFileContent
testfilename.checkFile(expected)
test "prepend at middle of file":
prepend(testfilename, "prepended data\n", "prepend me")
testfilename.checkFile(prependMiddleExpected)
test "prepend at end of file":
prepend(testfilename, "data\n", "end\n")
testfilename.checkFile(prependEndExpected)
################### Pipe #########################
test "pipe command into file":
when defined(windows):
pipe(testfilename, "ECHO foo")
testfilename.checkFile("foo")
else:
pipe(testfilename, "cat $file | grep 'this is text'")
testfilename.checkFile("this is text\nthis is text")
################# Append #########################
test "append file end":
append(testfilename, "data\n")
testfilename.checkFile(appendEndExpected)
test "append file middle":
append(testfilename, " data\n", "this is")
testfilename.checkFile(appendMiddleExpected)
################# FReplace #########################
test "freplace default empty":
freplace(testfilename, "this is text")
testfilename.checkFile(freplaceDefaultExpected)
test "freplace with content":
freplace(testfilename, "replace me", "foobar")
testfilename.checkFile(freplaceWithExpected)
test "freplace regex":
freplace(testfilename, re"this .*", "foobar")
testfilename.checkFile(freplaceRegexExpected)
####################### Comment ######################
test "comment":
comment(testfilename, "replace me", "3")
testfilename.checkFile(commentExpected)
test "comment over length":
comment(testfilename, "replace me", "10")
testfilename.checkFile(commentExpected)
test "comment negative":
comment(testfilename, "replace me", "-3")
testfilename.checkFile(testFileContent)
test "comment zero":
comment(testfilename, "replace me", "0")
testfilename.checkFile(testFileContent)
test "comment middle":
comment(testfilename, "is text", "2")
testfilename.checkFile(commentMiddleExpected)
############### Static inline removal ################
test "replace static inline with front braces at end of line":
let
file = dataDir / "teststaticfrontbraces.h"
resFile = dataDir / "teststaticexpectedfrontbraces.h"
test = readFile(file)
expected = readFile(resFile)
writeFile(testfilename, test)
removeStatic(testfilename)
testfilename.checkFile(expected)
reAddStatic(testfilename)
testfilename.checkFile(test)
test "replace static inline with newline before brace":
let
file = dataDir / "teststaticnewlinebraces.h"
resFile = dataDir / "teststaticexpectednewlinebraces.h"
reAddedFile = dataDir / "teststaticnewlinebracesreadded.h"
test = readFile(file)
expected = readFile(resFile)
reAdded = readFile(reAddedFile)
writeFile(testfilename, test)
removeStatic(testfilename)
testfilename.checkFile(expected)
reAddStatic(testfilename)
testfilename.checkFile(reAdded)
|