~ubuntu-branches/ubuntu/natty/miro/natty

« back to all changes in this revision

Viewing changes to lib/test/strippertest.py

  • Committer: Bazaar Package Importer
  • Author(s): Bryce Harrington
  • Date: 2011-01-22 02:46:33 UTC
  • mfrom: (1.4.10 upstream) (1.7.5 experimental)
  • Revision ID: james.westby@ubuntu.com-20110122024633-kjme8u93y2il5nmf
Tags: 3.5.1-1ubuntu1
* Merge from debian.  Remaining ubuntu changes:
  - Use python 2.7 instead of python 2.6
  - Relax dependency on python-dbus to >= 0.83.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
See tv/resources/testdata/stripperdata/ for test files.
 
3
 
 
4
Files ending with ``.in`` are input files.  These should be in utf-8
 
5
coding and the entire file is used as input.
 
6
 
 
7
Files ending with ``.expected`` are expected output files.  These
 
8
files are the repr(...) of the output from HTMLStripper.strip.
 
9
 
 
10
If you need to write new tests, write the test, run the unittest and
 
11
the test will fail--but StripperTest will tell you what the output is.
 
12
You can verify the output, then copy and paste it into a .expected
 
13
file.
 
14
"""
 
15
 
 
16
from StringIO import StringIO
 
17
 
 
18
import os.path
 
19
import os
 
20
from miro.test.framework import MiroTestCase
 
21
from miro import util
 
22
from miro.plat import resources
 
23
import unittest
 
24
 
 
25
class HTMLStripperTest(unittest.TestCase):
 
26
    def test_garbage(self):
 
27
        stripper = util.HTMLStripper()
 
28
 
 
29
        for mem in [(1, ("", [])),
 
30
                    (None, ("", [])),
 
31
                    ({}, ("", []))
 
32
                    ]:
 
33
            self.assertEquals(stripper.strip(mem[0]), mem[1])
 
34
 
 
35
        for mem in [("<html>", ("", [])),
 
36
                    ("<html></html>", ("", []))]:
 
37
            self.assertEquals(stripper.strip(mem[0]), mem[1])
 
38
 
 
39
    def test_simple(self):
 
40
        stripper = util.HTMLStripper()
 
41
 
 
42
        for mem in [("<html", ("<html", [])),
 
43
                    ("<html><html>", ("", [])),
 
44
                    ("</html></html>", ("", [])),
 
45
                    ("<p>foo</p>", ("foo", [])),
 
46
                    ("<p>foo</p><br/>", ("foo", []))
 
47
                    ]:
 
48
            self.assertEquals(stripper.strip(mem[0]), mem[1])
 
49
 
 
50
    def test_stripper_data(self):
 
51
        stripper = util.HTMLStripper()
 
52
 
 
53
        testdir = resources.path(os.path.join("testdata", "stripperdata"))
 
54
        tests = [m for m in os.listdir(testdir) if m.endswith(".in")]
 
55
 
 
56
        for mem in tests:
 
57
            mem = os.path.join(testdir, mem)
 
58
            if not os.path.isfile(mem):
 
59
                continue
 
60
 
 
61
            f = open(mem, "r")
 
62
            input_ = f.read()
 
63
            f.close()
 
64
 
 
65
            input_ = input_.decode("utf-8")
 
66
            output = stripper.strip(input_)
 
67
 
 
68
            expected = os.path.splitext(mem)[0] + ".expected"
 
69
            if not os.path.isfile(expected):
 
70
                self.assertEquals(0, 1, "%s not found." % expected)
 
71
            else:
 
72
                f = open(expected, "r")
 
73
                data = f.read().strip()
 
74
                f.close()
 
75
                self.assertEquals(
 
76
                    repr(output), data, "output: %s" % repr(output))