[osflash] Design By Contract on ActionScript 2
Bernd Will
bernd at ewill.de
Sat Oct 7 12:21:54 EDT 2006
Hello everybody,
don't know if you know Python's doctest feature. There you can embed tests
right into the classes instead of coding xxxTest classes all the way.
In Python you write down the header of the class, then start a comment tag
and put numerous tests together with the expected results, then you close
the comment. When done, you start writing the method.
It is all in one place, no huddling no searching, just scroll up to the top
of the method and you have the tests right before your nose.
Below is a short Python snippet demonstrating this nice feature.
In Python comments are enclosed by """ [.] """" triple signs.
Every test starts with the >>> sign, which is the call of the function.
Every test is written down as being called and after the call, you find the
awaited result.
This idea can't be transferred to ActionScript, since Python is a Basic like
interpreted language putting back results after every command.
But the idea of putting tests and results in one place side by side with the
code IMHO is worth thinking about as a nice feature.
def factorial(n):
"""Return the factorial of n, an exact integer >= 0.
If the result is small enough to fit in an int, return an int.
Else return a long. Here are the tests:
>>> [factorial(n) for n in range(6)]
[1, 1, 2, 6, 24, 120]
>>> [factorial(long(n)) for n in range(6)]
[1, 1, 2, 6, 24, 120]
>>> factorial(30)
265252859812191058636308480000000L
>>> factorial(30L)
265252859812191058636308480000000L
>>> factorial(-1)
Traceback (most recent call last):
...
ValueError: n must be >= 0
Factorials of floats are OK, but the float must be an exact integer:
>>> factorial(30.1)
Traceback (most recent call last):
...
ValueError: n must be exact integer
>>> factorial(30.0)
265252859812191058636308480000000L
It must also not be ridiculously large:
>>> factorial(1e100)
Traceback (most recent call last):
...
OverflowError: n too large
"""
import math
if not n >= 0:
raise ValueError("n must be >= 0")
if math.floor(n) != n:
raise ValueError("n must be exact integer")
if n+1 == n: # catch a value like 1e300
raise OverflowError("n too large")
result = 1
factor = 2
while factor <= n:
result *= factor
factor += 1
return result
def _test():
import doctest
doctest.testmod()
if __name__ == "__main__":
_test()
Regards
Bernd
_____
Von: osflash-bounces at osflash.org [mailto:osflash-bounces at osflash.org] Im
Auftrag von Michael Forrest
Gesendet: Samstag, 7. Oktober 2006 11:27
An: Open Source Flash Mailing List
Betreff: Re: [osflash] Design By Contract on ActionScript 2
Getting some unit testing working would be a start. AS2Lib has testing
libraries and the AS2Ant tasks give a way of running them from ANT scripts
as part of a build process. http://www.simonwacker.com
<http://www.simonwacker.com>
On 10/5/06, Marcelo de Moraes Serpa <celoserpa at gmail.com> wrote:
Is there any tool for AS2 that allows me to implement the DBC methodology?
Cheers,
Marceo.
_______________________________________________
osflash mailing list
osflash at osflash.org
http://osflash.org/mailman/listinfo/osflash_osflash.org
--
Michael Forrest
MP3 feed:
www.box.net/public/yk8y59h8da
Myspace page:
www.myspace.com/michaelforrest
Official site:
www.cinestatic.com/michaelforrest
Last.fm artist page:
www.last.fm/music/michael+forrest
Jamendo (cc music download):
www.jamendo.com/uk/artist/michael.forrest/
YouTube Videos:
www.youtube.com/profile?user=michaelforrest
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://osflash.org/pipermail/osflash_osflash.org/attachments/20061007/5acf5fc0/attachment.htm
More information about the osflash
mailing list