Add compact test for @list/@set item keep/removal.
authorDave Longley <dlongley@digitalbazaar.com>
Tue, 22 May 2012 14:25:14 -0400
changeset 673 5a516c550523
parent 667 558e03e1c0df
child 676 a141996335e7
Add compact test for @list/@set item keep/removal.

- Update to latest jsonld.js.
- Update frame-0021 to use @list.
playground/jsonld.js
test-suite/tests/compact-0019-context.jsonld
test-suite/tests/compact-0019-in.jsonld
test-suite/tests/compact-0019-out.jsonld
test-suite/tests/compact-manifest.jsonld
test-suite/tests/frame-0021-frame.jsonld
test-suite/tests/frame-0021-in.jsonld
test-suite/tests/frame-0021-out.jsonld
--- a/playground/jsonld.js	Mon May 21 20:52:37 2012 -0400
+++ b/playground/jsonld.js	Tue May 22 14:25:14 2012 -0400
@@ -309,7 +309,7 @@
         var framed = new Processor().frame(_input, _frame, options);
       }
       catch(ex) {
-        callback(ex);
+        return callback(ex);
       }
 
       // compact result (force @graph option to true)
@@ -754,20 +754,28 @@
  * @param value the value to add.
  * @param [propertyIsArray] true if the property is always an array, false
  *          if not (default: false).
+ * @param [propertyIsList] true if the property is a @list, false
+ *          if not (default: false).
  */
-jsonld.addValue = function(subject, property, value, propertyIsArray) {
+jsonld.addValue = function(
+  subject, property, value, propertyIsArray, propertyIsList) {
   propertyIsArray = _isUndefined(propertyIsArray) ? false : propertyIsArray;
+  propertyIsList = (_isUndefined(propertyIsList) ?
+    (property === '@list') : propertyIsList);
 
   if(_isArray(value)) {
     if(value.length === 0 && propertyIsArray && !(property in subject)) {
       subject[property] = [];
     }
     for(var i in value) {
-      jsonld.addValue(subject, property, value[i], propertyIsArray);
+      jsonld.addValue(
+        subject, property, value[i], propertyIsArray, propertyIsList);
     }
   }
   else if(property in subject) {
-    var hasValue = jsonld.hasValue(subject, property, value);
+    // check if subject already has value unless property is list
+    var hasValue = (!propertyIsList &&
+      jsonld.hasValue(subject, property, value));
 
     // make property an array if value not present or always an array
     if(!_isArray(subject[property]) && (!hasValue || propertyIsArray)) {
@@ -1140,7 +1148,7 @@
           (_isArray(v) && v.length === 0));
 
         // add compact value
-        jsonld.addValue(rval, prop, v, isArray);
+        jsonld.addValue(rval, prop, v, isArray, (container === '@list'));
       }
     }
     return rval;
--- a/test-suite/tests/compact-0019-context.jsonld	Mon May 21 20:52:37 2012 -0400
+++ b/test-suite/tests/compact-0019-context.jsonld	Tue May 22 14:25:14 2012 -0400
@@ -1,3 +1,6 @@
 {
-  "@context": {"": "http://example.com/default#"}
-}
+  "@context": {
+    "mylist": {"@id": "http://example.com/mylist", "@container": "@list"},
+    "myset": {"@id": "http://example.com/myset", "@container": "@set"}
+  }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test-suite/tests/compact-0019-in.jsonld	Tue May 22 14:25:14 2012 -0400
@@ -0,0 +1,16 @@
+[{
+  "@id": "http://example.org/id",
+  "http://example.com/mylist": [{
+    "@list": [
+      {"@value": 1},
+      {"@value": 2},
+      {"@value": 2},
+      {"@value": 3}
+    ]
+  }],
+  "http://example.com/myset": [
+    {"@value": 1},
+    {"@value": 2},
+    {"@value": 3}
+  ]
+}]
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test-suite/tests/compact-0019-out.jsonld	Tue May 22 14:25:14 2012 -0400
@@ -0,0 +1,9 @@
+{
+  "@context": {
+    "mylist": {"@id": "http://example.com/mylist", "@container": "@list"},
+    "myset": {"@id": "http://example.com/myset", "@container": "@set"}
+  },
+  "@id": "http://example.org/id",
+  "mylist": [1, 2, 2, 3],
+  "myset": [1, 2, 3]
+}
\ No newline at end of file
--- a/test-suite/tests/compact-manifest.jsonld	Mon May 21 20:52:37 2012 -0400
+++ b/test-suite/tests/compact-manifest.jsonld	Tue May 22 14:25:14 2012 -0400
@@ -113,5 +113,11 @@
     "input": "compact-0018-in.jsonld",
     "context": "compact-0018-context.jsonld",
     "expect": "compact-0018-out.jsonld"
+  }, {
+    "@type": ["test:TestCase", "jld:CompactTest"],
+    "name": "Keep duplicate values in @list, remove from @set",
+    "input": "compact-0019-in.jsonld",
+    "context": "compact-0019-context.jsonld",
+    "expect": "compact-0019-out.jsonld"
   }]
 }
