File: test_filter_tmpl.py

package info (click to toggle)
planet-venus 0~git9de2109-4.2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,884 kB
  • sloc: python: 4,393; xml: 871; makefile: 39; sed: 3; sh: 2
file content (67 lines) | stat: -rw-r--r-- 2,516 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/env python

import unittest, os, sys, glob, new, re, StringIO, time
from planet import config
from planet.shell import tmpl

testfiles = 'tests/data/filter/tmpl/%s.%s'

class FilterTmplTest(unittest.TestCase):
    desc_feed_re = re.compile("Description:\s*(.*?)\s*Expect:\s*(.*)\s*-->")
    desc_config_re = re.compile(";\s*Description:\s*(.*?)\s*;\s*Expect:\s*(.*)")
    simple_re = re.compile("^(\S+) == (u?'[^']*'|\([0-9, ]+\))$")

    def eval_feed(self, name):
        # read the test case
        try:
            testcase = open(testfiles % (name,'xml'))
            data = testcase.read()
            description, expect = self.desc_feed_re.search(data).groups()
            testcase.close()
        except:
            raise RuntimeError, "can't parse %s" % name

        # map to template info
        results = tmpl.template_info(data)

        # verify the results
        if not self.simple_re.match(expect):
            self.assertTrue(eval(expect, results), expect)
        else:
            lhs, rhs = self.simple_re.match(expect).groups()
            self.assertEqual(eval(rhs), eval(lhs, results))

    def eval_config(self, name):
        # read the test case
        try:
            testcase = open(testfiles % (name,'ini'))
            data = testcase.read()
            description, expect = self.desc_config_re.search(data).groups()
            testcase.close()
        except:
            raise RuntimeError, "can't parse %s" % name

        # map to template info
        config.load(testfiles % (name,'ini'))
        results = tmpl.template_info("<feed/>")

        # verify the results
        if not self.simple_re.match(expect):
            self.assertTrue(eval(expect, results), expect)
        else:
            lhs, rhs = self.simple_re.match(expect).groups()
            self.assertEqual(eval(rhs), eval(lhs, results))

# build a test method for each xml test file
for testcase in glob.glob(testfiles % ('*','xml')):
    root = os.path.splitext(os.path.basename(testcase))[0]
    func = lambda self, name=root: self.eval_feed(name)
    method = new.instancemethod(func, None, FilterTmplTest)
    setattr(FilterTmplTest, "test_" + root, method)

# build a test method for each ini test file
for testcase in glob.glob(testfiles % ('*','ini')):
    root = os.path.splitext(os.path.basename(testcase))[0]
    func = lambda self, name=root: self.eval_config(name)
    method = new.instancemethod(func, None, FilterTmplTest)
    setattr(FilterTmplTest, "test_" + root, method)