From cbd8aca553c6d47a0afa28e026d2906f6d28e02e Mon Sep 17 00:00:00 2001 From: mjtnix Date: Fri, 12 Oct 2007 22:56:35 +0000 Subject: pull in test cases from the perl URI::Template module. fix quoting to pass those test cases. git-svn-id: http://freebase-python.googlecode.com/svn/trunk@11 5914aa95-5b3a-0410-a3b5-7b719e7fe9b2 --- freebase/uritemplate.py | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) (limited to 'freebase') diff --git a/freebase/uritemplate.py b/freebase/uritemplate.py index 17a185f..91643d1 100755 --- a/freebase/uritemplate.py +++ b/freebase/uritemplate.py @@ -46,6 +46,10 @@ import os, sys, re +import urllib +def uri_encode_var(v): + return urllib.quote(v, safe="-_.~!$&'()*+,;=:/?[]#@") + class URITemplate(object): VARREF = re.compile(r'\{([0-9a-zA-Z_]+)\}') @@ -74,8 +78,7 @@ class URITemplate(object): def run (self, **args): def repl(m): key = m.group(1) - return args[key] - + return uri_encode_var(args.get(key, '')) uri = self.VARREF.sub(repl,self.template) # XXX for testing, compare this to args array @@ -92,5 +95,19 @@ class URITemplate(object): return dict(zip(self.params, m.groups())) if __name__ == '__main__': - ut = URITemplate('xxx{key}yyy') - print ut.run(key='ABC') + import urllib2, simplejson + fp = urllib2.urlopen('http://search.cpan.org/src/BRICAS/URI-Template-0.09/t/data/spec.json') + testsuite = simplejson.loads(fp.read()) + vars = dict([(k.encode('utf-8'),v) for k,v in testsuite['variables'].items()]) + nsucceed = 0 + nfail = 0 + for test in testsuite['tests']: + ut = URITemplate(test['template']) + uri = ut.run(**vars) + if uri != test['expected']: + print 'FAILED %r expected %r' % (uri, test['expected']) + print ' vars: %r' % (vars,) + nfail += 1 + else: + nsucceed += 1 + print 'tests: %d succeeded, %d failed' % (nsucceed, nfail) -- cgit v1.3.1