This commit is contained in:
commit
d9495b4122
21 files changed
+1720
No files matched your search
@@ -0,0 +1,90 @@
|
||||
--[[--
|
||||
维基文库客户端:api.php、坏响应、首页 Portal 只拉一次。
|
||||
|
||||
@module tests.wikisource.client_spec
|
||||
--]]
|
||||
---@diagnostic disable: undefined-field, missing-fields, redundant-parameter, inject-field
|
||||
|
||||
local Assert = require("support.assert")
|
||||
local JsonStub = require("support.json_stub")
|
||||
package.loaded["json"] = nil
|
||||
package.preload["json"] = function()
|
||||
return { decode = JsonStub.decode, encode = JsonStub.encode }
|
||||
end
|
||||
local JSON = require("json")
|
||||
local _ = require("l10n").get("moonsource_wikisource")
|
||||
|
||||
local UIManager = require("ui/uimanager")
|
||||
local queued_tick = UIManager.nextTick
|
||||
UIManager.nextTick = function(_, fn) fn() end
|
||||
|
||||
local calls, script = {}, {}
|
||||
package.preload["http.request"] = function()
|
||||
return {
|
||||
get = function(url, _opts, cb)
|
||||
calls[#calls + 1] = url
|
||||
local step = table.remove(script, 1) or { body = "{}" }
|
||||
if step.err then cb(nil, step.err) else cb(step.body) end
|
||||
return { cancel = function() end }
|
||||
end,
|
||||
}
|
||||
end
|
||||
|
||||
local Client = require("moonsource_wikisource.client")
|
||||
|
||||
local function restore()
|
||||
UIManager.nextTick = queued_tick
|
||||
end
|
||||
|
||||
do
|
||||
calls, script = {}, { { body = JSON.encode({
|
||||
parse = { title = "甲", text = "<p>文</p>" },
|
||||
}) } }
|
||||
local client = Client.new()
|
||||
local page, err
|
||||
client:parseAsync("甲/一", function(data, e) page, err = data, e end)
|
||||
Assert.is_nil(err)
|
||||
Assert.eq(page.title, "甲")
|
||||
Assert.eq(#calls, 1)
|
||||
Assert.is_true(calls[1]:find("action=parse", 1, true) ~= nil)
|
||||
Assert.is_true(calls[1]:find("page=%E7%94%B2%2F%E4%B8%80", 1, true) ~= nil)
|
||||
calls, script = {}, {}
|
||||
local again
|
||||
client:parseAsync("甲", function(data) again = data end)
|
||||
Assert.eq(again.title, "甲")
|
||||
Assert.eq(#calls, 0)
|
||||
end
|
||||
|
||||
do
|
||||
calls, script = {}, { { body = JSON.encode({
|
||||
parse = { wikitext = "[[Portal:典籍]]" },
|
||||
}) } }
|
||||
local client = Client.new()
|
||||
local first, second
|
||||
client:portalsAsync(function(data) first = data end)
|
||||
client:portalsAsync(function(data) second = data end)
|
||||
Assert.eq(first[1].id, "Portal:典籍")
|
||||
Assert.eq(second[1].id, "Portal:典籍")
|
||||
Assert.eq(#calls, 1)
|
||||
Assert.is_true(calls[1]:find("Wikisource%3A%E9%A6%96%E9%A1%B5", 1, true) ~= nil)
|
||||
end
|
||||
|
||||
do
|
||||
calls, script = {}, { { body = "<html>" }, { body = JSON.encode({ error = { code = "nosuchpage" } }) } }
|
||||
local client = Client.new()
|
||||
local err_html, err_api
|
||||
client:searchAsync("甲", function(_data, e) err_html = e end)
|
||||
client:searchAsync("乙", function(_data, e) err_api = e end)
|
||||
Assert.eq(err_html, _("响应无效"))
|
||||
Assert.eq(err_api, _("响应无效"))
|
||||
Assert.is_true(calls[1]:find("srsearch=%E7%94%B2", 1, true) ~= nil)
|
||||
end
|
||||
|
||||
do
|
||||
local client = Client.new()
|
||||
local err
|
||||
client:searchAsync("", function(_data, e) err = e end)
|
||||
Assert.eq(err, _("响应无效"))
|
||||
end
|
||||
|
||||
restore()
|
||||
@@ -0,0 +1,99 @@
|
||||
--[[--
|
||||
维基文库映射:Portal、子页面归书、按页面顺序取章节,全覽仅在没有分章时保留。
|
||||
|
||||
@module tests.wikisource.mapper_spec
|
||||
--]]
|
||||
---@diagnostic disable: undefined-field, need-check-nil
|
||||
|
||||
local Assert = require("support.assert")
|
||||
local _ = require("l10n").get("moonsource_wikisource")
|
||||
local Mapper = require("moonsource_wikisource.mapper")
|
||||
|
||||
do
|
||||
local portals, err = Mapper.portals("前文 [[Portal:典籍]] 和 [[Portal:小说|小說]] [[Portal:典籍]]")
|
||||
Assert.is_nil(err)
|
||||
Assert.eq(#portals, 2)
|
||||
Assert.eq(portals[1].id, "Portal:典籍")
|
||||
Assert.eq(portals[1].title, "典籍")
|
||||
Assert.eq(portals[2].id, "Portal:小说")
|
||||
Assert.eq(portals[2].title, "小說")
|
||||
end
|
||||
|
||||
do
|
||||
local portals, err = Mapper.portals("没有入口")
|
||||
Assert.is_nil(portals)
|
||||
Assert.eq(err, _("响应无效"))
|
||||
end
|
||||
|
||||
do
|
||||
local books, token, err = Mapper.portal({
|
||||
query = { pages = { {
|
||||
title = "Portal:典籍", ns = 100, links = {
|
||||
{ ns = 0, title = "紅樓夢" },
|
||||
{ ns = 0, title = "紅樓夢/第001回" },
|
||||
{ ns = 0, title = "論語" },
|
||||
} },
|
||||
} },
|
||||
["continue"] = { plcontinue = "NEXT" },
|
||||
}, "典籍")
|
||||
Assert.is_nil(err)
|
||||
Assert.eq(token, "NEXT")
|
||||
Assert.eq(#books, 2)
|
||||
Assert.eq(books[1].stable_id, "紅樓夢")
|
||||
Assert.eq(books[1].category, "典籍")
|
||||
Assert.eq(books[2].stable_id, "論語")
|
||||
end
|
||||
|
||||
do
|
||||
local books, err = Mapper.search({ query = { search = {
|
||||
{ title = "論語", snippet = "<span>一句</span>" },
|
||||
{ title = "論語集解/論語序", snippet = "序" },
|
||||
{ title = "論語/學而第一", snippet = "学" },
|
||||
} } })
|
||||
Assert.is_nil(err)
|
||||
Assert.eq(#books, 2)
|
||||
Assert.eq(books[1].stable_id, "論語")
|
||||
Assert.eq(books[1].intro, "一句")
|
||||
Assert.eq(books[2].stable_id, "論語集解")
|
||||
end
|
||||
|
||||
do
|
||||
local books, err = Mapper.search({ query = { search = {} } })
|
||||
Assert.is_nil(err)
|
||||
Assert.eq(#books, 0)
|
||||
end
|
||||
|
||||
do
|
||||
local html = '<a href="/wiki/%E7%94%B2/%E4%B8%80">一</a>'
|
||||
.. '<a href="/wiki/%E7%94%B2/%E5%85%A8%E8%A6%BD">全</a>'
|
||||
.. '<a href="/wiki/%E7%94%B2/%E4%B8%80_%E4%BA%8C">一二</a>'
|
||||
local toc, err = Mapper.toc({ title = "甲", html = html })
|
||||
Assert.is_nil(err)
|
||||
Assert.eq(#toc, 2)
|
||||
Assert.eq(toc[1].uid, "甲/一")
|
||||
Assert.eq(toc[1].idx, 1)
|
||||
Assert.eq(toc[2].uid, "甲/一 二")
|
||||
Assert.eq(toc[2].title, "一 二")
|
||||
end
|
||||
|
||||
do
|
||||
local toc = Mapper.toc({ title = "甲", html = '<a href="/wiki/%E7%94%B2/%E5%85%A8%E8%A7%88">全</a>' })
|
||||
Assert.eq(#toc, 1)
|
||||
Assert.eq(toc[1].uid, "甲/全览")
|
||||
end
|
||||
|
||||
do
|
||||
local toc = Mapper.toc({ title = "甲", html = "<p>正文</p>" })
|
||||
Assert.eq(#toc, 1)
|
||||
Assert.eq(toc[1].uid, "甲")
|
||||
Assert.eq(toc[1].title, "甲")
|
||||
end
|
||||
|
||||
do
|
||||
local html, err = Mapper.chapterHtml({ title = "甲", html = "<p>正文</p>" })
|
||||
Assert.is_nil(err)
|
||||
Assert.is_true(html:find(Mapper.CONTENT_MARK, 1, true) == 1 + #"<!--")
|
||||
local empty, empty_err = Mapper.chapterHtml({ title = "甲", html = "<div></div>" })
|
||||
Assert.is_nil(empty)
|
||||
Assert.eq(empty_err, _("这一章没有正文"))
|
||||
end
|
||||
Reference in new issue
Block a user