/// <summary> /// Tests the validity of an enumerable reference. /// </summary> /// <param name="reference">The reference to test.</param> /// <param name="targetValue">The actual collection pointed by the reference.</param> public static void TestReferenceEnumerable(ReferenceEnumerable reference, object targetValue) { var collection = (ICollection)targetValue; // Check that the reference is not null. Assert.NotNull(reference); // Check that the counts match. Assert.Equal(collection.Count, reference.Count); Assert.Equal(collection.Count, reference.Indices.Count); // Check that the object references match. foreach (var objReference in reference.Zip(collection.Cast <object>(), Tuple.Create)) { Assert.Equal(objReference.Item2, objReference.Item1.ObjectValue); if (objReference.Item2 != null) { TestNonNullObjectReference(objReference.Item1, objReference.Item2, true); } else { TestNullObjectReference(objReference.Item1); } } // TODO: rework reference system and enable this //Assert.Null(reference.Index); }
internal static IReference CreateReference(object objectValue, Type objectType, object index) { if (objectValue != null && !objectType.IsInstanceOfType(objectValue)) throw new ArgumentException(@"objectValue type does not match objectType", "objectValue"); ++creatingReference; IReference reference; var isCollection = HasCollectionReference(objectValue != null ? objectValue.GetType() : objectType); if (objectValue != null && isCollection && index == NotInCollection) { reference = new ReferenceEnumerable((IEnumerable)objectValue, objectType, index); } else { reference = new ObjectReference(objectValue, objectType, index); } --creatingReference; return reference; }
internal static IReference CreateReference(object objectValue, Type objectType, Index index) { if (objectValue != null && !objectType.IsInstanceOfType(objectValue)) throw new ArgumentException(@"objectValue type does not match objectType", nameof(objectValue)); if (!CreatingReference.IsValueCreated) CreatingReference.Value = 0; ++CreatingReference.Value; IReference reference; var isCollection = HasCollectionReference(objectValue?.GetType() ?? objectType); if (objectValue != null && isCollection && index.IsEmpty) { reference = new ReferenceEnumerable((IEnumerable)objectValue, objectType, index); } else { reference = new ObjectReference(objectValue, objectType, index); } --CreatingReference.Value; return reference; }