~ubuntu-branches/ubuntu/precise/enigmail/precise-security

« back to all changes in this revision

Viewing changes to config/tests/unit-Preprocessor.py

  • Committer: Package Import Robot
  • Author(s): Chris Coulson
  • Date: 2012-11-12 16:36:01 UTC
  • mfrom: (0.12.15)
  • Revision ID: package-import@ubuntu.com-20121112163601-t8e8skdfi3ni9iqp
Tags: 2:1.4.6-0ubuntu0.12.04.1
* New upstream release v1.4.6
  - see LP: #1080212 for USN information
* Drop unneeded patches
  - remove debian/patches/correct-version-number.diff
  - remove debian/patches/dont_register_cids_multiple_times.diff
  - update debian/patches/series
* Support building in an objdir
  - update debian/rules

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from __future__ import with_statement
1
2
import unittest
2
3
 
3
4
from StringIO import StringIO
4
5
import os
5
6
import sys
6
7
import os.path
7
 
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
 
8
from mozunit import main, MockedOpen
8
9
 
9
10
from Preprocessor import Preprocessor
10
11
 
11
 
class NamedIO(StringIO):
12
 
  def __init__(self, name, content):
13
 
    self.name = name
14
 
    StringIO.__init__(self, content)
 
12
def NamedIO(name, content):
 
13
  with open(name, 'w') as f:
 
14
    f.write(content)
 
15
  return name
15
16
 
16
17
class TestPreprocessor(unittest.TestCase):
17
18
  """
32
33
    self.pp.do_include(f)
33
34
    self.assertEqual(self.pp.out.getvalue(), "PASS\n")
34
35
 
 
36
  def test_no_marker(self):
 
37
    no_marker = """#if 0
 
38
PASS
 
39
#endif
 
40
"""
 
41
    f = NamedIO("no_marker.in", no_marker)
 
42
    self.pp.setMarker(None)
 
43
    self.pp.do_include(f)
 
44
    self.assertEqual(self.pp.out.getvalue(), no_marker)
 
45
 
35
46
  def test_string_value(self):
36
47
    f = NamedIO("string_value.in", """#define FOO STRING
37
48
#if FOO
171
182
  
172
183
  def test_filter_attemptSubstitution(self):
173
184
    f = NamedIO('filter_attemptSubstitution.in', '''#filter attemptSubstitution
174
 
P@VAR@ASS
 
185
@PASS@
175
186
#unfilter attemptSubstitution
176
187
''')
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")
179
190
  
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")
406
417
 
 
418
  def test_filterDefine(self):
 
419
    f = NamedIO('filterDefine.in', '''#filter substitution
 
420
#define VAR AS
 
421
#define VAR2 P@VAR@
 
422
@VAR2@S
 
423
''')
 
424
    self.pp.do_include(f)
 
425
    self.assertEqual(self.pp.out.getvalue(), "PASS\n")
 
426
 
407
427
  def test_number_value_equals(self):
408
428
    f = NamedIO("number_value_equals.in", """#define FOO 1000
409
429
#if FOO == 1000
490
510
    self.pp.do_include(f)
491
511
    self.assertEqual(self.pp.out.getvalue(), "octal value is not equal\n")
492
512
 
 
513
  def test_undefined_variable(self):
 
514
    f = NamedIO("undefined_variable.in", """#filter substitution
 
515
@foo@
 
516
""")
 
517
    try:
 
518
      self.pp.do_include(f)
 
519
    except Preprocessor.Error, exception:
 
520
      self.assertEqual(exception.key, 'UNDEFINED_VAR')
 
521
    else:
 
522
      self.fail("Expected a Preprocessor.Error")
 
523
 
 
524
  def test_include(self):
 
525
    with MockedOpen({"foo/test": """#define foo foobarbaz
 
526
#include @inc@
 
527
@bar@
 
528
""",
 
529
                     "bar": """#define bar barfoobaz
 
530
@foo@
 
531
"""}):
 
532
      f = NamedIO("include.in", """#filter substitution
 
533
#define inc ../bar
 
534
#include foo/test""")
 
535
      self.pp.do_include(f)
 
536
      self.assertEqual(self.pp.out.getvalue(), """foobarbaz
 
537
barfoobaz
 
538
""")
 
539
 
 
540
  def test_include_missing_file(self):
 
541
    f = NamedIO("include_missing_file.in", "#include foo")
 
542
    try:
 
543
      self.pp.do_include(f)
 
544
    except Preprocessor.Error, exception:
 
545
      self.assertEqual(exception.key, 'FILE_NOT_FOUND')
 
546
    else:
 
547
      self.fail("Expected a Preprocessor.Error")
 
548
 
 
549
  def test_include_undefined_variable(self):
 
550
    f = NamedIO("include_undefined_variable.in", """#filter substitution
 
551
#include @foo@
 
552
""")
 
553
    try:
 
554
      self.pp.do_include(f)
 
555
    except Preprocessor.Error, exception:
 
556
      self.assertEqual(exception.key, 'UNDEFINED_VAR')
 
557
    else:
 
558
      self.fail("Expected a Preprocessor.Error")
 
559
 
 
560
  def test_include_literal_at(self):
 
561
    with MockedOpen({"@foo@": "#define foo foobarbaz"}):
 
562
      f = NamedIO("include_literal_at.in", """#include @foo@
 
563
#filter substitution
 
564
@foo@
 
565
""")
 
566
      self.pp.do_include(f)
 
567
      self.assertEqual(self.pp.out.getvalue(), """foobarbaz
 
568
""")
 
569
 
 
570
  def test_command_line_literal_at(self):
 
571
    with MockedOpen({"@foo@.in": """@foo@
 
572
"""}):
 
573
      self.pp.handleCommandLine(['-Fsubstitution', '-Dfoo=foobarbaz', '@foo@.in'])
 
574
      self.assertEqual(self.pp.out.getvalue(), """foobarbaz
 
575
""")
 
576
 
493
577
if __name__ == '__main__':
494
 
  unittest.main()
 
578
  main()