From 0378b11af870c0a913f2bfb847c47bdad3c24081 Mon Sep 17 00:00:00 2001 From: Lucas Viana Date: Wed, 13 Sep 2023 20:28:05 -0300 Subject: [PATCH] test(sticky-index): introduce tests for Decode() --- .../StickyIndexes/DecodeTests.cs | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 Tests/YDotNet.Tests.Unit/StickyIndexes/DecodeTests.cs diff --git a/Tests/YDotNet.Tests.Unit/StickyIndexes/DecodeTests.cs b/Tests/YDotNet.Tests.Unit/StickyIndexes/DecodeTests.cs new file mode 100644 index 00000000..f50a09ea --- /dev/null +++ b/Tests/YDotNet.Tests.Unit/StickyIndexes/DecodeTests.cs @@ -0,0 +1,62 @@ +using NUnit.Framework; +using YDotNet.Document; +using YDotNet.Document.Options; +using YDotNet.Document.StickyIndexes; + +namespace YDotNet.Tests.Unit.StickyIndexes; + +public class DecodeTests +{ + [Test] + public void DecodesFromEmptyValue() + { + // Arrange + var doc = new Doc( + new DocOptions + { + Id = 91 + }); + var text = doc.Text("text"); + + var transaction = doc.WriteTransaction(); + var stickyIndex = text.StickyIndex(transaction, index: 0, StickyAssociationType.Before); + transaction.Commit(); + + // Act + transaction = doc.WriteTransaction(); + var decodedStickyIndex = StickyIndex.Decode(stickyIndex.Encode()); + var index = decodedStickyIndex?.Read(transaction); + transaction.Commit(); + + // Assert + Assert.That(decodedStickyIndex, Is.Not.Null); + Assert.That(index, Is.EqualTo(expected: 0)); + } + + [Test] + public void DecodesOnFilledValue() + { + // Arrange + var doc = new Doc( + new DocOptions + { + Id = 73 + }); + var text = doc.Text("text"); + + var transaction = doc.WriteTransaction(); + text.Insert(transaction, index: 0, "Lucas"); + var stickyIndex = text.StickyIndex(transaction, index: 3, StickyAssociationType.Before); + transaction.Commit(); + + // Act + transaction = doc.WriteTransaction(); + var decodedStickyIndex = StickyIndex.Decode(stickyIndex.Encode()); + var index = decodedStickyIndex?.Read(transaction); + transaction.Commit(); + + // Assert + Assert.That(decodedStickyIndex, Is.Not.Null); + Assert.That(index, Is.EqualTo(expected: 3)); + } +}