public void ExceptionTest() { AssertThrow<ArgumentNullException>(() => new EmberWriter(null, 1).Dispose()); AssertThrow<ArgumentException>(() => new EmberWriter(new MemoryStream(), 0).Dispose()); using (var writer = new EmberWriter(new MemoryStream(), 1)) { var outer = EmberId.CreateApplication(0); AssertThrow<ArgumentNullException>( () => writer.WriteValue(outer, (byte[])null), () => writer.WriteValue(outer, (int[])null), () => writer.WriteValue(outer, (string)null)); AssertThrow<ArgumentOutOfRangeException>( () => writer.WriteStartApplicationDefinedType(outer, InnerNumber.FirstApplication - 1)); writer.Dispose(); AssertThrow<ObjectDisposedException>( () => writer.WriteValue(outer, true), () => writer.WriteValue(outer, 0), () => writer.WriteValue(outer, new byte[] { }), () => writer.WriteValue(outer, 0.0), () => writer.WriteValue(outer, string.Empty), () => writer.WriteValue(outer, new int[] { }), () => writer.WriteStartSequence(outer), () => writer.WriteStartSet(outer), () => writer.WriteStartApplicationDefinedType(outer, InnerNumber.FirstApplication), () => writer.WriteEndContainer()); } }
public void ExceptionTest() { AssertThrow<ArgumentNullException>( () => new EmberType(null).Ignore(), () => new EmberTypeBag(null).Ignore(), () => new EmberConverter(null).Ignore()); AssertThrow<ArgumentException>(() => new EmberType().Ignore()); using (var stream = new MemoryStream()) using (var reader = new EmberReader(stream)) using (var writer = XmlWriter.Create(new StringBuilder())) { var converter = new EmberConverter(); AssertThrow<ArgumentNullException>( () => converter.ToXml((byte[])null, writer), () => converter.ToXml(new byte[0], null), () => converter.ToXml((EmberReader)null, writer), () => converter.ToXml(reader, null)); } using (var stringReader = new StringReader(string.Empty)) using (var reader = XmlReader.Create(stringReader)) using (var stream = new MemoryStream()) using (var writer = new EmberWriter(stream)) { var converter = new EmberConverter(); AssertThrow<ArgumentNullException>( () => converter.FromXml(null), () => converter.FromXml(null, writer), () => converter.FromXml(reader, null)); } AssertXmlException("<whatever type=\"A-11\"></whatever>", "Unknown field path: whatever."); AssertXmlException( "<A-0 type=\"Sequence\"><whatever type=\"Set\" /></A-0>", "Unknown field path: A-0.whatever."); AssertXmlException("<A-0 type=\"C-11\"></A-0>", "Unknown type: C-11."); AssertXmlException( "<A-0 type=\"A-11\" whatever=\"\"></A-0>", "Unexpected Attribute Count: Each element must have exactly one type attribute."); AssertXmlException( "<A-0 type=\"A-11\"><![CDATA[Whatever]]></A-0>", "Unexpected Node Type: Encountered CDATA while looking for Element."); AssertXmlException("<A-0 type=\"Boolean\" />", "Unexpected empty element for a field of type Boolean."); }
public void InvalidXmlCharactersTest() { using (var stream = new MemoryStream()) { using (var writer = new EmberWriter(stream)) { writer.WriteValue(EmberId.CreateContextSpecific(0), "\0"); } var builder = new StringBuilder(); using (var xmlWriter = XmlWriter.Create(builder)) { var converter = new EmberConverter(); converter.ToXml(stream.ToArray(), xmlWriter); } } }
private object CopyCore(EmberWriter writer, int inner) { switch (inner) { case Ember.InnerNumber.Boolean: var boolean = this.ReadContentsAsBoolean(); writer.WriteValue(this.outer.GetValueOrDefault(), boolean); return boolean; case Ember.InnerNumber.Integer: var int64 = this.ReadContentsAsInt64(); writer.WriteValue(this.outer.GetValueOrDefault(), int64); return int64; case Ember.InnerNumber.Octetstring: var byteArray = this.ReadContentsAsByteArray(); writer.WriteValue(this.outer.GetValueOrDefault(), byteArray); return byteArray; case Ember.InnerNumber.Real: var dbl = this.ReadContentsAsDouble(); writer.WriteValue(this.outer.GetValueOrDefault(), dbl); return dbl; case Ember.InnerNumber.Utf8String: var str = this.ReadContentsAsString(); writer.WriteValue(this.outer.GetValueOrDefault(), str); return str; case Ember.InnerNumber.RelativeObjectIdentifier: var int32Array = this.ReadContentsAsInt32Array(); writer.WriteValue(this.outer.GetValueOrDefault(), int32Array); return int32Array; case Ember.InnerNumber.Sequence: writer.WriteStartSequence(this.outer.GetValueOrDefault()); this.CopyToEndContainer(writer, null); return null; case Ember.InnerNumber.Set: writer.WriteStartSet(this.outer.GetValueOrDefault()); this.CopyToEndContainer(writer, null); return null; default: writer.WriteStartApplicationDefinedType(this.outer.GetValueOrDefault(), inner); this.CopyToEndContainer(writer, null); return null; } }
/// <summary>Reads data and writes it to <paramref name="writer"/> until the end of the current container is /// reached.</summary> /// <returns>The contents of the of the data value with the outer id <paramref name="outerId"/>, if such a /// data value was found in the current container and its contents is primitive; otherwise, <c>null</c>. /// </returns> /// <exception cref="ObjectDisposedException"><see cref="Dispose"/> has been called.</exception> /// <remarks> /// <para>While <see cref="Read"/> returns <c>true</c> and <see cref="InnerNumber"/> is not equal to /// <see cref="Ember.InnerNumber.EndContainer"/>, calls <see cref="Copy"/>.</para> /// </remarks> public object CopyToEndContainer(EmberWriter writer, EmberId? outerId) { if (writer == null) { throw new ArgumentNullException(nameof(writer)); } object result = null; var inner = -1; while (this.Read() && ((inner = this.innerNumber.GetValueOrDefault()) != Ember.InnerNumber.EndContainer)) { var candidate = this.CopyCore(writer, inner); if (this.outer.HasValue && (this.outer.Value == outerId)) { result = candidate; } } if (inner == Ember.InnerNumber.EndContainer) { writer.WriteEndContainer(); } return result; }
/// <summary>Reads the current data value and writes it to <paramref name="writer"/>.</summary> /// <returns>The contents of the of the data value if it is primitive; otherwise, <c>null</c>.</returns> /// <exception cref="InvalidOperationException"> /// <list type="bullet"> /// <item><see cref="Read"/> has never been called, or</item> /// <item>The last <see cref="Read"/> call returned <c>false</c> or threw an exception.</item> /// </list></exception> /// <exception cref="ObjectDisposedException"><see cref="Dispose"/> has been called.</exception> /// <remarks> /// <para>If the <see cref="EmberReader"/> instance is currently placed on the start of a container, then skips /// to the end of the container, such that calling <see cref="Read"/> afterwards will place the reader on either /// a sibling of the container, the end of the parent container or the end of the stream.</para> /// <para>This method has no effect, if the reader is currently placed on a data value with primitive encoding /// (the next call to <see cref="Read"/> will skip possibly unread contents anyway).</para> /// </remarks> public object Copy(EmberWriter writer) => this.CopyCore(writer, this.InnerNumber);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// private static void AssertEncode(Action<EmberWriter> write, params byte[] expected) { MemoryStream stream; using (stream = new MemoryStream()) using (var writer = new EmberWriter(stream, 1)) { write(writer); } CollectionAssert.AreEqual(expected, stream.ToArray()); }