# HG changeset patch # User Martin Geisler # Date 2009-05-17 19:47:24 # Node ID 0b93eff3721dacb038b3c9287db3b34aa0f0cd56 # Parent 15573b5dc78f546bf49fdbcb31f40baaf4f70da1 test-highlight: decouple test from get-with-headers.py The test copied get-with-headers.py from $TESTDIR and committed it to a test repository. The test output therefore depended unnecessarily on the exact content of get-with-headers.py. It has now been replaced with another small Python script. diff --git a/tests/test-highlight b/tests/test-highlight --- a/tests/test-highlight +++ b/tests/test-highlight @@ -11,7 +11,41 @@ EOF hg init test cd test -cp $TESTDIR/get-with-headers.py ./ +# create random Python file to exercise Pygments +cat < primes.py +#!/usr/bin/env python + +"""Fun with generators. Corresponding Haskell implementation: + +primes = 2 : sieve [3, 5..] + where sieve (p:ns) = p : sieve [n | n <- ns, mod n p /= 0] +""" + +from itertools import dropwhile, ifilter, islice, count, chain + +def primes(): + """Generate all primes.""" + def sieve(ns): + p = ns.next() + # It is important to yield *here* in order to stop the + # infinite recursion. + yield p + ns = ifilter(lambda n: n % p != 0, ns) + for n in sieve(ns): + yield n + + odds = ifilter(lambda i: i % 2 == 1, count()) + return chain([2], sieve(dropwhile(lambda n: n < 3, odds))) + +if __name__ == "__main__": + import sys + try: + n = int(sys.argv[1]) + except (ValueError, IndexError): + n = 10 + p = primes() + print "The first %d primes: %s" % (n, list(islice(p, n))) +EOF # check for UnicodeDecodeError with iso-8859-1 file contents python -c 'fp = open("isolatin.txt", "w"); fp.write("h\xFCbsch\n"); fp.close();' @@ -23,7 +57,7 @@ hg serve -p $HGPORT -d -n test --pid-fil cat hg.pid >> $DAEMON_PIDS echo % hgweb filerevision, html -("$TESTDIR/get-with-headers.py" localhost:$HGPORT '/file/tip/get-with-headers.py') \ +("$TESTDIR/get-with-headers.py" localhost:$HGPORT '/file/tip/primes.py') \ | sed "s/class=\"k\"/class=\"kn\"/g" | sed "s/class=\"mf\"/class=\"mi\"/g" echo % hgweb filerevision, html @@ -31,17 +65,17 @@ echo % hgweb filerevision, html | sed "s/class=\"k\"/class=\"kn\"/g" echo % hgweb fileannotate, html -("$TESTDIR/get-with-headers.py" localhost:$HGPORT '/annotate/tip/get-with-headers.py') \ +("$TESTDIR/get-with-headers.py" localhost:$HGPORT '/annotate/tip/primes.py') \ | sed "s/class=\"k\"/class=\"kn\"/g" | sed "s/class=\"mi\"/class=\"mf\"/g" echo % hgweb fileannotate, raw -("$TESTDIR/get-with-headers.py" localhost:$HGPORT '/annotate/tip/get-with-headers.py?style=raw') \ +("$TESTDIR/get-with-headers.py" localhost:$HGPORT '/annotate/tip/primes.py?style=raw') \ | sed "s/test@//" > a echo "200 Script output follows" > b echo "" >> b echo "" >> b -hg annotate "get-with-headers.py" >> b +hg annotate "primes.py" >> b echo "" >> b echo "" >> b echo "" >> b @@ -51,12 +85,12 @@ diff -u b a echo echo % hgweb filerevision, raw -("$TESTDIR/get-with-headers.py" localhost:$HGPORT '/file/tip/get-with-headers.py?style=raw') \ +("$TESTDIR/get-with-headers.py" localhost:$HGPORT '/file/tip/primes.py?style=raw') \ > a echo "200 Script output follows" > b echo "" >> b -hg cat get-with-headers.py >> b +hg cat primes.py >> b diff -u b a diff --git a/tests/test-highlight.out b/tests/test-highlight.out --- a/tests/test-highlight.out +++ b/tests/test-highlight.out @@ -1,5 +1,5 @@ -adding get-with-headers.py adding isolatin.txt +adding primes.py % hg serve % hgweb filerevision, html 200 Script output follows @@ -12,7 +12,7 @@ 200 Script output follows -test: 93b701795893 get-with-headers.py +test: 3e1445510fe7 primes.py @@ -23,27 +23,27 @@ 200 Script output follows mercurial

