Skip to content

Commit

Permalink
Add Text type to WriteValue Extension method (#106)
Browse files Browse the repository at this point in the history
* Added test cases to reproduce Text processing error

* Fix conversion of YText to Json
  • Loading branch information
twaddell authored Sep 9, 2024
1 parent a61709a commit 483eff1
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 2 deletions.
57 changes: 56 additions & 1 deletion Tests/YDotNet.Tests.Unit/Extensions/Conversion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Input>
{
{"value", Input.Text("Hello YDotNet")}
}));
}

using (var transaction = doc.ReadTransaction())
{
var inner = map.Get(transaction, "inner");

// Act
var parsed = inner.To<Expected>(transaction);

// Assert
Assert.That(parsed.Value, Is.EqualTo("Hello YDotNet"));
}
}

[Test]
public void ConvertToJson()
{
Expand All @@ -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");

Expand All @@ -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<string, Input>
{
{"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);
}
}
62 changes: 62 additions & 0 deletions Tests/YDotNet.Tests.Unit/Maps/InsertTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Input>
{
{ "id", Input.String(Guid.NewGuid().ToString()) },
{ "text", Input.String("Test") }
});

var item2 = Input.Map(new Dictionary<string, Input>
{
{ "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<string, Input>
{
{ "id", Input.String(Guid.NewGuid().ToString()) },
{ "text", Input.Text("Test") }
});

var item2 = Input.Map(new Dictionary<string, Input>
{
{ "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()
{
Expand Down
5 changes: 4 additions & 1 deletion YDotNet.Extensions/YDotNetExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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}\".");
}
}
}
Expand Down

0 comments on commit 483eff1

Please sign in to comment.