2015-08-14 17:47:56 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Copyright 2015 Realm Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var ArrayTests = {
|
|
|
|
testArrayLength: function() {
|
|
|
|
var realm = new Realm({schema: [LinkTypesObjectSchema, TestObjectSchema]});
|
|
|
|
realm.write(function() {
|
|
|
|
var obj = realm.create('LinkTypesObject', [[1], [2], [[3]]]);
|
|
|
|
TestCase.assertEqual(obj.arrayCol.length, 1);
|
|
|
|
|
|
|
|
obj.arrayCol = [];
|
|
|
|
TestCase.assertEqual(obj.arrayCol.length, 0);
|
|
|
|
|
|
|
|
obj.arrayCol = [[1], [2]];
|
|
|
|
TestCase.assertEqual(obj.arrayCol.length, 2);
|
2015-10-12 22:25:23 +00:00
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
obj.arrayCol.length = 0;
|
|
|
|
}, 'cannot set length property on lists');
|
2015-10-12 09:55:37 +00:00
|
|
|
});
|
2015-08-14 17:47:56 +00:00
|
|
|
},
|
|
|
|
|
2015-10-12 09:55:37 +00:00
|
|
|
testArraySubscriptGetters: function() {
|
2015-08-14 17:47:56 +00:00
|
|
|
var realm = new Realm({schema: [LinkTypesObjectSchema, TestObjectSchema]});
|
2015-10-12 09:55:37 +00:00
|
|
|
var array;
|
|
|
|
|
|
|
|
realm.write(function() {
|
|
|
|
var obj = realm.create('LinkTypesObject', [[1], [2], [[3], [4]]]);
|
|
|
|
array = obj.arrayCol;
|
|
|
|
});
|
2015-08-14 17:47:56 +00:00
|
|
|
|
|
|
|
TestCase.assertEqual(array[0].doubleCol, 3);
|
|
|
|
TestCase.assertEqual(array[1].doubleCol, 4);
|
2015-10-12 22:25:23 +00:00
|
|
|
TestCase.assertEqual(array[2], undefined);
|
|
|
|
TestCase.assertEqual(array[-1], undefined);
|
2015-08-14 17:47:56 +00:00
|
|
|
},
|
|
|
|
|
2015-10-12 09:55:37 +00:00
|
|
|
testArraySubscriptSetters: function() {
|
|
|
|
var realm = new Realm({schema: [LinkTypesObjectSchema, TestObjectSchema]});
|
|
|
|
var array;
|
|
|
|
|
|
|
|
realm.write(function() {
|
|
|
|
var obj = realm.create('LinkTypesObject', [[1], [2], [[3], [4]]]);
|
|
|
|
array = obj.arrayCol;
|
|
|
|
|
|
|
|
array[0] = [5];
|
|
|
|
array[1] = [6];
|
|
|
|
|
|
|
|
TestCase.assertEqual(array[0].doubleCol, 5);
|
|
|
|
TestCase.assertEqual(array[1].doubleCol, 6);
|
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
array[2] = [1];
|
|
|
|
}, 'cannot set list item beyond its bounds');
|
2015-10-12 22:25:23 +00:00
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
array[-1] = [1];
|
|
|
|
}, 'cannot set list item with negative index');
|
2015-10-12 09:55:37 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
array[0] = [3];
|
|
|
|
}, 'cannot set list item outside write transaction');
|
|
|
|
},
|
|
|
|
|
|
|
|
testArrayInvalidProperty: function() {
|
2015-08-14 17:47:56 +00:00
|
|
|
var realm = new Realm({schema: [LinkTypesObjectSchema, TestObjectSchema]});
|
2015-10-12 09:55:37 +00:00
|
|
|
var array;
|
|
|
|
|
|
|
|
realm.write(function() {
|
|
|
|
var obj = realm.create('LinkTypesObject', [[1], [2], [[3], [4]]]);
|
|
|
|
array = obj.arrayCol;
|
|
|
|
});
|
2015-08-14 17:47:56 +00:00
|
|
|
|
|
|
|
TestCase.assertEqual(undefined, array.ablasdf);
|
|
|
|
},
|
|
|
|
|
|
|
|
testArrayEnumerate: function() {
|
|
|
|
var realm = new Realm({schema: [LinkTypesObjectSchema, TestObjectSchema]});
|
2015-10-12 09:55:37 +00:00
|
|
|
var obj;
|
2015-08-14 17:47:56 +00:00
|
|
|
|
2015-10-12 09:55:37 +00:00
|
|
|
realm.write(function() {
|
|
|
|
obj = realm.create('LinkTypesObject', [[1], [2], []]);
|
|
|
|
});
|
|
|
|
|
|
|
|
for (var index in obj.arrayCol) {
|
|
|
|
TestCase.assertTrue(false, "No objects should have been enumerated: " + index);
|
2015-08-14 17:47:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
realm.write(function() {
|
|
|
|
obj.arrayCol = [[0], [1]];
|
|
|
|
TestCase.assertEqual(obj.arrayCol.length, 2);
|
|
|
|
});
|
|
|
|
|
|
|
|
var count = 0;
|
2015-10-12 09:55:37 +00:00
|
|
|
for (var index in obj.arrayCol) {
|
2015-08-14 17:47:56 +00:00
|
|
|
count++;
|
2015-10-12 09:55:37 +00:00
|
|
|
}
|
2015-08-14 17:47:56 +00:00
|
|
|
TestCase.assertEqual(2, count);
|
|
|
|
},
|
2015-08-24 16:22:37 +00:00
|
|
|
|
|
|
|
testPush: function() {
|
2015-08-27 20:33:20 +00:00
|
|
|
var realm = new Realm({schema: [LinkTypesObjectSchema, TestObjectSchema]});
|
|
|
|
var array;
|
2015-10-12 09:23:09 +00:00
|
|
|
|
2015-08-27 20:33:20 +00:00
|
|
|
realm.write(function() {
|
|
|
|
var obj = realm.create('LinkTypesObject', [[1], [2], [[3]]]);
|
|
|
|
TestCase.assertEqual(obj.arrayCol.length, 1);
|
|
|
|
|
|
|
|
array = obj.arrayCol;
|
2015-08-28 21:41:30 +00:00
|
|
|
TestCase.assertEqual(array.push([4]), 2);
|
2015-08-27 20:33:20 +00:00
|
|
|
TestCase.assertEqual(array.length, 2);
|
|
|
|
TestCase.assertEqual(array[1].doubleCol, 4);
|
|
|
|
|
2015-08-28 21:41:30 +00:00
|
|
|
TestCase.assertEqual(array.push(obj.objectCol, obj.objectCol1), 4);
|
|
|
|
TestCase.assertEqual(array.length, 4);
|
2015-08-27 20:33:20 +00:00
|
|
|
TestCase.assertEqual(array[2].doubleCol, 1);
|
2015-08-28 21:41:30 +00:00
|
|
|
TestCase.assertEqual(array[3].doubleCol, 2);
|
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
array.push();
|
|
|
|
});
|
2015-10-12 22:25:23 +00:00
|
|
|
});
|
2015-08-27 20:33:20 +00:00
|
|
|
|
2015-08-28 21:41:30 +00:00
|
|
|
TestCase.assertEqual(array.length, 4);
|
2015-10-12 09:23:09 +00:00
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
array.push([1]);
|
|
|
|
}, 'can only push in a write transaction');
|
2015-08-28 21:49:15 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
testPop: function() {
|
|
|
|
var realm = new Realm({schema: [LinkTypesObjectSchema, TestObjectSchema]});
|
|
|
|
var array;
|
2015-10-12 09:23:09 +00:00
|
|
|
|
2015-08-28 21:49:15 +00:00
|
|
|
realm.write(function() {
|
2015-08-28 22:11:50 +00:00
|
|
|
var obj = realm.create('LinkTypesObject', [[1], [2], [[3], [4]]]);
|
2015-08-28 21:49:15 +00:00
|
|
|
array = obj.arrayCol;
|
|
|
|
|
2015-08-28 22:11:50 +00:00
|
|
|
TestCase.assertEqual(array.pop().doubleCol, 4);
|
2015-08-28 21:49:15 +00:00
|
|
|
TestCase.assertEqual(array.pop().doubleCol, 3);
|
|
|
|
TestCase.assertEqual(array.length, 0);
|
|
|
|
|
|
|
|
TestCase.assertEqual(array.pop(), undefined);
|
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
array.pop(1);
|
|
|
|
});
|
2015-08-27 20:33:20 +00:00
|
|
|
});
|
2015-08-28 21:49:15 +00:00
|
|
|
|
2015-10-12 09:23:09 +00:00
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
array.pop();
|
|
|
|
}, 'can only pop in a write transaction');
|
2015-08-28 21:41:30 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
testUnshift: function() {
|
|
|
|
var realm = new Realm({schema: [LinkTypesObjectSchema, TestObjectSchema]});
|
|
|
|
var array;
|
2015-10-12 09:23:09 +00:00
|
|
|
|
2015-08-28 21:41:30 +00:00
|
|
|
realm.write(function() {
|
|
|
|
var obj = realm.create('LinkTypesObject', [[1], [2], [[3]]]);
|
|
|
|
TestCase.assertEqual(obj.arrayCol.length, 1);
|
|
|
|
|
|
|
|
array = obj.arrayCol;
|
|
|
|
TestCase.assertEqual(array.unshift([5]), 2);
|
|
|
|
TestCase.assertEqual(array.length, 2);
|
|
|
|
TestCase.assertEqual(array[0].doubleCol, 5);
|
|
|
|
|
|
|
|
TestCase.assertEqual(array.unshift(obj.objectCol, obj.objectCol1), 4);
|
|
|
|
TestCase.assertEqual(array.length, 4);
|
|
|
|
TestCase.assertEqual(array[0].doubleCol, 1);
|
|
|
|
TestCase.assertEqual(array[1].doubleCol, 2);
|
|
|
|
});
|
|
|
|
|
|
|
|
TestCase.assertEqual(array.length, 4);
|
2015-10-12 09:23:09 +00:00
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
array.unshift([1]);
|
|
|
|
}, 'can only unshift in a write transaction');
|
2015-08-28 21:41:30 +00:00
|
|
|
},
|
|
|
|
|
2015-08-28 22:11:50 +00:00
|
|
|
testShift: function() {
|
|
|
|
var realm = new Realm({schema: [LinkTypesObjectSchema, TestObjectSchema]});
|
|
|
|
var array;
|
2015-10-12 09:23:09 +00:00
|
|
|
|
2015-08-28 22:11:50 +00:00
|
|
|
realm.write(function() {
|
|
|
|
var obj = realm.create('LinkTypesObject', [[1], [2], [[3], [4]]]);
|
|
|
|
array = obj.arrayCol;
|
|
|
|
|
|
|
|
TestCase.assertEqual(array.shift().doubleCol, 3);
|
|
|
|
TestCase.assertEqual(array.shift().doubleCol, 4);
|
|
|
|
TestCase.assertEqual(array.length, 0);
|
|
|
|
|
|
|
|
TestCase.assertEqual(array.shift(), undefined);
|
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
array.shift(1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-10-12 09:23:09 +00:00
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
array.shift();
|
|
|
|
}, 'can only shift in a write transaction');
|
2015-08-28 22:11:50 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
testSplice: function() {
|
|
|
|
var realm = new Realm({schema: [LinkTypesObjectSchema, TestObjectSchema]});
|
2015-10-12 09:23:09 +00:00
|
|
|
var array;
|
2015-09-28 23:00:24 +00:00
|
|
|
|
2015-08-28 22:11:50 +00:00
|
|
|
realm.write(function() {
|
|
|
|
var obj = realm.create('LinkTypesObject', [[1], [2], [[3], [4]]]);
|
2015-09-28 23:00:24 +00:00
|
|
|
var removed;
|
2015-08-28 22:11:50 +00:00
|
|
|
|
2015-10-12 09:23:09 +00:00
|
|
|
array = obj.arrayCol;
|
|
|
|
|
2015-09-28 23:00:24 +00:00
|
|
|
removed = array.splice(0, 0, obj.objectCol, obj.objectCol1);
|
|
|
|
TestCase.assertEqual(removed.length, 0);
|
2015-08-28 22:11:50 +00:00
|
|
|
TestCase.assertEqual(array.length, 4);
|
|
|
|
TestCase.assertEqual(array[0].doubleCol, 1);
|
|
|
|
TestCase.assertEqual(array[1].doubleCol, 2);
|
|
|
|
|
2015-09-28 23:00:24 +00:00
|
|
|
removed = array.splice(2, 2, [5], [6]);
|
|
|
|
TestCase.assertEqual(removed.length, 2);
|
|
|
|
TestCase.assertEqual(removed[0].doubleCol, 3);
|
|
|
|
TestCase.assertEqual(removed[1].doubleCol, 4);
|
2015-08-28 22:11:50 +00:00
|
|
|
TestCase.assertEqual(array.length, 4);
|
|
|
|
TestCase.assertEqual(array[2].doubleCol, 5);
|
|
|
|
TestCase.assertEqual(array[3].doubleCol, 6);
|
|
|
|
|
2015-09-28 23:00:24 +00:00
|
|
|
removed = array.splice(2, 2);
|
|
|
|
TestCase.assertEqual(removed.length, 2);
|
|
|
|
TestCase.assertEqual(removed[0].doubleCol, 5);
|
|
|
|
TestCase.assertEqual(removed[1].doubleCol, 6);
|
|
|
|
TestCase.assertEqual(array.length, 2);
|
2015-08-28 22:11:50 +00:00
|
|
|
TestCase.assertEqual(array[0].doubleCol, 1);
|
|
|
|
TestCase.assertEqual(array[1].doubleCol, 2);
|
|
|
|
|
2015-09-28 23:00:24 +00:00
|
|
|
removed = array.splice(-1, 1);
|
|
|
|
TestCase.assertEqual(removed.length, 1);
|
|
|
|
TestCase.assertEqual(removed[0].doubleCol, 2);
|
2015-08-28 22:11:50 +00:00
|
|
|
TestCase.assertEqual(array.length, 1);
|
|
|
|
TestCase.assertEqual(array[0].doubleCol, 1);
|
|
|
|
|
2015-09-28 23:00:24 +00:00
|
|
|
removed = array.splice(0, 2);
|
|
|
|
TestCase.assertEqual(removed.length, 1);
|
|
|
|
TestCase.assertEqual(removed[0].doubleCol, 1);
|
|
|
|
TestCase.assertEqual(array.length, 0);
|
2015-10-12 23:50:45 +00:00
|
|
|
|
|
|
|
removed = array.splice('0', '0', obj.objectCol);
|
|
|
|
TestCase.assertEqual(removed.length, 0);
|
|
|
|
TestCase.assertEqual(array.length, 1);
|
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
array.splice('cat', 1);
|
|
|
|
});
|
|
|
|
|
2015-08-28 22:11:50 +00:00
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
array.splice(0, 0, 0);
|
|
|
|
});
|
|
|
|
});
|
2015-10-12 09:23:09 +00:00
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
obj.arrayCol.splice(0, 0, obj.objectCol);
|
|
|
|
}, 'can only splice in a write transaction');
|
2015-08-28 22:11:50 +00:00
|
|
|
},
|
2015-08-14 17:47:56 +00:00
|
|
|
};
|