test

-

view get-with-headers.py @ 0:93b701795893

+

view primes.py @ 0:3e1445510fe7

@@ -98,7 +98,7 @@ 200 Script output follows -test: 93b701795893 isolatin.txt +test: 3e1445510fe7 isolatin.txt @@ -109,27 +109,27 @@ 200 Script output follows mercurial

test

-

view isolatin.txt @ 0:93b701795893

+

view isolatin.txt @ 0:3e1445510fe7

@@ -184,7 +184,7 @@ 200 Script output follows -test: get-with-headers.py annotate +test: primes.py annotate @@ -195,28 +195,28 @@ 200 Script output follows mercurial

test

-

annotate get-with-headers.py @ 0:93b701795893

+

annotate primes.py @ 0:3e1445510fe7

@@ -258,213 +258,227 @@ files, or words in the commit message -test@0 +test@0 1 #!/usr/bin/env python -test@0 +test@0 2 -test@0 +test@0 - 3 """This does HTTP GET requests given a host:port and path and returns + 3 """Fun with generators. Corresponding Haskell implementation: -test@0 +test@0 - 4 a subset of the headers plus the body of the result.""" + 4 -test@0 +test@0 - 5 + 5 primes = 2 : sieve [3, 5..] -test@0 +test@0 - 6 import httplib, sys, re + 6 where sieve (p:ns) = p : sieve [n | n <- ns, mod n p /= 0] -test@0 +test@0 - 7 + 7 """ -test@0 +test@0 - 8 try: + 8 -test@0 +test@0 - 9 import msvcrt, os + 9 from itertools import dropwhile, ifilter, islice, count, chain -test@0 +test@0 - 10 msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) + 10 -test@0 +test@0 - 11 msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY) + 11 def primes(): -test@0 +test@0 - 12 except ImportError: + 12 """Generate all primes.""" -test@0 +test@0 - 13 pass + 13 def sieve(ns): -test@0 +test@0 - 14 + 14 p = ns.next() -test@0 +test@0 - 15 headers = [h.lower() for h in sys.argv[3:]] + 15 # It is important to yield *here* in order to stop the -test@0 +test@0 - 16 conn = httplib.HTTPConnection(sys.argv[1]) + 16 # infinite recursion. -test@0 +test@0 - 17 conn.request("GET", sys.argv[2]) + 17 yield p -test@0 +test@0 - 18 response = conn.getresponse() + 18 ns = ifilter(lambda n: n % p != 0, ns) -test@0 +test@0 - 19 print response.status, response.reason + 19 for n in sieve(ns): -test@0 +test@0 - 20 for h in headers: + 20 yield n -test@0 +test@0 - 21 if response.getheader(h, None) is not None: + 21 -test@0 +test@0 - 22 print "%s: %s" % (h, response.getheader(h)) + 22 odds = ifilter(lambda i: i % 2 == 1, count()) -test@0 +test@0 - 23 print + 23 return chain([2], sieve(dropwhile(lambda n: n < 3, odds))) -test@0 +test@0 - 24 data = response.read() + 24 -test@0 +test@0 - 25 data = re.sub('\d+ years', 'many years', data) + 25 if __name__ == "__main__": -test@0 +test@0 - 26 sys.stdout.write(data) + 26 import sys -test@0 +test@0 - 27 + 27 try: -test@0 +test@0 - 28 if 200 <= response.status <= 299: + 28 n = int(sys.argv[1]) -test@0 +test@0 - 29 sys.exit(0) + 29 except (ValueError, IndexError): -test@0 +test@0 - 30 sys.exit(1) + 30 n = 10 + + + +test@0 + + 31 p = primes() + + + +test@0 + + 32 print "The first %d primes: %s" % (n, list(islice(p, n)))