Skip to content

Commit

Permalink
Merge pull request #329 from hnakamur/good-if-style-in-fs-tests
Browse files Browse the repository at this point in the history
Use good-style if-statements (avoid one-line if-statements).
  • Loading branch information
rphillips committed Aug 21, 2012
2 parents 0a80cb6 + bd6ec21 commit c0e5cbd
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 41 deletions.
32 changes: 24 additions & 8 deletions tests/test-fs-append-file.lua
Expand Up @@ -41,13 +41,17 @@ local ncallbacks = 0
-- test that empty file will be created and have content added
FS.appendFile(filename, s, function(e)
if e then return e end
if e then
return e
end
ncallbacks = ncallbacks + 1
p('appended to file')
FS.readFile(filename, function(e, buffer)
if e then return e end
if e then
return e
end
p('file read')
ncallbacks = ncallbacks + 1
assert(#buffer == #s)
Expand All @@ -59,13 +63,17 @@ local filename2 = join(__dirname, 'fixtures', 'append2.txt')
FS.writeFileSync(filename2, currentFileData)
FS.appendFile(filename2, s, function(e)
if e then return e end
if e then
return e
end
ncallbacks = ncallbacks + 1
p('appended to file2')
FS.readFile(filename2, function(e, buffer)
if e then return e end
if e then
return e
end
p('file2 read')
ncallbacks = ncallbacks + 1
assert(#buffer == #s + #currentFileData)
Expand All @@ -80,13 +88,17 @@ local buf = Buffer:new(s)
p('appending to ' .. filename3)
FS.appendFile(filename3, buf:toString(), function(e)
if e then return e end
if e then
return e
end
ncallbacks = ncallbacks + 1
p('appended to file3')
FS.readFile(filename3, function(e, buffer)
if e then return e end
if e then
return e
end
p('file3 read')
ncallbacks = ncallbacks + 1
assert(#buffer == buf.length + #currentFileData)
Expand All @@ -100,13 +112,17 @@ FS.writeFileSync(filename4, currentFileData)
p('appending to ' .. filename4)
FS.appendFile(filename4, tostring(n), function(e)
if e then return e end
if e then
return e
end
ncallbacks = ncallbacks + 1
p('appended to file4')
FS.readFile(filename4, function(e, buffer)
if e then return e end
if e then
return e
end
p('file4 read')
ncallbacks = ncallbacks + 1
assert(#buffer == #tostring(n) + #currentFileData)
Expand Down
12 changes: 9 additions & 3 deletions tests/test-fs-fsync.lua
Expand Up @@ -29,7 +29,9 @@ p('open ' .. file)
FS.open(file, 'a', '0777', function(err, fd)
p('fd ' .. fd)
if err then return err end
if err then
return err
end
FS.fdatasyncSync(fd)
p('fdatasync SYNC: ok')
Expand All @@ -40,11 +42,15 @@ FS.open(file, 'a', '0777', function(err, fd)
successes = successes + 1
FS.fdatasync(fd, function(err)
if err then return err end
if err then
return err
end
p('fdatasync ASYNC: ok')
successes = successes + 1
FS.fsync(fd, function(err)
if err then return err end
if err then
return err
end
p('fsync ASYNC: ok')
successes = successes + 1
end)
Expand Down
8 changes: 6 additions & 2 deletions tests/test-fs-long-path.lua
Expand Up @@ -35,11 +35,15 @@ p('fileName=' .. fileName)
p('fileNameLength=' .. #fileName)
FS.writeFile(fileName, 'ok', function(err)
if err then return err end
if err then
return err
end
successes = successes + 1
FS.stat(fileName, function(err, stats)
if err then return err end
if err then
return err
end
successes = successes + 1
end)
end)
Expand Down
8 changes: 6 additions & 2 deletions tests/test-fs-open.lua
Expand Up @@ -30,15 +30,19 @@ assert(err.source == 'open')
local openFd
FS.open(__filename, 'r', function(err, fd)
if err then return err end
if err then
return err
end
openFd = fd
end)
-- TODO: Support file open flag 's'
--[[
local openFd2
FS.open(__filename, 'rs', function(err, fd)
if err then return err end
if err then
return err
end
openFd2 = fd
end)
--]]
Expand Down
4 changes: 3 additions & 1 deletion tests/test-fs-readfile-zero-byte-liar.lua
Expand Up @@ -31,7 +31,9 @@ FS._fstatSync = FS.fstatSync
FS.fstat = function(fd, cb)
FS._fstat(fd, function(er, st)
if er then return cb(er) end
if er then
return cb(er)
end
st.size = 0
return cb(er, st)
end)
Expand Down
60 changes: 45 additions & 15 deletions tests/test-fs-truncate.lua
Expand Up @@ -60,21 +60,33 @@ FS.closeSync(fd)
function testTruncate(cb)
FS.writeFile(filename, data, function(er)
if er then return cb(er) end
if er then
return cb(er)
end
FS.stat(filename, function(er, stat)
if er then return cb(er) end
if er then
return cb(er)
end
assert(stat.size == 1024 * 16)
FS.truncate(filename, 1024, function(er)
if er then return cb(er) end
if er then
return cb(er)
end
FS.stat(filename, function(er, stat)
if er then return cb(er) end
if er then
return cb(er)
end
assert(stat.size == 1024)
FS.truncate(filename, function(er)
if er then return cb(er) end
if er then
return cb(er)
end
FS.stat(filename, function(er, stat)
if er then return cb(er) end
if er then
return cb(er)
end
assert(stat.size == 0)
cb()
end)
Expand All @@ -88,23 +100,37 @@ end
function testFtruncate(cb)
FS.writeFile(filename, data, function(er)
if er then return cb(er) end
if er then
return cb(er)
end
FS.stat(filename, function(er, stat)
if er then return cb(er) end
if er then
return cb(er)
end
assert(stat.size == 1024 * 16)
FS.open(filename, 'w', function(er, fd)
if er then return cb(er) end
if er then
return cb(er)
end
FS.ftruncate(fd, 1024, function(er)
if er then return cb(er) end
if er then
return cb(er)
end
FS.stat(filename, function(er, stat)
if er then return cb(er) end
if er then
return cb(er)
end
assert(stat.size == 1024)
FS.ftruncate(fd, function(er)
if er then return cb(er) end
if er then
return cb(er)
end
FS.stat(filename, function(er, stat)
if er then return cb(er) end
if er then
return cb(er)
end
assert(stat.size == 0)
FS.close(fd, cb)
end)
Expand All @@ -119,10 +145,14 @@ end
-- async tests
local success = 0
testTruncate(function(er)
if er then return er end
if er then
return er
end
success = success + 1
testFtruncate(function(er)
if er then return er end
if er then
return er
end
success = success + 1
end)
end)
Expand Down
24 changes: 18 additions & 6 deletions tests/test-fs-write-file.lua
Expand Up @@ -37,13 +37,17 @@ local s = '南越国是前203年至前111年存在于岭南地区的一个国家
local ncallbacks = 0
FS.writeFile(filename, s, function(err)
if err then return err end
if err then
return err
end
ncallbacks = ncallbacks + 1
p('file written')
FS.readFile(filename, function(err, buffer)
if err then return err end
if err then
return err
end
p('file read')
ncallbacks = ncallbacks + 1
assert(#s == #buffer)
Expand All @@ -60,13 +64,17 @@ local buf = Buffer:new(s)
p('writing to ' .. filename2)
FS.writeFile(filename, buf, function(err)
if err then return err end
if err then
return err
end
ncallbacks = ncallbacks + 1
p('file2 written')
FS.readFile(filename2, function(err, buffer)
if err then return err end
if err then
return err
end
p('file2 read')
ncallbacks = ncallbacks + 1
assert(#buf == #buffer)
Expand All @@ -82,13 +90,17 @@ local filename3 = Path.join(__dirname, 'fixtures', 'test3.txt')
p('writing to ' .. filename3)
FS.writeFile(filename, n, function(err)
if err then return err end
if err then
return err
end
ncallbacks = ncallbacks + 1
p('file3 written')
FS.readFile(filename3, function(err, buffer)
if err then return err end
if err then
return err
end
p('file3 read')
ncallbacks = ncallbacks + 1
assert(#tostring(n) == #buffer)
Expand Down
16 changes: 12 additions & 4 deletions tests/test-fs-write.lua
Expand Up @@ -29,15 +29,19 @@ local expected = 'ümlaut.'
local found, found2
FS.open(fn, 'w', tonumber('0644', 8), function(err, fd)
if err then return err end
if err then
return err
end
p('open done')
-- TODO: support same arguments as fs.write in node.js
FS.write(fd, 0, '', function(err, written)
assert(0 == written)
end)
FS.write(fd, 0, expected, function(err, written)
p('write done')
if err then return err end
if err then
return err
end
assert(#expected == written)
FS.closeSync(fd)
found = FS.readFileSync(fn)
Expand All @@ -50,14 +54,18 @@ end)
FS.open(fn2, 'w', tonumber('0644', 8),
function(err, fd)
if err then return err end
if err then
return err
end
p('open done')
FS.write(fd, 0, '', function(err, written)
assert(0 == written)
end)
FS.write(fd, 0, expected, function(err, written)
p('write done')
if err then return err end
if err then
return err
end
assert(#expected == written)
FS.closeSync(fd)
found2 = FS.readFileSync(fn2)
Expand Down

0 comments on commit c0e5cbd

Please sign in to comment.