[Python-modules-commits] [python-pyld] 51/276: Rename @literal to @value, bug fixes, use json-ld.org test-suite.

Wolfgang Borgert debacle at moszumanska.debian.org
Wed Oct 8 23:47:52 UTC 2014


This is an automated email from the git hooks/post-receive script.

debacle pushed a commit to branch master
in repository python-pyld.

commit ba83fdc49bae43a6a3c497ff18781b20e2bcd7dc
Author: Dave Longley <dlongley at digitalbazaar.com>
Date:   Wed Jan 11 23:30:04 2012 -0500

    Rename @literal to @value, bug fixes, use json-ld.org test-suite.
---
 lib/pyld/jsonld.py  | 64 ++++++++++++++++++++++---------------------
 tests/TestRunner.py | 78 +++++++++++++++++++++++++++++------------------------
 2 files changed, 76 insertions(+), 66 deletions(-)

diff --git a/lib/pyld/jsonld.py b/lib/pyld/jsonld.py
index 28d858a..d5bc717 100644
--- a/lib/pyld/jsonld.py
+++ b/lib/pyld/jsonld.py
@@ -60,7 +60,7 @@ def _getKeywords(ctx):
     rval = {
        '@id': '@id',
        '@language': '@language',
-       '@literal': '@literal',
+       '@value': '@value',
        '@type': '@type'
     }
 
@@ -221,10 +221,10 @@ def _isSubject(value):
 
     # Note: A value is a subject if all of these hold true:
     # 1. It is an Object.
-    # 2. It is not a literal.
+    # 2. It is not a literal (@value).
     # 3. It has more than 1 key OR any existing key is not '@id'.
     if (value is not None and isinstance(value, dict) and
-        '@literal' not in value):
+        '@value' not in value):
         rval = len(value.keys()) > 1 or '@id' not in value
 
     return rval
@@ -311,9 +311,9 @@ def _compareObjects(o1, o2):
     elif isinstance(o2, basestring):
         rval = 1
     else:
-        rval = _compareObjectKeys(o1, o2, '@literal')
+        rval = _compareObjectKeys(o1, o2, '@value')
         if rval == 0:
-            if '@literal' in o1:
+            if '@value' in o1:
                 rval = _compareObjectKeys(o1, o2, '@type')
                 if rval == 0:
                     rval = _compareObjectKeys(o1, o2, '@language')
@@ -340,8 +340,8 @@ def _compareBlankNodeObjects(a, b):
     # 3.2.1. The bnode with fewer non-bnodes is first.
     # 3.2.2. The bnode with a string object is first.
     # 3.2.3. The bnode with the alphabetically-first string is first.
-    # 3.2.4. The bnode with a @literal is first.
-    # 3.2.5. The bnode with the alphabetically-first @literal is first.
+    # 3.2.4. The bnode with a @value is first.
+    # 3.2.5. The bnode with the alphabetically-first @value is first.
     # 3.2.6. The bnode with the alphabetically-first @type is first.
     # 3.2.7. The bnode with a @language is first.
     # 3.2.8. The bnode with the alphabetically-first @language is first.
@@ -461,7 +461,7 @@ def _flatten(parent, parentProperty, value, subjects):
             _flatten(parent, parentProperty, i, subjects)
     elif isinstance(value, dict):
         # already-expanded value or special-case reference-only @type
-        if '@literal' in value or parentProperty == '@type':
+        if '@value' in value or parentProperty == '@type':
             flattened = copy.copy(value)
         # graph literal/disjoint graph
         elif isinstance(value['@id'], list):
@@ -667,8 +667,8 @@ class Processor:
                     if isinstance(value, dict):
                         if '@id' in value:
                             rval = value['@id']
-                        elif '@literal' in value:
-                            rval = value['@literal']
+                        elif '@value' in value:
+                            rval = value['@value']
                     else:
                         rval = value
 
@@ -778,7 +778,7 @@ class Processor:
                         value = 'true' if value else 'false'
                     else:
                         value = '%s' % value
-                    rval['@literal'] = value
+                    rval['@value'] = value
             # nothing to coerce
             else:
                 rval = '' + value
