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); } } }
public void PerformanceTest() { byte[] payload; using (var memoryStream = new MemoryStream()) { using (var resourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream( typeof(EmberPayloads), "BigPayload.ember")) { resourceStream.CopyTo(memoryStream); } payload = memoryStream.ToArray(); } var stopwatch = new Stopwatch(); var converter = new EmberConverter(GlowTypes.Instance); GC.Collect(); GC.WaitForPendingFinalizers(); for (int index = 0; index < 100; ++index) { using (var writer = XmlWriter.Create(TextWriter.Null)) { stopwatch.Start(); using (var stream = new MemoryStream(payload)) using (var reader = new EmberReader(stream)) { converter.ToXml(reader, writer); } stopwatch.Stop(); } } stopwatch.Start(); GC.Collect(); GC.WaitForPendingFinalizers(); stopwatch.Stop(); Console.WriteLine(stopwatch.ElapsedMilliseconds); }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// private static void AssertXmlException(string xml, string expectedMessage) { using (var stringReader = new StringReader(xml)) using (var reader = XmlReader.Create(stringReader)) { var converter = new EmberConverter(); try { converter.FromXml(reader); throw new ArgumentException( string.Format(CultureInfo.InvariantCulture, "Unexpected success with xml {0}.", xml), nameof(xml)); } catch (XmlException ex) { Assert.AreEqual(expectedMessage, ex.Message); } } }
public void ReadFileTest() { var settings = new XmlWriterSettings() { Indent = true, ConformanceLevel = ConformanceLevel.Fragment }; var converter = new EmberConverter(GlowTypes.Instance); int totalInputLength = 0; int totalOutputLength = 0; foreach (var resourceName in Assembly.GetExecutingAssembly().GetManifestResourceNames()) { if (resourceName.Contains("Ember.EmberDataPayloads") && resourceName.EndsWith(".ember", StringComparison.Ordinal)) { byte[] input; using (var source = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)) using (var destination = new MemoryStream()) { source.CopyTo(destination); input = destination.ToArray(); } string xml; using (var stringWriter = new StringWriter(CultureInfo.InvariantCulture)) { using (var writer = XmlWriter.Create(stringWriter, settings)) { converter.ToXml(input, writer); } xml = stringWriter.ToString(); } Console.WriteLine(xml); byte[] output; using (var stringReader = new StringReader(xml)) using (var reader = XmlReader.Create(stringReader)) { output = converter.FromXml(reader); } totalInputLength += input.Length; totalOutputLength += output.Length; Console.WriteLine("Ratio: {0:P}", (double)output.Length / input.Length); Console.WriteLine(); using (var stringWriter = new StringWriter(CultureInfo.InvariantCulture)) { using (var writer = XmlWriter.Create(stringWriter, settings)) { converter.ToXml(output, writer); } Assert.AreEqual(xml, stringWriter.ToString()); } } } Console.WriteLine("Overall Ratio: {0:P}", (double)totalOutputLength / totalInputLength); Console.WriteLine(); }