test(chore): remove side effects

pull/2522/head
Qingping Hou 7 years ago
parent 0bbb4bbc16
commit 0b9c9d6ddb

@ -1,6 +1,8 @@
require("commonrequire")
describe("eink optimization setting", function() describe("eink optimization setting", function()
setup(function()
require("commonrequire")
end)
it("should be correctly loaded", function() it("should be correctly loaded", function()
G_reader_settings:saveSetting("eink", true) G_reader_settings:saveSetting("eink", true)
assert.Equals(require("device").screen.eink, true) assert.Equals(require("device").screen.eink, true)

@ -1,9 +1,11 @@
require("commonrequire")
local GestureRange = require("ui/gesturerange")
local Geom = require("ui/geometry")
describe("gesturerange module", function() describe("gesturerange module", function()
local GestureRange, Geom
setup(function()
require("commonrequire")
GestureRange = require("ui/gesturerange")
Geom = require("ui/geometry")
end)
it("should match tap event within range", function() it("should match tap event within range", function()
local g = GestureRange:new{ local g = GestureRange:new{
ges = "tap", ges = "tap",

@ -1,12 +1,13 @@
require("commonrequire")
local UIManager = require("ui/uimanager")
local DEBUG = require("dbg")
--DEBUG:turnOn()
-- set true to test httpclient -- set true to test httpclient
DUSE_TURBO_LIB = false DUSE_TURBO_LIB = false
describe("HTTP client module #notest #nocov", function() describe("HTTP client module #notest #nocov", function()
local UIManager
setup(function()
require("commonrequire")
UIManager = require("ui/uimanager")
end)
local requests = 0 local requests = 0
local function response_callback(res) local function response_callback(res)
requests = requests - 1 requests = requests - 1
@ -14,6 +15,7 @@ describe("HTTP client module #notest #nocov", function()
assert(not res.error, "error occurs") assert(not res.error, "error occurs")
assert(res.body) assert(res.body)
end end
it("should get response from async GET request", function() it("should get response from async GET request", function()
local HTTPClient = require("httpclient") local HTTPClient = require("httpclient")
local async_client = HTTPClient:new() local async_client = HTTPClient:new()

@ -1,9 +1,3 @@
require("commonrequire")
local UIManager = require("ui/uimanager")
local DEBUG = require("dbg")
local md5 = require("ffi/MD5")
--DEBUG:turnOn()
local service = [[ local service = [[
{ {
"base_url" : "https://vislab.bjmu.edu.cn:7200", "base_url" : "https://vislab.bjmu.edu.cn:7200",
@ -57,26 +51,36 @@ local service = [[
]] ]]
describe("KOSync modules #notest #nocov", function() describe("KOSync modules #notest #nocov", function()
local HTTPClient = require("httpclient") local UIManager, logger, md5, client
local Spore = require("Spore") local username, password, doc, percentage, progress, device
local client = Spore.new_from_string(service)
package.loaded['Spore.Middleware.GinClient'] = {} setup(function()
require('Spore.Middleware.GinClient').call = function(self, req) require("commonrequire")
req.headers['accept'] = "application/vnd.koreader.v1+json" UIManager = require("ui/uimanager")
end logger = require("logger")
package.loaded['Spore.Middleware.KOSyncAuth'] = {} md5 = require("ffi/MD5")
require('Spore.Middleware.KOSyncAuth').call = function(args, req) local HTTPClient = require("httpclient")
req.headers['x-auth-user'] = args.username local Spore = require("Spore")
req.headers['x-auth-key'] = args.userkey client = Spore.new_from_string(service)
end package.loaded['Spore.Middleware.GinClient'] = {}
-- password should be hashed before submitting to server require('Spore.Middleware.GinClient').call = function(self, req)
local username, password = "koreader", md5.sum("koreader") req.headers['accept'] = "application/vnd.koreader.v1+json"
-- fake progress data end
local doc, percentage, progress, device = package.loaded['Spore.Middleware.KOSyncAuth'] = {}
"41cce710f34e5ec21315e19c99821415", -- fast digest of the document require('Spore.Middleware.KOSyncAuth').call = function(args, req)
0.356, -- percentage of the progress req.headers['x-auth-user'] = args.username
"69", -- page number or xpointer req.headers['x-auth-key'] = args.userkey
"my kpw" -- device name end
-- password should be hashed before submitting to server
username, password = "koreader", md5.sum("koreader")
-- fake progress data
doc, percentage, progress, device =
"41cce710f34e5ec21315e19c99821415", -- fast digest of the document
0.356, -- percentage of the progress
"69", -- page number or xpointer
"my kpw" -- device name
end)
it("should create new user", function() it("should create new user", function()
client:reset_middlewares() client:reset_middlewares()
client:enable('Format.JSON') client:enable('Format.JSON')
@ -89,14 +93,15 @@ describe("KOSync modules #notest #nocov", function()
end) end)
if ok then if ok then
if res.status == 200 then if res.status == 200 then
DEBUG("register successful to ", res.body.username) logger.dbg("register successful to ", res.body.username)
elseif res.status == 402 then elseif res.status == 402 then
DEBUG("register unsuccessful: ", res.body.message) logger.dbg("register unsuccessful: ", res.body.message)
end end
else else
DEBUG("Please retry later", res) logger.dbg("Please retry later", res)
end end
end) end)
it("should authorize user", function() it("should authorize user", function()
client:reset_middlewares() client:reset_middlewares()
client:enable('Format.JSON') client:enable('Format.JSON')
@ -112,12 +117,13 @@ describe("KOSync modules #notest #nocov", function()
if res.status == 200 then if res.status == 200 then
assert.are.same("OK", res.body.authorized) assert.are.same("OK", res.body.authorized)
else else
DEBUG(res.body) logger.dbg(res.body)
end end
else else
DEBUG("Please retry later", res) logger.dbg("Please retry later", res)
end end
end) end)
it("should update progress", function() it("should update progress", function()
client:reset_middlewares() client:reset_middlewares()
client:enable('Format.JSON') client:enable('Format.JSON')
@ -141,12 +147,13 @@ describe("KOSync modules #notest #nocov", function()
assert.are.same(percentage, result.percentage) assert.are.same(percentage, result.percentage)
assert.are.same(device, result.device) assert.are.same(device, result.device)
else else
DEBUG(res.body.message) logger.dbg(res.body.message)
end end
else else
DEBUG("Please retry later", res) logger.dbg("Please retry later", res)
end end
end) end)
it("should get progress", function() it("should get progress", function()
client:reset_middlewares() client:reset_middlewares()
client:enable('Format.JSON') client:enable('Format.JSON')
@ -167,10 +174,10 @@ describe("KOSync modules #notest #nocov", function()
assert.are.same(percentage, result.percentage) assert.are.same(percentage, result.percentage)
assert.are.same(device, result.device) assert.are.same(device, result.device)
else else
DEBUG(res.body.message) logger.dbg(res.body.message)
end end
else else
DEBUG("Please retry later", res) logger.dbg("Please retry later", res)
end end
end) end)

@ -1,4 +1,3 @@
local navigation_sample = [[ local navigation_sample = [[
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<!-- <!--
@ -227,11 +226,15 @@ local facet_sample = [[
</feed> </feed>
]] ]]
require("commonrequire")
local OPDSParser = require("ui/opdsparser")
local DEBUG = require("dbg")
describe("OPDS module #nocov", function() describe("OPDS module #nocov", function()
local OPDSParser, OPDSBrowser
setup(function()
require("commonrequire")
OPDSParser = require("ui/opdsparser")
OPDSBrowser = require("ui/widget/opdsbrowser")
end)
describe("OPDS parser module", function() describe("OPDS parser module", function()
it("should parse OPDS navigation catalog", function() it("should parse OPDS navigation catalog", function()
local catalog = OPDSParser:parse(navigation_sample) local catalog = OPDSParser:parse(navigation_sample)
@ -260,14 +263,12 @@ describe("OPDS module #nocov", function()
assert.are.same(entry.link[1].href, "//www.gutenberg.org/ebooks/42474.epub.images") assert.are.same(entry.link[1].href, "//www.gutenberg.org/ebooks/42474.epub.images")
end) end)
end) end)
describe("OPDS browser module", function()
local OPDSBrowser = require("ui/widget/opdsbrowser")
describe("OPDS browser module", function()
describe("URL generation", function() describe("URL generation", function()
it("should generate URL on rel=subsection", function() it("should generate URL on rel=subsection", function()
local catalog = OPDSParser:parse(navigation_sample) local catalog = OPDSParser:parse(navigation_sample)
local item_table = OPDSBrowser:genItemTableFromCatalog(catalog, "http://m.gutenberg.org/ebooks.opds/?format=opds") local item_table = OPDSBrowser:genItemTableFromCatalog(catalog, "http://m.gutenberg.org/ebooks.opds/?format=opds")
--DEBUG(item_table)
assert.truthy(item_table) assert.truthy(item_table)
assert.are.same(item_table[1].title, "Popular") assert.are.same(item_table[1].title, "Popular")
@ -276,7 +277,6 @@ describe("OPDS module #nocov", function()
it("should generate URL on rel=popular and rel=new", function() it("should generate URL on rel=popular and rel=new", function()
local catalog = OPDSParser:parse(popular_new_sample) local catalog = OPDSParser:parse(popular_new_sample)
local item_table = OPDSBrowser:genItemTableFromCatalog(catalog, "http://www.feedbooks.com/publicdomain/catalog.atom") local item_table = OPDSBrowser:genItemTableFromCatalog(catalog, "http://www.feedbooks.com/publicdomain/catalog.atom")
--DEBUG(item_table)
assert.truthy(item_table) assert.truthy(item_table)
assert.are.same(item_table[1].title, "Most Popular") assert.are.same(item_table[1].title, "Most Popular")
@ -287,7 +287,6 @@ describe("OPDS module #nocov", function()
it("should use the main URL for faceted links as long as faceted links aren't properly supported", function() it("should use the main URL for faceted links as long as faceted links aren't properly supported", function()
local catalog = OPDSParser:parse(facet_sample) local catalog = OPDSParser:parse(facet_sample)
local item_table = OPDSBrowser:genItemTableFromCatalog(catalog, "http://flibusta.net/opds") local item_table = OPDSBrowser:genItemTableFromCatalog(catalog, "http://flibusta.net/opds")
--DEBUG(item_table)
assert.truthy(item_table) assert.truthy(item_table)
assert.are.same(item_table[1].url, "http://flibusta.net/opds/author/75357") assert.are.same(item_table[1].url, "http://flibusta.net/opds/author/75357")
@ -297,7 +296,6 @@ describe("OPDS module #nocov", function()
it("should not fill item table incorrectly with thumbnail or image URL", function() it("should not fill item table incorrectly with thumbnail or image URL", function()
local catalog = OPDSParser:parse(facet_sample) local catalog = OPDSParser:parse(facet_sample)
local item_table = OPDSBrowser:genItemTableFromCatalog(catalog, "http://flibusta.net/opds") local item_table = OPDSBrowser:genItemTableFromCatalog(catalog, "http://flibusta.net/opds")
--DEBUG(item_table)
assert.truthy(item_table) assert.truthy(item_table)
assert.are_not.same(item_table[1].image, "http://flibusta.net/opds/author/75357") assert.are_not.same(item_table[1].image, "http://flibusta.net/opds/author/75357")

@ -1,9 +1,3 @@
require("commonrequire")
local UIManager = require("ui/uimanager")
local HTTPClient = require("httpclient")
local DEBUG = require("dbg")
--DEBUG:turnOn()
local service = [[ local service = [[
{ {
"base_url" : "http://httpbin.org", "base_url" : "http://httpbin.org",
@ -38,14 +32,20 @@ local service = [[
]] ]]
describe("Lua Spore modules #notest #nocov", function() describe("Lua Spore modules #notest #nocov", function()
local Spore = require("Spore") local Spore, client
local client = Spore.new_from_string(service) setup(function()
client:enable('Format.JSON') require("commonrequire")
Spore = require("Spore")
client = Spore.new_from_string(service)
client:enable('Format.JSON')
end)
it("should complete GET request", function() it("should complete GET request", function()
local info = {user = 'john', age = '25'} local info = {user = 'john', age = '25'}
local res = client:get_info(info) local res = client:get_info(info)
assert.are.same(res.body.args, info) assert.are.same(res.body.args, info)
end) end)
it("should complete POST request", function() it("should complete POST request", function()
local info = {user = 'sam', age = '26'} local info = {user = 'sam', age = '26'}
local res = client:post_info(info) local res = client:post_info(info)
@ -54,34 +54,42 @@ describe("Lua Spore modules #notest #nocov", function()
end) end)
describe("Lua Spore modules with async http request #notest #nocov", function() describe("Lua Spore modules with async http request #notest #nocov", function()
local Spore = require("Spore") local client, UIManager
local client = Spore.new_from_string(service)
local async_http_client = HTTPClient:new() setup(function()
package.loaded['Spore.Middleware.AsyncHTTP'] = {} require("commonrequire")
require('Spore.Middleware.AsyncHTTP').call = function(args, req) UIManager = require("ui/uimanager")
req:finalize() local HTTPClient = require("httpclient")
local result local Spore = require("Spore")
async_http_client:request({ client = Spore.new_from_string(service)
url = req.url, local async_http_client = HTTPClient:new()
method = req.method, package.loaded['Spore.Middleware.AsyncHTTP'] = {}
body = req.env.spore.payload, require('Spore.Middleware.AsyncHTTP').call = function(args, req)
on_headers = function(headers) req:finalize()
for header, value in pairs(req.headers) do local result
if type(header) == 'string' then async_http_client:request({
headers:add(header, value) url = req.url,
method = req.method,
body = req.env.spore.payload,
on_headers = function(headers)
for header, value in pairs(req.headers) do
if type(header) == 'string' then
headers:add(header, value)
end
end end
end end,
end, }, function(res)
}, function(res) result = res
result = res -- Turbo HTTP client uses code instead of status
-- Turbo HTTP client uses code instead of status -- change to status so that Spore can understand
-- change to status so that Spore can understand result.status = res.code
result.status = res.code coroutine.resume(args.thread)
coroutine.resume(args.thread) UIManager.INPUT_TIMEOUT = 100 -- no need in production
UIManager.INPUT_TIMEOUT = 100 -- no need in production end)
end) return coroutine.create(function() coroutine.yield(result) end)
return coroutine.create(function() coroutine.yield(result) end) end
end end)
it("should complete GET request", function() it("should complete GET request", function()
UIManager:quit() UIManager:quit()
local co = coroutine.create(function() local co = coroutine.create(function()
@ -96,6 +104,7 @@ describe("Lua Spore modules with async http request #notest #nocov", function()
coroutine.resume(co) coroutine.resume(co)
UIManager:runForever() UIManager:runForever()
end) end)
it("should complete POST request", function() it("should complete POST request", function()
UIManager:quit() UIManager:quit()
local co = coroutine.create(function() local co = coroutine.create(function()

Loading…
Cancel
Save