@@ -1470,7 +1470,14 @@ class Processor:
 
         # apply context
         if ctx is not None and rval is not None:
-            rval = compact(ctx, rval)
+            # preserve top-level array by compacting individual entries
+            if isinstance(rval, list):
+                tmp = rval
+                rval = []
+                for i in range(0, len(tmp)):
+                    rval.append(compact(ctx, tmp[i]))
+            else:
+                rval = compact(ctx, rval)
 
         return rval
 
@@ -1805,7 +1812,7 @@ def _serializeProperties(b):
                             rval += '<' + o['@id'] + '>'
                     # literal
                     else:
-                        rval += '"' + o['@literal'] + '"'
+                        rval += '"' + o['@value'] + '"'
 
                         # type literal
                         if '@type' in o:
@@ -1877,27 +1884,22 @@ def compact(ctx, input):
         # fully expand input
         input = expand(input)
 
-        if isinstance(input, list):
-            rval = []
-            tmp = input
-        else:
-            tmp = [input]
-
-        for value in tmp:
-            # setup output context
-            ctxOut = {}
-
-            # compact
-            output = Processor().compact(copy.copy(ctx), None, value, ctxOut)
+        # setup output context
+        ctxOut = {}
 
-            # add context if used
-            if len(ctxOut.keys()) > 0:
-                output["@context"] = ctxOut
+        # compact
+        output = Processor().compact(
+            copy.copy(ctx), None, input, ctxOut)
 
-            if rval is None:
-                rval = output
+        # add context if used
+        rval = output
+        if len(ctxOut.keys()) > 0:
+            rval = {'@context': ctxOut}
+            if isinstance(output, list):
+                rval[_getKeywords(ctxOut)['@id']] = output
             else:
-                rval.append(output)
+                for key, value in output.items():
+                    rval[key] = value
 
     return rval
 
diff --git a/tests/TestRunner.py b/tests/TestRunner.py
index 008907a..9346267 100644
--- a/tests/TestRunner.py
+++ b/tests/TestRunner.py
@@ -5,7 +5,7 @@
 # @author Mike Johnson
 # @author Dave Longley
 # 
-# Copyright 2011 Digital Bazaar, Inc. All Rights Reserved.
+# Copyright 2011-2012 Digital Bazaar, Inc. All Rights Reserved.
 import os, sys, json
 from os.path import join
 from optparse import OptionParser
@@ -25,7 +25,7 @@ def _ntriple(s, p, o):
     else:
         # object is a literal
         return "<%s> <%s> \"%s\"^^<%s> ." % \
-            (s, p, o["@literal"], o["@type"])
+            (s, p, o["@value"], o["@type"])
 
 ##
 # TestRunner unit testing class.
@@ -45,8 +45,8 @@ class TestRunner:
         self.testdir = None
 
         ##
-        # The list of test files to run.
-        self.testfiles = []
+        # The list of manifest files to run.
+        self.manifestfiles = []
 
     ##
     # The main function for parsing options and running tests.
@@ -74,8 +74,8 @@ class TestRunner:
         if self.options.file != None:
             if (os.path.exists(self.options.file) and
                 os.path.isfile(self.options.file)):
-                # add test file to the file list
-                self.testfiles.append(os.path.abspath(self.options.file))
+                # add manifest file to the file list
+                self.manifestfiles.append(os.path.abspath(self.options.file))
                 self.testdir = os.path.dirname(self.options.file)
             else:
                 print "Invalid test file."
@@ -85,37 +85,48 @@ class TestRunner:
         if self.options.directory != None:
             if (os.path.exists(self.options.directory) and
                 os.path.isdir(self.options.directory)):
-                # load test files from test directory
+                # load manifest files from test directory
                 for self.testdir, dirs, files in os.walk(self.options.directory):
-                    for testfile in files:
-                        # add all .test files to the file list
-                        if testfile.endswith(".test"):
-                            self.testfiles.append(join(self.testdir, testfile))
+                    for manifest in files:
+                        # add all .jsonld manifest files to the file list
+                        if (manifest.find('manifest') != -1 and
+                            manifest.endswith(".jsonld")):
+                            self.manifestfiles.append(join(self.testdir, manifest))
             else:
                 print "Invalid test directory."
                 return
 
