1
from __future__ import with_statement
3
4
from StringIO import StringIO
7
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
8
from mozunit import main, MockedOpen
9
10
from Preprocessor import Preprocessor
11
class NamedIO(StringIO):
12
def __init__(self, name, content):
14
StringIO.__init__(self, content)
12
def NamedIO(name, content):
13
with open(name, 'w') as f:
16
17
class TestPreprocessor(unittest.TestCase):
32
33
self.pp.do_include(f)
33
34
self.assertEqual(self.pp.out.getvalue(), "PASS\n")
36
def test_no_marker(self):
41
f = NamedIO("no_marker.in", no_marker)
42
self.pp.setMarker(None)
44
self.assertEqual(self.pp.out.getvalue(), no_marker)
35
46
def test_string_value(self):
36
47
f = NamedIO("string_value.in", """#define FOO STRING
172
183
def test_filter_attemptSubstitution(self):
173
184
f = NamedIO('filter_attemptSubstitution.in', '''#filter attemptSubstitution
175
186
#unfilter attemptSubstitution
177
188
self.pp.do_include(f)
178
self.assertEqual(self.pp.out.getvalue(), "PASS\n")
189
self.assertEqual(self.pp.out.getvalue(), "@PASS@\n")
180
191
def test_filter_emptyLines(self):
181
192
f = NamedIO('filter_emptyLines.in', '''lines with a
404
415
self.pp.do_include(f)
405
416
self.assertEqual(self.pp.out.getvalue(), "first\rsecond\r")
418
def test_filterDefine(self):
419
f = NamedIO('filterDefine.in', '''#filter substitution
424
self.pp.do_include(f)
425
self.assertEqual(self.pp.out.getvalue(), "PASS\n")
407
427
def test_number_value_equals(self):
408
428
f = NamedIO("number_value_equals.in", """#define FOO 1000
490
510
self.pp.do_include(f)
491
511
self.assertEqual(self.pp.out.getvalue(), "octal value is not equal\n")
513
def test_undefined_variable(self):
514
f = NamedIO("undefined_variable.in", """#filter substitution
518
self.pp.do_include(f)
519
except Preprocessor.Error, exception:
520
self.assertEqual(exception.key, 'UNDEFINED_VAR')
522
self.fail("Expected a Preprocessor.Error")
524
def test_include(self):
525
with MockedOpen({"foo/test": """#define foo foobarbaz
529
"bar": """#define bar barfoobaz
532
f = NamedIO("include.in", """#filter substitution
534
#include foo/test""")
535
self.pp.do_include(f)
536
self.assertEqual(self.pp.out.getvalue(), """foobarbaz
540
def test_include_missing_file(self):
541
f = NamedIO("include_missing_file.in", "#include foo")
543
self.pp.do_include(f)
544
except Preprocessor.Error, exception:
545
self.assertEqual(exception.key, 'FILE_NOT_FOUND')
547
self.fail("Expected a Preprocessor.Error")
549
def test_include_undefined_variable(self):
550
f = NamedIO("include_undefined_variable.in", """#filter substitution
554
self.pp.do_include(f)
555
except Preprocessor.Error, exception:
556
self.assertEqual(exception.key, 'UNDEFINED_VAR')
558
self.fail("Expected a Preprocessor.Error")
560
def test_include_literal_at(self):
561
with MockedOpen({"@foo@": "#define foo foobarbaz"}):
562
f = NamedIO("include_literal_at.in", """#include @foo@
566
self.pp.do_include(f)
567
self.assertEqual(self.pp.out.getvalue(), """foobarbaz
570
def test_command_line_literal_at(self):
571
with MockedOpen({"@foo@.in": """@foo@
573
self.pp.handleCommandLine(['-Fsubstitution', '-Dfoo=foobarbaz', '@foo@.in'])
574
self.assertEqual(self.pp.out.getvalue(), """foobarbaz
493
577
if __name__ == '__main__':