Best Python code snippet using hypothesis
simpleTests.py
Source:simpleTests.py
...268 # Add two lines separated with U+2028 and ensure it is seen as two lines269 # Then remove U+2028 and should be just 1 lines270 self.ed.Lexer = self.ed.SCLEX_CPP271 self.ed.SetCodePage(65001)272 self.ed.SetLineEndTypesAllowed(1)273 self.ed.AddText(5, b"x\xe2\x80\xa8y")274 self.assertEquals(self.ed.LineCount, 2)275 self.assertEquals(self.ed.GetLineEndPosition(0), 1)276 self.assertEquals(self.ed.GetLineEndPosition(1), 5)277 self.assertEquals(self.ed.LineLength(0), 4)278 self.assertEquals(self.ed.LineLength(1), 1)279 self.ed.TargetStart = 1280 self.ed.TargetEnd = 4281 self.ed.ReplaceTarget(0, b"")282 self.assertEquals(self.ed.LineCount, 1)283 self.assertEquals(self.ed.LineLength(0), 2)284 self.assertEquals(self.ed.GetLineEndPosition(0), 2)285 self.assertEquals(self.ed.LineEndTypesSupported, 1)286 def testUnicodeLineEndsWithCodePage0(self):287 # Try the Unicode line ends when not in Unicode mode -> should remain 1 line288 self.ed.SetCodePage(0)289 self.ed.AddText(5, b"x\xe2\x80\xa8y")290 self.assertEquals(self.ed.LineCount, 1)291 self.ed.AddText(4, b"x\xc2\x85y")292 self.assertEquals(self.ed.LineCount, 1)293 def testUnicodeLineEndsSwitchToUnicodeAndBack(self):294 # Add the Unicode line ends when not in Unicode mode295 self.ed.SetCodePage(0)296 self.ed.AddText(5, b"x\xe2\x80\xa8y")297 self.assertEquals(self.ed.LineCount, 1)298 # Into UTF-8 mode - should now be interpreting as two lines299 self.ed.Lexer = self.ed.SCLEX_CPP300 self.ed.SetCodePage(65001)301 self.ed.SetLineEndTypesAllowed(1)302 self.assertEquals(self.ed.LineCount, 2)303 # Back to code page 0 and 1 line304 self.ed.SetCodePage(0)305 self.assertEquals(self.ed.LineCount, 1)306 def testUFragmentedEOLCompletion(self):307 # Add 2 starting bytes of UTF-8 line end then complete it308 self.ed.ClearAll()309 self.ed.AddText(4, b"x\xe2\x80y")310 self.assertEquals(self.ed.LineCount, 1)311 self.assertEquals(self.ed.GetLineEndPosition(0), 4)312 self.ed.SetSel(3,3)313 self.ed.AddText(1, b"\xa8")314 self.assertEquals(self.ed.Contents(), b"x\xe2\x80\xa8y")315 self.assertEquals(self.ed.LineCount, 2)316 # Add 1 starting bytes of UTF-8 line end then complete it317 self.ed.ClearAll()318 self.ed.AddText(3, b"x\xe2y")319 self.assertEquals(self.ed.LineCount, 1)320 self.assertEquals(self.ed.GetLineEndPosition(0), 3)321 self.ed.SetSel(2,2)322 self.ed.AddText(2, b"\x80\xa8")323 self.assertEquals(self.ed.Contents(), b"x\xe2\x80\xa8y")324 self.assertEquals(self.ed.LineCount, 2)325 def testUFragmentedEOLStart(self):326 # Add end of UTF-8 line end then insert start327 self.ed.Lexer = self.ed.SCLEX_CPP328 self.ed.SetCodePage(65001)329 self.ed.SetLineEndTypesAllowed(1)330 self.assertEquals(self.ed.LineCount, 1)331 self.ed.AddText(4, b"x\x80\xa8y")332 self.assertEquals(self.ed.LineCount, 1)333 self.ed.SetSel(1,1)334 self.ed.AddText(1, b"\xe2")335 self.assertEquals(self.ed.LineCount, 2)336 def testUBreakApartEOL(self):337 # Add two lines separated by U+2029 then remove and add back each byte ensuring338 # only one line after each removal of any byte in line end and 2 lines after reinsertion339 self.ed.Lexer = self.ed.SCLEX_CPP340 self.ed.SetCodePage(65001)341 self.ed.SetLineEndTypesAllowed(1)342 text = b"x\xe2\x80\xa9y";343 self.ed.AddText(5, text)344 self.assertEquals(self.ed.LineCount, 2)345 for i in range(len(text)):346 self.ed.TargetStart = i347 self.ed.TargetEnd = i + 1348 self.ed.ReplaceTarget(0, b"")349 if i in [0, 4]:350 # Removing text characters does not change number of lines351 self.assertEquals(self.ed.LineCount, 2)352 else:353 # Removing byte from line end, removes 1 line354 self.assertEquals(self.ed.LineCount, 1)355 self.ed.TargetEnd = i356 self.ed.ReplaceTarget(1, text[i:i+1])357 self.assertEquals(self.ed.LineCount, 2)358 def testURemoveEOLFragment(self):359 # Add UTF-8 line end then delete each byte causing line end to disappear360 self.ed.Lexer = self.ed.SCLEX_CPP361 self.ed.SetCodePage(65001)362 self.ed.SetLineEndTypesAllowed(1)363 for i in range(3):364 self.ed.ClearAll()365 self.ed.AddText(5, b"x\xe2\x80\xa8y")366 self.assertEquals(self.ed.LineCount, 2)367 self.ed.TargetStart = i+1368 self.ed.TargetEnd = i+2369 self.ed.ReplaceTarget(0, b"")370 self.assertEquals(self.ed.LineCount, 1)371 # Several tests for unicode NEL line ends U+0085372 def testNELLineEnds(self):373 # Add two lines separated with U+0085 and ensure it is seen as two lines374 # Then remove U+0085 and should be just 1 lines375 self.ed.Lexer = self.ed.SCLEX_CPP376 self.ed.SetCodePage(65001)377 self.ed.SetLineEndTypesAllowed(1)378 self.ed.AddText(4, b"x\xc2\x85y")379 self.assertEquals(self.ed.LineCount, 2)380 self.assertEquals(self.ed.GetLineEndPosition(0), 1)381 self.assertEquals(self.ed.GetLineEndPosition(1), 4)382 self.assertEquals(self.ed.LineLength(0), 3)383 self.assertEquals(self.ed.LineLength(1), 1)384 self.ed.TargetStart = 1385 self.ed.TargetEnd = 3386 self.ed.ReplaceTarget(0, b"")387 self.assertEquals(self.ed.LineCount, 1)388 self.assertEquals(self.ed.LineLength(0), 2)389 self.assertEquals(self.ed.GetLineEndPosition(0), 2)390 def testNELFragmentedEOLCompletion(self):391 # Add starting byte of UTF-8 NEL then complete it392 self.ed.AddText(3, b"x\xc2y")393 self.assertEquals(self.ed.LineCount, 1)394 self.assertEquals(self.ed.GetLineEndPosition(0), 3)395 self.ed.SetSel(2,2)396 self.ed.AddText(1, b"\x85")397 self.assertEquals(self.ed.Contents(), b"x\xc2\x85y")398 self.assertEquals(self.ed.LineCount, 2)399 def testNELFragmentedEOLStart(self):400 # Add end of UTF-8 NEL then insert start401 self.ed.Lexer = self.ed.SCLEX_CPP402 self.ed.SetCodePage(65001)403 self.ed.SetLineEndTypesAllowed(1)404 self.assertEquals(self.ed.LineCount, 1)405 self.ed.AddText(4, b"x\x85y")406 self.assertEquals(self.ed.LineCount, 1)407 self.ed.SetSel(1,1)408 self.ed.AddText(1, b"\xc2")409 self.assertEquals(self.ed.LineCount, 2)410 def testNELBreakApartEOL(self):411 # Add two lines separated by U+0085 then remove and add back each byte ensuring412 # only one line after each removal of any byte in line end and 2 lines after reinsertion413 self.ed.Lexer = self.ed.SCLEX_CPP414 self.ed.SetCodePage(65001)415 self.ed.SetLineEndTypesAllowed(1)416 text = b"x\xc2\x85y";417 self.ed.AddText(4, text)418 self.assertEquals(self.ed.LineCount, 2)419 for i in range(len(text)):420 self.ed.TargetStart = i421 self.ed.TargetEnd = i + 1422 self.ed.ReplaceTarget(0, b"")423 if i in [0, 3]:424 # Removing text characters does not change number of lines425 self.assertEquals(self.ed.LineCount, 2)426 else:427 # Removing byte from line end, removes 1 line428 self.assertEquals(self.ed.LineCount, 1)429 self.ed.TargetEnd = i430 self.ed.ReplaceTarget(1, text[i:i+1])431 self.assertEquals(self.ed.LineCount, 2)432 def testNELRemoveEOLFragment(self):433 # Add UTF-8 NEL then delete each byte causing line end to disappear434 self.ed.SetCodePage(65001)435 for i in range(2):436 self.ed.ClearAll()437 self.ed.AddText(4, b"x\xc2\x85y")438 self.assertEquals(self.ed.LineCount, 2)439 self.ed.TargetStart = i+1440 self.ed.TargetEnd = i+2441 self.ed.ReplaceTarget(0, b"")442 self.assertEquals(self.ed.LineCount, 1)443 def testGoto(self):444 self.ed.AddText(5, b"a\nb\nc")445 self.assertEquals(self.ed.CurrentPos, 5)446 self.ed.GotoLine(1)447 self.assertEquals(self.ed.CurrentPos, 2)448 self.ed.GotoPos(4)449 self.assertEquals(self.ed.CurrentPos, 4)450 def testCutCopyPaste(self):451 self.ed.AddText(5, b"a1b2c")452 self.ed.SetSel(1,3)453 self.ed.Cut()454 # Clipboard = "1b"455 self.assertEquals(self.ed.Contents(), b"a2c")456 self.assertEquals(self.ed.CanPaste(), 1)457 self.ed.SetSel(0, 0)458 self.ed.Paste()459 self.assertEquals(self.ed.Contents(), b"1ba2c")460 self.ed.SetSel(4,5)461 self.ed.Copy()462 # Clipboard = "c"463 self.ed.SetSel(1,3)464 self.ed.Paste()465 self.assertEquals(self.ed.Contents(), b"1c2c")466 self.ed.SetSel(2,4)467 self.ed.Clear()468 self.assertEquals(self.ed.Contents(), b"1c")469 def testCopyAllowLine(self):470 lineEndType = self.ed.EOLMode471 self.ed.EOLMode = self.ed.SC_EOL_LF472 self.ed.AddText(5, b"a1\nb2")473 self.ed.SetSel(1,1)474 self.ed.CopyAllowLine()475 # Clipboard = "a1\n"476 self.assertEquals(self.ed.CanPaste(), 1)477 self.ed.SetSel(0, 0)478 self.ed.Paste()479 self.ed.EOLMode = lineEndType480 self.assertEquals(self.ed.Contents(), b"a1\na1\nb2")481 def testDuplicate(self):482 self.ed.AddText(3, b"1b2")483 self.ed.SetSel(1,2)484 self.ed.SelectionDuplicate()485 self.assertEquals(self.ed.Contents(), b"1bb2")486 def testTransposeLines(self):487 self.ed.AddText(8, b"a1\nb2\nc3")488 self.ed.SetSel(3,3)489 self.ed.LineTranspose()490 self.assertEquals(self.ed.Contents(), b"b2\na1\nc3")491 def testGetSet(self):492 self.ed.SetContents(b"abc")493 self.assertEquals(self.ed.TextLength, 3)494 # String buffer containing exactly 5 digits495 result = ctypes.create_string_buffer(b"12345", 5)496 self.assertEquals(result.raw, b"12345")497 length = self.ed.GetText(4, result)498 self.assertEquals(length, 3)499 self.assertEquals(result.value, b"abc")500 # GetText has written the 3 bytes of text and a terminating NUL but left the final digit 5501 self.assertEquals(result.raw, b"abc\x005")502 def testAppend(self):503 self.ed.SetContents(b"abc")504 self.assertEquals(self.ed.SelectionStart, 0)505 self.assertEquals(self.ed.SelectionEnd, 0)506 text = b"12"507 self.ed.AppendText(len(text), text)508 self.assertEquals(self.ed.SelectionStart, 0)509 self.assertEquals(self.ed.SelectionEnd, 0)510 self.assertEquals(self.ed.Contents(), b"abc12")511 def testTarget(self):512 self.ed.SetContents(b"abcd")513 self.ed.TargetStart = 1514 self.ed.TargetEnd = 3515 self.assertEquals(self.ed.TargetStart, 1)516 self.assertEquals(self.ed.TargetEnd, 3)517 rep = b"321"518 self.ed.ReplaceTarget(len(rep), rep)519 self.assertEquals(self.ed.Contents(), b"a321d")520 self.ed.SearchFlags = self.ed.SCFIND_REGEXP521 self.assertEquals(self.ed.SearchFlags, self.ed.SCFIND_REGEXP)522 searchString = b"\([1-9]+\)"523 pos = self.ed.SearchInTarget(len(searchString), searchString)524 self.assertEquals(1, pos)525 tagString = self.ed.GetTag(1)526 self.assertEquals(tagString, b"321")527 rep = b"\\1"528 self.ed.TargetStart = 0529 self.ed.TargetEnd = 0530 self.ed.ReplaceTargetRE(len(rep), rep)531 self.assertEquals(self.ed.Contents(), b"321a321d")532 self.ed.SetSel(4,5)533 self.ed.TargetFromSelection()534 self.assertEquals(self.ed.TargetStart, 4)535 self.assertEquals(self.ed.TargetEnd, 5)536 def testTargetWhole(self):537 self.ed.SetContents(b"abcd")538 self.ed.TargetStart = 1539 self.ed.TargetEnd = 3540 self.ed.TargetWholeDocument()541 self.assertEquals(self.ed.TargetStart, 0)542 self.assertEquals(self.ed.TargetEnd, 4)543 def testTargetEscape(self):544 # Checks that a literal \ can be in the replacement. Bug #2959876545 self.ed.SetContents(b"abcd")546 self.ed.TargetStart = 1547 self.ed.TargetEnd = 3548 rep = b"\\\\n"549 self.ed.ReplaceTargetRE(len(rep), rep)550 self.assertEquals(self.ed.Contents(), b"a\\nd")551 def testTargetVirtualSpace(self):552 self.ed.SetContents(b"a\nbcd")553 self.assertEquals(self.ed.TargetStart, 0)554 self.assertEquals(self.ed.TargetStartVirtualSpace, 0)555 self.assertEquals(self.ed.TargetEnd, 5)556 self.assertEquals(self.ed.TargetEndVirtualSpace, 0)557 self.ed.TargetStart = 1558 self.ed.TargetStartVirtualSpace = 2559 self.ed.TargetEnd = 3560 self.ed.TargetEndVirtualSpace = 4561 # Adds 2 spaces to first line due to virtual space, and replace 2 characters with 3562 rep = b"12\n"563 self.ed.ReplaceTarget(len(rep), rep)564 self.assertEquals(self.ed.Contents(), b"a 12\ncd")565 # 1+2v realized to 3566 self.assertEquals(self.ed.TargetStart, 3)567 self.assertEquals(self.ed.TargetStartVirtualSpace, 0)568 self.assertEquals(self.ed.TargetEnd, 6)569 self.assertEquals(self.ed.TargetEndVirtualSpace, 0)570 def testPointsAndPositions(self):571 self.ed.AddText(1, b"x")572 # Start of text573 self.assertEquals(self.ed.PositionFromPoint(0,0), 0)574 # End of text575 self.assertEquals(self.ed.PositionFromPoint(0,100), 1)576 def testLinePositions(self):577 text = b"ab\ncd\nef"578 nl = b"\n"579 if sys.version_info[0] == 3:580 nl = ord(b"\n")581 self.ed.AddText(len(text), text)582 self.assertEquals(self.ed.LineFromPosition(-1), 0)583 line = 0584 for pos in range(len(text)+1):585 self.assertEquals(self.ed.LineFromPosition(pos), line)586 if pos < len(text) and text[pos] == nl:587 line += 1588 def testWordPositions(self):589 text = b"ab cd\tef"590 self.ed.AddText(len(text), text)591 self.assertEquals(self.ed.WordStartPosition(3, 0), 2)592 self.assertEquals(self.ed.WordStartPosition(4, 0), 3)593 self.assertEquals(self.ed.WordStartPosition(5, 0), 3)594 self.assertEquals(self.ed.WordStartPosition(6, 0), 5)595 self.assertEquals(self.ed.WordEndPosition(2, 0), 3)596 self.assertEquals(self.ed.WordEndPosition(3, 0), 5)597 self.assertEquals(self.ed.WordEndPosition(4, 0), 5)598 self.assertEquals(self.ed.WordEndPosition(5, 0), 6)599 self.assertEquals(self.ed.WordEndPosition(6, 0), 8)600 def testWordRange(self):601 text = b"ab cd\t++"602 self.ed.AddText(len(text), text)603 self.assertEquals(self.ed.IsRangeWord(0, 0), 0)604 self.assertEquals(self.ed.IsRangeWord(0, 1), 0)605 self.assertEquals(self.ed.IsRangeWord(0, 2), 1)606 self.assertEquals(self.ed.IsRangeWord(0, 3), 0)607 self.assertEquals(self.ed.IsRangeWord(0, 4), 0)608 self.assertEquals(self.ed.IsRangeWord(0, 5), 1)609 self.assertEquals(self.ed.IsRangeWord(6, 7), 0)610 self.assertEquals(self.ed.IsRangeWord(6, 8), 1)611MODI = 1612UNDO = 2613REDO = 4614class TestContainerUndo(unittest.TestCase):615 def setUp(self):616 self.xite = Xite.xiteFrame617 self.ed = self.xite.ed618 self.ed.ClearAll()619 self.ed.EmptyUndoBuffer()620 self.data = b"xy"621 def UndoState(self):622 return (MODI if self.ed.Modify else 0) | \623 (UNDO if self.ed.CanUndo() else 0) | \624 (REDO if self.ed.CanRedo() else 0)625 def testContainerActNoCoalesce(self):626 self.ed.InsertText(0, self.data)627 self.assertEquals(self.ed.Length, 2)628 self.assertEquals(self.UndoState(), MODI | UNDO)629 self.ed.AddUndoAction(5, 0)630 self.ed.Undo()631 self.assertEquals(self.ed.Length, 2)632 self.assertEquals(self.UndoState(), MODI | UNDO | REDO)633 self.ed.Redo()634 self.assertEquals(self.ed.Length, 2)635 self.assertEquals(self.UndoState(), MODI | UNDO)636 self.ed.Undo()637 def testContainerActCoalesce(self):638 self.ed.InsertText(0, self.data)639 self.ed.AddUndoAction(5, 1)640 self.ed.Undo()641 self.assertEquals(self.ed.Length, 0)642 self.assertEquals(self.UndoState(), REDO)643 self.ed.Redo()644 self.assertEquals(self.ed.Length, 2)645 self.assertEquals(self.UndoState(), MODI | UNDO)646 def testContainerMultiStage(self):647 self.ed.InsertText(0, self.data)648 self.ed.AddUndoAction(5, 1)649 self.ed.AddUndoAction(5, 1)650 self.assertEquals(self.ed.Length, 2)651 self.assertEquals(self.UndoState(), MODI | UNDO)652 self.ed.Undo()653 self.assertEquals(self.ed.Length, 0)654 self.assertEquals(self.UndoState(), REDO)655 self.ed.Redo()656 self.assertEquals(self.ed.Length, 2)657 self.assertEquals(self.UndoState(), MODI | UNDO)658 self.ed.AddUndoAction(5, 1)659 self.assertEquals(self.ed.Length, 2)660 self.assertEquals(self.UndoState(), MODI | UNDO)661 self.ed.Undo()662 self.assertEquals(self.ed.Length, 0)663 self.assertEquals(self.UndoState(), REDO)664 def testContainerMultiStageNoText(self):665 self.ed.AddUndoAction(5, 1)666 self.ed.AddUndoAction(5, 1)667 self.assertEquals(self.UndoState(), MODI | UNDO)668 self.ed.Undo()669 self.assertEquals(self.UndoState(), REDO)670 self.ed.Redo()671 self.assertEquals(self.UndoState(), MODI | UNDO)672 self.ed.AddUndoAction(5, 1)673 self.assertEquals(self.UndoState(), MODI | UNDO)674 self.ed.Undo()675 self.assertEquals(self.UndoState(), REDO)676 def testContainerActCoalesceEnd(self):677 self.ed.AddUndoAction(5, 1)678 self.assertEquals(self.ed.Length, 0)679 self.assertEquals(self.UndoState(), MODI | UNDO)680 self.ed.InsertText(0, self.data)681 self.assertEquals(self.ed.Length, 2)682 self.assertEquals(self.UndoState(), MODI | UNDO)683 self.ed.Undo()684 self.assertEquals(self.ed.Length, 0)685 self.assertEquals(self.UndoState(), REDO)686 self.ed.Redo()687 self.assertEquals(self.ed.Length, 2)688 self.assertEquals(self.UndoState(), MODI | UNDO)689 def testContainerBetweenInsertAndInsert(self):690 self.assertEquals(self.ed.Length, 0)691 self.ed.InsertText(0, self.data)692 self.assertEquals(self.ed.Length, 2)693 self.assertEquals(self.UndoState(), MODI | UNDO)694 self.ed.AddUndoAction(5, 1)695 self.assertEquals(self.ed.Length, 2)696 self.assertEquals(self.UndoState(), MODI | UNDO)697 self.ed.InsertText(2, self.data)698 self.assertEquals(self.ed.Length, 4)699 self.assertEquals(self.UndoState(), MODI | UNDO)700 # Undoes both insertions and the containerAction in the middle701 self.ed.Undo()702 self.assertEquals(self.ed.Length, 0)703 self.assertEquals(self.UndoState(), REDO)704 def testContainerNoCoalesceBetweenInsertAndInsert(self):705 self.assertEquals(self.ed.Length, 0)706 self.ed.InsertText(0, self.data)707 self.assertEquals(self.ed.Length, 2)708 self.assertEquals(self.UndoState(), MODI | UNDO)709 self.ed.AddUndoAction(5, 0)710 self.assertEquals(self.ed.Length, 2)711 self.assertEquals(self.UndoState(), MODI | UNDO)712 self.ed.InsertText(2, self.data)713 self.assertEquals(self.ed.Length, 4)714 self.assertEquals(self.UndoState(), MODI | UNDO)715 # Undo last insertion716 self.ed.Undo()717 self.assertEquals(self.ed.Length, 2)718 self.assertEquals(self.UndoState(), MODI | UNDO | REDO)719 # Undo container720 self.ed.Undo()721 self.assertEquals(self.ed.Length, 2)722 self.assertEquals(self.UndoState(), MODI | UNDO | REDO)723 # Undo first insertion724 self.ed.Undo()725 self.assertEquals(self.ed.Length, 0)726 self.assertEquals(self.UndoState(), REDO)727 def testContainerBetweenDeleteAndDelete(self):728 self.ed.InsertText(0, self.data)729 self.ed.EmptyUndoBuffer()730 self.assertEquals(self.ed.Length, 2)731 self.assertEquals(self.UndoState(), 0)732 self.ed.SetSel(2,2)733 self.ed.DeleteBack()734 self.assertEquals(self.ed.Length, 1)735 self.ed.AddUndoAction(5, 1)736 self.ed.DeleteBack()737 self.assertEquals(self.ed.Length, 0)738 # Undoes both deletions and the containerAction in the middle739 self.ed.Undo()740 self.assertEquals(self.ed.Length, 2)741 self.assertEquals(self.UndoState(), REDO)742 def testContainerBetweenInsertAndDelete(self):743 self.assertEquals(self.ed.Length, 0)744 self.ed.InsertText(0, self.data)745 self.assertEquals(self.ed.Length, 2)746 self.assertEquals(self.UndoState(), MODI | UNDO)747 self.ed.AddUndoAction(5, 1)748 self.assertEquals(self.UndoState(), MODI | UNDO)749 self.ed.SetSel(0,1)750 self.ed.Cut()751 self.assertEquals(self.ed.Length, 1)752 self.assertEquals(self.UndoState(), MODI | UNDO)753 self.ed.Undo() # Only undoes the deletion754 self.assertEquals(self.ed.Length, 2)755 self.assertEquals(self.UndoState(), MODI | UNDO | REDO)756class TestKeyCommands(unittest.TestCase):757 """ These commands are normally assigned to keys and take no arguments """758 def setUp(self):759 self.xite = Xite.xiteFrame760 self.ed = self.xite.ed761 self.ed.ClearAll()762 self.ed.EmptyUndoBuffer()763 def selRange(self):764 return self.ed.CurrentPos, self.ed.Anchor765 def testLineMove(self):766 self.ed.AddText(8, b"x1\ny2\nz3")767 self.ed.SetSel(0,0)768 self.ed.ChooseCaretX()769 self.ed.LineDown()770 self.ed.LineDown()771 self.assertEquals(self.selRange(), (6, 6))772 self.ed.LineUp()773 self.assertEquals(self.selRange(), (3, 3))774 self.ed.LineDownExtend()775 self.assertEquals(self.selRange(), (6, 3))776 self.ed.LineUpExtend()777 self.ed.LineUpExtend()778 self.assertEquals(self.selRange(), (0, 3))779 def testCharMove(self):780 self.ed.AddText(8, b"x1\ny2\nz3")781 self.ed.SetSel(0,0)782 self.ed.CharRight()783 self.ed.CharRight()784 self.assertEquals(self.selRange(), (2, 2))785 self.ed.CharLeft()786 self.assertEquals(self.selRange(), (1, 1))787 self.ed.CharRightExtend()788 self.assertEquals(self.selRange(), (2, 1))789 self.ed.CharLeftExtend()790 self.ed.CharLeftExtend()791 self.assertEquals(self.selRange(), (0, 1))792 def testWordMove(self):793 self.ed.AddText(10, b"a big boat")794 self.ed.SetSel(3,3)795 self.ed.WordRight()796 self.ed.WordRight()797 self.assertEquals(self.selRange(), (10, 10))798 self.ed.WordLeft()799 self.assertEquals(self.selRange(), (6, 6))800 self.ed.WordRightExtend()801 self.assertEquals(self.selRange(), (10, 6))802 self.ed.WordLeftExtend()803 self.ed.WordLeftExtend()804 self.assertEquals(self.selRange(), (2, 6))805 def testHomeEndMove(self):806 self.ed.AddText(10, b"a big boat")807 self.ed.SetSel(3,3)808 self.ed.Home()809 self.assertEquals(self.selRange(), (0, 0))810 self.ed.LineEnd()811 self.assertEquals(self.selRange(), (10, 10))812 self.ed.SetSel(3,3)813 self.ed.HomeExtend()814 self.assertEquals(self.selRange(), (0, 3))815 self.ed.LineEndExtend()816 self.assertEquals(self.selRange(), (10, 3))817 def testStartEndMove(self):818 self.ed.AddText(10, b"a\nbig\nboat")819 self.ed.SetSel(3,3)820 self.ed.DocumentStart()821 self.assertEquals(self.selRange(), (0, 0))822 self.ed.DocumentEnd()823 self.assertEquals(self.selRange(), (10, 10))824 self.ed.SetSel(3,3)825 self.ed.DocumentStartExtend()826 self.assertEquals(self.selRange(), (0, 3))827 self.ed.DocumentEndExtend()828 self.assertEquals(self.selRange(), (10, 3))829class TestMarkers(unittest.TestCase):830 def setUp(self):831 self.xite = Xite.xiteFrame832 self.ed = self.xite.ed833 self.ed.ClearAll()834 self.ed.EmptyUndoBuffer()835 self.ed.AddText(5, b"x\ny\nz")836 def testMarker(self):837 handle = self.ed.MarkerAdd(1,1)838 self.assertEquals(self.ed.MarkerLineFromHandle(handle), 1)839 self.ed.MarkerDelete(1,1)840 self.assertEquals(self.ed.MarkerLineFromHandle(handle), -1)841 def testTwiceAddedDelete(self):842 handle = self.ed.MarkerAdd(1,1)843 self.assertEquals(self.ed.MarkerGet(1), 2)844 handle2 = self.ed.MarkerAdd(1,1)845 self.assertEquals(self.ed.MarkerGet(1), 2)846 self.ed.MarkerDelete(1,1)847 self.assertEquals(self.ed.MarkerGet(1), 2)848 self.ed.MarkerDelete(1,1)849 self.assertEquals(self.ed.MarkerGet(1), 0)850 def testMarkerDeleteAll(self):851 h1 = self.ed.MarkerAdd(0,1)852 h2 = self.ed.MarkerAdd(1,2)853 self.assertEquals(self.ed.MarkerLineFromHandle(h1), 0)854 self.assertEquals(self.ed.MarkerLineFromHandle(h2), 1)855 self.ed.MarkerDeleteAll(1)856 self.assertEquals(self.ed.MarkerLineFromHandle(h1), -1)857 self.assertEquals(self.ed.MarkerLineFromHandle(h2), 1)858 self.ed.MarkerDeleteAll(-1)859 self.assertEquals(self.ed.MarkerLineFromHandle(h1), -1)860 self.assertEquals(self.ed.MarkerLineFromHandle(h2), -1)861 def testMarkerDeleteHandle(self):862 handle = self.ed.MarkerAdd(0,1)863 self.assertEquals(self.ed.MarkerLineFromHandle(handle), 0)864 self.ed.MarkerDeleteHandle(handle)865 self.assertEquals(self.ed.MarkerLineFromHandle(handle), -1)866 def testMarkerBits(self):867 self.assertEquals(self.ed.MarkerGet(0), 0)868 self.ed.MarkerAdd(0,1)869 self.assertEquals(self.ed.MarkerGet(0), 2)870 self.ed.MarkerAdd(0,2)871 self.assertEquals(self.ed.MarkerGet(0), 6)872 def testMarkerAddSet(self):873 self.assertEquals(self.ed.MarkerGet(0), 0)874 self.ed.MarkerAddSet(0,5)875 self.assertEquals(self.ed.MarkerGet(0), 5)876 self.ed.MarkerDeleteAll(-1)877 def testMarkerNext(self):878 self.assertEquals(self.ed.MarkerNext(0, 2), -1)879 h1 = self.ed.MarkerAdd(0,1)880 h2 = self.ed.MarkerAdd(2,1)881 self.assertEquals(self.ed.MarkerNext(0, 2), 0)882 self.assertEquals(self.ed.MarkerNext(1, 2), 2)883 self.assertEquals(self.ed.MarkerNext(2, 2), 2)884 self.assertEquals(self.ed.MarkerPrevious(0, 2), 0)885 self.assertEquals(self.ed.MarkerPrevious(1, 2), 0)886 self.assertEquals(self.ed.MarkerPrevious(2, 2), 2)887 def testMarkerNegative(self):888 self.assertEquals(self.ed.MarkerNext(-1, 2), -1)889 def testLineState(self):890 self.assertEquals(self.ed.MaxLineState, 0)891 self.assertEquals(self.ed.GetLineState(0), 0)892 self.assertEquals(self.ed.GetLineState(1), 0)893 self.assertEquals(self.ed.GetLineState(2), 0)894 self.ed.SetLineState(1, 100)895 self.assertNotEquals(self.ed.MaxLineState, 0)896 self.assertEquals(self.ed.GetLineState(0), 0)897 self.assertEquals(self.ed.GetLineState(1), 100)898 self.assertEquals(self.ed.GetLineState(2), 0)899 def testSymbolRetrieval(self):900 self.ed.MarkerDefine(1,3)901 self.assertEquals(self.ed.MarkerSymbolDefined(1), 3)902 def markerFromLineIndex(self, line, index):903 """ Helper that returns (handle, number) """904 return (self.ed.MarkerHandleFromLine(line, index), self.ed.MarkerNumberFromLine(line, index))905 def markerSetFromLine(self, line):906 """ Helper that returns set of (handle, number) """907 markerSet = set()908 index = 0909 while 1:910 marker = self.markerFromLineIndex(line, index)911 if marker[0] == -1:912 return markerSet913 markerSet.add(marker)914 index += 1915 def testMarkerFromLine(self):916 self.assertEquals(self.ed.MarkerHandleFromLine(1, 0), -1)917 self.assertEquals(self.ed.MarkerNumberFromLine(1, 0), -1)918 self.assertEquals(self.markerFromLineIndex(1, 0), (-1, -1))919 self.assertEquals(self.markerSetFromLine(1), set())920 handle = self.ed.MarkerAdd(1,10)921 self.assertEquals(self.markerFromLineIndex(1, 0), (handle, 10))922 self.assertEquals(self.markerFromLineIndex(1, 1), (-1, -1))923 self.assertEquals(self.markerSetFromLine(1), {(handle, 10)})924 handle2 = self.ed.MarkerAdd(1,11)925 # Don't want to depend on ordering so check as sets926 self.assertEquals(self.markerSetFromLine(1), {(handle, 10), (handle2, 11)})927 self.ed.MarkerDeleteHandle(handle2)928 self.assertEquals(self.markerSetFromLine(1), {(handle, 10)})929class TestIndicators(unittest.TestCase):930 def setUp(self):931 self.xite = Xite.xiteFrame932 self.ed = self.xite.ed933 self.ed.ClearAll()934 self.ed.EmptyUndoBuffer()935 def indicatorValueString(self, indicator):936 # create a string with -/X where indicator off/on to make checks simple937 return "".join("-X"[self.ed.IndicatorValueAt(indicator, index)] for index in range(self.ed.TextLength))938 def testSetIndicator(self):939 self.assertEquals(self.ed.IndicGetStyle(0), 1)940 self.assertEquals(self.ed.IndicGetFore(0), 0x007f00)941 self.ed.IndicSetStyle(0, 2)942 self.ed.IndicSetFore(0, 0xff0080)943 self.assertEquals(self.ed.IndicGetStyle(0), 2)944 self.assertEquals(self.ed.IndicGetFore(0), 0xff0080)945 def testIndicatorFill(self):946 self.ed.InsertText(0, b"abc")947 self.ed.IndicatorCurrent = 3948 self.ed.IndicatorFillRange(1,1)949 self.assertEquals(self.indicatorValueString(3), "-X-")950 self.assertEquals(self.ed.IndicatorStart(3, 0), 0)951 self.assertEquals(self.ed.IndicatorEnd(3, 0), 1)952 self.assertEquals(self.ed.IndicatorStart(3, 1), 1)953 self.assertEquals(self.ed.IndicatorEnd(3, 1), 2)954 self.assertEquals(self.ed.IndicatorStart(3, 2), 2)955 self.assertEquals(self.ed.IndicatorEnd(3, 2), 3)956 def testIndicatorAtEnd(self):957 self.ed.InsertText(0, b"ab")958 self.ed.IndicatorCurrent = 3959 self.ed.IndicatorFillRange(1,1)960 self.assertEquals(self.ed.IndicatorValueAt(3, 0), 0)961 self.assertEquals(self.ed.IndicatorValueAt(3, 1), 1)962 self.assertEquals(self.ed.IndicatorStart(3, 0), 0)963 self.assertEquals(self.ed.IndicatorEnd(3, 0), 1)964 self.assertEquals(self.ed.IndicatorStart(3, 1), 1)965 self.assertEquals(self.ed.IndicatorEnd(3, 1), 2)966 self.ed.DeleteRange(1, 1)967 # Now only one character left and does not have indicator so indicator 3 is null968 self.assertEquals(self.ed.IndicatorValueAt(3, 0), 0)969 # Since null, remaining calls return 0970 self.assertEquals(self.ed.IndicatorStart(3, 0), 0)971 self.assertEquals(self.ed.IndicatorEnd(3, 0), 0)972 self.assertEquals(self.ed.IndicatorStart(3, 1), 0)973 self.assertEquals(self.ed.IndicatorEnd(3, 1), 0)974 def testIndicatorMovement(self):975 # Create a two character indicator over "BC" in "aBCd" and ensure that it doesn't976 # leak onto surrounding characters when insertions made at either end but977 # insertion inside indicator does extend length978 self.ed.InsertText(0, b"aBCd")979 self.ed.IndicatorCurrent = 3980 self.ed.IndicatorFillRange(1,2)981 self.assertEquals(self.indicatorValueString(3), "-XX-")982 # Insertion 1 before983 self.ed.InsertText(0, b"1")984 self.assertEquals(b"1aBCd", self.ed.Contents())985 self.assertEquals(self.indicatorValueString(3), "--XX-")986 # Insertion at start of indicator987 self.ed.InsertText(2, b"2")988 self.assertEquals(b"1a2BCd", self.ed.Contents())989 self.assertEquals(self.indicatorValueString(3), "---XX-")990 # Insertion inside indicator991 self.ed.InsertText(4, b"3")992 self.assertEquals(b"1a2B3Cd", self.ed.Contents())993 self.assertEquals(self.indicatorValueString(3), "---XXX-")994 # Insertion at end of indicator995 self.ed.InsertText(6, b"4")996 self.assertEquals(b"1a2B3C4d", self.ed.Contents())997 self.assertEquals(self.indicatorValueString(3), "---XXX--")998 # Insertion 1 after999 self.ed.InsertText(8, b"5")1000 self.assertEquals(self.indicatorValueString(3), "---XXX---")1001 self.assertEquals(b"1a2B3C4d5", self.ed.Contents())1002class TestScrolling(unittest.TestCase):1003 def setUp(self):1004 self.xite = Xite.xiteFrame1005 self.ed = self.xite.ed1006 self.ed.ClearAll()1007 self.ed.EmptyUndoBuffer()1008 # 150 should be enough lines1009 self.ed.InsertText(0, b"a" * 150 + b"\n" * 150)1010 def testTop(self):1011 self.ed.GotoLine(0)1012 self.assertEquals(self.ed.FirstVisibleLine, 0)1013 def testLineScroll(self):1014 self.ed.GotoLine(0)1015 self.ed.LineScroll(0, 3)1016 self.assertEquals(self.ed.FirstVisibleLine, 3)1017 self.ed.LineScroll(0, -2)1018 self.assertEquals(self.ed.FirstVisibleLine, 1)1019 self.assertEquals(self.ed.XOffset, 0)1020 self.ed.LineScroll(10, 0)1021 self.assertGreater(self.ed.XOffset, 0)1022 scroll_width = float(self.ed.XOffset) / 101023 self.ed.LineScroll(-2, 0)1024 self.assertEquals(self.ed.XOffset, scroll_width * 8)1025 def testVisibleLine(self):1026 self.ed.FirstVisibleLine = 71027 self.assertEquals(self.ed.FirstVisibleLine, 7)1028class TestSearch(unittest.TestCase):1029 def setUp(self):1030 self.xite = Xite.xiteFrame1031 self.ed = self.xite.ed1032 self.ed.ClearAll()1033 self.ed.EmptyUndoBuffer()1034 self.ed.InsertText(0, b"a\tbig boat\t")1035 def testFind(self):1036 pos = self.ed.FindBytes(0, self.ed.Length, b"zzz", 0)1037 self.assertEquals(pos, -1)1038 pos = self.ed.FindBytes(0, self.ed.Length, b"big", 0)1039 self.assertEquals(pos, 2)1040 def testFindEmpty(self):1041 pos = self.ed.FindBytes(0, self.ed.Length, b"", 0)1042 self.assertEquals(pos, 0)1043 def testCaseFind(self):1044 self.assertEquals(self.ed.FindBytes(0, self.ed.Length, b"big", 0), 2)1045 self.assertEquals(self.ed.FindBytes(0, self.ed.Length, b"bIg", 0), 2)1046 self.assertEquals(self.ed.FindBytes(0, self.ed.Length, b"bIg",1047 self.ed.SCFIND_MATCHCASE), -1)1048 def testWordFind(self):1049 self.assertEquals(self.ed.FindBytes(0, self.ed.Length, b"bi", 0), 2)1050 self.assertEquals(self.ed.FindBytes(0, self.ed.Length, b"bi",1051 self.ed.SCFIND_WHOLEWORD), -1)1052 def testWordStartFind(self):1053 self.assertEquals(self.ed.FindBytes(0, self.ed.Length, b"bi", 0), 2)1054 self.assertEquals(self.ed.FindBytes(0, self.ed.Length, b"bi",1055 self.ed.SCFIND_WORDSTART), 2)1056 self.assertEquals(self.ed.FindBytes(0, self.ed.Length, b"ig", 0), 3)1057 self.assertEquals(self.ed.FindBytes(0, self.ed.Length, b"ig",1058 self.ed.SCFIND_WORDSTART), -1)1059 def testREFind(self):1060 flags = self.ed.SCFIND_REGEXP1061 self.assertEquals(-1, self.ed.FindBytes(0, self.ed.Length, b"b.g", 0))1062 self.assertEquals(2, self.ed.FindBytes(0, self.ed.Length, b"b.g", flags))1063 self.assertEquals(2, self.ed.FindBytes(0, self.ed.Length, b"\<b.g\>", flags))1064 self.assertEquals(-1, self.ed.FindBytes(0, self.ed.Length, b"b[A-Z]g",1065 flags | self.ed.SCFIND_MATCHCASE))1066 self.assertEquals(2, self.ed.FindBytes(0, self.ed.Length, b"b[a-z]g", flags))1067 self.assertEquals(6, self.ed.FindBytes(0, self.ed.Length, b"b[a-z]*t", flags))1068 self.assertEquals(0, self.ed.FindBytes(0, self.ed.Length, b"^a", flags))1069 self.assertEquals(10, self.ed.FindBytes(0, self.ed.Length, b"\t$", flags))1070 self.assertEquals(0, self.ed.FindBytes(0, self.ed.Length, b"\([a]\).*\0", flags))1071 def testPosixREFind(self):1072 flags = self.ed.SCFIND_REGEXP | self.ed.SCFIND_POSIX1073 self.assertEquals(-1, self.ed.FindBytes(0, self.ed.Length, b"b.g", 0))1074 self.assertEquals(2, self.ed.FindBytes(0, self.ed.Length, b"b.g", flags))1075 self.assertEquals(2, self.ed.FindBytes(0, self.ed.Length, b"\<b.g\>", flags))1076 self.assertEquals(-1, self.ed.FindBytes(0, self.ed.Length, b"b[A-Z]g",1077 flags | self.ed.SCFIND_MATCHCASE))1078 self.assertEquals(2, self.ed.FindBytes(0, self.ed.Length, b"b[a-z]g", flags))1079 self.assertEquals(6, self.ed.FindBytes(0, self.ed.Length, b"b[a-z]*t", flags))1080 self.assertEquals(0, self.ed.FindBytes(0, self.ed.Length, b"^a", flags))1081 self.assertEquals(10, self.ed.FindBytes(0, self.ed.Length, b"\t$", flags))1082 self.assertEquals(0, self.ed.FindBytes(0, self.ed.Length, b"([a]).*\0", flags))1083 def testPhilippeREFind(self):1084 # Requires 1.,721085 flags = self.ed.SCFIND_REGEXP1086 self.assertEquals(0, self.ed.FindBytes(0, self.ed.Length, b"\w", flags))1087 self.assertEquals(1, self.ed.FindBytes(0, self.ed.Length, b"\W", flags))1088 self.assertEquals(-1, self.ed.FindBytes(0, self.ed.Length, b"\d", flags))1089 self.assertEquals(0, self.ed.FindBytes(0, self.ed.Length, b"\D", flags))1090 self.assertEquals(1, self.ed.FindBytes(0, self.ed.Length, b"\s", flags))1091 self.assertEquals(0, self.ed.FindBytes(0, self.ed.Length, b"\S", flags))1092 self.assertEquals(2, self.ed.FindBytes(0, self.ed.Length, b"\x62", flags))1093 def testRENonASCII(self):1094 self.ed.InsertText(0, b"\xAD")1095 flags = self.ed.SCFIND_REGEXP1096 self.assertEquals(-1, self.ed.FindBytes(0, self.ed.Length, b"\\x10", flags))1097 self.assertEquals(2, self.ed.FindBytes(0, self.ed.Length, b"\\x09", flags))1098 self.assertEquals(-1, self.ed.FindBytes(0, self.ed.Length, b"\\xAB", flags))1099 self.assertEquals(0, self.ed.FindBytes(0, self.ed.Length, b"\\xAD", flags))1100 def testMultipleAddSelection(self):1101 # Find both 'a'1102 self.assertEquals(self.ed.MultipleSelection, 0)1103 self.ed.MultipleSelection = 11104 self.assertEquals(self.ed.MultipleSelection, 1)1105 self.ed.TargetWholeDocument()1106 self.ed.SearchFlags = 01107 self.ed.SetSelection(1, 0)1108 self.assertEquals(self.ed.Selections, 1)1109 self.ed.MultipleSelectAddNext()1110 self.assertEquals(self.ed.Selections, 2)1111 self.assertEquals(self.ed.GetSelectionNAnchor(0), 0)1112 self.assertEquals(self.ed.GetSelectionNCaret(0), 1)1113 self.assertEquals(self.ed.GetSelectionNAnchor(1), 8)1114 self.assertEquals(self.ed.GetSelectionNCaret(1), 9)1115 self.ed.MultipleSelection = 01116 def testMultipleAddEachSelection(self):1117 # Find each 'b'1118 self.assertEquals(self.ed.MultipleSelection, 0)1119 self.ed.MultipleSelection = 11120 self.assertEquals(self.ed.MultipleSelection, 1)1121 self.ed.TargetWholeDocument()1122 self.ed.SearchFlags = 01123 self.ed.SetSelection(3, 2)1124 self.assertEquals(self.ed.Selections, 1)1125 self.ed.MultipleSelectAddEach()1126 self.assertEquals(self.ed.Selections, 2)1127 self.assertEquals(self.ed.GetSelectionNAnchor(0), 2)1128 self.assertEquals(self.ed.GetSelectionNCaret(0), 3)1129 self.assertEquals(self.ed.GetSelectionNAnchor(1), 6)1130 self.assertEquals(self.ed.GetSelectionNCaret(1), 7)1131 self.ed.MultipleSelection = 01132class TestRepresentations(unittest.TestCase):1133 def setUp(self):1134 self.xite = Xite.xiteFrame1135 self.ed = self.xite.ed1136 self.ed.ClearAll()1137 self.ed.EmptyUndoBuffer()1138 def testGetControl(self):1139 result = self.ed.GetRepresentation(b"\001")1140 self.assertEquals(result, b"SOH")1141 def testClearControl(self):1142 result = self.ed.GetRepresentation(b"\002")1143 self.assertEquals(result, b"STX")1144 self.ed.ClearRepresentation(b"\002")1145 result = self.ed.GetRepresentation(b"\002")1146 self.assertEquals(result, b"")1147 def testSetOhm(self):1148 ohmSign = b"\xe2\x84\xa6"1149 ohmExplained = b"U+2126 \xe2\x84\xa6"1150 self.ed.SetRepresentation(ohmSign, ohmExplained)1151 result = self.ed.GetRepresentation(ohmSign)1152 self.assertEquals(result, ohmExplained)1153class TestProperties(unittest.TestCase):1154 def setUp(self):1155 self.xite = Xite.xiteFrame1156 self.ed = self.xite.ed1157 self.ed.ClearAll()1158 self.ed.EmptyUndoBuffer()1159 def testSet(self):1160 self.ed.SetProperty(b"test", b"12")1161 self.assertEquals(self.ed.GetPropertyInt(b"test"), 12)1162 result = self.ed.GetProperty(b"test")1163 self.assertEquals(result, b"12")1164 self.ed.SetProperty(b"test.plus", b"[$(test)]")1165 result = self.ed.GetPropertyExpanded(b"test.plus")1166 self.assertEquals(result, b"[12]")1167class TestTextMargin(unittest.TestCase):1168 def setUp(self):1169 self.xite = Xite.xiteFrame1170 self.ed = self.xite.ed1171 self.ed.ClearAll()1172 self.ed.EmptyUndoBuffer()1173 self.txt = b"abcd"1174 self.ed.AddText(1, b"x")1175 def testAscent(self):1176 lineHeight = self.ed.TextHeight(0)1177 self.assertEquals(self.ed.ExtraAscent, 0)1178 self.assertEquals(self.ed.ExtraDescent, 0)1179 self.ed.ExtraAscent = 11180 self.assertEquals(self.ed.ExtraAscent, 1)1181 self.ed.ExtraDescent = 21182 self.assertEquals(self.ed.ExtraDescent, 2)1183 lineHeightIncreased = self.ed.TextHeight(0)1184 self.assertEquals(lineHeightIncreased, lineHeight + 2 + 1)1185 def testTextMargin(self):1186 self.ed.MarginSetText(0, self.txt)1187 result = self.ed.MarginGetText(0)1188 self.assertEquals(result, self.txt)1189 self.ed.MarginTextClearAll()1190 def testTextMarginStyle(self):1191 self.ed.MarginSetText(0, self.txt)1192 self.ed.MarginSetStyle(0, 33)1193 self.assertEquals(self.ed.MarginGetStyle(0), 33)1194 self.ed.MarginTextClearAll()1195 def testTextMarginStyles(self):1196 styles = b"\001\002\003\004"1197 self.ed.MarginSetText(0, self.txt)1198 self.ed.MarginSetStyles(0, styles)1199 result = self.ed.MarginGetStyles(0)1200 self.assertEquals(result, styles)1201 self.ed.MarginTextClearAll()1202 def testTextMarginStyleOffset(self):1203 self.ed.MarginSetStyleOffset(300)1204 self.assertEquals(self.ed.MarginGetStyleOffset(), 300)1205class TestAnnotation(unittest.TestCase):1206 def setUp(self):1207 self.xite = Xite.xiteFrame1208 self.ed = self.xite.ed1209 self.ed.ClearAll()1210 self.ed.EmptyUndoBuffer()1211 self.txt = b"abcd"1212 self.ed.AddText(1, b"x")1213 def testTextAnnotation(self):1214 self.assertEquals(self.ed.AnnotationGetLines(), 0)1215 self.ed.AnnotationSetText(0, self.txt)1216 self.assertEquals(self.ed.AnnotationGetLines(), 1)1217 result = self.ed.AnnotationGetText(0)1218 self.assertEquals(len(result), 4)1219 self.assertEquals(result, self.txt)1220 self.ed.AnnotationClearAll()1221 def testTextAnnotationStyle(self):1222 self.ed.AnnotationSetText(0, self.txt)1223 self.ed.AnnotationSetStyle(0, 33)1224 self.assertEquals(self.ed.AnnotationGetStyle(0), 33)1225 self.ed.AnnotationClearAll()1226 def testTextAnnotationStyles(self):1227 styles = b"\001\002\003\004"1228 self.ed.AnnotationSetText(0, self.txt)1229 self.ed.AnnotationSetStyles(0, styles)1230 result = self.ed.AnnotationGetStyles(0)1231 self.assertEquals(result, styles)1232 self.ed.AnnotationClearAll()1233 def testExtendedStyles(self):1234 start0 = self.ed.AllocateExtendedStyles(0)1235 self.assertEquals(start0, 256)1236 start1 = self.ed.AllocateExtendedStyles(10)1237 self.assertEquals(start1, 256)1238 start2 = self.ed.AllocateExtendedStyles(20)1239 self.assertEquals(start2, start1 + 10)1240 # Reset by changing lexer1241 self.ed.ReleaseAllExtendedStyles()1242 start0 = self.ed.AllocateExtendedStyles(0)1243 self.assertEquals(start0, 256)1244 def testTextAnnotationStyleOffset(self):1245 self.ed.AnnotationSetStyleOffset(300)1246 self.assertEquals(self.ed.AnnotationGetStyleOffset(), 300)1247 def testTextAnnotationVisible(self):1248 self.assertEquals(self.ed.AnnotationGetVisible(), 0)1249 self.ed.AnnotationSetVisible(2)1250 self.assertEquals(self.ed.AnnotationGetVisible(), 2)1251 self.ed.AnnotationSetVisible(0)1252def selectionPositionRepresentation(selectionPosition):1253 position, virtualSpace = selectionPosition1254 representation = str(position)1255 if virtualSpace > 0:1256 representation += "+" + str(virtualSpace) + "v"1257 return representation1258def selectionRangeRepresentation(selectionRange):1259 anchor, caret = selectionRange1260 return selectionPositionRepresentation(anchor) + "-" + selectionPositionRepresentation(caret)1261class TestMultiSelection(unittest.TestCase):1262 def setUp(self):1263 self.xite = Xite.xiteFrame1264 self.ed = self.xite.ed1265 self.ed.ClearAll()1266 self.ed.EmptyUndoBuffer()1267 # 3 lines of 3 characters1268 t = b"xxx\nxxx\nxxx"1269 self.ed.AddText(len(t), t)1270 def textOfSelection(self, n):1271 self.ed.TargetStart = self.ed.GetSelectionNStart(n)1272 self.ed.TargetEnd = self.ed.GetSelectionNEnd(n)1273 return bytes(self.ed.GetTargetText())1274 def testSelectionCleared(self):1275 self.ed.ClearSelections()1276 self.assertEquals(self.ed.Selections, 1)1277 self.assertEquals(self.ed.MainSelection, 0)1278 self.assertEquals(self.ed.GetSelectionNCaret(0), 0)1279 self.assertEquals(self.ed.GetSelectionNAnchor(0), 0)1280 def test1Selection(self):1281 self.ed.SetSelection(1, 2)1282 self.assertEquals(self.ed.Selections, 1)1283 self.assertEquals(self.ed.MainSelection, 0)1284 self.assertEquals(self.ed.GetSelectionNCaret(0), 1)1285 self.assertEquals(self.ed.GetSelectionNAnchor(0), 2)1286 self.assertEquals(self.ed.GetSelectionNStart(0), 1)1287 self.assertEquals(self.ed.GetSelectionNEnd(0), 2)1288 self.ed.SwapMainAnchorCaret()1289 self.assertEquals(self.ed.Selections, 1)1290 self.assertEquals(self.ed.MainSelection, 0)1291 self.assertEquals(self.ed.GetSelectionNCaret(0), 2)1292 self.assertEquals(self.ed.GetSelectionNAnchor(0), 1)1293 def test1SelectionReversed(self):1294 self.ed.SetSelection(2, 1)1295 self.assertEquals(self.ed.Selections, 1)1296 self.assertEquals(self.ed.MainSelection, 0)1297 self.assertEquals(self.ed.GetSelectionNCaret(0), 2)1298 self.assertEquals(self.ed.GetSelectionNAnchor(0), 1)1299 self.assertEquals(self.ed.GetSelectionNStart(0), 1)1300 self.assertEquals(self.ed.GetSelectionNEnd(0), 2)1301 def test1SelectionByStartEnd(self):1302 self.ed.SetSelectionNStart(0, 2)1303 self.ed.SetSelectionNEnd(0, 3)1304 self.assertEquals(self.ed.Selections, 1)1305 self.assertEquals(self.ed.MainSelection, 0)1306 self.assertEquals(self.ed.GetSelectionNAnchor(0), 2)1307 self.assertEquals(self.ed.GetSelectionNCaret(0), 3)1308 self.assertEquals(self.ed.GetSelectionNStart(0), 2)1309 self.assertEquals(self.ed.GetSelectionNEnd(0), 3)1310 def test2Selections(self):1311 self.ed.SetSelection(1, 2)1312 self.ed.AddSelection(4, 5)1313 self.assertEquals(self.ed.Selections, 2)1314 self.assertEquals(self.ed.MainSelection, 1)1315 self.assertEquals(self.ed.GetSelectionNCaret(0), 1)1316 self.assertEquals(self.ed.GetSelectionNAnchor(0), 2)1317 self.assertEquals(self.ed.GetSelectionNCaret(1), 4)1318 self.assertEquals(self.ed.GetSelectionNAnchor(1), 5)1319 self.assertEquals(self.ed.GetSelectionNStart(0), 1)1320 self.assertEquals(self.ed.GetSelectionNEnd(0), 2)1321 self.ed.MainSelection = 01322 self.assertEquals(self.ed.MainSelection, 0)1323 self.ed.RotateSelection()1324 self.assertEquals(self.ed.MainSelection, 1)1325 def testRectangularSelection(self):1326 self.ed.RectangularSelectionAnchor = 11327 self.assertEquals(self.ed.RectangularSelectionAnchor, 1)1328 self.ed.RectangularSelectionCaret = 101329 self.assertEquals(self.ed.RectangularSelectionCaret, 10)1330 self.assertEquals(self.ed.Selections, 3)1331 self.assertEquals(self.ed.MainSelection, 2)1332 self.assertEquals(self.ed.GetSelectionNAnchor(0), 1)1333 self.assertEquals(self.ed.GetSelectionNCaret(0), 2)1334 self.assertEquals(self.ed.GetSelectionNAnchor(1), 5)1335 self.assertEquals(self.ed.GetSelectionNCaret(1), 6)1336 self.assertEquals(self.ed.GetSelectionNAnchor(2), 9)1337 self.assertEquals(self.ed.GetSelectionNCaret(2), 10)1338 def testVirtualSpace(self):1339 self.ed.SetSelection(3, 7)1340 self.ed.SetSelectionNCaretVirtualSpace(0, 3)1341 self.assertEquals(self.ed.GetSelectionNCaretVirtualSpace(0), 3)1342 self.ed.SetSelectionNAnchorVirtualSpace(0, 2)1343 self.assertEquals(self.ed.GetSelectionNAnchorVirtualSpace(0), 2)1344 self.assertEquals(self.ed.GetSelectionNStartVirtualSpace(0), 3)1345 self.assertEquals(self.ed.GetSelectionNEndVirtualSpace(0), 2)1346 # Does not check that virtual space is valid by being at end of line1347 self.ed.SetSelection(1, 1)1348 self.ed.SetSelectionNCaretVirtualSpace(0, 3)1349 self.assertEquals(self.ed.GetSelectionNCaretVirtualSpace(0), 3)1350 self.assertEquals(self.ed.GetSelectionNStartVirtualSpace(0), 0)1351 self.assertEquals(self.ed.GetSelectionNEndVirtualSpace(0), 3)1352 def testRectangularVirtualSpace(self):1353 self.ed.VirtualSpaceOptions=11354 self.ed.RectangularSelectionAnchor = 31355 self.assertEquals(self.ed.RectangularSelectionAnchor, 3)1356 self.ed.RectangularSelectionCaret = 71357 self.assertEquals(self.ed.RectangularSelectionCaret, 7)1358 self.ed.RectangularSelectionAnchorVirtualSpace = 11359 self.assertEquals(self.ed.RectangularSelectionAnchorVirtualSpace, 1)1360 self.ed.RectangularSelectionCaretVirtualSpace = 101361 self.assertEquals(self.ed.RectangularSelectionCaretVirtualSpace, 10)1362 self.assertEquals(self.ed.Selections, 2)1363 self.assertEquals(self.ed.MainSelection, 1)1364 self.assertEquals(self.ed.GetSelectionNAnchor(0), 3)1365 self.assertEquals(self.ed.GetSelectionNAnchorVirtualSpace(0), 1)1366 self.assertEquals(self.ed.GetSelectionNCaret(0), 3)1367 self.assertEquals(self.ed.GetSelectionNCaretVirtualSpace(0), 10)1368 def testRectangularVirtualSpaceOptionOff(self):1369 # Same as previous test but virtual space option off so no virtual space in result1370 self.ed.VirtualSpaceOptions=01371 self.ed.RectangularSelectionAnchor = 31372 self.assertEquals(self.ed.RectangularSelectionAnchor, 3)1373 self.ed.RectangularSelectionCaret = 71374 self.assertEquals(self.ed.RectangularSelectionCaret, 7)1375 self.ed.RectangularSelectionAnchorVirtualSpace = 11376 self.assertEquals(self.ed.RectangularSelectionAnchorVirtualSpace, 1)1377 self.ed.RectangularSelectionCaretVirtualSpace = 101378 self.assertEquals(self.ed.RectangularSelectionCaretVirtualSpace, 10)1379 self.assertEquals(self.ed.Selections, 2)1380 self.assertEquals(self.ed.MainSelection, 1)1381 self.assertEquals(self.ed.GetSelectionNAnchor(0), 3)1382 self.assertEquals(self.ed.GetSelectionNAnchorVirtualSpace(0), 0)1383 self.assertEquals(self.ed.GetSelectionNCaret(0), 3)1384 self.assertEquals(self.ed.GetSelectionNCaretVirtualSpace(0), 0)1385 def testDropSelectionN(self):1386 self.ed.SetSelection(1, 2)1387 # Only one so dropping has no effect1388 self.ed.DropSelectionN(0)1389 self.assertEquals(self.ed.Selections, 1)1390 self.ed.AddSelection(4, 5)1391 self.assertEquals(self.ed.Selections, 2)1392 # Outside bounds so no effect1393 self.ed.DropSelectionN(2)1394 self.assertEquals(self.ed.Selections, 2)1395 # Dropping before main so main decreases1396 self.ed.DropSelectionN(0)1397 self.assertEquals(self.ed.Selections, 1)1398 self.assertEquals(self.ed.MainSelection, 0)1399 self.assertEquals(self.ed.GetSelectionNCaret(0), 4)1400 self.assertEquals(self.ed.GetSelectionNAnchor(0), 5)1401 self.ed.AddSelection(10, 11)1402 self.ed.AddSelection(20, 21)1403 self.assertEquals(self.ed.Selections, 3)1404 self.assertEquals(self.ed.MainSelection, 2)1405 self.ed.MainSelection = 11406 # Dropping after main so main does not change1407 self.ed.DropSelectionN(2)1408 self.assertEquals(self.ed.MainSelection, 1)1409 # Dropping first selection so wraps around to new last.1410 self.ed.AddSelection(30, 31)1411 self.ed.AddSelection(40, 41)1412 self.assertEquals(self.ed.Selections, 4)1413 self.ed.MainSelection = 01414 self.ed.DropSelectionN(0)1415 self.assertEquals(self.ed.MainSelection, 2)1416 def partFromSelection(self, n):1417 # Return a tuple (order, text) from a selection part1418 # order is a boolean whether the caret is before the anchor1419 self.ed.TargetStart = self.ed.GetSelectionNStart(n)1420 self.ed.TargetEnd = self.ed.GetSelectionNEnd(n)1421 return (self.ed.GetSelectionNCaret(n) < self.ed.GetSelectionNAnchor(n), self.textOfSelection(n))1422 def replacePart(self, n, part):1423 startSelection = self.ed.GetSelectionNStart(n)1424 self.ed.TargetStart = startSelection1425 self.ed.TargetEnd = self.ed.GetSelectionNEnd(n)1426 direction, text = part1427 length = len(text)1428 self.ed.ReplaceTarget(len(text), text)1429 if direction:1430 self.ed.SetSelectionNCaret(n, startSelection)1431 self.ed.SetSelectionNAnchor(n, startSelection + length)1432 else:1433 self.ed.SetSelectionNAnchor(n, startSelection)1434 self.ed.SetSelectionNCaret(n, startSelection + length)1435 def swapSelections(self):1436 # Swap the selections1437 part0 = self.partFromSelection(0)1438 part1 = self.partFromSelection(1)1439 self.replacePart(1, part0)1440 self.replacePart(0, part1)1441 def checkAdjacentSelections(self, selections, invert):1442 # Only called from testAdjacentSelections to try one permutation1443 self.ed.ClearAll()1444 self.ed.EmptyUndoBuffer()1445 t = b"ab"1446 texts = (b'b', b'a') if invert else (b'a', b'b')1447 self.ed.AddText(len(t), t)1448 sel0, sel1 = selections1449 self.ed.SetSelection(sel0[0], sel0[1])1450 self.ed.AddSelection(sel1[0], sel1[1])1451 self.assertEquals(self.ed.Selections, 2)1452 self.assertEquals(self.textOfSelection(0), texts[0])1453 self.assertEquals(self.textOfSelection(1), texts[1])1454 self.swapSelections()1455 self.assertEquals(self.ed.Contents(), b'ba')1456 self.assertEquals(self.ed.Selections, 2)1457 self.assertEquals(self.textOfSelection(0), texts[1])1458 self.assertEquals(self.textOfSelection(1), texts[0])1459 def selectionRepresentation(self, n):1460 anchor = (self.ed.GetSelectionNAnchor(0), self.ed.GetSelectionNAnchorVirtualSpace(0))1461 caret = (self.ed.GetSelectionNCaret(0), self.ed.GetSelectionNCaretVirtualSpace(0))1462 return selectionRangeRepresentation((anchor, caret))1463 def testAdjacentSelections(self):1464 # For various permutations of selections, try swapping the text and ensure that the1465 # selections remain distinct1466 self.checkAdjacentSelections(((1, 0),(1, 2)), False)1467 self.checkAdjacentSelections(((0, 1),(1, 2)), False)1468 self.checkAdjacentSelections(((1, 0),(2, 1)), False)1469 self.checkAdjacentSelections(((0, 1),(2, 1)), False)1470 # Reverse order, first selection is after second1471 self.checkAdjacentSelections(((1, 2),(1, 0)), True)1472 self.checkAdjacentSelections(((1, 2),(0, 1)), True)1473 self.checkAdjacentSelections(((2, 1),(1, 0)), True)1474 self.checkAdjacentSelections(((2, 1),(0, 1)), True)1475 def testInsertBefore(self):1476 self.ed.ClearAll()1477 t = b"a"1478 self.ed.AddText(len(t), t)1479 self.ed.SetSelection(0, 1)1480 self.assertEquals(self.textOfSelection(0), b'a')1481 self.ed.SetTargetRange(0, 0)1482 self.ed.ReplaceTarget(1, b'1')1483 self.assertEquals(self.ed.Contents(), b'1a')1484 self.assertEquals(self.textOfSelection(0), b'a')1485 def testInsertAfter(self):1486 self.ed.ClearAll()1487 t = b"a"1488 self.ed.AddText(len(t), t)1489 self.ed.SetSelection(0, 1)1490 self.assertEquals(self.textOfSelection(0), b'a')1491 self.ed.SetTargetRange(1, 1)1492 self.ed.ReplaceTarget(1, b'9')1493 self.assertEquals(self.ed.Contents(), b'a9')1494 self.assertEquals(self.textOfSelection(0), b'a')1495 def testInsertBeforeVirtualSpace(self):1496 self.ed.SetContents(b"a")1497 self.ed.SetSelection(1, 1)1498 self.ed.SetSelectionNAnchorVirtualSpace(0, 2)1499 self.ed.SetSelectionNCaretVirtualSpace(0, 2)1500 self.assertEquals(self.selectionRepresentation(0), "1+2v-1+2v")1501 self.assertEquals(self.textOfSelection(0), b'')1502 # Append '1'1503 self.ed.SetTargetRange(1, 1)1504 self.ed.ReplaceTarget(1, b'1')1505 # Selection moved on 1, but still empty1506 self.assertEquals(self.selectionRepresentation(0), "2+1v-2+1v")1507 self.assertEquals(self.ed.Contents(), b'a1')1508 self.assertEquals(self.textOfSelection(0), b'')1509 def testInsertThroughVirtualSpace(self):1510 self.ed.SetContents(b"a")1511 self.ed.SetSelection(1, 1)1512 self.ed.SetSelectionNAnchorVirtualSpace(0, 2)1513 self.ed.SetSelectionNCaretVirtualSpace(0, 3)1514 self.assertEquals(self.selectionRepresentation(0), "1+2v-1+3v")1515 self.assertEquals(self.textOfSelection(0), b'')1516 # Append '1' past current virtual space1517 self.ed.SetTargetRange(1, 1)1518 self.ed.SetTargetStartVirtualSpace(4)1519 self.ed.SetTargetEndVirtualSpace(5)1520 self.ed.ReplaceTarget(1, b'1')1521 # Virtual space of selection all converted to real positions1522 self.assertEquals(self.selectionRepresentation(0), "3-4")1523 self.assertEquals(self.ed.Contents(), b'a 1')1524 self.assertEquals(self.textOfSelection(0), b' ')1525class TestModalSelection(unittest.TestCase):1526 def setUp(self):1527 self.xite = Xite.xiteFrame1528 self.ed = self.xite.ed1529 self.ed.ClearAll()1530 self.ed.EmptyUndoBuffer()1531 # 3 lines of 3 characters1532 t = b"xxx\nxxx\nxxx"1533 self.ed.AddText(len(t), t)1534 def testCharacterSelection(self):1535 self.ed.SetSelection(1, 1)1536 self.assertEquals(self.ed.Selections, 1)1537 self.assertEquals(self.ed.MainSelection, 0)1538 self.assertEquals(self.ed.GetSelectionNCaret(0), 1)1539 self.assertEquals(self.ed.GetSelectionNAnchor(0), 1)1540 self.ed.SelectionMode = self.ed.SC_SEL_STREAM1541 self.assertEquals(self.ed.Selections, 1)1542 self.assertEquals(self.ed.MainSelection, 0)1543 self.assertEquals(self.ed.GetSelectionNCaret(0), 1)1544 self.assertEquals(self.ed.GetSelectionNAnchor(0), 1)1545 self.ed.CharRight()1546 self.assertEquals(self.ed.Selections, 1)1547 self.assertEquals(self.ed.MainSelection, 0)1548 self.assertEquals(self.ed.GetSelectionNCaret(0), 2)1549 self.assertEquals(self.ed.GetSelectionNAnchor(0), 1)1550 self.ed.LineDown()1551 self.assertEquals(self.ed.Selections, 1)1552 self.assertEquals(self.ed.MainSelection, 0)1553 self.assertEquals(self.ed.GetSelectionNCaret(0), 6)1554 self.assertEquals(self.ed.GetSelectionNAnchor(0), 1)1555 self.ed.ClearSelections()1556 def testRectangleSelection(self):1557 self.ed.SetSelection(1, 1)1558 self.assertEquals(self.ed.Selections, 1)1559 self.assertEquals(self.ed.MainSelection, 0)1560 self.assertEquals(self.ed.GetSelectionNCaret(0), 1)1561 self.assertEquals(self.ed.GetSelectionNAnchor(0), 1)1562 self.ed.SelectionMode = self.ed.SC_SEL_RECTANGLE1563 self.assertEquals(self.ed.Selections, 1)1564 self.assertEquals(self.ed.MainSelection, 0)1565 self.assertEquals(self.ed.GetSelectionNCaret(0), 1)1566 self.assertEquals(self.ed.GetSelectionNAnchor(0), 1)1567 self.ed.CharRight()1568 self.assertEquals(self.ed.Selections, 1)1569 self.assertEquals(self.ed.MainSelection, 0)1570 self.assertEquals(self.ed.GetSelectionNCaret(0), 2)1571 self.assertEquals(self.ed.GetSelectionNAnchor(0), 1)1572 self.ed.LineDown()1573 self.assertEquals(self.ed.Selections, 2)1574 self.assertEquals(self.ed.MainSelection, 1)1575 self.assertEquals(self.ed.GetSelectionNCaret(0), 2)1576 self.assertEquals(self.ed.GetSelectionNAnchor(0), 1)1577 self.assertEquals(self.ed.GetSelectionNCaret(1), 6)1578 self.assertEquals(self.ed.GetSelectionNAnchor(1), 5)1579 self.ed.ClearSelections()1580 def testLinesSelection(self):1581 self.ed.SetSelection(1, 1)1582 self.assertEquals(self.ed.Selections, 1)1583 self.assertEquals(self.ed.MainSelection, 0)1584 self.assertEquals(self.ed.GetSelectionNCaret(0), 1)1585 self.assertEquals(self.ed.GetSelectionNAnchor(0), 1)1586 self.ed.SelectionMode = self.ed.SC_SEL_LINES1587 self.assertEquals(self.ed.Selections, 1)1588 self.assertEquals(self.ed.MainSelection, 0)1589 self.assertEquals(self.ed.GetSelectionNCaret(0), 0)1590 self.assertEquals(self.ed.GetSelectionNAnchor(0), 3)1591 self.ed.CharRight()1592 self.assertEquals(self.ed.Selections, 1)1593 self.assertEquals(self.ed.MainSelection, 0)1594 self.assertEquals(self.ed.GetSelectionNCaret(0), 0)1595 self.assertEquals(self.ed.GetSelectionNAnchor(0), 3)1596 self.ed.LineDown()1597 self.assertEquals(self.ed.Selections, 1)1598 self.assertEquals(self.ed.MainSelection, 0)1599 self.assertEquals(self.ed.GetSelectionNCaret(0), 7)1600 self.assertEquals(self.ed.GetSelectionNAnchor(0), 0)1601 self.ed.ClearSelections()1602class TestStyleAttributes(unittest.TestCase):1603 """ These tests are just to ensure that the calls set and retrieve values.1604 They do not check the visual appearance of the style attributes.1605 """1606 def setUp(self):1607 self.xite = Xite.xiteFrame1608 self.ed = self.xite.ed1609 self.ed.ClearAll()1610 self.ed.EmptyUndoBuffer()1611 self.testColour = 0x1716151612 self.testFont = b"Georgia"1613 def tearDown(self):1614 self.ed.StyleResetDefault()1615 def testFont(self):1616 self.ed.StyleSetFont(self.ed.STYLE_DEFAULT, self.testFont)1617 self.assertEquals(self.ed.StyleGetFont(self.ed.STYLE_DEFAULT), self.testFont)1618 def testSize(self):1619 self.ed.StyleSetSize(self.ed.STYLE_DEFAULT, 12)1620 self.assertEquals(self.ed.StyleGetSize(self.ed.STYLE_DEFAULT), 12)1621 self.assertEquals(self.ed.StyleGetSizeFractional(self.ed.STYLE_DEFAULT), 12*self.ed.SC_FONT_SIZE_MULTIPLIER)1622 self.ed.StyleSetSizeFractional(self.ed.STYLE_DEFAULT, 1234)1623 self.assertEquals(self.ed.StyleGetSizeFractional(self.ed.STYLE_DEFAULT), 1234)1624 def testBold(self):1625 self.ed.StyleSetBold(self.ed.STYLE_DEFAULT, 1)1626 self.assertEquals(self.ed.StyleGetBold(self.ed.STYLE_DEFAULT), 1)1627 self.assertEquals(self.ed.StyleGetWeight(self.ed.STYLE_DEFAULT), self.ed.SC_WEIGHT_BOLD)1628 self.ed.StyleSetWeight(self.ed.STYLE_DEFAULT, 530)1629 self.assertEquals(self.ed.StyleGetWeight(self.ed.STYLE_DEFAULT), 530)1630 def testItalic(self):1631 self.ed.StyleSetItalic(self.ed.STYLE_DEFAULT, 1)1632 self.assertEquals(self.ed.StyleGetItalic(self.ed.STYLE_DEFAULT), 1)1633 def testUnderline(self):1634 self.assertEquals(self.ed.StyleGetUnderline(self.ed.STYLE_DEFAULT), 0)1635 self.ed.StyleSetUnderline(self.ed.STYLE_DEFAULT, 1)1636 self.assertEquals(self.ed.StyleGetUnderline(self.ed.STYLE_DEFAULT), 1)1637 def testFore(self):1638 self.assertEquals(self.ed.StyleGetFore(self.ed.STYLE_DEFAULT), 0)1639 self.ed.StyleSetFore(self.ed.STYLE_DEFAULT, self.testColour)1640 self.assertEquals(self.ed.StyleGetFore(self.ed.STYLE_DEFAULT), self.testColour)1641 def testBack(self):1642 self.assertEquals(self.ed.StyleGetBack(self.ed.STYLE_DEFAULT), 0xffffff)1643 self.ed.StyleSetBack(self.ed.STYLE_DEFAULT, self.testColour)1644 self.assertEquals(self.ed.StyleGetBack(self.ed.STYLE_DEFAULT), self.testColour)1645 def testEOLFilled(self):1646 self.assertEquals(self.ed.StyleGetEOLFilled(self.ed.STYLE_DEFAULT), 0)1647 self.ed.StyleSetEOLFilled(self.ed.STYLE_DEFAULT, 1)1648 self.assertEquals(self.ed.StyleGetEOLFilled(self.ed.STYLE_DEFAULT), 1)1649 def testCharacterSet(self):1650 self.ed.StyleSetCharacterSet(self.ed.STYLE_DEFAULT, self.ed.SC_CHARSET_RUSSIAN)1651 self.assertEquals(self.ed.StyleGetCharacterSet(self.ed.STYLE_DEFAULT), self.ed.SC_CHARSET_RUSSIAN)1652 def testCase(self):1653 self.assertEquals(self.ed.StyleGetCase(self.ed.STYLE_DEFAULT), self.ed.SC_CASE_MIXED)1654 self.ed.StyleSetCase(self.ed.STYLE_DEFAULT, self.ed.SC_CASE_UPPER)1655 self.assertEquals(self.ed.StyleGetCase(self.ed.STYLE_DEFAULT), self.ed.SC_CASE_UPPER)1656 self.ed.StyleSetCase(self.ed.STYLE_DEFAULT, self.ed.SC_CASE_LOWER)1657 self.assertEquals(self.ed.StyleGetCase(self.ed.STYLE_DEFAULT), self.ed.SC_CASE_LOWER)1658 def testVisible(self):1659 self.assertEquals(self.ed.StyleGetVisible(self.ed.STYLE_DEFAULT), 1)1660 self.ed.StyleSetVisible(self.ed.STYLE_DEFAULT, 0)1661 self.assertEquals(self.ed.StyleGetVisible(self.ed.STYLE_DEFAULT), 0)1662 def testChangeable(self):1663 self.assertEquals(self.ed.StyleGetChangeable(self.ed.STYLE_DEFAULT), 1)1664 self.ed.StyleSetChangeable(self.ed.STYLE_DEFAULT, 0)1665 self.assertEquals(self.ed.StyleGetChangeable(self.ed.STYLE_DEFAULT), 0)1666 def testHotSpot(self):1667 self.assertEquals(self.ed.StyleGetHotSpot(self.ed.STYLE_DEFAULT), 0)1668 self.ed.StyleSetHotSpot(self.ed.STYLE_DEFAULT, 1)1669 self.assertEquals(self.ed.StyleGetHotSpot(self.ed.STYLE_DEFAULT), 1)1670 def testFoldDisplayTextStyle(self):1671 self.assertEquals(self.ed.FoldDisplayTextGetStyle(), 0)1672 self.ed.FoldDisplayTextSetStyle(self.ed.SC_FOLDDISPLAYTEXT_BOXED)1673 self.assertEquals(self.ed.FoldDisplayTextGetStyle(), self.ed.SC_FOLDDISPLAYTEXT_BOXED)1674 def testDefaultFoldDisplayText(self):1675 self.assertEquals(self.ed.GetDefaultFoldDisplayText(), b"")1676 self.ed.SetDefaultFoldDisplayText(0, b"...")1677 self.assertEquals(self.ed.GetDefaultFoldDisplayText(), b"...")1678class TestIndices(unittest.TestCase):1679 def setUp(self):1680 self.xite = Xite.xiteFrame1681 self.ed = self.xite.ed1682 self.ed.ClearAll()1683 self.ed.EmptyUndoBuffer()1684 self.ed.SetCodePage(65001)1685 # Text includes one non-BMP character1686 t = "aÃ¥\U00010348ï¬ï¬-\n"1687 self.tv = t.encode("UTF-8")1688 def tearDown(self):1689 self.ed.SetCodePage(0)1690 def testAllocation(self):1691 self.assertEquals(self.ed.GetLineCharacterIndex(), self.ed.SC_LINECHARACTERINDEX_NONE)1692 self.ed.AllocateLineCharacterIndex(self.ed.SC_LINECHARACTERINDEX_UTF32)1693 self.assertEquals(self.ed.GetLineCharacterIndex(), self.ed.SC_LINECHARACTERINDEX_UTF32)1694 self.ed.ReleaseLineCharacterIndex(self.ed.SC_LINECHARACTERINDEX_UTF32)1695 self.assertEquals(self.ed.GetLineCharacterIndex(), self.ed.SC_LINECHARACTERINDEX_NONE)1696 def testUTF32(self):1697 self.assertEquals(self.ed.GetLineCharacterIndex(), self.ed.SC_LINECHARACTERINDEX_NONE)1698 self.ed.SetContents(self.tv)1699 self.ed.AllocateLineCharacterIndex(self.ed.SC_LINECHARACTERINDEX_UTF32)1700 self.assertEquals(self.ed.IndexPositionFromLine(0, self.ed.SC_LINECHARACTERINDEX_UTF32), 0)1701 self.assertEquals(self.ed.IndexPositionFromLine(1, self.ed.SC_LINECHARACTERINDEX_UTF32), 7)1702 self.ed.ReleaseLineCharacterIndex(self.ed.SC_LINECHARACTERINDEX_UTF32)1703 self.assertEquals(self.ed.GetLineCharacterIndex(), self.ed.SC_LINECHARACTERINDEX_NONE)1704 def testUTF16(self):1705 self.assertEquals(self.ed.GetLineCharacterIndex(), self.ed.SC_LINECHARACTERINDEX_NONE)1706 t = "aÃ¥\U00010348ï¬ï¬-"1707 tv = t.encode("UTF-8")1708 self.ed.SetContents(self.tv)1709 self.ed.AllocateLineCharacterIndex(self.ed.SC_LINECHARACTERINDEX_UTF16)1710 self.assertEquals(self.ed.IndexPositionFromLine(0, self.ed.SC_LINECHARACTERINDEX_UTF16), 0)1711 self.assertEquals(self.ed.IndexPositionFromLine(1, self.ed.SC_LINECHARACTERINDEX_UTF16), 8)1712 self.ed.ReleaseLineCharacterIndex(self.ed.SC_LINECHARACTERINDEX_UTF16)1713 self.assertEquals(self.ed.GetLineCharacterIndex(), self.ed.SC_LINECHARACTERINDEX_NONE)1714 def testBoth(self):1715 # Set text before turning indices on1716 self.assertEquals(self.ed.GetLineCharacterIndex(), self.ed.SC_LINECHARACTERINDEX_NONE)1717 self.ed.SetContents(self.tv)1718 self.ed.AllocateLineCharacterIndex(self.ed.SC_LINECHARACTERINDEX_UTF32+self.ed.SC_LINECHARACTERINDEX_UTF16)1719 self.assertEquals(self.ed.IndexPositionFromLine(0, self.ed.SC_LINECHARACTERINDEX_UTF32), 0)1720 self.assertEquals(self.ed.IndexPositionFromLine(1, self.ed.SC_LINECHARACTERINDEX_UTF32), 7)1721 self.assertEquals(self.ed.IndexPositionFromLine(0, self.ed.SC_LINECHARACTERINDEX_UTF16), 0)1722 self.assertEquals(self.ed.IndexPositionFromLine(1, self.ed.SC_LINECHARACTERINDEX_UTF16), 8)1723 # Test the inverse: position->line1724 self.assertEquals(self.ed.LineFromIndexPosition(0, self.ed.SC_LINECHARACTERINDEX_UTF32), 0)1725 self.assertEquals(self.ed.LineFromIndexPosition(7, self.ed.SC_LINECHARACTERINDEX_UTF32), 1)1726 self.assertEquals(self.ed.LineFromIndexPosition(0, self.ed.SC_LINECHARACTERINDEX_UTF16), 0)1727 self.assertEquals(self.ed.LineFromIndexPosition(8, self.ed.SC_LINECHARACTERINDEX_UTF16), 1)1728 self.ed.ReleaseLineCharacterIndex(self.ed.SC_LINECHARACTERINDEX_UTF32+self.ed.SC_LINECHARACTERINDEX_UTF16)1729 self.assertEquals(self.ed.GetLineCharacterIndex(), self.ed.SC_LINECHARACTERINDEX_NONE)1730 def testMaintenance(self):1731 # Set text after turning indices on1732 self.assertEquals(self.ed.GetLineCharacterIndex(), self.ed.SC_LINECHARACTERINDEX_NONE)1733 self.ed.AllocateLineCharacterIndex(self.ed.SC_LINECHARACTERINDEX_UTF32+self.ed.SC_LINECHARACTERINDEX_UTF16)1734 self.ed.SetContents(self.tv)1735 self.assertEquals(self.ed.IndexPositionFromLine(0, self.ed.SC_LINECHARACTERINDEX_UTF32), 0)1736 self.assertEquals(self.ed.IndexPositionFromLine(1, self.ed.SC_LINECHARACTERINDEX_UTF32), 7)1737 self.assertEquals(self.ed.IndexPositionFromLine(0, self.ed.SC_LINECHARACTERINDEX_UTF16), 0)1738 self.assertEquals(self.ed.IndexPositionFromLine(1, self.ed.SC_LINECHARACTERINDEX_UTF16), 8)1739 self.ed.ReleaseLineCharacterIndex(self.ed.SC_LINECHARACTERINDEX_UTF32+self.ed.SC_LINECHARACTERINDEX_UTF16)1740 self.assertEquals(self.ed.GetLineCharacterIndex(), self.ed.SC_LINECHARACTERINDEX_NONE)1741class TestCharacterNavigation(unittest.TestCase):1742 def setUp(self):1743 self.xite = Xite.xiteFrame1744 self.ed = self.xite.ed1745 self.ed.ClearAll()1746 self.ed.EmptyUndoBuffer()1747 self.ed.SetCodePage(65001)1748 def tearDown(self):1749 self.ed.SetCodePage(0)1750 def testBeforeAfter(self):1751 t = "aÃ¥ï¬ï¬-"1752 tv = t.encode("UTF-8")1753 self.ed.SetContents(tv)1754 pos = 01755 for i in range(len(t)-1):1756 after = self.ed.PositionAfter(pos)1757 self.assert_(after > i)1758 back = self.ed.PositionBefore(after)1759 self.assertEquals(pos, back)1760 pos = after1761 def testRelative(self):1762 # \x61 \xc3\xa5 \xef\xac\x82 \xef\xac\x94 \x2d1763 t = "aÃ¥ï¬ï¬-"1764 tv = t.encode("UTF-8")1765 self.ed.SetContents(tv)1766 self.assertEquals(self.ed.PositionRelative(1, 2), 6)1767 self.assertEquals(self.ed.CountCharacters(1, 6), 2)1768 self.assertEquals(self.ed.PositionRelative(6, -2), 1)1769 pos = 01770 previous = 01771 for i in range(1, len(t)):1772 after = self.ed.PositionRelative(pos, i)1773 self.assert_(after > pos)1774 self.assert_(after > previous)1775 previous = after1776 pos = len(t)1777 previous = pos1778 for i in range(1, len(t)-1):1779 after = self.ed.PositionRelative(pos, -i)1780 self.assert_(after < pos)1781 self.assert_(after < previous)1782 previous = after1783 def testRelativeNonBOM(self):1784 # \x61 \xF0\x90\x8D\x88 \xef\xac\x82 \xef\xac\x94 \x2d1785 t = "a\U00010348ï¬ï¬-"1786 tv = t.encode("UTF-8")1787 self.ed.SetContents(tv)1788 self.assertEquals(self.ed.PositionRelative(1, 2), 8)1789 self.assertEquals(self.ed.CountCharacters(1, 8), 2)1790 self.assertEquals(self.ed.CountCodeUnits(1, 8), 3)1791 self.assertEquals(self.ed.PositionRelative(8, -2), 1)1792 self.assertEquals(self.ed.PositionRelativeCodeUnits(8, -3), 1)1793 pos = 01794 previous = 01795 for i in range(1, len(t)):1796 after = self.ed.PositionRelative(pos, i)1797 self.assert_(after > pos)1798 self.assert_(after > previous)1799 previous = after1800 pos = len(t)1801 previous = pos1802 for i in range(1, len(t)-1):1803 after = self.ed.PositionRelative(pos, -i)1804 self.assert_(after < pos)1805 self.assert_(after <= previous)1806 previous = after1807 def testLineEnd(self):1808 t = "a\r\nb\nc"1809 tv = t.encode("UTF-8")1810 self.ed.SetContents(tv)1811 for i in range(0, len(t)):1812 self.assertEquals(self.ed.CountCharacters(0, i), i)1813class TestCaseMapping(unittest.TestCase):1814 def setUp(self):1815 self.xite = Xite.xiteFrame1816 self.ed = self.xite.ed1817 self.ed.ClearAll()1818 self.ed.EmptyUndoBuffer()1819 def tearDown(self):1820 self.ed.SetCodePage(0)1821 self.ed.StyleSetCharacterSet(self.ed.STYLE_DEFAULT, self.ed.SC_CHARSET_DEFAULT)1822 def testEmpty(self):1823 # Trying to upper case an empty string caused a crash at one stage1824 t = b"x"1825 self.ed.SetContents(t)1826 self.ed.UpperCase()1827 self.assertEquals(self.ed.Contents(), b"x")1828 def testASCII(self):1829 t = b"x"1830 self.ed.SetContents(t)1831 self.ed.SetSel(0,1)1832 self.ed.UpperCase()1833 self.assertEquals(self.ed.Contents(), b"X")1834 def testLatin1(self):1835 t = "Ã¥".encode("Latin-1")1836 r = "Ã
".encode("Latin-1")1837 self.ed.SetContents(t)1838 self.ed.SetSel(0,1)1839 self.ed.UpperCase()1840 self.assertEquals(self.ed.Contents(), r)1841 def testRussian(self):1842 if sys.platform == "win32":1843 self.ed.StyleSetCharacterSet(self.ed.STYLE_DEFAULT, self.ed.SC_CHARSET_RUSSIAN)1844 else:1845 self.ed.StyleSetCharacterSet(self.ed.STYLE_DEFAULT, self.ed.SC_CHARSET_CYRILLIC)1846 t = "Ð".encode("Windows-1251")1847 r = "б".encode("Windows-1251")1848 self.ed.SetContents(t)1849 self.ed.SetSel(0,1)1850 self.ed.LowerCase()1851 self.assertEquals(self.ed.Contents(), r)1852 def testUTF(self):1853 self.ed.SetCodePage(65001)1854 t = "Ã¥".encode("UTF-8")1855 r = "Ã
".encode("UTF-8")1856 self.ed.SetContents(t)1857 self.ed.SetSel(0,2)1858 self.ed.UpperCase()1859 self.assertEquals(self.ed.Contents(), r)1860 def testUTFDifferentLength(self):1861 self.ed.SetCodePage(65001)1862 t = "ı".encode("UTF-8")1863 r = "I".encode("UTF-8")1864 self.ed.SetContents(t)1865 self.assertEquals(self.ed.Length, 2)1866 self.ed.SetSel(0,2)1867 self.ed.UpperCase()1868 self.assertEquals(self.ed.Length, 1)1869 self.assertEquals(self.ed.Contents(), r)1870 def testUTFGrows(self):1871 # This crashed at one point in debug builds due to looking past end of shorter string1872 self.ed.SetCodePage(65001)1873 # ï¬ is a single character ligature taking 3 bytes in UTF8: EF AC 961874 t = 'ï¬ï¬'.encode("UTF-8")1875 self.ed.SetContents(t)1876 self.assertEquals(self.ed.Length, 6)1877 self.ed.SetSel(0,self.ed.Length)1878 self.ed.UpperCase()1879 # To convert to upper case the ligature is separated into Õ¾ and Õ¶ then uppercased to Õ and Õ1880 # each of which takes 2 bytes in UTF-8: D5 8E D5 861881 r = 'ÕÕÕÕ'.encode("UTF-8")1882 self.assertEquals(self.ed.Length, 8)1883 self.assertEquals(self.ed.Contents(), r)1884 self.assertEquals(self.ed.SelectionEnd, self.ed.Length)1885 def testUTFShrinks(self):1886 self.ed.SetCodePage(65001)1887 # ï¬ is a single character ligature taking 3 bytes in UTF8: EF AC 811888 t = 'ï¬ï¬'.encode("UTF-8")1889 self.ed.SetContents(t)1890 self.assertEquals(self.ed.Length, 6)1891 self.ed.SetSel(0,self.ed.Length)1892 self.ed.UpperCase()1893 # To convert to upper case the ligature is separated into f and i then uppercased to F and I1894 # each of which takes 1 byte in UTF-8: 46 491895 r = 'FIFI'.encode("UTF-8")1896 self.assertEquals(self.ed.Length, 4)1897 self.assertEquals(self.ed.Contents(), r)1898 self.assertEquals(self.ed.SelectionEnd, self.ed.Length)1899class TestCaseInsensitiveSearch(unittest.TestCase):1900 def setUp(self):1901 self.xite = Xite.xiteFrame1902 self.ed = self.xite.ed1903 self.ed.ClearAll()1904 self.ed.EmptyUndoBuffer()1905 def tearDown(self):1906 self.ed.SetCodePage(0)1907 self.ed.StyleSetCharacterSet(self.ed.STYLE_DEFAULT, self.ed.SC_CHARSET_DEFAULT)1908 def testEmpty(self):1909 text = b" x X"1910 searchString = b""1911 self.ed.SetContents(text)1912 self.ed.TargetStart = 01913 self.ed.TargetEnd = self.ed.Length-11914 self.ed.SearchFlags = 01915 pos = self.ed.SearchInTarget(len(searchString), searchString)1916 self.assertEquals(0, pos)1917 def testASCII(self):1918 text = b" x X"1919 searchString = b"X"1920 self.ed.SetContents(text)1921 self.ed.TargetStart = 01922 self.ed.TargetEnd = self.ed.Length-11923 self.ed.SearchFlags = 01924 pos = self.ed.SearchInTarget(len(searchString), searchString)1925 self.assertEquals(1, pos)1926 def testLatin1(self):1927 text = "FrÃ¥nd Ã
Ã¥".encode("Latin-1")1928 searchString = "Ã
".encode("Latin-1")1929 self.ed.SetContents(text)1930 self.ed.TargetStart = 01931 self.ed.TargetEnd = self.ed.Length-11932 self.ed.SearchFlags = 01933 pos = self.ed.SearchInTarget(len(searchString), searchString)1934 self.assertEquals(2, pos)1935 def testRussian(self):1936 self.ed.StyleSetCharacterSet(self.ed.STYLE_DEFAULT, self.ed.SC_CHARSET_RUSSIAN)1937 text = "=(Ð tex б)".encode("Windows-1251")1938 searchString = "б".encode("Windows-1251")1939 self.ed.SetContents(text)1940 self.ed.TargetStart = 01941 self.ed.TargetEnd = self.ed.Length-11942 self.ed.SearchFlags = 01943 pos = self.ed.SearchInTarget(len(searchString), searchString)1944 self.assertEquals(2, pos)1945 def testUTF(self):1946 self.ed.SetCodePage(65001)1947 text = "FrÃ¥nd Ã
Ã¥".encode("UTF-8")1948 searchString = "Ã
".encode("UTF-8")1949 self.ed.SetContents(text)1950 self.ed.TargetStart = 01951 self.ed.TargetEnd = self.ed.Length-11952 self.ed.SearchFlags = 01953 pos = self.ed.SearchInTarget(len(searchString), searchString)1954 self.assertEquals(2, pos)1955 def testUTFDifferentLength(self):1956 # Searching for a two byte string finds a single byte1957 self.ed.SetCodePage(65001)1958 # two byte string "Å¿" single byte "s"1959 text = "FrÃ¥nds Ã
åſ $".encode("UTF-8")1960 searchString = "ſ".encode("UTF-8")1961 firstPosition = len("Frånd".encode("UTF-8"))1962 self.assertEquals(len(searchString), 2)1963 self.ed.SetContents(text)1964 self.ed.TargetStart = 01965 self.ed.TargetEnd = self.ed.Length-11966 self.ed.SearchFlags = 01967 pos = self.ed.SearchInTarget(len(searchString), searchString)1968 self.assertEquals(firstPosition, pos)1969 self.assertEquals(firstPosition+1, self.ed.TargetEnd)1970class TestLexer(unittest.TestCase):1971 def setUp(self):1972 self.xite = Xite.xiteFrame1973 self.ed = self.xite.ed1974 self.ed.ClearAll()1975 self.ed.EmptyUndoBuffer()1976 def testLexerNumber(self):1977 self.ed.Lexer = self.ed.SCLEX_CPP1978 self.assertEquals(self.ed.GetLexer(), self.ed.SCLEX_CPP)1979 def testLexerName(self):1980 self.ed.LexerLanguage = b"cpp"1981 self.assertEquals(self.ed.GetLexer(), self.ed.SCLEX_CPP)1982 name = self.ed.GetLexerLanguage(0)1983 self.assertEquals(name, b"cpp")1984 def testPropertyNames(self):1985 propertyNames = self.ed.PropertyNames()1986 self.assertNotEquals(propertyNames, b"")1987 # The cpp lexer has a boolean property named lexer.cpp.allow.dollars1988 propNameDollars = b"lexer.cpp.allow.dollars"1989 propertyType = self.ed.PropertyType(propNameDollars)1990 self.assertEquals(propertyType, self.ed.SC_TYPE_BOOLEAN)1991 propertyDescription = self.ed.DescribeProperty(propNameDollars)1992 self.assertNotEquals(propertyDescription, b"")1993 def testWordListDescriptions(self):1994 wordSet = self.ed.DescribeKeyWordSets()1995 self.assertNotEquals(wordSet, b"")1996class TestSubStyles(unittest.TestCase):1997 ''' These tests include knowledge of the current implementation in the cpp lexer1998 and may have to change when that implementation changes.1999 Currently supports subStyles for IDENTIFIER 11 and COMMENTDOCKEYWORD 17 '''2000 def setUp(self):2001 self.xite = Xite.xiteFrame2002 self.ed = self.xite.ed2003 self.ed.ClearAll()2004 self.ed.EmptyUndoBuffer()2005 def testInfo(self):2006 self.ed.Lexer = self.ed.SCLEX_CPP2007 bases = self.ed.GetSubStyleBases()2008 self.assertEquals(bases, b"\x0b\x11") # 11, 172009 self.assertEquals(self.ed.DistanceToSecondaryStyles(), 0x40)2010 def testAllocate(self):2011 firstSubStyle = 0x80 # Current implementation2012 self.ed.Lexer = self.ed.SCLEX_CPP2013 self.assertEquals(self.ed.GetStyleFromSubStyle(firstSubStyle), firstSubStyle)2014 self.assertEquals(self.ed.GetSubStylesStart(self.ed.SCE_C_IDENTIFIER), 0)2015 self.assertEquals(self.ed.GetSubStylesLength(self.ed.SCE_C_IDENTIFIER), 0)2016 numSubStyles = 52017 subs = self.ed.AllocateSubStyles(self.ed.SCE_C_IDENTIFIER, numSubStyles)2018 self.assertEquals(subs, firstSubStyle)2019 self.assertEquals(self.ed.GetSubStylesStart(self.ed.SCE_C_IDENTIFIER), firstSubStyle)2020 self.assertEquals(self.ed.GetSubStylesLength(self.ed.SCE_C_IDENTIFIER), numSubStyles)2021 self.assertEquals(self.ed.GetStyleFromSubStyle(subs), self.ed.SCE_C_IDENTIFIER)2022 self.assertEquals(self.ed.GetStyleFromSubStyle(subs+numSubStyles-1), self.ed.SCE_C_IDENTIFIER)2023 self.assertEquals(self.ed.GetStyleFromSubStyle(self.ed.SCE_C_IDENTIFIER), self.ed.SCE_C_IDENTIFIER)2024 # Now free and check same as start2025 self.ed.FreeSubStyles()2026 self.assertEquals(self.ed.GetStyleFromSubStyle(subs), subs)2027 self.assertEquals(self.ed.GetSubStylesStart(self.ed.SCE_C_IDENTIFIER), 0)2028 self.assertEquals(self.ed.GetSubStylesLength(self.ed.SCE_C_IDENTIFIER), 0)2029 def testInactive(self):2030 firstSubStyle = 0x80 # Current implementation2031 inactiveDistance = self.ed.DistanceToSecondaryStyles()2032 self.ed.Lexer = self.ed.SCLEX_CPP2033 numSubStyles = 52034 subs = self.ed.AllocateSubStyles(self.ed.SCE_C_IDENTIFIER, numSubStyles)2035 self.assertEquals(subs, firstSubStyle)2036 self.assertEquals(self.ed.GetStyleFromSubStyle(subs), self.ed.SCE_C_IDENTIFIER)2037 self.assertEquals(self.ed.GetStyleFromSubStyle(subs+inactiveDistance), self.ed.SCE_C_IDENTIFIER+inactiveDistance)2038 self.ed.FreeSubStyles()2039 def testSecondary(self):2040 inactiveDistance = self.ed.DistanceToSecondaryStyles()2041 self.assertEquals(self.ed.GetPrimaryStyleFromStyle(self.ed.SCE_C_IDENTIFIER+inactiveDistance), self.ed.SCE_C_IDENTIFIER)2042class TestCallTip(unittest.TestCase):2043 def setUp(self):2044 self.xite = Xite.xiteFrame2045 self.ed = self.xite.ed2046 self.ed.ClearAll()2047 self.ed.EmptyUndoBuffer()2048 # 1 line of 4 characters2049 t = b"fun("2050 self.ed.AddText(len(t), t)2051 def testBasics(self):2052 self.assertEquals(self.ed.CallTipActive(), 0)2053 self.ed.CallTipShow(1, "fun(int x)")2054 self.assertEquals(self.ed.CallTipActive(), 1)2055 self.assertEquals(self.ed.CallTipPosStart(), 4)2056 self.ed.CallTipSetPosStart(1)2057 self.assertEquals(self.ed.CallTipPosStart(), 1)2058 self.ed.CallTipCancel()2059 self.assertEquals(self.ed.CallTipActive(), 0)2060class TestEdge(unittest.TestCase):2061 def setUp(self):2062 self.xite = Xite.xiteFrame2063 self.ed = self.xite.ed2064 self.ed.ClearAll()2065 def testBasics(self):2066 self.ed.EdgeColumn = 32067 self.assertEquals(self.ed.EdgeColumn, 3)2068 self.ed.SetEdgeColour(0xA0)2069 self.assertEquals(self.ed.GetEdgeColour(), 0xA0)2070 def testMulti(self):2071 self.assertEquals(self.ed.GetMultiEdgeColumn(-1), -1)2072 self.assertEquals(self.ed.GetMultiEdgeColumn(0), -1)2073 self.ed.MultiEdgeAddLine(5, 0x50)2074 self.assertEquals(self.ed.GetMultiEdgeColumn(0), 5)2075 self.assertEquals(self.ed.GetMultiEdgeColumn(1), -1)2076 self.ed.MultiEdgeAddLine(6, 0x60)2077 self.assertEquals(self.ed.GetMultiEdgeColumn(0), 5)2078 self.assertEquals(self.ed.GetMultiEdgeColumn(1), 6)2079 self.assertEquals(self.ed.GetMultiEdgeColumn(2), -1)2080 self.ed.MultiEdgeAddLine(4, 0x40)2081 self.assertEquals(self.ed.GetMultiEdgeColumn(0), 4)2082 self.assertEquals(self.ed.GetMultiEdgeColumn(1), 5)2083 self.assertEquals(self.ed.GetMultiEdgeColumn(2), 6)2084 self.assertEquals(self.ed.GetMultiEdgeColumn(3), -1)2085 self.ed.MultiEdgeClearAll()2086 self.assertEquals(self.ed.GetMultiEdgeColumn(0), -1)2087 def testSameTwice(self):2088 # Tests that adding a column twice retains both2089 self.ed.MultiEdgeAddLine(5, 0x50)2090 self.ed.MultiEdgeAddLine(5, 0x55)2091 self.assertEquals(self.ed.GetMultiEdgeColumn(0), 5)2092 self.assertEquals(self.ed.GetMultiEdgeColumn(1), 5)2093 self.assertEquals(self.ed.GetMultiEdgeColumn(2), -1)2094 self.ed.MultiEdgeClearAll()2095class TestAutoComplete(unittest.TestCase):2096 def setUp(self):2097 self.xite = Xite.xiteFrame2098 self.ed = self.xite.ed2099 self.ed.ClearAll()2100 self.ed.EmptyUndoBuffer()2101 # 1 line of 3 characters2102 t = b"xxx\n"2103 self.ed.AddText(len(t), t)2104 def testDefaults(self):2105 self.assertEquals(self.ed.AutoCGetSeparator(), ord(' '))2106 self.assertEquals(self.ed.AutoCGetMaxHeight(), 5)2107 self.assertEquals(self.ed.AutoCGetMaxWidth(), 0)2108 self.assertEquals(self.ed.AutoCGetTypeSeparator(), ord('?'))2109 self.assertEquals(self.ed.AutoCGetIgnoreCase(), 0)2110 self.assertEquals(self.ed.AutoCGetAutoHide(), 1)2111 self.assertEquals(self.ed.AutoCGetDropRestOfWord(), 0)2112 def testChangeDefaults(self):2113 self.ed.AutoCSetSeparator(ord('-'))2114 self.assertEquals(self.ed.AutoCGetSeparator(), ord('-'))2115 self.ed.AutoCSetSeparator(ord(' '))2116 self.ed.AutoCSetMaxHeight(100)2117 self.assertEquals(self.ed.AutoCGetMaxHeight(), 100)2118 self.ed.AutoCSetMaxHeight(5)2119 self.ed.AutoCSetMaxWidth(100)2120 self.assertEquals(self.ed.AutoCGetMaxWidth(), 100)2121 self.ed.AutoCSetMaxWidth(0)2122 self.ed.AutoCSetTypeSeparator(ord('@'))2123 self.assertEquals(self.ed.AutoCGetTypeSeparator(), ord('@'))2124 self.ed.AutoCSetTypeSeparator(ord('?'))2125 self.ed.AutoCSetIgnoreCase(1)2126 self.assertEquals(self.ed.AutoCGetIgnoreCase(), 1)2127 self.ed.AutoCSetIgnoreCase(0)2128 self.ed.AutoCSetAutoHide(0)2129 self.assertEquals(self.ed.AutoCGetAutoHide(), 0)2130 self.ed.AutoCSetAutoHide(1)2131 self.ed.AutoCSetDropRestOfWord(1)2132 self.assertEquals(self.ed.AutoCGetDropRestOfWord(), 1)2133 self.ed.AutoCSetDropRestOfWord(0)2134 def testAutoShow(self):2135 self.assertEquals(self.ed.AutoCActive(), 0)2136 self.ed.SetSel(0, 0)2137 self.ed.AutoCShow(0, b"za defn ghi")2138 self.assertEquals(self.ed.AutoCActive(), 1)2139 #~ time.sleep(2)2140 self.assertEquals(self.ed.AutoCPosStart(), 0)2141 self.assertEquals(self.ed.AutoCGetCurrent(), 0)2142 t = self.ed.AutoCGetCurrentText(5)2143 #~ self.assertEquals(l, 3)2144 self.assertEquals(t, b"za")2145 self.ed.AutoCCancel()2146 self.assertEquals(self.ed.AutoCActive(), 0)2147 def testAutoShowComplete(self):2148 self.assertEquals(self.ed.AutoCActive(), 0)2149 self.ed.SetSel(0, 0)2150 self.ed.AutoCShow(0, b"za defn ghi")2151 self.ed.AutoCComplete()2152 self.assertEquals(self.ed.Contents(), b"zaxxx\n")2153 self.assertEquals(self.ed.AutoCActive(), 0)2154 def testAutoShowSelect(self):2155 self.assertEquals(self.ed.AutoCActive(), 0)2156 self.ed.SetSel(0, 0)2157 self.ed.AutoCShow(0, b"za defn ghi")2158 self.ed.AutoCSelect(0, b"d")2159 self.ed.AutoCComplete()2160 self.assertEquals(self.ed.Contents(), b"defnxxx\n")2161 self.assertEquals(self.ed.AutoCActive(), 0)2162 def testWriteOnly(self):2163 """ Checks that setting attributes doesn't crash or change tested behaviour2164 but does not check that the changed attributes are effective. """2165 self.ed.AutoCStops(0, b"abcde")2166 self.ed.AutoCSetFillUps(0, b"1234")2167class TestDirectAccess(unittest.TestCase):2168 def setUp(self):2169 self.xite = Xite.xiteFrame2170 self.ed = self.xite.ed2171 self.ed.ClearAll()2172 self.ed.EmptyUndoBuffer()2173 def testGapPosition(self):2174 text = b"abcd"2175 self.ed.SetContents(text)2176 self.assertEquals(self.ed.GapPosition, 4)2177 self.ed.TargetStart = 12178 self.ed.TargetEnd = 12179 rep = b"-"2180 self.ed.ReplaceTarget(len(rep), rep)2181 self.assertEquals(self.ed.GapPosition, 2)2182 def testCharacterPointerAndRangePointer(self):2183 text = b"abcd"2184 self.ed.SetContents(text)2185 characterPointer = self.ed.CharacterPointer2186 rangePointer = self.ed.GetRangePointer(0,3)2187 self.assertEquals(characterPointer, rangePointer)2188 cpBuffer = ctypes.c_char_p(characterPointer)2189 self.assertEquals(cpBuffer.value, text)2190 # Gap will not be moved as already moved for CharacterPointer call2191 rangePointer = self.ed.GetRangePointer(1,3)2192 cpBuffer = ctypes.c_char_p(rangePointer)2193 self.assertEquals(cpBuffer.value, text[1:])2194class TestWordChars(unittest.TestCase):2195 def setUp(self):2196 self.xite = Xite.xiteFrame2197 self.ed = self.xite.ed2198 self.ed.ClearAll()2199 self.ed.EmptyUndoBuffer()2200 def tearDown(self):2201 self.ed.SetCharsDefault()2202 def _setChars(self, charClass, chars):2203 """ Wrapper to call self.ed.Set*Chars with the right type2204 @param charClass {str} the character class, "word", "space", etc.2205 @param chars {iterable of int} characters to set2206 """2207 if sys.version_info.major == 2:2208 # Python 2, use latin-1 encoded str2209 unichars = (unichr(x) for x in chars if x != 0)2210 # can't use literal u"", that's a syntax error in Py3k2211 # uncode() doesn't exist in Py3k, but we never run it there2212 result = unicode("").join(unichars).encode("latin-1")2213 else:2214 # Python 3, use bytes()2215 result = bytes(x for x in chars if x != 0)2216 meth = getattr(self.ed, "Set%sChars" % (charClass.capitalize()))2217 return meth(None, result)2218 def assertCharSetsEqual(self, first, second, *args, **kwargs):2219 """ Assert that the two character sets are equal.2220 If either set are an iterable of numbers, convert them to chars2221 first. """2222 first_set = set()2223 for c in first:2224 first_set.add(chr(c) if isinstance(c, int) else c)2225 second_set = set()2226 for c in second:2227 second_set.add(chr(c) if isinstance(c, int) else c)2228 return self.assertEqual(first_set, second_set, *args, **kwargs)2229 def testDefaultWordChars(self):2230 # check that the default word chars are as expected2231 import string2232 data = self.ed.GetWordChars(None)2233 expected = set(string.digits + string.ascii_letters + '_') | \2234 set(chr(x) for x in range(0x80, 0x100))2235 self.assertCharSetsEqual(data, expected)2236 def testDefaultWhitespaceChars(self):2237 # check that the default whitespace chars are as expected2238 import string2239 data = self.ed.GetWhitespaceChars(None)2240 expected = (set(chr(x) for x in (range(0, 0x20))) | set(' ')) - \2241 set(['\r', '\n'])2242 self.assertCharSetsEqual(data, expected)2243 def testDefaultPunctuationChars(self):2244 # check that the default punctuation chars are as expected2245 import string2246 data = self.ed.GetPunctuationChars(None)2247 expected = set(chr(x) for x in range(0x20, 0x80)) - \2248 set(string.ascii_letters + string.digits + "\r\n_ ")2249 self.assertCharSetsEqual(data, expected)2250 def testCustomWordChars(self):2251 # check that setting things to whitespace chars makes them not words2252 self._setChars("whitespace", range(1, 0x100))2253 data = self.ed.GetWordChars(None)2254 expected = set()2255 self.assertCharSetsEqual(data, expected)2256 # and now set something to make sure that works too2257 expected = set(range(1, 0x100, 2))2258 self._setChars("word", expected)2259 data = self.ed.GetWordChars(None)2260 self.assertCharSetsEqual(data, expected)2261 def testCustomWhitespaceChars(self):2262 # check setting whitespace chars to non-default values2263 self._setChars("word", range(1, 0x100))2264 # we can't change chr(0) from being anything but whitespace2265 expected = set([0])2266 data = self.ed.GetWhitespaceChars(None)2267 self.assertCharSetsEqual(data, expected)2268 # now try to set it to something custom2269 expected = set(range(1, 0x100, 2)) | set([0])2270 self._setChars("whitespace", expected)2271 data = self.ed.GetWhitespaceChars(None)2272 self.assertCharSetsEqual(data, expected)2273 def testCustomPunctuationChars(self):2274 # check setting punctuation chars to non-default values2275 self._setChars("word", range(1, 0x100))2276 expected = set()2277 data = self.ed.GetPunctuationChars(0)2278 self.assertEquals(set(data), expected)2279 # now try to set it to something custom2280 expected = set(range(1, 0x100, 1))2281 self._setChars("punctuation", expected)2282 data = self.ed.GetPunctuationChars(None)2283 self.assertCharSetsEqual(data, expected)2284 def testCharacterCategoryOptimization(self):2285 self.assertEquals(self.ed.CharacterCategoryOptimization, 0x100)2286 self.ed.CharacterCategoryOptimization = 0x10002287 self.assertEquals(self.ed.CharacterCategoryOptimization, 0x1000)2288class TestExplicitTabStops(unittest.TestCase):2289 def setUp(self):2290 self.xite = Xite.xiteFrame2291 self.ed = self.xite.ed2292 self.ed.ClearAll()2293 self.ed.EmptyUndoBuffer()2294 # 2 lines of 4 characters2295 self.t = b"fun(\nint)"2296 self.ed.AddText(len(self.t), self.t)2297 def testAddingAndClearing(self):2298 self.assertEquals(self.ed.GetNextTabStop(0,0), 0)2299 # Add a tab stop at 72300 self.ed.AddTabStop(0, 7)2301 # Check added2302 self.assertEquals(self.ed.GetNextTabStop(0,0), 7)2303 # Check does not affect line 12304 self.assertEquals(self.ed.GetNextTabStop(1,0), 0)2305 # Add a tab stop at 182306 self.ed.AddTabStop(0, 18)2307 # Check added2308 self.assertEquals(self.ed.GetNextTabStop(0,0), 7)2309 self.assertEquals(self.ed.GetNextTabStop(0,7), 18)2310 # Check does not affect line 12311 self.assertEquals(self.ed.GetNextTabStop(1,0), 0)2312 self.assertEquals(self.ed.GetNextTabStop(1,7), 0)2313 # Add a tab stop between others at 132314 self.ed.AddTabStop(0, 13)2315 # Check added2316 self.assertEquals(self.ed.GetNextTabStop(0,0), 7)2317 self.assertEquals(self.ed.GetNextTabStop(0,7), 13)2318 self.assertEquals(self.ed.GetNextTabStop(0,13), 18)2319 # Check does not affect line 12320 self.assertEquals(self.ed.GetNextTabStop(1,0), 0)2321 self.assertEquals(self.ed.GetNextTabStop(1,7), 0)2322 self.ed.ClearTabStops(0)2323 # Check back to original state2324 self.assertEquals(self.ed.GetNextTabStop(0,0), 0)2325 def testLineInsertionDeletion(self):2326 # Add a tab stop at 7 on line 12327 self.ed.AddTabStop(1, 7)2328 # Check added2329 self.assertEquals(self.ed.GetNextTabStop(1,0), 7)2330 # More text at end2331 self.ed.AddText(len(self.t), self.t)2332 self.assertEquals(self.ed.GetNextTabStop(0,0), 0)2333 self.assertEquals(self.ed.GetNextTabStop(1,0), 7)2334 self.assertEquals(self.ed.GetNextTabStop(2,0), 0)2335 self.assertEquals(self.ed.GetNextTabStop(3,0), 0)2336 # Another 2 lines before explicit line moves the explicit tab stop2337 data = b"x\ny\n"2338 self.ed.InsertText(4, data)2339 self.assertEquals(self.ed.GetNextTabStop(0,0), 0)2340 self.assertEquals(self.ed.GetNextTabStop(1,0), 0)2341 self.assertEquals(self.ed.GetNextTabStop(2,0), 0)2342 self.assertEquals(self.ed.GetNextTabStop(3,0), 7)2343 self.assertEquals(self.ed.GetNextTabStop(4,0), 0)2344 self.assertEquals(self.ed.GetNextTabStop(5,0), 0)2345 # Undo moves the explicit tab stop back2346 self.ed.Undo()2347 self.assertEquals(self.ed.GetNextTabStop(0,0), 0)2348 self.assertEquals(self.ed.GetNextTabStop(1,0), 7)2349 self.assertEquals(self.ed.GetNextTabStop(2,0), 0)2350 self.assertEquals(self.ed.GetNextTabStop(3,0), 0)2351if __name__ == '__main__':2352 uu = Xite.main("simpleTests")2353 #~ for x in sorted(uu.keys()):2354 #~ print(x, uu[x])...
ed_menu.py
Source:ed_menu.py
...441 for item in KeyBinder.keyprofile.keys():442 itemlst.append(u"%s=%s%s" % (_FindStringRep(item),443 self.GetBinding(item).lstrip(),444 os.linesep))445 writer.writelines(sorted(itemlst))446 writer.close()447 else:448 util.Log("[keybinder][err] Failed to open %s for writing" % ppath)449450 @classmethod451 def SetBinding(cls, item_id, keys):452 """Set the keybinding of a menu id453 @param cls: Class Object454 @param item_id: item to set455 @param keys: string or list of key strings ['Ctrl', 'S']456457 """458 if isinstance(keys, basestring):459 keys = [ key.strip() for key in keys.split(u'+')
...
training.py
Source:training.py
1import os2from math import ceil3from torchtext.data import BucketIterator4from enet.util import run_over_data5def train(model, train_set, dev_set, test_set, optimizer_constructor, epochs, tester, parser, other_testsets):6 # build batch on cpu7 train_iter = BucketIterator(train_set, batch_size=parser.batch, train=False, shuffle=True, device=-1,8 sort_key=lambda x: len(x.POSTAGS))9 dev_iter = BucketIterator(dev_set, batch_size=parser.batch, train=False, shuffle=True, device=-1,10 sort_key=lambda x: len(x.POSTAGS))11 test_iter = BucketIterator(test_set, batch_size=parser.batch, train=False, shuffle=True, device=-1,12 sort_key=lambda x: len(x.POSTAGS))13 scores = 0.014 now_bad = 015 restart_used = 016 print("\nStarting training...\n")17 lr = parser.lr18 optimizer = optimizer_constructor(lr=lr)19 for i in range(epochs):20 # Training Phrase21 print("Epoch", i + 1)22 training_loss, training_ed_p, training_ed_r, training_ed_f1, \23 training_ae_p, training_ae_r, training_ae_f1 = run_over_data(data_iter=train_iter,24 optimizer=optimizer,25 model=model,26 need_backward=True,27 MAX_STEP=ceil(len(train_set) / parser.batch),28 tester=tester,29 hyps=model.hyperparams,30 device=model.device,31 maxnorm=parser.maxnorm,32 word_i2s=parser.word_i2s,33 label_i2s=parser.label_i2s,34 role_i2s=parser.role_i2s,35 weight=parser.label_weight,36 save_output=os.path.join(parser.out,37 "training_epoch_%d.txt" % (38 i + 1)))39 print("\nEpoch", i + 1, " training loss: ", training_loss,40 "\ntraining ed p: ", training_ed_p,41 " training ed r: ", training_ed_r,42 " training ed f1: ", training_ed_f1,43 "\ntraining ae p: ", training_ae_p,44 " training ae r: ", training_ae_r,45 " training ae f1: ", training_ae_f1)46 parser.writer.add_scalar('train/loss', training_loss, i)47 parser.writer.add_scalar('train/ed/p', training_ed_p, i)48 parser.writer.add_scalar('train/ed/r', training_ed_r, i)49 parser.writer.add_scalar('train/ed/f1', training_ed_f1, i)50 parser.writer.add_scalar('train/ae/p', training_ae_p, i)51 parser.writer.add_scalar('train/ae/r', training_ae_r, i)52 parser.writer.add_scalar('train/ae/f1', training_ae_f1, i)53 # Validation Phrase54 dev_loss, dev_ed_p, dev_ed_r, dev_ed_f1, \55 dev_ae_p, dev_ae_r, dev_ae_f1 = run_over_data(data_iter=dev_iter,56 optimizer=optimizer,57 model=model,58 need_backward=False,59 MAX_STEP=ceil(len(dev_set) / parser.batch),60 tester=tester,61 hyps=model.hyperparams,62 device=model.device,63 maxnorm=parser.maxnorm,64 word_i2s=parser.word_i2s,65 label_i2s=parser.label_i2s,66 role_i2s=parser.role_i2s,67 weight=parser.label_weight,68 save_output=os.path.join(parser.out,69 "dev_epoch_%d.txt" % (70 i + 1)))71 print("\nEpoch", i + 1, " dev loss: ", dev_loss,72 "\ndev ed p: ", dev_ed_p,73 " dev ed r: ", dev_ed_r,74 " dev ed f1: ", dev_ed_f1,75 "\ndev ae p: ", dev_ae_p,76 " dev ae r: ", dev_ae_r,77 " dev ae f1: ", dev_ae_f1)78 parser.writer.add_scalar('dev/loss', dev_loss, i)79 parser.writer.add_scalar('dev/ed/p', dev_ed_p, i)80 parser.writer.add_scalar('dev/ed/r', dev_ed_r, i)81 parser.writer.add_scalar('dev/ed/f1', dev_ed_f1, i)82 parser.writer.add_scalar('dev/ae/p', dev_ae_p, i)83 parser.writer.add_scalar('dev/ae/r', dev_ae_r, i)84 parser.writer.add_scalar('dev/ae/f1', dev_ae_f1, i)85 # Testing Phrase86 test_loss, test_ed_p, test_ed_r, test_ed_f1, \87 test_ae_p, test_ae_r, test_ae_f1 = run_over_data(data_iter=test_iter,88 optimizer=optimizer,89 model=model,90 need_backward=False,91 MAX_STEP=ceil(len(test_set) / parser.batch),92 tester=tester,93 hyps=model.hyperparams,94 device=model.device,95 maxnorm=parser.maxnorm,96 word_i2s=parser.word_i2s,97 label_i2s=parser.label_i2s,98 role_i2s=parser.role_i2s,99 weight=parser.label_weight,100 save_output=os.path.join(parser.out,101 "test_epoch_%d.txt" % (102 i + 1)))103 print("\nEpoch", i + 1, " test loss: ", test_loss,104 "\ntest ed p: ", test_ed_p,105 " test ed r: ", test_ed_r,106 " test ed f1: ", test_ed_f1,107 "\ntest ae p: ", test_ae_p,108 " test ae r: ", test_ae_r,109 " test ae f1: ", test_ae_f1)110 parser.writer.add_scalar('test/loss', test_loss, i)111 parser.writer.add_scalar('test/ed/p', test_ed_p, i)112 parser.writer.add_scalar('test/ed/r', test_ed_r, i)113 parser.writer.add_scalar('test/ed/f1', test_ed_f1, i)114 parser.writer.add_scalar('test/ae/p', test_ae_p, i)115 parser.writer.add_scalar('test/ae/r', test_ae_r, i)116 parser.writer.add_scalar('test/ae/f1', test_ae_f1, i)117 # Early Stop118 if scores <= dev_ed_f1 + dev_ae_f1:119 scores = dev_ed_f1 + dev_ae_f1120 # Move model parameters to CPU121 model.save_model(os.path.join(parser.out, "model.pt"))122 print("Save CPU model at Epoch", i + 1)123 now_bad = 0124 else:125 now_bad += 1126 if now_bad >= parser.earlystop:127 if restart_used >= parser.restart:128 print("Restart opportunity are run out")129 break130 restart_used += 1131 print("lr decays and best model is reloaded")132 lr = lr * 0.1133 model.load_model(os.path.join(parser.out, "model.pt"))134 optimizer = optimizer_constructor(lr=lr)135 print("Restart in Epoch %d" % (i + 2))136 now_bad = 0137 # Testing Phrase138 test_loss, test_ed_p, test_ed_r, test_ed_f1, \139 test_ae_p, test_ae_r, test_ae_f1 = run_over_data(data_iter=test_iter,140 optimizer=optimizer,141 model=model,142 need_backward=False,143 MAX_STEP=ceil(len(test_set) / parser.batch),144 tester=tester,145 hyps=model.hyperparams,146 device=model.device,147 maxnorm=parser.maxnorm,148 word_i2s=parser.word_i2s,149 label_i2s=parser.label_i2s,150 role_i2s=parser.role_i2s,151 weight=parser.label_weight,152 save_output=os.path.join(parser.out, "test_final.txt"))153 print("\nFinally test loss: ", test_loss,154 "\ntest ed p: ", test_ed_p,155 " test ed r: ", test_ed_r,156 " test ed f1: ", test_ed_f1,157 "\ntest ae p: ", test_ae_p,158 " test ae r: ", test_ae_r,159 " test ae f1: ", test_ae_f1)160 for name, additional_test_set in other_testsets.items():161 additional_test_iter = BucketIterator(additional_test_set, batch_size=parser.batch, train=False, shuffle=True,162 device=-1,163 sort_key=lambda x: len(x.POSTAGS))164 additional_test_loss, additional_test_ed_p, additional_test_ed_r, additional_test_ed_f1, \165 additional_test_ae_p, additional_test_ae_r, additional_test_ae_f1 = run_over_data(166 data_iter=additional_test_iter,167 optimizer=optimizer,168 model=model,169 need_backward=False,170 MAX_STEP=ceil(len(additional_test_set) / parser.batch),171 tester=tester,172 hyps=model.hyperparams,173 device=model.device,174 maxnorm=parser.maxnorm,175 word_i2s=parser.word_i2s,176 label_i2s=parser.label_i2s,177 role_i2s=parser.role_i2s,178 weight=parser.label_weight,179 save_output=os.path.join(parser.out, "%s.txt") % (name))180 print("\nFor ", name, ", additional test loss: ", additional_test_loss,181 " additional ed test p: ", additional_test_ed_p,182 " additional ed test r: ", additional_test_ed_r,183 " additional ed test f1: ", additional_test_ed_f1,184 " additional ae test p: ", additional_test_ae_p,185 " additional ae test r: ", additional_test_ae_r,186 " additional ae test f1: ", additional_test_ae_f1)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!