Skip to content

Commit

Permalink
test: update json test
Browse files Browse the repository at this point in the history
  • Loading branch information
saecki committed Jul 23, 2024
1 parent 4b9579b commit 5e1e246
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
8 changes: 4 additions & 4 deletions lua/crates/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ local JSON_DECODE_OPTS = { luanil = { object = true, array = true } }
---comment
---@param json_str string
---@return table
local function parse_json(json_str)
function M.parse_json(json_str)
---@type any
local json = vim.json.decode(json_str, JSON_DECODE_OPTS)
assert(type(json) == "table")
Expand Down Expand Up @@ -180,7 +180,7 @@ end
---@param json_str string
---@return ApiCrateSummary[]?
function M.parse_search(json_str)
local json = parse_json(json_str)
local json = M.parse_json(json_str)
if not (json and json.crates) then
return
end
Expand Down Expand Up @@ -304,7 +304,7 @@ function M.parse_crate(index_json_str, meta_json)
---@type table<string,ApiVersion>
local versions = {}
for _, line in ipairs(lines) do
local json = parse_json(line)
local json = M.parse_json(line)
assert(json.vers ~= nil)

---@type ApiVersion
Expand Down Expand Up @@ -546,7 +546,7 @@ local function fetch_crate(name, callbacks)
-- the crates.io api is case and hyphen/underscore insensitive, but the sparse index
-- requires the exact crate name. If the name doesn't match refetch the index with the
-- exact name.
local ok, json = pcall(parse_json, json_str)
local ok, json = pcall(M.parse_json, json_str)
meta_json = (ok and json) or nil
if ok and json.crate and json.crate.id ~= name then
refetch = true
Expand Down
7 changes: 4 additions & 3 deletions test/json_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,20 @@ describe("json", function()
---@type string
local index_json_str
---@type string
local meta_json_str
local meta_json
it("read index file", function()
index_json_str = io.open("test/diesel_index.json"):read("a")
assert.equals("string", type(index_json_str))
end)

it("read meta file", function()
meta_json_str = io.open("test/diesel_meta.json"):read("a")
local meta_json_str = io.open("test/diesel_meta.json"):read("a")
assert.equals("string", type(meta_json_str))
meta_json = api.parse_json(meta_json_str)
end)

it("parse crate", function()
local crate = api.parse_crate(index_json_str, meta_json_str)
local crate = api.parse_crate(index_json_str, meta_json)
assert.is_not_nil(crate)

---@type ApiCrate
Expand Down

0 comments on commit 5e1e246

Please sign in to comment.