-        # see if any tests have been specified
-        if len(self.testfiles) == 0:
-            print "No tests found."
+        # see if any manifests have been specified
+        if len(self.manifestfiles) == 0:
+            print "No manifest files found."
             return
 
         # FIXME: 
-        #self.testFiles.sort()
+        #self.manifestfiles.sort()
 
         run = 0
         passed = 0
         failed = 0
 
-        # run the tests from each test file
-        for testfile in self.testfiles:
-            # test group in test file
-            testgroup = json.load(open(testfile, 'r'))
+        # run the tests from each manifest file
+        for manifestfile in self.manifestfiles:
+            manifest = json.load(open(manifestfile, 'r'))
             count = 1
 
-            for test in testgroup['tests']:
+            for test in manifest['sequence']:
+                # skip unsupported types
+                testType = test['@type']
+                if ('jld:NormalizeTest' not in testType and
+                    'jld:ExpandTest' not in testType and
+                    'jld:CompactTest' not in testType and
+                    'jld:FrameTest' not in testType and
+                    'jld:TriplesTest' not in testType):
+                    print 'Skipping test: %s...' % test['name']
+                    continue
+
                 print 'Test: %s %04d/%s...' % (
-                    testgroup['group'], count, test['name']),
+                    manifest['name'], count, test['name']),
+
                 run += 1
                 count += 1
 
@@ -124,7 +135,7 @@ class TestRunner:
                 expectFile = open(join(self.testdir, test['expect']))
                 inputJson = json.load(inputFile)
                 expectType = os.path.splitext(test['expect'])[1][1:]
-                if expectType == 'json':
+                if expectType == 'jsonld':
                     expect = json.load(expectFile)
                 elif expectType == 'nt':
                     # read, strip non-data lines, stripe front/back whitespace, and sort
@@ -139,44 +150,41 @@ class TestRunner:
 
                 result = None
 
-                testType = test['type']
-                if testType == 'normalize':
+                if 'jld:NormalizeTest' in testType:
                     result = jsonld.normalize(inputJson)
-                elif testType == 'expand':
+                elif 'jld:ExpandTest' in testType:
                     result = jsonld.expand(inputJson)
-                elif testType == 'compact':
+                elif 'jld:CompactTest' in testType:
                     contextFile = open(join(self.testdir, test['context']))
                     contextJson = json.load(contextFile)
-                    result = jsonld.compact(contextJson, inputJson)
-                elif testType == 'frame':
+                    result = jsonld.compact(contextJson['@context'], inputJson)
+                elif 'jld:FrameTest' in testType:
                     frameFile = open(join(self.testdir, test['frame']))
                     frameJson = json.load(frameFile)
                     result = jsonld.frame(inputJson, frameJson)
-                elif testType == 'triples':
+                elif 'jld:TriplesTest' in testType:
                     result = '\n'.join(
                         sorted(jsonld.triples(inputJson, callback=_ntriple)))
-                else:
-                    print "Unknown test type."
 
                 # check the expected value against the test result
                 if expect == result:
                     passed += 1
                     print 'PASS'
                     if self.options.verbose:
-                        print 'Expect:', json.dumps(expect, indent=4)
+                        print 'Expect:', json.dumps(expect, indent=2)
                         print 'Result:',
                         if expectType == 'json':
-                            print json.dumps(result, indent=4)
+                            print json.dumps(result, indent=2)
                         else:
                             print
                             print result
                 else:
                     failed += 1
                     print 'FAIL'
-                    print 'Expect:', json.dumps(expect, indent=4)
+                    print 'Expect:', json.dumps(expect, indent=2)
                     print 'Result:',
                     if expectType == 'json':
-                        print json.dumps(result, indent=4)
+                        print json.dumps(result, indent=2)
                     else:
                         print
                         print result

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/python-pyld.git



More information about the Python-modules-commits mailing list