test_codemirror.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. from typing import Dict, List
  2. import pytest
  3. from nicegui import ui
  4. from nicegui.testing import Screen
  5. # pylint: disable=protected-access
  6. def test_codemirror(screen: Screen):
  7. ui.codemirror('Line 1\nLine 2\nLine 3')
  8. screen.open('/')
  9. screen.should_contain('Line 2')
  10. def test_supported_values(screen: Screen):
  11. values: Dict[str, List[str]] = {}
  12. @ui.page('/')
  13. def page():
  14. editor = ui.codemirror()
  15. async def fetch():
  16. values['languages'] = await editor.run_method('getLanguages')
  17. values['themes'] = await editor.run_method('getThemes')
  18. values['supported_themes'] = editor.supported_themes
  19. values['supported_languages'] = editor.supported_languages
  20. ui.label('Done')
  21. ui.button('Fetch', on_click=fetch)
  22. screen.open('/')
  23. screen.click('Fetch')
  24. screen.wait_for('Done')
  25. assert values['languages'] == values['supported_languages']
  26. assert values['themes'] == values['supported_themes']
  27. @pytest.mark.parametrize('doc, sections, inserted, expected', [
  28. ('', [0, 1], [['A']], 'A'),
  29. ('', [0, 2], [['AB']], 'AB'),
  30. ('X', [1, 2], [['AB']], 'AB'),
  31. ('X', [1, -1], [], 'X'),
  32. ('X', [1, -1, 0, 1], [[], ['Y']], 'XY'),
  33. ('Hello', [5, -1, 0, 8], [[], [', world!']], 'Hello, world!'),
  34. ('Hello, world!', [5, -1, 7, 0, 1, -1], [], 'Hello!'),
  35. ('Hello, hello!', [2, -1, 3, 1, 4, -1, 3, 1, 1, -1], [[], ['y'], [], ['y']], 'Hey, hey!'),
  36. ('Hello, world!', [5, -1, 1, 3, 7, -1], [[], [' 🙂']], 'Hello 🙂 world!'),
  37. ('Hey! 🙂', [7, -1, 0, 4], [[], [' Ho!']], 'Hey! 🙂 Ho!'),
  38. ('Ha 🙂\nha 🙂', [3, -1, 2, 0, 4, -1, 2, 0], [[], [''], [], ['']], 'Ha \nha '),
  39. ])
  40. def test_change_set(screen: Screen, doc: str, sections: List[int], inserted: List[List[str]], expected: str):
  41. editor = ui.codemirror(doc)
  42. screen.open('/')
  43. assert editor._apply_change_set(sections, inserted) == expected
  44. def test_encode_codepoints():
  45. assert ui.codemirror._encode_codepoints('') == b''
  46. assert ui.codemirror._encode_codepoints('Hello') == bytes([1, 1, 1, 1, 1])
  47. assert ui.codemirror._encode_codepoints('🙂') == bytes([0, 1])
  48. assert ui.codemirror._encode_codepoints('Hello 🙂') == bytes([1, 1, 1, 1, 1, 1, 0, 1])
  49. assert ui.codemirror._encode_codepoints('😎😎😎') == bytes([0, 1, 0, 1, 0, 1])