private Document(string id, MetadataStack metadata, FilePath source, Stream stream, object streamLock, IEnumerable <KeyValuePair <string, object> > items, bool disposeStream) { if (source?.IsAbsolute == false) { throw new ArgumentException("Document sources must be absolute", nameof(source)); } Id = id ?? throw new ArgumentNullException(nameof(id)); Source = source; _metadata = items == null ? metadata : metadata.Clone(items); if (stream == null) { _stream = Stream.Null; } else { if (!stream.CanRead) { throw new ArgumentException("Document stream must support reading.", nameof(stream)); } if (!stream.CanSeek) { _stream = new SeekableStream(stream, disposeStream); _disposeStream = true; } else { _stream = stream; _disposeStream = disposeStream; } } _streamLock = streamLock ?? new object(); }
public void CanCloneWithNewValues() { // Given MetadataDictionary initialMetadata = new MetadataDictionary(); MetadataStack metadata = new MetadataStack(initialMetadata); // When metadata = metadata.Clone(new[] { new KeyValuePair <string, object>("A", "a") }); // Then Assert.AreEqual("a", metadata["A"]); }
public void ConvertStringToIntArray() { // Given MetadataDictionary initialMetadata = new MetadataDictionary(); MetadataStack metadata = new MetadataStack(initialMetadata); // When metadata = metadata.Clone(new[] { new KeyValuePair <string, object>("A", "1") }); IMetadata <int[]> metadataAs = metadata.MetadataAs <int[]>(); // Then Assert.AreEqual("1", metadata["A"]); CollectionAssert.AreEqual(new int[] { 1 }, (IEnumerable)metadataAs["A"]); }
public void ConvertIntArrayToStringEnumerable() { // Given MetadataDictionary initialMetadata = new MetadataDictionary(); MetadataStack metadata = new MetadataStack(initialMetadata); // When metadata = metadata.Clone(new[] { new KeyValuePair <string, object>("A", new int[] { 1, 2, 3 }) }); IMetadata <IEnumerable <string> > metadataAs = metadata.MetadataAs <IEnumerable <string> >(); // Then CollectionAssert.AreEqual(new int[] { 1, 2, 3 }, (IEnumerable)metadata["A"]); CollectionAssert.AreEqual(new string[] { "1", "2", "3" }, metadataAs["A"]); }
public void ConvertIntToString() { // Given MetadataDictionary initialMetadata = new MetadataDictionary(); MetadataStack metadata = new MetadataStack(initialMetadata); // When metadata = metadata.Clone(new[] { new KeyValuePair <string, object>("A", 1) }); IMetadata <string> metadataAs = metadata.MetadataAs <string>(); // Then Assert.AreEqual(1, metadata["A"]); Assert.AreEqual("1", metadataAs["A"]); }
public void ReturnsCorrectDirectoryPathForDirectoryPath(string path, string expected) { // Given MetadataDictionary initialMetadata = new MetadataDictionary(); MetadataStack metadata = new MetadataStack(initialMetadata); // When metadata = metadata.Clone(new[] { new KeyValuePair <string, object>("A", new DirectoryPath(path)) }); object result = metadata.DirectoryPath("A"); // Then Assert.IsInstanceOf <DirectoryPath>(result); Assert.AreEqual(expected, ((DirectoryPath)result).FullPath); }
public void ReturnsCorrectStringForFilePath(string path, string expected) { // Given MetadataDictionary initialMetadata = new MetadataDictionary(); MetadataStack metadata = new MetadataStack(initialMetadata); // When metadata = metadata.Clone(new[] { new KeyValuePair <string, object>("A", new FilePath(path)) }); object result = metadata.String("A"); // Then Assert.IsInstanceOf <string>(result); Assert.AreEqual(expected, result); }
public void ContainsNewValues() { // Given MetadataDictionary initialMetadata = new MetadataDictionary { ["A"] = "a" }; MetadataStack metadata = new MetadataStack(initialMetadata); // When MetadataStack clone = metadata.Clone(new Dictionary <string, object> { { "B", "b" } }); // Then Assert.AreEqual("b", clone["B"]); }
public void ClonedMetadataDoesNotContainNewValues() { // Given MetadataDictionary initialMetadata = new MetadataDictionary { ["A"] = "a" }; MetadataStack metadata = new MetadataStack(initialMetadata); // When MetadataStack clone = metadata.Clone(new Dictionary <string, object> { { "B", "b" } }); // Then Assert.IsFalse(metadata.ContainsKey("B")); }
public void ReplacesValue() { // Given MetadataDictionary initialMetadata = new MetadataDictionary { ["A"] = "a" }; MetadataStack metadata = new MetadataStack(initialMetadata); // When MetadataStack clone = metadata.Clone(new Dictionary <string, object> { { "A", "b" } }); // Then Assert.AreEqual("a", metadata["A"]); Assert.AreEqual("b", clone["A"]); }
public void ReturnsCorrectFilePathForString(string path, string expected) { // Given MetadataDictionary initialMetadata = new MetadataDictionary(); MetadataStack metadata = new MetadataStack(initialMetadata); // When metadata = metadata.Clone(new[] { new KeyValuePair <string, object>("A", path) }); object result = metadata.FilePath("A"); // Then if (expected == null) { Assert.IsNull(result); } else { Assert.IsInstanceOf <FilePath>(result); Assert.AreEqual(expected, ((FilePath)result).FullPath); } }