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
88
89
90
91
92
93
94
95
96
97
98
99
|
# -*- coding: utf-8 -*-
"""
Tests for other lexers
~~~~~~~~~~~~~~~~~~~~~~
:copyright: Copyright 2006-2015 by the Pygments team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
import glob
import os
import unittest
from pygments.lexers import guess_lexer
from pygments.lexers.scripting import EasytrieveLexer, JclLexer, RexxLexer
def _exampleFilePath(filename):
return os.path.join(os.path.dirname(__file__), 'examplefiles', filename)
_MAX_LENGTH = 80
def safe_repr(obj, short=False):
try:
result = repr(obj)
except Exception:
result = object.__repr__(obj)
if not short or len(result) < _MAX_LENGTH:
return result
return result[:_MAX_LENGTH] + ' [truncated]...'
class MyTestCase(unittest.TestCase):
### Assert less is 2.7+ only.
def assertLess(self, a, b, msg=None):
"""Just like self.assertTrue(a < b), but with a nicer default message."""
if not a < b:
standardMsg = '%s not less than %s' % (safe_repr(a), safe_repr(b))
self.fail(self._formatMessage(msg, standardMsg))
class AnalyseTextTest(unittest.TestCase):
def _testCanRecognizeAndGuessExampleFiles(self, lexer):
assert lexer is not None
for pattern in lexer.filenames:
exampleFilesPattern = _exampleFilePath(pattern)
for exampleFilePath in glob.glob(exampleFilesPattern):
with open(exampleFilePath, 'rb') as fp:
text = fp.read().decode('utf-8')
probability = lexer.analyse_text(text)
self.assertTrue(probability > 0,
'%s must recognize %r' % (
lexer.name, exampleFilePath))
guessedLexer = guess_lexer(text)
self.assertEqual(guessedLexer.name, lexer.name)
def testCanRecognizeAndGuessExampleFiles(self):
LEXERS_TO_TEST = [
EasytrieveLexer,
JclLexer,
RexxLexer,
]
for lexerToTest in LEXERS_TO_TEST:
self._testCanRecognizeAndGuessExampleFiles(lexerToTest)
class EasyTrieveLexerTest(MyTestCase):
def testCanGuessFromText(self):
self.assertLess(0, EasytrieveLexer.analyse_text('MACRO'))
self.assertLess(0, EasytrieveLexer.analyse_text('\nMACRO'))
self.assertLess(0, EasytrieveLexer.analyse_text(' \nMACRO'))
self.assertLess(0, EasytrieveLexer.analyse_text(' \n MACRO'))
self.assertLess(0, EasytrieveLexer.analyse_text('*\nMACRO'))
self.assertLess(0, EasytrieveLexer.analyse_text(
'*\n *\n\n \n*\n MACRO'))
class RexxLexerTest(unittest.TestCase):
def testCanGuessFromText(self):
self.assertAlmostEqual(0.01,
RexxLexer.analyse_text('/* */'))
self.assertAlmostEqual(1.0,
RexxLexer.analyse_text('''/* Rexx */
say "hello world"'''))
val = RexxLexer.analyse_text('/* */\n'
'hello:pRoceduRe\n'
' say "hello world"')
self.assertTrue(val > 0.5, val)
val = RexxLexer.analyse_text('''/* */
if 1 > 0 then do
say "ok"
end
else do
say "huh?"
end''')
self.assertTrue(val > 0.2, val)
val = RexxLexer.analyse_text('''/* */
greeting = "hello world!"
parse value greeting "hello" name "!"
say name''')
self.assertTrue(val > 0.2, val)
|