spec/util_json_spec.lua
author Kim Alvefur <zash@zash.se>
Tue, 14 May 2024 17:07:47 +0200
changeset 13494 6f840763fc73
parent 10595 d78c5c9b0cf6
permissions -rw-r--r--
net.server_epoll: Add support for systemd socket activation Allows creating listening sockets and accepting client connections before Prosody starts. This is unlike normal Prosody dynamic resource management, where ports may added and removed at any time, and the ports defined by the config. Weird things happen if these are closed (e.g. due to reload) so here we prevent closing and ensure sockets are reused when opened again.


local json = require "util.json";
local array = require "util.array";

describe("util.json", function()
	describe("#encode()", function()
		it("should work", function()
			local function test(f, j, e)
				if e then
					assert.are.equal(f(j), e);
				end
				assert.are.equal(f(j), f(json.decode(f(j))));
			end
			test(json.encode, json.null, "null")
			test(json.encode, {}, "{}")
			test(json.encode, {a=1});
			test(json.encode, {a={1,2,3}});
			test(json.encode, {1}, "[1]");
		end);
	end);

	describe("#decode()", function()
		it("should work", function()
			local empty_array = json.decode("[]");
			assert.are.equal(type(empty_array), "table");
			assert.are.equal(#empty_array, 0);
			assert.are.equal(next(empty_array), nil);
		end);
	end);

	describe("testcases", function()

		local valid_data = {};
		local invalid_data = {};

		local skip = "fail1.json fail9.json fail18.json fail15.json fail13.json fail25.json fail26.json fail27.json fail28.json fail17.json pass1.json";

		setup(function()
			local lfs = require "lfs";
			local path = "spec/json";
			for name in lfs.dir(path) do
				if name:match("%.json$") then
					local f = assert(io.open(path.."/"..name));
					local content = assert(f:read("*a"));
					assert(f:close());
					if skip:find(name) then --luacheck: ignore 542
						-- Skip
					elseif name:match("^pass") then
						valid_data[name] = content;
					elseif name:match("^fail") then
						invalid_data[name] = content;
					end
				end
			end
		end)

		it("should pass valid testcases", function()
			for name, content in pairs(valid_data) do
				local parsed, err = json.decode(content);
				assert(parsed, name..": "..tostring(err));
			end
		end);

		it("should fail invalid testcases", function()
			for name, content in pairs(invalid_data) do
				local parsed, err = json.decode(content);
				assert(not parsed, name..": "..tostring(err));
			end
		end);
	end)

	describe("util.array integration", function ()
		it("works", function ()
			assert.equal("[]", json.encode(array()));
			assert.equal("[1,2,3]", json.encode(array({1,2,3})));
			assert.equal(getmetatable(array()), getmetatable(json.decode("[]")));
		end);
	end);

end);