diff --git a/Tests/YDotNet.Tests.Unit/Extensions/Conversion.cs b/Tests/YDotNet.Tests.Unit/Extensions/Conversion.cs index d10b2e41..83db7b19 100644 --- a/Tests/YDotNet.Tests.Unit/Extensions/Conversion.cs +++ b/Tests/YDotNet.Tests.Unit/Extensions/Conversion.cs @@ -70,6 +70,33 @@ public void CanParseMapsToObject() } } + [Test] + public void CanParseTextToString() + { + // Arrange + var doc = new Doc(); + var map = doc.Map("map"); + + using (var transaction = doc.WriteTransaction()) + { + map.Insert(transaction, "inner", Input.Map(new Dictionary + { + {"value", Input.Text("Hello YDotNet")} + })); + } + + using (var transaction = doc.ReadTransaction()) + { + var inner = map.Get(transaction, "inner"); + + // Act + var parsed = inner.To(transaction); + + // Assert + Assert.That(parsed.Value, Is.EqualTo("Hello YDotNet")); + } + } + [Test] public void ConvertToJson() { @@ -87,7 +114,7 @@ public void ConvertToJson() // Act string json; - using (var transaction = map.WriteTransaction()) + using (var transaction = map.ReadTransaction()) { var inner = map.Get(transaction, "inner"); @@ -97,4 +124,32 @@ public void ConvertToJson() // Assert StringAssert.Contains("Hello YDotNet", json); } + + [Test] + public void ConvertTextToJson() + { + // Arrange + var doc = new Doc(); + var map = doc.Map("map"); + + using (var transaction = doc.WriteTransaction()) + { + map.Insert(transaction, "inner", Input.Map(new Dictionary + { + {"value", Input.Text("Hello YDotNet")} + })); + } + + // Act + string json; + using (var transaction = map.WriteTransaction()) + { + var inner = map.Get(transaction, "inner"); + + json = inner.ToJson(transaction); + } + + // Assert + StringAssert.Contains("Hello YDotNet", json); + } } diff --git a/Tests/YDotNet.Tests.Unit/Maps/InsertTests.cs b/Tests/YDotNet.Tests.Unit/Maps/InsertTests.cs index ddb08fa8..937a476a 100644 --- a/Tests/YDotNet.Tests.Unit/Maps/InsertTests.cs +++ b/Tests/YDotNet.Tests.Unit/Maps/InsertTests.cs @@ -328,6 +328,68 @@ public void InsertDifferentTypeOnExistingKey() Assert.That(length, Is.EqualTo(expected: 1)); } + [Test] + public void InsertNestedString() + { + // Arrange + var (doc, map) = ArrangeDoc(); + + // Act + var transaction = doc.WriteTransaction(); + + var item1 = Input.Map(new Dictionary + { + { "id", Input.String(Guid.NewGuid().ToString()) }, + { "text", Input.String("Test") } + }); + + var item2 = Input.Map(new Dictionary + { + { "id", Input.String(Guid.NewGuid().ToString()) }, + { "text", Input.String("Describe the problem") } + }); + + var data = Input.Array(new[] { item1, item2 }); + map.Insert(transaction, "data", data); + var length = map.Length(transaction); + + transaction.Commit(); + + // Assert + Assert.That(length, Is.EqualTo(expected: 1)); + } + + [Test] + public void InsertNestedText() + { + // Arrange + var (doc, map) = ArrangeDoc(); + + // Act + var transaction = doc.WriteTransaction(); + + var item1 = Input.Map(new Dictionary + { + { "id", Input.String(Guid.NewGuid().ToString()) }, + { "text", Input.Text("Test") } + }); + + var item2 = Input.Map(new Dictionary + { + { "id", Input.String(Guid.NewGuid().ToString()) }, + { "text", Input.Text("Describe the problem") } + }); + + var data = Input.Array(new[] { item1, item2 }); + map.Insert(transaction, "data", data); + var length = map.Length(transaction); + + transaction.Commit(); + + // Assert + Assert.That(length, Is.EqualTo(expected: 1)); + } + [Test] public void InsertNestedMap() { diff --git a/YDotNet.Extensions/YDotNetExtensions.cs b/YDotNet.Extensions/YDotNetExtensions.cs index 1f98772b..4587d2f5 100644 --- a/YDotNet.Extensions/YDotNetExtensions.cs +++ b/YDotNet.Extensions/YDotNetExtensions.cs @@ -136,6 +136,9 @@ static void WriteValue(Output output, Utf8JsonWriter jsonWriter, Transaction tra case OutputTag.String: jsonWriter.WriteStringValue(output.String); break; + case OutputTag.Text: + jsonWriter.WriteStringValue(output.Text.String(transaction)); + break; case OutputTag.JsonArray: WriteCollection(output.JsonArray, jsonWriter, transaction); break; @@ -157,7 +160,7 @@ static void WriteValue(Output output, Utf8JsonWriter jsonWriter, Transaction tra case OutputTag.Doc: break; default: - throw new InvalidOperationException("Unsupported data type."); + throw new InvalidOperationException($"Unsupported data type \"{output.Tag}\"."); } } }