local assert = require("luassert")
local Cache = require("hlchunk.utils.cache")

describe("cache", function()
    it("cache with one", function()
        local cache = Cache("bufnr")
        assert.equal(nil, cache:get(1))
        cache:set(0, "a")
        cache:set(1, "b")
        assert.equal("a", cache:get(0))
        assert.equal("b", cache:get(1))
        cache:set(0, "c")
        assert.equal("c", cache:get(0))
        assert.is_true(cache:has(0))
        assert.is_false(cache:has(2))
        cache:clear()
        assert.equal(nil, cache:get(0))
    end)
    it("cache with two field", function()
        local cache = Cache("bufnr", "line")
        assert.equal(nil, cache:get(1, 1))
        cache:set(0, 0, "a")
        cache:set(0, 1, "b")
        cache:set(1, 0, "c")
        cache:set(1, 1, "d")
        assert.equal("a", cache:get(0, 0))
        assert.equal("b", cache:get(0, 1))
        assert.equal("c", cache:get(1, 0))
        assert.equal("d", cache:get(1, 1))
        cache:set(0, 0, "e")
        assert.equal("e", cache:get(0, 0))
        assert.is_true(cache:has(0, 0))
        assert.is_false(cache:has(2, 2))
        cache:clear(0)
        assert.equal(nil, cache:get(0, 0))
        assert.equal("c", cache:get(1, 0))
        cache:clear()
        assert.equal(nil, cache:get(0, 0))
        assert.equal(nil, cache:get(1, 0))
    end)
    it("cache with three field", function()
        local cache = Cache("bufnr", "line", "col")
        assert.equal(nil, cache:get(1, 1, 1))
        cache:set(0, 0, 0, "a")
        cache:set(0, 0, 1, "b")
        cache:set(0, 1, 0, "c")
        cache:set(0, 1, 1, "d")
        cache:set(1, 0, 0, "e")
        cache:set(1, 0, 1, "f")
        cache:set(1, 1, 0, "g")
        cache:set(1, 1, 1, "h")
        assert.equal("a", cache:get(0, 0, 0))
        assert.equal("b", cache:get(0, 0, 1))
        assert.equal("c", cache:get(0, 1, 0))
        assert.equal("d", cache:get(0, 1, 1))
        assert.equal("e", cache:get(1, 0, 0))
        assert.equal("f", cache:get(1, 0, 1))
        assert.equal("g", cache:get(1, 1, 0))
        assert.equal("h", cache:get(1, 1, 1))
        cache:set(0, 0, 0, "i")
        assert.equal("i", cache:get(0, 0, 0))
        assert.is_true(cache:has(0, 0, 0))
        assert.is_false(cache:has(2, 2, 2))
        cache:clear(0, 0)
        assert.equal(nil, cache:get(0, 0, 0))
        assert.equal("c", cache:get(0, 1, 0))
        cache:clear()
        assert.equal(nil, cache:get(0, 0, 0))
        assert.equal(nil, cache:get(1, 0, 0))
    end)
    it("cache remove", function()
        local cache = Cache("bufnr", "line", "col")
        cache:set(0, 0, 0, "a")
        cache:set(0, 0, 1, "b")
        cache:set(0, 1, 0, "c")

        cache:remove(0, 0, 0)
        assert.equal(nil, cache:get(0, 0, 0))
        assert.equal("b", cache:get(0, 0, 1))
        assert.equal("c", cache:get(0, 1, 0))
    end)
end)
