forked from FabriceSalvaire/CodeReview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_TextDocumentDiffModel.py
More file actions
87 lines (61 loc) · 3.02 KB
/
test_TextDocumentDiffModel.py
File metadata and controls
87 lines (61 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
####################################################################################################
#
# obj: TextView, link to pair
# replace TextFragment
#
####################################################################################################
import os
import unittest
from pygments.lexers import get_lexer_for_filename
####################################################################################################
from CodeReview.Diff.RawTextDocument import RawTextDocument
from CodeReview.Diff.RawTextDocumentDiff import TwoWayFileDiffFactory
from CodeReview.Diff.SyntaxHighlighter import HighlightedText
from CodeReview.Diff.TextDocumentDiffModel import TextDocumentDiffModelFactory, highlight_document
####################################################################################################
class TestTextDocumentModel(unittest.TestCase):
##############################################
@staticmethod
def _join_data_path(filename):
return os.path.join(os.path.dirname(__file__), 'data', filename)
##############################################
def test(self):
with open(self._join_data_path('test_file1.py')) as f:
text1 = f.read()
with open(self._join_data_path('test_file2.py')) as f:
text2 = f.read()
lexer = get_lexer_for_filename('data/test_file1.py', stripnl=False)
raw_text_document1 = RawTextDocument(text1)
raw_text_document2 = RawTextDocument(text2)
file_diff = TwoWayFileDiffFactory().process(raw_text_document1, raw_text_document2)
document_model1, document_model2 = TextDocumentDiffModelFactory().process(file_diff)
highlighted_text1 = HighlightedText(raw_text_document1, lexer)
print('Document 1:')
self._pretty_print(document_model1)
print('\nHighlighted Document 1:')
highlighted_document1 = highlight_document(document_model1, highlighted_text1)
self._pretty_print(highlighted_document1)
# print '\nDocument 2:'
# self._pretty_print(document_model2)
##############################################
def _pretty_print(self, document_model):
for text_block in document_model:
print('='*100)
print(text_block)
for text_fragment in text_block:
margin = ' '*2
print(margin + '-'*48)
print(margin + ('\n' + margin).join(repr(text_fragment).splitlines()))
if bool(text_fragment):
line = '#'*100
print(line)
print(str(text_fragment).rstrip())
print(line)
####################################################################################################
if __name__ == '__main__':
unittest.main()
####################################################################################################
#
# End
#
####################################################################################################