Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to use plenary to unit test #48

Closed
windwp opened this issue Jan 13, 2021 · 7 comments
Closed

how to use plenary to unit test #48

windwp opened this issue Jan 13, 2021 · 7 comments

Comments

@windwp
Copy link

windwp commented Jan 13, 2021

i want to use plenary to do some unit test but i got a some issue with automatic feedkeys function.

describe('pair input test ', function()
  it("test case 1",function()
      local line=1
      local pos =4;
      vim.fn.setline(line , before)
      vim.fn.setpos('.' ,{0 , line , pos , 0})
      vim.fn.feedkeys("i(")
      vim.fn.feedkeys("<ESC>")

        vim.wait(5,function()
            local result = vim.fn.getline(line)
            --  text result still no change ?
            print('result'..vim.inspect(result))
            local pos = vim.fn.getpos('.')
            return true
         end)
end)
end)

how do i test the text result after i use feedkeys function?

Thank

@Conni2461
Copy link
Collaborator

luassert is bundled with plenary and is used to check if both entries are the same and more.
So what you wanna do is assert.are.equal(expected, result).
You can find a lot of examples in our test directory like curl_spec and the documentation how to test here.

@windwp
Copy link
Author

windwp commented Jan 14, 2021

hi it is not my issue
my issue is about feedkeys function it is not change the text line and execute my mapping function.
i already use vim.wait but it still not change a text.

@Conni2461
Copy link
Collaborator

Oh sorry my bad i completely misread the issue.
If i remember correctly tj also had issues with nvim_feedkeys for telescope integration tests so it might be best to wait for him but FWIW this first it runs this test which will make use of this feed function. Maybe this helps you a little bit more

@tjdevries
Copy link
Member

That's not how vim.wait works. I am not sure what you're trying to do exactly.

@windwp
Copy link
Author

windwp commented Jan 15, 2021

@tjdevries
i want to use feedkeys and get a result text after that. My usecase is very simple i want to test my mapping function in insermode

You can get full code here.

https://github.com/windwp/nvim-autopairs/blob/testfeedkeys/tests/pairs_spec.lua

Thank

@Julian
Copy link

Julian commented Jan 16, 2021

@windwp I don't know if you mean to ask about the same issue I had (how to write tests that insert text, then check for a result) but if that's what you're asking about, it took me about an hour or two to figure out the missing piece was passing 'x' to feedkeys, after which finally text actually gets inserted that I can read back.

Example is here via this helper.

Still can't figure out how to get empty buffers so that I don't have to do cleanup myself, but yeah I think that's maybe what you're after?

@windwp
Copy link
Author

windwp commented Jan 17, 2021

Thank @Julian
passing 'x' to feedkeys function will fix my issues.

@windwp windwp closed this as completed Jan 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants