From 5e1e2464cd785449f8bd4455339d95e990f8bf3b Mon Sep 17 00:00:00 2001 From: Tobias Schmitz Date: Wed, 24 Jul 2024 01:03:39 +0200 Subject: [PATCH] test: update json test --- lua/crates/api.lua | 8 ++++---- test/json_spec.lua | 7 ++++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/lua/crates/api.lua b/lua/crates/api.lua index 9803722..f11f46c 100644 --- a/lua/crates/api.lua +++ b/lua/crates/api.lua @@ -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") @@ -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 @@ -304,7 +304,7 @@ function M.parse_crate(index_json_str, meta_json) ---@type table 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 @@ -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 diff --git a/test/json_spec.lua b/test/json_spec.lua index 920c479..4924d77 100644 --- a/test/json_spec.lua +++ b/test/json_spec.lua @@ -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