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
|
#!/usr/bin/env python
# **********************************************************************
#
# Copyright (c) 2003-2013 ZeroC, Inc. All rights reserved.
#
# This copy of Ice is licensed to you under the terms described in the
# ICE_LICENSE file included in this distribution.
#
# **********************************************************************
import sys
import Expect
def dequote(s):
cur = 0
end = len(s)
havequote = False
quote = None
items = []
while cur < len(s):
if not quote:
if s[cur] == "'" or s[cur] == '"':
quote = s[cur]
start = cur+1
else:
if s[cur] == quote:
items.append(s[start:cur])
quote = None
cur = cur + 1
return items
def mkregexp(s):
s = s.replace('(', '\\(')
s = s.replace(')', '\\)')
s = s.replace('.', '\\.')
s = s.replace('+', '\\+')
s = s.replace('*', '\\*')
return s
def run(client, server):
sys.stdout.write("populating database... ")
sys.stdout.flush()
f = open("books", "r")
books = []
for l in f:
client.sendline(l)
client.expect('added new book')
isbn, title, author = dequote(l)
books.append((isbn, title, author))
print("ok")
byauthor = {}
for b in books:
isbn, title, author = b
if author not in byauthor:
byauthor[author] = []
byauthor[author].append(b)
sys.stdout.write("testing isbn... ")
sys.stdout.flush()
for b in books:
isbn, title, author = b
client.sendline('isbn %s' %(isbn))
client.expect('current book is:')
client.expect('isbn: %s' %(isbn))
client.expect('title: %s' %(mkregexp(title)))
client.expect('authors: %s' %(author))
client.sendline('isbn 1000')
client.expect('no book with that')
print("ok")
sys.stdout.write("testing authors... ")
sys.stdout.flush()
for a, bl in byauthor.items():
client.sendline('authors "%s"' %(a))
client.expect('number of books found: ([0-9]+)')
n = int(client.match.group(1))
assert len(bl) == n
for i in range(0, n):
client.expect('current book is:')
client.expect('isbn: ([a-zA-Z0-9]+)\n')
findisbn = client.match.group(1)
nbl = []
for b in bl:
isbn, title, author = b
if not findisbn:
nbl.append(b)
if isbn == findisbn:
findisbn = None
client.expect('title: %s' %(mkregexp(title)))
client.expect('authors: %s' %(author))
bl = nbl
assert not findisbn
client.sendline('next')
client.expect('no current book')
assert len(bl) == 0
client.sendline('authors foo')
client.expect('number of books found: 0')
client.expect('no current book')
print("ok")
sys.stdout.write("testing rent/return... ")
sys.stdout.flush()
isbn, title, author = books[0]
client.sendline('isbn %s' % (isbn))
client.expect('current book is:.*isbn.*\ntitle.*\nauthors')
client.sendline('rent matthew')
client.expect("the book is now rented by `matthew'")
client.sendline('current')
client.expect('rented: matthew')
client.sendline('rent john')
client.expect('the book has already been rented')
client.sendline('return')
client.expect('the book has been returned')
client.sendline('current')
try:
client.expect('rented:', timeout=2)
except Expect.TIMEOUT:
pass
print("ok")
sys.stdout.write("testing remove... ")
sys.stdout.flush()
isbn, title, author = books[0]
client.sendline('isbn %s' % (isbn))
client.expect('current book is:.*isbn.*\ntitle.*\nauthors')
client.sendline('remove')
client.expect('removed current book')
client.sendline('remove')
client.expect('current book no longer exists')
client.sendline('shutdown')
if client != server:
server.waitTestSuccess()
client.sendline('exit')
client.waitTestSuccess()
print("ok")
|