def test_03(): "Factory" factory = Factory() texel = TextModel("123\n\n567890 2 4 6 8 0\n").texel boxes = factory.create_boxes(texel) assert str(boxes) == "(TB('123'), NL, NL, TB('567890 2 4 6 8 0'), NL)" paragraphs = create_paragraphs(boxes) stack = ParagraphStack(paragraphs) assert str(tuple(stack.childs)) == "(Paragraph[Row[TB('123'), NL]], " \ "Paragraph[Row[NL]], Paragraph[Row[TB('567890 2 4 6 8 0'), NL]])" # line break paragraphs = create_paragraphs(boxes, 5) # Depends on the line break algorithm. Here breaking after space # is assumed. assert repr(paragraphs) == "[Paragraph[Row[TB('123'), NL]]," \ " Paragraph[Row[NL]], Paragraph[Row[TB('56789')], Row[TB('0 2 ')], " \ "Row[TB('4 6 ')], Row[TB('8 0'), NL]]]" texel = TextModel("123\t\t567890 2 4 6 8 0\n").texel boxes = factory.create_boxes(texel) factory.create_boxes(texel) paragraphs = create_paragraphs(boxes)
def test_02(): "ParagraphStack" factory = Factory() texel = TextModel("123\n567\n").texel boxes = factory.create_boxes(texel) assert listtools.calc_length(boxes) == 8 paragraphs = create_paragraphs(boxes) stack = ParagraphStack(paragraphs) assert check_box(stack, texel) texel = TextModel("123\n\n5\n67\n").texel boxes = factory.create_boxes(texel) assert listtools.calc_length(boxes) == 10 paragraphs = create_paragraphs(boxes) stack = ParagraphStack(paragraphs) assert check_box(stack, texel) texel = TextModel("123\n").texel boxes = factory.create_boxes(texel) paragraphs = create_paragraphs(boxes) stack = ParagraphStack(paragraphs) par = stack.childs[-1] assert len(par) == 4 assert len(stack) == 4 if 0: for p in paragraphs: p.dump_boxes(0, 0, 0) assert stack.get_info(3, 0, 0)[-2:] == (3, 0) texel = TextModel("").texel boxes = factory.create_boxes(texel) paragraphs = create_paragraphs(boxes) stack = ParagraphStack(paragraphs)
def test_04(): "copy cells" model = TextModel('') tmp = TextModel(u'for a in range(5):\n print a') cell = Cell([tmp.texel], [Characters(u'')]) model.insert(len(model), mk_textmodel(cell)) tmp = model.copy(0, len(model)) model.insert(0, tmp)
def output(self, obj, iserr=False): if isinstance(obj, matplotlib.figure.Figure): texel = Figure(obj) elif isinstance(obj, unicode): texel = TextModel(obj).texel else: texel = TextModel(unicode(obj)).texel self.texel = self.texel.insert(self.i, texel) self.i += len(texel)
def test_01(): factory = Factory() boxes = factory.create_boxes(TextModel("123").texel) assert listtools.calc_length(boxes) == 3 boxes = factory.create_boxes(TextModel("123\n567").get_xtexel()) assert listtools.calc_length(boxes) == 8 paragraphs = create_paragraphs(boxes) assert len(paragraphs) == 2 assert len(paragraphs[0]) == 4 assert len(paragraphs[1]) == 4 assert listtools.calc_length(paragraphs) == 8
def test_08(): "linewrap" model = TextModel("aa bb cc dd ee") updater = Updater(model, maxw=0) updater.get_layout().dump_boxes(0, 0, 0) model = TextModel("aa bb cc dd ee") updater.set_maxw(5) updater.get_layout().dump_boxes(0, 0, 0) layout = updater.get_layout() for j1, j2, x, y, child in layout.iter(0, 0, 0): print j1, j2, x, y, child
def test_11(): ns = init_testing(redirect=True) model = ns['model'] model.remove(0, len(model)) tmp = TextModel(u'for a in range(16):\n print a') cell = Cell(tmp.texel, Text(u'')) model.insert(len(model), mk_textmodel(cell)) assert model.index2position(0) == (0, 0) assert model.index2position(1) == (1, 0) cell = model.texel assert find_cell(model.texel, 1) == (0, cell) view = ns['view'] view.index = 1 #print model.texel view.execute() check_box(view.builder._layout, model.texel) #view.layout.dump_boxes(0, 0, 0) #assert inside_cell(view.layout, 68, 68) model.insert_text(68, u'x') assert model.get_text()[65:71] == '14\nx15' #view.layout.dump_boxes(0, 0, 0) model.remove(68, 69) #view.layout.dump_boxes(0, 0, 0) assert model.get_text()[65:70] == '14\n15' model.insert(0, mk_textmodel(cell)) model.remove(0, length(cell)) # insert new cell, insert input view.insert(0, TextModel("a=1")) # insert output view.insert(5, TextModel("x")) # remove the complete input-part model.remove(1, 4) # remove the complete output-part model.remove(2, 3) return ns
def test_03(): "find_cell" tmp1 = TextModel(u'for a in range(3):\n print a') tmp2 = TextModel(u'for a in range(10):\n print a') cell1 = Cell([tmp1.texel], [Characters(u'')]) cell2 = Cell([tmp2.texel], [Characters(u'')]) model = TextModel('') model.insert(len(model), mk_textmodel(cell1)) model.insert(len(model), mk_textmodel(cell2)) assert find_cell(model.texel, 1) == (0, cell1) assert find_cell(model.texel, len(cell1) - 1) == (0, cell1) assert find_cell(model.texel, len(cell1)) == (len(cell1), cell2) assert find_cell(model.texel, len(cell1) + 5) == (len(cell1), cell2)
def test_07(): # Problem: when clicking right beside a row the cursor is inserted # in a wrong position. model = TextModel("123\n567") updater = Updater(model, maxw=0) layout = updater._layout assert layout.get_index(100, 0.5) == 3
def test_000(): ns = init_testing(False) frac = Fraction(T(u'Zähler'), T(u'Nenner')) factory = Builder(TextModel()) # not very nice box = factory.Fraction_handler(frac, 0, length(frac))[0] assert len(box) == length(frac) assert check_box(box, frac) assert check_box(box.nominator) assert check_box(box.denominator) model = ns['model'] #model.insert_text(0, "1234") model.insert_fraction(len(model)) #model.texel.dump() model.insert_text(0, "x") model.remove(0, 1) #model.texel.dump() model.insert_text(1, "x") model.remove(1, 2) #model.texel.dump() layout = ns['view'].layout assert check_box(layout)
def output(self, obj, iserr=False): if iserr: properties = {'textcolor': 'red'} else: properties = {} if has_classname(obj, "matplotlib.figure.Figure"): new = mk_textmodel(Figure(obj)) elif isinstance(obj, unicode): new = TextModel(obj, **properties) elif isinstance(obj, str): u = unicode(obj, 'utf-8') new = TextModel(u, **properties) else: new = TextModel(str(obj), **properties) self.model.insert(self.i, new) self.i += len(new)
def test_06(): model = TextModel("123\n") updater = Updater(model, maxw=0) layout = updater._layout assert layout.get_info(4, 0, 0) assert str(layout.get_info(3, 0, 0)) == "(NL, 0, 3, 0)" assert layout.get_info(3, 0, 0)[-2:] == (3, 0) assert layout.get_info(4, 0, 0)[-2:] == (0, 1)
def test_04(): "insert/remove" factory = Factory() model = TextModel("123\n\n567890 2 4 6 8 0") updater = Updater(model, maxw=0) layout = updater._layout assert repr(layout) == "ParagraphStack[Paragraph[Row[TB('123'), " \ "NL]], Paragraph[Row[NL]], Paragraph[Row[TB('567890 2 4 6 8 0'), " \ "ENDMARK]]]" assert len(layout) == len(model) + 1 assert layout.height == 3 ins = TextModel("xyz\n") model.insert(2, ins) updater.inserted(2, len(ins)) assert len(updater._layout) == len(model) + 1 assert repr(updater._layout) == "ParagraphStack[Paragraph[Row[TB('12xyz'),"\ " NL]], Paragraph[Row[TB('3'), NL]], Paragraph[Row[NL]], "\ "Paragraph[Row[TB('567890 2 4 6 8 0'), ENDMARK]]]" assert updater._layout.height == 4 model.remove(2, 2 + len(ins)) updater.removed(2, len(ins)) assert len(updater._layout) == len(model) + 1 assert repr(updater._layout) == "ParagraphStack[Paragraph[Row[TB('123'), " \ "NL]], Paragraph[Row[NL]], Paragraph[Row[TB('567890 2 4 6 8 0'), " \ "ENDMARK]]]" assert updater._layout.height == 3 factory = Factory() model = TextModel("123") updater = Updater(model, maxw=0) layout = updater._layout ins = TextModel("xyz\n") i = len(model) model.insert(i, ins) updater.inserted(i, len(ins)) for c in "abc": ins = TextModel(c) i = len(model) model.insert(i, ins) updater.inserted(i, len(ins)) assert str(updater._layout) == \ "ParagraphStack[Paragraph[Row[TB('123xyz'), NL]], " \ "Paragraph[Row[TB('abc'), ENDMARK]]]"
def test_01(): ns = init_testing(False) model = ns['model'] model.remove(0, len(model)) model.insert(0, TextModel(__doc__)) text = """Try to edit the following formulas: tan(x) = """ model.insert(len(model), TextModel(text)) frac = Fraction(Characters(u'sin(x)'), Characters(u'cos(x)')) model.insert(len(model), mk_textmodel(frac)) model.insert(len(model), TextModel("\n\n ")) root = Root(Characters(u'2')) model.insert(len(model), mk_textmodel(root)) model.insert(len(model), TextModel("= 1.4142135623730951 ...\n")) model.insert(len(model), TextModel("\n\n ")) i = len(model) root = Root(Characters(u'')) for j in range(4): model.insert(i+j, mk_textmodel(root)) model.insert(i+4, TextModel("2")) model.insert(len(model), TextModel("= 1.04427378243 ...\n")) view = ns['view'] view.index = len(model) return ns
def demo_02(): ns = init_testing(True) model = ns['model'] model.remove(0, len(model)) tmp = TextModel(__doc__) cell = Cell([tmp.texel], []) model.insert(len(model), mk_textmodel(cell)) for code in examples.split('---'): code = code.strip() tmp = TextModel(code) cell = Cell([tmp.texel], []) model.insert(len(model), mk_textmodel(cell)) view = ns['view'] view.index = 1 ns['app'].MainLoop()
def test_01(): ns = init_testing(True) model = ns['model'] model.remove(0, len(model)) model.insert(0, TextModel(__doc__)) text = """Try to edit the following formulas: tan(x) = """ model.insert(len(model), TextModel(text)) frac = Fraction([Characters(u'sin(x)')], [Characters(u'cos(x)')]) model.insert(len(model), mk_textmodel(frac)) model.insert(len(model), TextModel("\n\n ")) root = Root([Characters(u'2')]) model.insert(len(model), mk_textmodel(root)) model.insert(len(model), TextModel("= 1.4142135623730951\n")) view = ns['view'] view.cursor = len(model) return ns
def print_temp(self, text): new = TextModel(text) new.set_properties(0, len(new), textcolor='blue', temp=True) i = self.index self.model.insert(i, new) j1, j2 = self.temp_range if j1 == j2: self.temp_range = i, i + len(new) else: self.temp_range = j1, i + len(new)
def test_12(): ns = init_testing(True) model = ns['model'] model.remove(0, len(model)) tmp = TextModel(u'for a in range(5):\n print a') cell = Cell(tmp.texel, NULL_TEXEL) model.insert(len(model), mk_textmodel(cell)) assert find_cell(model.texel, 1) == (0, cell) view = ns['view'] view.index = 1 return ns
def test_05(): device = TESTDEVICE model = TextModel("123\n\n567890 2 4 6 8 0") updater = Updater(model, maxw=0) def check(box): if not box.device is device: print box assert box.device is device if hasattr(box, 'iter'): for j1, j2, x1, y1, child in box.riter(0, 0, 0): check(child) check(updater.get_layout())
def init_testing(redirect=True): app = wx.App(redirect=redirect) model = TextModel(u"") frame = wx.Frame(None) win = wx.Panel(frame, -1) view = WXMathTextView(win, -1, style=wx.SUNKEN_BORDER) view.model = model box = wx.BoxSizer(wx.VERTICAL) box.Add(view, 1, wx.ALL|wx.GROW, 1) win.SetSizer(box) win.SetAutoLayout(True) frame.Show() return locals()
def test_10(): "Factory" ns = init_testing(False) cell = Cell([Characters(u'a')], [Characters(u'b')]) factory = Updater(TextModel('')) boxes = factory.create_boxes(cell) assert len(boxes) == 1 cellbox = boxes[0] assert len(cellbox) == 5 assert len(cell) == 5 check_box(cellbox) check_box(cellbox.input) check_box(cellbox.output) return ns
def test_10(): "Factory" ns = init_testing(False) cell = Cell(Text(u'a'), Text(u'b')) factory = Builder(TextModel('')) boxes = factory.create_all(cell) assert len(boxes) == 1 cellbox = boxes[0] assert len(cellbox) == 5 assert length(cell) == 5 check_box(cellbox) check_box(cellbox.input) check_box(cellbox.output) return ns
def demo_02(): ns = init_testing(True) model = ns['model'] model.remove(0, len(model)) tmp = TextModel('"""\n' + __doc__ + '"""') cell = Cell(tmp.texel, NULL_TEXEL) model.insert(len(model), mk_textmodel(cell)) for code in examples.split('---'): code = code.strip() tmp = TextModel(code) cell = Cell(tmp.texel, NULL_TEXEL) model.insert(len(model), mk_textmodel(cell)) view = ns['view'] view.index = 1 wx.CallAfter(view.Scroll, 0, 0) view.SetFocus() if 0: INTERPRETER.namespace.update(ns) from wxtextview import testing testing.pyshell(ns) ns['app'].MainLoop()
def test_11(): ns = init_testing(False) model = ns['model'] model.remove(0, len(model)) tmp = TextModel(u'for a in range(5):\n print a') cell = Cell([tmp.texel], [Characters(u'')]) model.insert(len(model), mk_textmodel(cell)) assert find_cell(model.texel, 1) == (0, cell) view = ns['view'] view.index = 1 #print model.texel view.execute() check_box(view.updater.layout, model.texel) return ns
def init_testing(redirect=True): app = wx.App(redirect=redirect) model = TextModel('') frame = wx.Frame(None) win = wx.Panel(frame) view = WXTextView(win) view.model = model box = wx.BoxSizer(wx.VERTICAL) box.Add(view, 1, wx.ALL | wx.GROW, 1) win.SetSizer(box) win.SetAutoLayout(True) INTERPRETER.namespace.update(dict(__notebook__=view, )) frame.Show() return locals()
def test_15(): "get_word, print_temp, remove_temp" ns = init_testing(False) model = ns['model'] tmp = TextModel(u'for a in range(5):\n print a') cell = Cell(tmp.texel, NULL_TEXEL) model.insert(len(model), mk_textmodel(cell)) view = ns['view'] assert view.get_word(12) == 'ra' text = model.get_text() view.index = 12 view.print_temp('\n[raise range raw_input]\n') #print model.get_text() view.clear_temp() assert model.get_text() == text
def test_14(): "cell cursor" ns = init_testing(False) model = ns['model'] model.remove(0, len(model)) tmp = TextModel(u'for a in range(5):\n print a') cell = Cell([tmp.texel], []) model.insert(len(model), mk_textmodel(cell)) assert find_cell(model.texel, 1) == (0, cell) view = ns['view'] view.index = 1 layout = view.updater.layout r1 = layout.get_rect(0, 0, 0) assert r1.x2 - r1.x1 == sepwidth r2 = layout.get_cursorrect(0, 0, 0, defaultstyle) r3 = layout.get_cursorrect(len(model), 0, 0, defaultstyle) assert r3.x2 - r3.x1 == sepwidth assert r3.y1 > r2.y1
def test_11(): ns = init_testing(True) #False) model = ns['model'] model.remove(0, len(model)) tmp = TextModel(u'for a in range(25):\n print a') cell = Cell(tmp.texel, Characters(u'')) model.insert(len(model), mk_textmodel(cell)) assert model.index2position(0) == (0, 0) assert model.index2position(1) == (1, 0) cell = model.texel assert find_cell(model.texel, 1) == (0, cell) view = ns['view'] view.index = 1 #print model.texel view.execute() check_box(view.updater._layout, model.texel) return ns
def test_13(): ns = init_testing(False) model = ns['model'] model.remove(0, len(model)) tmp = TextModel(u'''import matplotlib.pyplot as plt figure = plt.figure( facecolor='white', figsize=(3, 2.5)) figure.set_frameon(False) plot = figure.add_subplot ( 111 ) plt.plot([1,2,3,4]) plt.ylabel('some numbers') output(figure)''') cell = Cell([tmp.texel], []) model.insert(len(model), mk_textmodel(cell)) assert find_cell(model.texel, 1) == (0, cell) view = ns['view'] view.index = 1 view.execute() return ns
def mk_textmodel(texel): model = TextModel() model.texel = texel model.linelengths = texel.get_linelengths() return model