test_draft_ui.py (2772B)
1 try: 2 import talon.experimental.textarea 3 running_in_talon = True 4 except ModuleNotFoundError: 5 # Some shenanigans to stub out the Talon imports 6 import imp, sys 7 name = "talon.experimental.textarea" 8 module = imp.new_module(name) 9 sys.modules[name] = module 10 exec( 11 "\n".join([ 12 "TextArea = 1", 13 "Span = 1", 14 "DarkThemeLabels = 1", 15 "LightThemeLabels = 1" 16 ]), 17 module.__dict__ 18 ) 19 running_in_talon = False 20 21 from unittest import TestCase 22 from functools import wraps 23 24 from .draft_ui import calculate_text_anchors 25 26 27 class CalculateAnchorsTest(TestCase): 28 """ 29 Tests calculate_text_anchors 30 """ 31 32 def test_finds_anchors(self): 33 examples = [ 34 ("one-word", [("a", 0, 8, 8)]), 35 ("two words", [("a", 0, 3, 4), ("b", 4, 9, 9)]), 36 ("two\nwords", [("a", 0, 3, 4), ("b", 4, 9, 9)]), 37 ] 38 anchor_labels = ["a", "b"] 39 for text, expected in examples: 40 # Given an example 41 42 # When we calculate the result and turn it into a list 43 result = list(calculate_text_anchors(text, 0, anchor_labels=anchor_labels)) 44 45 # Then it matches what we expect 46 self.assertEqual(result, expected, text) 47 48 def test_positions_anchors_around_cursor(self): 49 # In these examples the cursor is at the asterisk which is stripped by the test 50 # code. Indicies after the asterisk have to take this into account. 51 examples = [ 52 ("one*-word", [("a", 0, 8, 8)]), 53 ("one-word*", [("a", 0, 8, 8)]), 54 ( 55 "the three words*", 56 [("a", 0, 3, 4), ("b", 4, 9, 10), ("c", 10, 15, 15)] 57 ), 58 ( 59 "*the three words", 60 [("a", 0, 3, 4), ("b", 4, 9, 10), ("c", 10, 15, 15)] 61 ), 62 ( 63 "too many* words for the number of anchors", 64 [("a", 0, 3, 4), ("b", 4, 8, 9), ("c", 9, 14, 15)] 65 ), 66 ( 67 "too many words fo*r the number of anchors", 68 [("a", 9, 14, 15), ("b", 15, 18, 19), ("c", 19, 22, 23)] 69 ), 70 ] 71 anchor_labels = ["a", "b", "c"] 72 73 for text_with_cursor, expected in examples: 74 # Given an example 75 cursor_pos = text_with_cursor.index("*") 76 text = text_with_cursor.replace("*", "") 77 78 # When we calculate the result and turn it into a list 79 result = list(calculate_text_anchors( 80 text, 81 cursor_pos, 82 anchor_labels=anchor_labels 83 )) 84 85 # Then it matches what we expect 86 self.assertEqual(result, expected, text)