-- Define helper aliases
local new_set = MiniTest.new_set
local expect, eq = MiniTest.expect, MiniTest.expect.equality

-- Create (but not start) child Neovim object
local child = MiniTest.new_child_neovim()

-- Define main test set of this file
local T = new_set({
  -- Register hooks
  hooks = {
    -- This will be executed before every (even nested) case
    pre_case = function()
      -- Restart child process with custom 'init.lua' script
      child.restart({ "-u", "scripts/minimal_init.lua" })
      -- Load tested plugin
      child.lua([[require('python').setup()]])
    end,
    -- This will be executed one after all tests from this set are finished
    post_once = child.stop,
  },
})

local get_lines = function()
  return child.api.nvim_buf_get_lines(0, 0, -1, true)
end

T["wrap_cursor"] = MiniTest.new_set({
  hooks = {
    pre_case = function()
      child.cmd("e _not_existing_new_buffer.py")
      child.type_keys("cc", [["TEST"]], "<Esc>", "0")
    end,
  },
})

T["wrap_cursor"]["normal"] = function()
  child.cmd("Python treesitter wrap_cursor test(%s)")
  eq(get_lines(), { [[test("TEST")]] })
end

T["wrap_cursor"]["visual"] = function()
  child.type_keys("l", "v", "$")
  child.type_keys([[:Python treesitter wrap_cursor test(%s)<cr>]])
  eq(get_lines(), { [["test(TEST")]] })
end

T["wrap_cursor"]["visual_with_selection"] = function()
  child.type_keys("l", "v", "$")
  child.type_keys([[:Python treesitter wrap_cursor<cr>1<cr>]])
  eq(get_lines(), { [["print(TEST")]] })
end

T["wrap_cursor"]["with_selection"] = function()
  child.type_keys([[:Python treesitter wrap_cursor<cr>1<cr>]])
  eq(get_lines(), { [[print("TEST")]] })
end

T["enumerate"] = function()
  child.cmd("e _not_existing_new_buffer.py")
  child.type_keys("cc", "for i in [1, 2, 3]:", "<Esc>", "0")
  child.type_keys([[:Python treesitter toggle_enumerate<cr>]])
  eq(get_lines(), { "for idx, i in enumerate([1, 2, 3]):" })
end

-- Return test set which will be collected and execute inside `MiniTest.run()`
return T
