Mercurial > hg > pyloader
annotate tests/objects.py @ 41:2c228e3cd6d8
split reading and interpreting the .ini into different functions
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Wed, 08 Jun 2011 17:50:05 -0700 |
parents | f6616a5f468f |
children | 49f88cde6219 |
rev | line source |
---|---|
11 | 1 """ |
2 test objects | |
3 """ | |
4 | |
5 def stringsort(*strings): | |
6 return ''.join(sorted(''.join(strings))) | |
13 | 7 |
15
0bea5297c156
introduce a more betterer test
Jeff Hammel <jhammel@mozilla.com>
parents:
13
diff
changeset
|
8 class StringMunge(object): |
0bea5297c156
introduce a more betterer test
Jeff Hammel <jhammel@mozilla.com>
parents:
13
diff
changeset
|
9 def __init__(self, prefix, delimeter=':', callback=None): |
0bea5297c156
introduce a more betterer test
Jeff Hammel <jhammel@mozilla.com>
parents:
13
diff
changeset
|
10 self.delimeter = delimeter |
0bea5297c156
introduce a more betterer test
Jeff Hammel <jhammel@mozilla.com>
parents:
13
diff
changeset
|
11 self.prefix = prefix |
0bea5297c156
introduce a more betterer test
Jeff Hammel <jhammel@mozilla.com>
parents:
13
diff
changeset
|
12 self.callback = callback |
0bea5297c156
introduce a more betterer test
Jeff Hammel <jhammel@mozilla.com>
parents:
13
diff
changeset
|
13 def __call__(self, string): |
0bea5297c156
introduce a more betterer test
Jeff Hammel <jhammel@mozilla.com>
parents:
13
diff
changeset
|
14 if self.callback: |
0bea5297c156
introduce a more betterer test
Jeff Hammel <jhammel@mozilla.com>
parents:
13
diff
changeset
|
15 string = self.callback(string) |
0bea5297c156
introduce a more betterer test
Jeff Hammel <jhammel@mozilla.com>
parents:
13
diff
changeset
|
16 return self.prefix + self.delimeter + string |
0bea5297c156
introduce a more betterer test
Jeff Hammel <jhammel@mozilla.com>
parents:
13
diff
changeset
|
17 |
13 | 18 class Foo(object): |
19 def __init__(self, a, b): | |
20 self.factor = a*100 + b | |
21 def __call__(self, number): | |
22 return number * self.factor | |
36
362912842985
start stubbing out wrappers....i am somewhat perplexed
Jeff Hammel <jhammel@mozilla.com>
parents:
15
diff
changeset
|
23 |
362912842985
start stubbing out wrappers....i am somewhat perplexed
Jeff Hammel <jhammel@mozilla.com>
parents:
15
diff
changeset
|
24 class Wrapper(object): |
362912842985
start stubbing out wrappers....i am somewhat perplexed
Jeff Hammel <jhammel@mozilla.com>
parents:
15
diff
changeset
|
25 def __init__(self, app): |
362912842985
start stubbing out wrappers....i am somewhat perplexed
Jeff Hammel <jhammel@mozilla.com>
parents:
15
diff
changeset
|
26 self.app = app |
362912842985
start stubbing out wrappers....i am somewhat perplexed
Jeff Hammel <jhammel@mozilla.com>
parents:
15
diff
changeset
|
27 def __call__(self, *args, **kwargs): |
362912842985
start stubbing out wrappers....i am somewhat perplexed
Jeff Hammel <jhammel@mozilla.com>
parents:
15
diff
changeset
|
28 retval = self.app(*args, **kwargs) |
362912842985
start stubbing out wrappers....i am somewhat perplexed
Jeff Hammel <jhammel@mozilla.com>
parents:
15
diff
changeset
|
29 values = {1: 'one', |
362912842985
start stubbing out wrappers....i am somewhat perplexed
Jeff Hammel <jhammel@mozilla.com>
parents:
15
diff
changeset
|
30 2: 'two', |
362912842985
start stubbing out wrappers....i am somewhat perplexed
Jeff Hammel <jhammel@mozilla.com>
parents:
15
diff
changeset
|
31 3: 'three'} # etc |
362912842985
start stubbing out wrappers....i am somewhat perplexed
Jeff Hammel <jhammel@mozilla.com>
parents:
15
diff
changeset
|
32 if retval in values: |
362912842985
start stubbing out wrappers....i am somewhat perplexed
Jeff Hammel <jhammel@mozilla.com>
parents:
15
diff
changeset
|
33 return values[retval] |
362912842985
start stubbing out wrappers....i am somewhat perplexed
Jeff Hammel <jhammel@mozilla.com>
parents:
15
diff
changeset
|
34 return retval |
362912842985
start stubbing out wrappers....i am somewhat perplexed
Jeff Hammel <jhammel@mozilla.com>
parents:
15
diff
changeset
|
35 |
362912842985
start stubbing out wrappers....i am somewhat perplexed
Jeff Hammel <jhammel@mozilla.com>
parents:
15
diff
changeset
|
36 def wrap(app): |
362912842985
start stubbing out wrappers....i am somewhat perplexed
Jeff Hammel <jhammel@mozilla.com>
parents:
15
diff
changeset
|
37 return Wrapper(app) |
362912842985
start stubbing out wrappers....i am somewhat perplexed
Jeff Hammel <jhammel@mozilla.com>
parents:
15
diff
changeset
|
38 |
362912842985
start stubbing out wrappers....i am somewhat perplexed
Jeff Hammel <jhammel@mozilla.com>
parents:
15
diff
changeset
|
39 def fib(n): |
362912842985
start stubbing out wrappers....i am somewhat perplexed
Jeff Hammel <jhammel@mozilla.com>
parents:
15
diff
changeset
|
40 """return the nth fibonacci term""" |
37
5268ab85fe1b
stub a fibonacci sequence for testing
Jeff Hammel <jhammel@mozilla.com>
parents:
36
diff
changeset
|
41 sequence = [1,1] |
38 | 42 while len(sequence) - 1 < n: |
43 sequence.append(sequence[len(sequence)-1] + sequence[len(sequence)-2]) | |
37
5268ab85fe1b
stub a fibonacci sequence for testing
Jeff Hammel <jhammel@mozilla.com>
parents:
36
diff
changeset
|
44 return sequence[n] |