public void CourtesyFlush() { var sw = new StringWriter(); var ss = new StreamSpy(new MemoryStream(), sw); string instructions; var xs = Enumerable.Repeat(1, 64); var json = "[" + String.Join(",", xs) + "]"; sw.WriteLine("# " + @"[ + String.Join(',', Enumerable.Repeat(1, 64)) + ]"); Assert.Equal(json, ToJson(xs, ss, out instructions)); sw.WriteLine(); xs = Enumerable.Repeat(int.MinValue, 64); json = "[" + String.Join(",", xs) + "]"; sw.WriteLine("# " + @"[ + String.Join(',', Enumerable.Repeat(int.MinValue, 64)) + ]"); Assert.Equal(json, ToJson(xs, ss, out instructions)); // poke around the boundaries of filling up the 64 byte _Buffer foreach (int len in new[] { 0, 1, 44, 45, 46, 89, 90, 91 }) { sw.WriteLine(); var bs = new byte[len]; json = "\"" + Convert.ToBase64String(bs) + "\""; sw.WriteLine("# [0,...] -> base 64 -- " + len); Assert.Equal(json, ToJson(bs, ss, out instructions)); } ApprovalTests.Approvals.Verify(sw.ToString()); }
public void EnumerableValues() { var sw = new StringWriter(); var ss = new StreamSpy(new MemoryStream(), sw); AssertEqual("[1]", new List <int> { 1 }, ss); AssertEqual("[12]", new List <int> { 12 }, ss); AssertEqual("[1,null]", new List <int?> { 1, null }, ss); AssertEqual("[1]", new[] { 1 }, ss); AssertEqual("[1,null]", new int?[] { 1, null }, ss); AssertEqual("{\"i\":1}", A(1), ss); AssertEqual("{\"i\":0.5,\"j\":\"00000000-0000-0000-0000-000000000000\"}", A(0.5, Guid.Empty), ss); AssertEqual("{\"i\":1,\"j\":2}", new Dictionary <char, int> { { 'i', 1 }, { 'j', 2 }, }, ss); AssertEqual("[{\"i\":1}]", new[] { A(1) }, ss); AssertEqual("{\"i\":[1]}", A(new[] { 1 }), ss); ApprovalTests.Approvals.Verify(sw.ToString()); }
public void StreamInstructions() { var sb = new StringBuilder(); // not trying to approve the result of this stream spy since we're only interested in IL here var sw = new StringWriter(); var ms = new StreamSpy(new MemoryStream(), sw); string instructions; sb.AppendLine("# IEnumerable<int>"); sb.AppendLine(); ToJson <IEnumerable <int> >(null, ms, out instructions); sb.AppendLine(instructions); sb.AppendLine(); sb.AppendLine("# string"); sb.AppendLine(); ToJson <string>(null, ms, out instructions); sb.AppendLine(instructions); sb.AppendLine(); sb.AppendLine("# string[]"); sb.AppendLine(); ToJson <string[]>(null, ms, out instructions); sb.AppendLine(instructions); sb.AppendLine(); sb.AppendLine("# List<string>"); sb.AppendLine(); ToJson <List <string> >(null, ms, out instructions); sb.AppendLine(instructions); ApprovalTests.Approvals.Verify(sb.ToString()); }
public void DisposeReaderStreamSpy() { if (ReaderStreamSpy != null) { ReaderStreamSpy.Dispose(); ReaderStreamSpy = null; } }
public void DisposeWriterStreamSpy() { if (WriterStreamSpy != null) { WriterStreamSpy.Dispose(); WriterStreamSpy = null; } }
private void DisposeReaderStreamSpy() { if (readerStreamSpy != null) { readerStreamSpy.Dispose(); readerStreamSpy.ClonedStream.Dispose(); readerStreamSpy = null; } }
void AssertValue <T>(T value, string description, StreamSpy ss) { string instructions; var json = Newtonsoft.Json.JsonConvert.SerializeObject(value, new NewtonsoftConverters.IPAddressConverter()); ss.Log.WriteLine("# " + description); Assert.Equal(json, ToJson(value, ss, out instructions)); ss.Log.WriteLine(); }
void AssertString(string value, string description, StreamSpy ss) { string instructions; var json = Newtonsoft.Json.JsonConvert.SerializeObject(value); ss.Log.WriteLine("# " + description); Assert.Equal(json, ToJson(value, ss, out instructions)); ss.Log.WriteLine(); }
private void DisposeWriterStreamSpy() { if (writerStreamSpy != null) { writerStreamSpy.Dispose(); writerStreamSpy.ClonedStream.Dispose(); writerStreamSpy = null; } }
protected override XmlReader GetReaderForMessage(SoapClientMessage message, int bufferSize) { // Dispose previous reader stream spy. DisposeReaderStreamSpy(); readerStreamSpy = new StreamSpy(message.Stream); xmlReader = new XmlTextReader(readerStreamSpy); return(xmlReader); }
void AssertEqual <T>(string expected, T value, StreamSpy ss) { ss.Log.WriteLine("# " + expected); string instructions; Assert.Equal(expected, ToJson(value, ss, out instructions)); ss.Log.WriteLine(); }
protected override XmlWriter GetWriterForMessage(SoapClientMessage message, int bufferSize) { // Dispose previous writer stream spy. DisposeWriterStreamSpy(); writerStreamSpy = new StreamSpy(message.Stream); // XML should always support UTF8. xmlWriter = new XmlTextWriter(writerStreamSpy, Encoding.UTF8); return(xmlWriter); }
void AssertNullableValue <T>(T value, string expected, StreamSpy ms) where T : struct { ms.Log.WriteLine("# " + expected); Assert.Equal(expected, ToJson(value, ms)); ms.Log.WriteLine(); ms.Log.WriteLine("# " + expected); Assert.Equal(expected, ToJson((T?)value, ms)); ms.Log.WriteLine(); ms.Log.WriteLine("# null"); Assert.Equal("null", ToJson((T?)null, ms)); ms.Log.WriteLine(); }
public void SimpleValues() { var sw = new StringWriter(); var ms = new StreamSpy(new MemoryStream(), sw); AssertNullableValue(1, "1", ms); AssertNullableValue(true, "true", ms); AssertNullableValue(false, "false", ms); var g = Guid.Parse("171d7051-0c40-4475-9568-e8df9da1fb53"); AssertNullableValue(g, "\"" + g + "\"", ms); var d = new DateTime(2014, 11, 18, 7, 42, 41, 111, DateTimeKind.Utc); AssertNullableValue(d, Newtonsoft.Json.JsonConvert.SerializeObject(d), ms); AssertEqual("\"1.2.3.4\"", System.Net.IPAddress.Parse("1.2.3.4"), ms); ApprovalTests.Approvals.Verify(sw.ToString()); }
unsafe string ToJson <T>(T value, StreamSpy ss, out string instructions) { var ms = ss.Underlying as MemoryStream; ms.Position = 0; try { var schema = Schema.Reflect(typeof(T)); var emit = DelegateBuilder.CreateStream <T>(schema); var del = emit.CreateDelegate <Serializer.WriteToStream <T> >(out instructions); fixed(byte *ptr = _Buffer) { del.Invoke(ref value, ptr, ss, _Buffer); } } catch (Sigil.SigilVerificationException sve) { Trace.WriteLine(sve); instructions = sve.GetDebugInfo(); throw; } return(Encoding.UTF8.GetString(ms.GetBuffer(), 0, (int)ms.Position)); }
unsafe string ToJson <T>(T value, StreamSpy ms) { string instructions; return(ToJson(value, ms, out instructions)); }
/// <summary>Get Reader for Response</summary> protected override XmlReader GetReaderForMessage(SoapClientMessage message, int bufferSize) { DisposeReaderStreamSpy(); return(StreamSpy.GetReaderForMessage(message, bufferSize, out ReaderStreamSpy)); }
public void DirectStrings() { var sw = new StringWriter(); var ss = new StreamSpy(new MemoryStream(), sw); string theValue; theValue = null; AssertString(theValue, "null", ss); theValue = ""; AssertString(theValue, "empty string", ss); theValue = "a"; AssertString(theValue, "a", ss); theValue = String.Concat(Enumerable.Repeat("a", 62)); AssertString(theValue, "a * 62", ss); theValue = String.Concat(Enumerable.Repeat("a", 63)); AssertString(theValue, "a * 63", ss); theValue = String.Concat(Enumerable.Repeat("a", 124)); AssertString(theValue, "a * 124", ss); theValue = String.Concat(Enumerable.Repeat("a", 125)); AssertString(theValue, "a * 125", ss); theValue = String.Concat(Enumerable.Repeat("a", 126)); AssertString(theValue, "a * 126", ss); theValue = ((char)0).ToString(); AssertString(theValue, "(char)0", ss); theValue = String.Concat(Enumerable.Repeat(((char)0).ToString(), 10)); AssertString(theValue, "(char)0 * 10", ss); theValue = String.Concat(Enumerable.Repeat(((char)0).ToString(), 11)); AssertString(theValue, "(char)0 * 11", ss); theValue = String.Concat(Enumerable.Repeat("\"", 31)); AssertString(theValue, "\" * 31", ss); theValue = String.Concat(Enumerable.Repeat("\"", 32)); AssertString(theValue, "\" * 32", ss); theValue = String.Concat(Enumerable.Repeat("\u00EE", 32)); AssertString(theValue, "\\u00EE * 32", ss); theValue = String.Concat(Enumerable.Repeat("𝕡𝕣𝕚𝕫𝕖", 8)); AssertString(theValue, "unicode 'prize' * 8", ss); AssertValue(new[] { "one" }, "[ one ]", ss); List <string> xs; xs = Enumerable.Range(0, 26).Select(i => i.ToString()).ToList(); AssertValue(xs, "[ \"0\", ... , \"25\" ]", ss); xs = Enumerable.Range(0, 27).Select(i => i.ToString()).ToList(); AssertValue(xs, "[ \"0\", ... , \"26\" ]", ss); xs = Enumerable.Range(0, 192).Select(i => ((char)i).ToString()).ToList(); AssertValue(xs, "[ \"\\u0000\", ... , \"\\u00bf\" ]", ss); xs = Enumerable.Range(0, 26).Select(i => (string)null).ToList(); AssertValue(xs, "[ null * 25 ]", ss); AssertValue(A("b", "c"), "{ i: b, j: c }", ss); ApprovalTests.Approvals.Verify(sw.ToString()); }
/// <summary>Get Writer for Request</summary> protected override XmlWriter GetWriterForMessage(SoapClientMessage message, int bufferSize) { DisposeWriterStreamSpy(); return(StreamSpy.GetWriterForMessage(message, bufferSize, base.RequestEncoding, out WriterStreamSpy)); }