local util = require("overseer.util")

describe("util", function()
  describe("get_stdout_line_iter", function()
    it("iterates over lines", function()
      local stdout_iter = util.get_stdout_line_iter()
      local ret = stdout_iter({ "first", "second", "" })
      assert.are.same({ "first", "second" }, ret)
    end)
    it("line can be split over multiple calls", function()
      local stdout_iter = util.get_stdout_line_iter()
      local ret = stdout_iter({ "foo" })
      vim.list_extend(ret, stdout_iter({ "bar" }))
      vim.list_extend(ret, stdout_iter({ "" }))
      assert.are.same({ "foobar" }, ret)
    end)
    it("line can have a break immediately after", function()
      local stdout_iter = util.get_stdout_line_iter()
      local ret = stdout_iter({ "foo", "" })
      vim.list_extend(ret, stdout_iter({ "bar" }))
      vim.list_extend(ret, stdout_iter({ "" }))
      assert.are.same({ "foo", "bar" }, ret)
    end)
    it("line can have a break on the next set of data", function()
      local stdout_iter = util.get_stdout_line_iter()
      local ret = stdout_iter({ "foo" })
      vim.list_extend(ret, stdout_iter({ "", "bar" }))
      vim.list_extend(ret, stdout_iter({ "" }))
      assert.are.same({ "foo", "bar" }, ret)
    end)
    it("line can split over calls", function()
      local stdout_iter = util.get_stdout_line_iter()
      local ret = stdout_iter({ "fo" })
      vim.list_extend(ret, stdout_iter({ "o", "bar" }))
      vim.list_extend(ret, stdout_iter({ "" }))
      assert.are.same({ "foo", "bar" }, ret)
    end)
    it("removes carriage returns", function()
      local stdout_iter = util.get_stdout_line_iter()
      local ret = stdout_iter({ "foobar\r" })
      vim.list_extend(ret, stdout_iter({ "" }))
      assert.are.same({ "foobar" }, ret)
    end)
    it("handles text in multiple chunks", function()
      local stdout_iter = util.get_stdout_line_iter()
      local ret = stdout_iter({ "", "foo", "bar", "baz" })
      vim.list_extend(ret, stdout_iter({ "" }))
      assert.are.same({ "", "foo", "bar", "baz" }, ret)
    end)
  end)
end)
