Skip to content

Commit 483eff1

Browse files
authored
Add Text type to WriteValue Extension method (#106)
* Added test cases to reproduce Text processing error * Fix conversion of YText to Json
1 parent a61709a commit 483eff1

File tree

3 files changed

+122
-2
lines changed

3 files changed

+122
-2
lines changed

Tests/YDotNet.Tests.Unit/Extensions/Conversion.cs

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,33 @@ public void CanParseMapsToObject()
7070
}
7171
}
7272

73+
[Test]
74+
public void CanParseTextToString()
75+
{
76+
// Arrange
77+
var doc = new Doc();
78+
var map = doc.Map("map");
79+
80+
using (var transaction = doc.WriteTransaction())
81+
{
82+
map.Insert(transaction, "inner", Input.Map(new Dictionary<string, Input>
83+
{
84+
{"value", Input.Text("Hello YDotNet")}
85+
}));
86+
}
87+
88+
using (var transaction = doc.ReadTransaction())
89+
{
90+
var inner = map.Get(transaction, "inner");
91+
92+
// Act
93+
var parsed = inner.To<Expected>(transaction);
94+
95+
// Assert
96+
Assert.That(parsed.Value, Is.EqualTo("Hello YDotNet"));
97+
}
98+
}
99+
73100
[Test]
74101
public void ConvertToJson()
75102
{
@@ -87,7 +114,7 @@ public void ConvertToJson()
87114

88115
// Act
89116
string json;
90-
using (var transaction = map.WriteTransaction())
117+
using (var transaction = map.ReadTransaction())
91118
{
92119
var inner = map.Get(transaction, "inner");
93120

@@ -97,4 +124,32 @@ public void ConvertToJson()
97124
// Assert
98125
StringAssert.Contains("Hello YDotNet", json);
99126
}
127+
128+
[Test]
129+
public void ConvertTextToJson()
130+
{
131+
// Arrange
132+
var doc = new Doc();
133+
var map = doc.Map("map");
134+
135+
using (var transaction = doc.WriteTransaction())
136+
{
137+
map.Insert(transaction, "inner", Input.Map(new Dictionary<string, Input>
138+
{
139+
{"value", Input.Text("Hello YDotNet")}
140+
}));
141+
}
142+
143+
// Act
144+
string json;
145+
using (var transaction = map.WriteTransaction())
146+
{
147+
var inner = map.Get(transaction, "inner");
148+
149+
json = inner.ToJson(transaction);
150+
}
151+
152+
// Assert
153+
StringAssert.Contains("Hello YDotNet", json);
154+
}
100155
}

Tests/YDotNet.Tests.Unit/Maps/InsertTests.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,68 @@ public void InsertDifferentTypeOnExistingKey()
328328
Assert.That(length, Is.EqualTo(expected: 1));
329329
}
330330

331+
[Test]
332+
public void InsertNestedString()
333+
{
334+
// Arrange
335+
var (doc, map) = ArrangeDoc();
336+
337+
// Act
338+
var transaction = doc.WriteTransaction();
339+
340+
var item1 = Input.Map(new Dictionary<string, Input>
341+
{
342+
{ "id", Input.String(Guid.NewGuid().ToString()) },
343+
{ "text", Input.String("Test") }
344+
});
345+
346+
var item2 = Input.Map(new Dictionary<string, Input>
347+
{
348+
{ "id", Input.String(Guid.NewGuid().ToString()) },
349+
{ "text", Input.String("Describe the problem") }
350+
});
351+
352+
var data = Input.Array(new[] { item1, item2 });
353+
map.Insert(transaction, "data", data);
354+
var length = map.Length(transaction);
355+
356+
transaction.Commit();
357+
358+
// Assert
359+
Assert.That(length, Is.EqualTo(expected: 1));
360+
}
361+
362+
[Test]
363+
public void InsertNestedText()
364+
{
365+
// Arrange
366+
var (doc, map) = ArrangeDoc();
367+
368+
// Act
369+
var transaction = doc.WriteTransaction();
370+
371+
var item1 = Input.Map(new Dictionary<string, Input>
372+
{
373+
{ "id", Input.String(Guid.NewGuid().ToString()) },
374+
{ "text", Input.Text("Test") }
375+
});
376+
377+
var item2 = Input.Map(new Dictionary<string, Input>
378+
{
379+
{ "id", Input.String(Guid.NewGuid().ToString()) },
380+
{ "text", Input.Text("Describe the problem") }
381+
});
382+
383+
var data = Input.Array(new[] { item1, item2 });
384+
map.Insert(transaction, "data", data);
385+
var length = map.Length(transaction);
386+
387+
transaction.Commit();
388+
389+
// Assert
390+
Assert.That(length, Is.EqualTo(expected: 1));
391+
}
392+
331393
[Test]
332394
public void InsertNestedMap()
333395
{

YDotNet.Extensions/YDotNetExtensions.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ static void WriteValue(Output output, Utf8JsonWriter jsonWriter, Transaction tra
136136
case OutputTag.String:
137137
jsonWriter.WriteStringValue(output.String);
138138
break;
139+
case OutputTag.Text:
140+
jsonWriter.WriteStringValue(output.Text.String(transaction));
141+
break;
139142
case OutputTag.JsonArray:
140143
WriteCollection(output.JsonArray, jsonWriter, transaction);
141144
break;
@@ -157,7 +160,7 @@ static void WriteValue(Output output, Utf8JsonWriter jsonWriter, Transaction tra
157160
case OutputTag.Doc:
158161
break;
159162
default:
160-
throw new InvalidOperationException("Unsupported data type.");
163+
throw new InvalidOperationException($"Unsupported data type \"{output.Tag}\".");
161164
}
162165
}
163166
}

0 commit comments

Comments
 (0)