Mercurial > pylearn
annotate _test_lookup_list.py @ 280:b39327d5506f
passed test_lookup_list.py to unittest
author | Frederic Bastien <bastienf@iro.umontreal.ca> |
---|---|
date | Fri, 06 Jun 2008 13:34:25 -0400 |
parents | test_lookup_list.py@323909110d1c |
children | 29f394813c3c |
rev | line source |
---|---|
275
323909110d1c
added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
1 from lookup_list import * |
280
b39327d5506f
passed test_lookup_list.py to unittest
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
275
diff
changeset
|
2 import unittest |
275
323909110d1c
added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
3 def have_raised(to_eval, **var): |
323909110d1c
added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
4 have_thrown = False |
323909110d1c
added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
5 try: |
323909110d1c
added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
6 eval(to_eval) |
323909110d1c
added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
7 except : |
323909110d1c
added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
8 have_thrown = True |
323909110d1c
added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
9 return have_thrown |
323909110d1c
added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
10 |
323909110d1c
added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
11 def have_raised2(f, *args, **kwargs): |
323909110d1c
added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
12 have_thrown = False |
323909110d1c
added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
13 try: |
323909110d1c
added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
14 f(*args, **kwargs) |
323909110d1c
added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
15 except : |
323909110d1c
added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
16 have_thrown = True |
323909110d1c
added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
17 return have_thrown |
323909110d1c
added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
18 |
280
b39327d5506f
passed test_lookup_list.py to unittest
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
275
diff
changeset
|
19 class T_LookUpList(unittest.TestCase): |
b39327d5506f
passed test_lookup_list.py to unittest
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
275
diff
changeset
|
20 def test_LookupList(self): |
b39327d5506f
passed test_lookup_list.py to unittest
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
275
diff
changeset
|
21 #test only the example in the doc??? |
b39327d5506f
passed test_lookup_list.py to unittest
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
275
diff
changeset
|
22 example = LookupList(['x','y','z'],[1,2,3]) |
b39327d5506f
passed test_lookup_list.py to unittest
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
275
diff
changeset
|
23 example['x'] = [1, 2, 3] # set or change a field |
b39327d5506f
passed test_lookup_list.py to unittest
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
275
diff
changeset
|
24 x, y, z = example |
b39327d5506f
passed test_lookup_list.py to unittest
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
275
diff
changeset
|
25 x = example[0] |
b39327d5506f
passed test_lookup_list.py to unittest
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
275
diff
changeset
|
26 x = example["x"] |
b39327d5506f
passed test_lookup_list.py to unittest
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
275
diff
changeset
|
27 assert example.keys()==['x','y','z'] |
b39327d5506f
passed test_lookup_list.py to unittest
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
275
diff
changeset
|
28 assert example.values()==[[1,2,3],2,3] |
b39327d5506f
passed test_lookup_list.py to unittest
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
275
diff
changeset
|
29 assert example.items()==[('x',[1,2,3]),('y',2),('z',3)] |
b39327d5506f
passed test_lookup_list.py to unittest
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
275
diff
changeset
|
30 example.append_keyval('u',0) # adds item with name 'u' and value 0 |
b39327d5506f
passed test_lookup_list.py to unittest
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
275
diff
changeset
|
31 assert len(example)==4 # number of items = 4 here |
b39327d5506f
passed test_lookup_list.py to unittest
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
275
diff
changeset
|
32 example2 = LookupList(['v','w'], ['a','b']) |
b39327d5506f
passed test_lookup_list.py to unittest
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
275
diff
changeset
|
33 example3 = LookupList(['x','y','z','u','v','w'], [[1, 2, 3],2,3,0,'a','b']) |
b39327d5506f
passed test_lookup_list.py to unittest
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
275
diff
changeset
|
34 assert example+example2==example3 |
b39327d5506f
passed test_lookup_list.py to unittest
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
275
diff
changeset
|
35 assert have_raised("var['x']+var['x']",x=example) |
275
323909110d1c
added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
36 |
280
b39327d5506f
passed test_lookup_list.py to unittest
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
275
diff
changeset
|
37 del example, example2, example3, x, y ,z |
275
323909110d1c
added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
38 |
323909110d1c
added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
39 if __name__=='__main__': |
280
b39327d5506f
passed test_lookup_list.py to unittest
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
275
diff
changeset
|
40 unittest.main() |