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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
#!/usr/bin/env python
import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
import Xlib.rdb
import unittest
resources = """
! Single component
single: name
Single: class
?: wild
! name vs class vs ?
p.first.second: n.n
p.first.Second: n.c
p.first.?: n.w
p.First.second: c.n
p.First.Second: c.c
p.First.?: c.w
p.?.second: w.n
p.?.Second: w.c
p.?.?: w.w
! Tight over loose bindings
b.tight.match: tight
b.tight*match: bad
b.loose*match: loose
! skip matches
s.*end: default
s.foo*end: default foo
s.foo.bar.end: bar foo
! Multiple skip matches
ss.*mid*end: default
ss.foo*mid*end: default foo
ss.foo*mid.bar*end: bar foo
! First component unbound
*fie.fum: skipfirst
fie.fum: matchtwo
feh.fie.fum: matchfirst
"""
queries = (
# Single component
('single', 'Single', 'name'),
('noname', 'Single', 'class'),
('noname', 'Noclass', 'wild'),
# Name vs class vs ?
('p.first.second', 'P.First.Second', 'n.n'),
('p.first.noname', 'P.First.Second', 'n.c'),
('p.first.noname', 'P.First.Noclass', 'n.w'),
('p.noname.second', 'P.First.Second', 'c.n'),
('p.noname.noname', 'P.First.Second', 'c.c'),
('p.noname.noname', 'P.First.Noclass', 'c.w'),
('p.noname.second', 'P.Noclass.Second', 'w.n'),
('p.noname.noname', 'P.Noclass.Second', 'w.c'),
('p.noname.noname', 'P.Noclass.Noclass', 'w.w'),
# Tight over loose bindings
('b.tight.match', 'B.Tight.Match', 'tight'),
('b.loose.match', 'B.Loose.Match', 'loose'),
# skip matches
('s.bar.end', 'S.Bar.End', 'default'),
('s.foo.bar.end', 'S.Foo.Bar.End', 'bar foo'),
('s.foo.gazonk.end', 'S.Foo.Gazonk.End', 'default foo'),
# Multiple skip matches
('ss.x.mid.x.end', 'Ss.X.Mid.X.End', 'default'),
('ss.foo.x.mid.x.end', 'Ss.Foo.X.Mid.X.End', 'default foo'),
('ss.foo.x.mid.bar.x.end', 'Ss.Foo.X.Mid.Bar.X.End', 'bar foo'),
('ss.foo.mid.x.mid.bar.x.end', 'Ss.Foo.Mid.X.Mid.Bar.X.End', 'default foo'),
('ss.foo.x.mid.x.mid.bar.x.end', 'Ss.Foo.X.Mid.X.Mid.Bar.X.End', 'default foo'),
# First component unbound
('fie.fum', 'Fie.Fum', 'matchtwo'),
('noname.fie.fum', 'Noclass.Fie.Fum', 'skipfirst'),
('feh.fie.fum', 'Feh.Fie.Fum', 'matchfirst'),
)
resource_set1 = (
('foo.bar', 1,),
('foo.bar.gazonk', 2),
('*bar*gazonk', 3),
)
resource_set2 = (
('foo.bar', 10,), # Changing the value of an item
('foo.bar.whee', 11), # Adding entries to existing component
('foo.bar*whoho', 12),
('foo.fie', 13), # Copy new resources
('foo.fie*fum', 14),
('*foo.bar', 15),
)
class TestRDB(unittest.TestCase):
def testParseAndQuery(self):
# Test string parsing and querying
db = Xlib.rdb.ResourceDB(string = resources)
for name, cls, value in queries:
try:
v = db[name, cls]
except KeyError:
raise AssertionError('Value not found for %s/%s:\n expected %s' % (name, cls, value))
if v != value:
raise AssertionError('Value mismatch for %s/%s:\n expected %s, got %s' % (name, cls, value, v))
def testUpdate(self):
# Test update. An update should have the same result as
# inserting all the resource entries in the manually
db1 = Xlib.rdb.ResourceDB()
db2 = Xlib.rdb.ResourceDB()
db3 = Xlib.rdb.ResourceDB()
db1.insert_resources(resource_set1)
db2.insert_resources(resource_set2)
db1.update(db2)
db3.insert_resources(resource_set1)
db3.insert_resources(resource_set2)
assert db1.db == db3.db
if __name__ == '__main__':
unittest.main()
|