example_spec.lua 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. -- To run this test:
  2. -- TEST_FILE=test/functional/example_spec.lua make functionaltest
  3. local helpers = require('test.functional.helpers')(after_each)
  4. local Screen = require('test.functional.ui.screen')
  5. local clear = helpers.clear
  6. local command = helpers.command
  7. local eq = helpers.eq
  8. local feed = helpers.feed
  9. describe('example', function()
  10. local screen
  11. before_each(function()
  12. clear()
  13. screen = Screen.new(20,5)
  14. screen:attach()
  15. screen:set_default_attr_ids( {
  16. [0] = {bold=true, foreground=Screen.colors.Blue},
  17. [1] = {bold=true, foreground=Screen.colors.Brown}
  18. } )
  19. end)
  20. it('screen test', function()
  21. -- Do some stuff.
  22. feed('iline1<cr>line2<esc>')
  23. -- For debugging only: prints the current screen.
  24. -- screen:snapshot_util()
  25. -- Assert the expected state.
  26. screen:expect([[
  27. line1 |
  28. line^2 |
  29. {0:~ }|
  30. {0:~ }|
  31. |
  32. ]])
  33. end)
  34. it('override UI event-handler', function()
  35. -- Example: override the "tabline_update" UI event handler.
  36. --
  37. -- screen.lua defines default handlers for UI events, but tests
  38. -- may sometimes want to override a handler.
  39. -- The UI must declare that it wants to handle the UI events.
  40. -- For this example, we enable `ext_tabline`:
  41. screen:detach()
  42. screen = Screen.new(25, 5)
  43. screen:attach({rgb=true, ext_tabline=true})
  44. -- From ":help ui" we find that `tabline_update` receives `curtab` and
  45. -- `tabs` objects. So we declare the UI handler like this:
  46. local event_tabs, event_curtab
  47. function screen:_handle_tabline_update(curtab, tabs)
  48. event_curtab, event_tabs = curtab, tabs
  49. end
  50. -- Create a tabpage...
  51. command('tabedit foo')
  52. -- Use screen:expect{condition=…} to check the result.
  53. screen:expect{condition=function()
  54. eq({ id = 2 }, event_curtab)
  55. eq({
  56. {tab = { id = 1 }, name = '[No Name]'},
  57. {tab = { id = 2 }, name = 'foo'},
  58. },
  59. event_tabs)
  60. end}
  61. end)
  62. end)