--- a/test-suite/tests/frame-0021-frame.jsonld	Mon May 21 20:52:37 2012 -0400
+++ b/test-suite/tests/frame-0021-frame.jsonld	Tue May 22 14:25:14 2012 -0400
@@ -1,6 +1,7 @@
 {
   "@context": {
     "dc": "http://purl.org/dc/elements/1.1/",
-    "ex": "http://example.org/vocab#"
+    "ex": "http://example.org/vocab#",
+    "dc:list": {"@container": "@list"}
   }
 }
\ No newline at end of file
--- a/test-suite/tests/frame-0021-in.jsonld	Mon May 21 20:52:37 2012 -0400
+++ b/test-suite/tests/frame-0021-in.jsonld	Tue May 22 14:25:14 2012 -0400
@@ -5,7 +5,8 @@
     "xsd": "http://www.w3.org/2001/XMLSchema#",
     "ex:contains": {
       "@type": "@id"
-    }
+    },
+    "dc:list": {"@container": "@list"}
   },
   "@graph": [
   {
--- a/test-suite/tests/frame-0021-out.jsonld	Mon May 21 20:52:37 2012 -0400
+++ b/test-suite/tests/frame-0021-out.jsonld	Tue May 22 14:25:14 2012 -0400
@@ -1,7 +1,8 @@
 {
   "@context": {
     "dc": "http://purl.org/dc/elements/1.1/",
-    "ex": "http://example.org/vocab#"
+    "ex": "http://example.org/vocab#",
+    "dc:list": {"@container": "@list"}
   },
   "@graph": [
   {
@@ -20,7 +21,7 @@
         "@type": "ex:Chapter",
         "dc:description": "An introductory chapter on The Republic.",
         "dc:title": "The Introduction",
-        "dc:list": [1, 2, 3, 4, 5]
+        "dc:list": [1, 2, 3, 4, 4, 4, 5]
       }
     }
   }, {
@@ -31,7 +32,7 @@
       "@type": "ex:Chapter",
       "dc:description": "An introductory chapter on The Republic.",
       "dc:title": "The Introduction",
-      "dc:list": [1, 2, 3, 4, 5]
+      "dc:list": [1, 2, 3, 4, 4, 4, 5]
     },
     "dc:creator": "Plato",
     "dc:title": "The Republic"
@@ -39,7 +40,7 @@
     "@id": "http://example.org/library/the-republic#introduction",
     "@type": "ex:Chapter",
     "dc:description": "An introductory chapter on The Republic.",
-    "dc:list": [1, 2, 3, 4, 5],
+    "dc:list": [1, 2, 3, 4, 4, 4, 5],
     "dc:title": "The Introduction"
   }]
 }
\ No newline at end of file