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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
|
#pylint: disable=too-many-public-methods,import-error,too-few-public-methods,missing-docstring,unused-variable
import unittest
from gleetex.typesetting import LaTeXDocument
import gleetex.typesetting as typesetting
class test_typesetting(unittest.TestCase):
def test_formula_is_embedded(self):
formula = 'E = m \\cdot c^2'
doc = LaTeXDocument(formula)
self.assertTrue(formula in str(doc),
"formula must be contained in LaTeX typesetting as it was inserted.")
def test_if_displaymath_unset_correct_env_used(self):
doc = LaTeXDocument(r'A = \pi r^2')
doc.set_displaymath(False)
self.assertTrue('\\(' in str(doc))
self.assertTrue('\\)' in str(doc))
def test_if_displaymath_is_set_correct_env_used(self):
doc = LaTeXDocument(r'A = \pi r^2')
doc.set_displaymath(True)
self.assertTrue('\\[' in str(doc))
self.assertTrue('\\]' in str(doc))
def test_preamble_is_included(self):
preamble = '\\usepackage{eurosym}'
doc = LaTeXDocument('moooo')
doc.set_preamble_string(preamble)
self.assertTrue(preamble in str(doc))
def test_obviously_wrong_encoding_trigger_exception(self):
doc = LaTeXDocument('f00')
self.assertRaises(ValueError, doc.set_encoding, 'latin1:')
self.assertRaises(ValueError, doc.set_encoding, 'utf66')
# the following passes (assertRaisesNot)
doc.set_encoding('utf-8')
def test_that_latex_maths_env_is_used(self):
doc = LaTeXDocument('f00')
doc.set_latex_environment('flalign*')
self.assertTrue(r'\begin{flalign*}' in str(doc))
self.assertTrue(r'\end{flalign*}' in str(doc))
################################################################################
class test_replace_unicode_characters(unittest.TestCase):
def test_that_ascii_strings_are_returned_verbatim(self):
for string in ['abc.\\', '`~[]}{:<>']:
textmode = typesetting.replace_unicode_characters(string, False)
self.assertEqual(textmode, string, 'expected %s, got %s' % (string, textmode))
mathmode = typesetting.replace_unicode_characters(string, True)
self.assertEqual(textmode, string, 'expected %s, got %s' % (string, mathmode))
def test_that_alphabetical_characters_are_replaced_by_default(self):
textmode = typesetting.replace_unicode_characters('ö', False)
self.assertTrue('\\"' in textmode)
mathmode = typesetting.replace_unicode_characters('ö', True)
self.assertTrue('\\ddot' in mathmode)
def test_that_alphabetical_characters_are_kept_in_text_mode_if_specified(self):
self.assertEqual(typesetting.replace_unicode_characters('ö', False, # text mode
replace_alphabeticals=False), 'ö')
self.assertEqual(typesetting.replace_unicode_characters('æ', False,
replace_alphabeticals=False), 'æ')
def test_that_alphanumericals_are_replaced_in_mathmode_even_if_replace_alphabeticals_set(self):
self.assertNotEqual(typesetting.replace_unicode_characters('öäü', True,
replace_alphabeticals=True), 'öäü')
self.assertNotEqual(typesetting.replace_unicode_characters('æø', True,
replace_alphabeticals=True), 'æø')
def test_that_charachters_not_present_in_file_raise_exception(self):
with self.assertRaises(ValueError):
typesetting.replace_unicode_characters('€', True)
def test_that_formulas_are_replaced(self):
self.assertNotEqual(typesetting.replace_unicode_characters('π', True),
'π')
self.assertNotEqual(typesetting.replace_unicode_characters('π', False),
'π')
class test_get_matching_brace(unittest.TestCase):
def test_closing_brace_found_when_only_one_brace_present(self):
text = 'text{ok}'
self.assertEqual(typesetting.get_matching_brace(text, 4), len(text) - 1)
self.assertEqual(typesetting.get_matching_brace(text + 'foo', 4), len(text) - 1)
def test_outer_brace_found(self):
text = 'text{o, bla\\"{o}dfdx.}ds'
self.assertEqual(typesetting.get_matching_brace(text, 4), len(text)-3)
def test_inner_brace_is_matched(self):
text = 'text{o, bla\\"{o}dfdx.}ds'
self.assertEqual(typesetting.get_matching_brace(text, 13), 15)
def test_that_unmatched_braces_raise_exception(self):
with self.assertRaises(ValueError):
typesetting.get_matching_brace('text{foooooooo', 4)
with self.assertRaises(ValueError):
typesetting.get_matching_brace('text{jo\"{o....}', 4)
def test_wrong_position_for_opening_brace_raises(self):
with self.assertRaises(ValueError):
typesetting.get_matching_brace('moo', 1)
class test_escape_unicode_maths(unittest.TestCase):
"""These tests assume that the tests written above work!"""
def test_that_mathmode_and_textmode_are_treated_differently(self):
math = typesetting.escape_unicode_maths('ö')
self.assertNotEqual(math, 'ö')
text = typesetting.escape_unicode_maths('\\text{ö}')
self.assertFalse('ö' in text)
# check whether characters got transcribed differently; it's enough to
# check one character of the generated sequence, they should differ
self.assertNotEqual(math[:2], text[6:8])
def test_that_flag_to_preserve_alphas_is_passed_through(self):
res = typesetting.escape_unicode_maths('\\text{ö}',
replace_alphabeticals=False)
self.assertEqual(res, '\\text{ö}')
def test_that_all_characters_are_preserved_when_no_replacements_happen(self):
text = 'This is a \\text{test} mate.'
self.assertEqual(typesetting.escape_unicode_maths(text), text)
self.assertEqual(typesetting.escape_unicode_maths(text,
replace_alphabeticals=False), text)
text = 'But yeah but no' * 20 + ', oh my god!'
self.assertEqual(typesetting.escape_unicode_maths(text), text)
self.assertEqual(typesetting.escape_unicode_maths(text,
replace_alphabeticals=False), text)
def test_that_everything_around_surrounded_character_is_preserved(self):
text = 'This is a \\text{über} test. ;)'
result = typesetting.escape_unicode_maths(text,
replace_alphabeticals=True)
ue_pos = text.index('ü')
# text in front is unchanged
self.assertEqual(result[:ue_pos], text[:ue_pos])
# find b character, which is the start of the remaining string
b_pos = result[ue_pos:].find('b') + ue_pos
# check that text after umlaut matches
self.assertEqual(result[b_pos:], text[ue_pos+1:])
text = 'But yeah but no' * 20 + ', oh my god!ø'
o_strok_pos = text.index('ø')
res = typesetting.escape_unicode_maths(text)
self.assertEqual(res[:o_strok_pos], text[:o_strok_pos])
def test_that_unknown_unicode_characters_raise_exception(self):
# you know that Santa Clause character? Seriously, if you don't know it,
# you should have a look. LaTeX does indeed not have command for this
# (2016, one never knows)
santa = chr(127877)
with self.assertRaises(typesetting.DocumentSerializationException):
typesetting.escape_unicode_maths(santa)
def test_that_two_text_environments_preserve_all_characters(self):
text = r'a\cdot b \text{equals} b\cdot c} \mbox{ is not equal } u^{v\cdot k}'
self.assertEqual(typesetting.escape_unicode_maths(text), text)
def test_color_names_in_backgroundare_accepted(self):
doc = LaTeXDocument(r'A = \pi r^2')
doc.set_background_color('cyan')
doc = str(doc)
self.assertTrue('pagecolor{cyan}' in doc,
"Expected \\pagecolor in document, got: %s" % doc)
self.assertTrue('\\color' not in doc)
self.assertFalse('definecolor' in doc)
def test_color_names_in_foregroundare_accepted(self):
doc = LaTeXDocument(r'A = \pi r^2')
doc.set_foreground_color('cyan')
doc = str(doc)
self.assertTrue('pagecolor' not in doc,
"Expected \\pagecolor in document, got: %s" % doc)
self.assertTrue('\\color{cyan' in doc,
"expeccted \\color{cyan, got:\n" + doc)
self.assertFalse('definecolor' in doc)
def test_hex_colours_with_leading_0s_work(self):
doc = LaTeXDocument(r'A = \pi r^2')
doc.set_background_color('00FFCC')
doc = str(doc)
self.assertTrue('pagecolor{background}' in doc,
"Expected \\pagecolor in document, got: %s" % doc)
self.assertTrue('definecolor' in doc)
self.assertTrue('00FFCC' in doc)
def test_color_rgb_in_foregroundare_accepted(self):
doc = LaTeXDocument(r'A = \pi r^2')
doc.set_foreground_color('FFAACC')
doc = str(doc)
self.assertTrue('pagecolor{}' not in doc,
"Expected \\pagecolor in document, got: %s" % doc)
self.assertTrue('\\color{foreground' in doc,
"document misses \\color command: %s" % doc)
self.assertTrue('definecolor' in doc)
self.assertTrue('FFAACC' in doc)
def test_color_rgb_in_backgroundare_accepted(self):
doc = LaTeXDocument(r'A = \pi r^2')
doc.set_background_color('FFAACC')
doc = str(doc)
self.assertTrue('pagecolor{background}' in doc,
"Expected \\pagecolor in document, got: %s" % doc)
self.assertTrue('\\color' not in doc)
self.assertTrue('definecolor' in doc)
self.assertTrue('FFAACC' in doc)
def test_no_colors_no_color_definitions(self):
doc = str(LaTeXDocument(r'A = \pi r^2'))
self.assertFalse('pagecolor' in doc)
self.assertFalse('\\color' in doc)
self.assertFalse('definecolor' in doc)
|