File: test_gen_map.py

package info (click to toggle)
keymapper 0.5.3-7
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 308 kB
  • ctags: 210
  • sloc: python: 1,611; makefile: 68; sh: 6
file content (75 lines) | stat: -rw-r--r-- 1,779 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
68
69
70
71
72
73
74
75
#!/usr/bin/python

# Copyright (c) 2005 by Matthias Urlichs <smurf@smurf.noris.de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of Version 2 of the GNU General Public License as
# published by the Free Software Foundation. See the file COPYING.txt
# or (on Debian systems) /usr/share/common-licenses/GPL-2 for details.

"""\
This test script grabs a number of interesting key maps from
keymapper.fakemaps, generates a decision tree, saves that in an
in-memory file, and then runs each key map against the file's
interpreter to see if the correct map is returned.
"""

from keymapper.fakemaps import maps
from keymapper.tree import Tree, gen_report
from keymapper.file import FileReporter
from keymapper.graph import GraphReporter
from keymapper.script import Script
from keymapper.fakequery import FakeQuery

import sys
import codecs

sys.stdout = codecs.getwriter("utf-8")(sys.stdout)

import keymapper.tree
keymapper.tree.trace = 1

try:
	from cStringIO import StringIO
except ImportError:
	from StringIO import StringIO

keymaps = []
t = Tree()
for k in maps():
	t.add(k)
	print k.dump()
	keymaps.append(k)
t.gen_tree()

buf = StringIO()
gen_report(t,FileReporter(codecs.getwriter("utf-8")(buf)))

g = codecs.getwriter("latin1")(open("test.dot","w"),"replace")
gen_report(t,GraphReporter(g))

# 'buf' now contains our script.

buf.seek(0,0)
print buf.read(),

err = 0
for k in keymaps:
	buf.seek(0,0)
	print "Testing keymap %s" % (k.dump(),)
	s = Script(codecs.getreader("utf-8")(buf))
	name = s.run(FakeQuery(k))
	if name != k.name:
		print "SCRIPT ERROR: %s != %s" % (name,k.name)
		err += 1
	else:
		print "... OK."
	print

if err:
	print "%d errors found!" % err
	sys.exit(1)
else:
	print "Everything works."
	sys.exit(0)