Skip to content

Commit

Permalink
Merge pull request #29 from LSViana/27-create-the-undomanager-bindings
Browse files Browse the repository at this point in the history
Create the UndoManager bindings
  • Loading branch information
LSViana authored Sep 5, 2023
2 parents cb538c5 + c8d69b7 commit 68f1b7a
Show file tree
Hide file tree
Showing 33 changed files with 2,357 additions and 18 deletions.
73 changes: 73 additions & 0 deletions Tests/YDotNet.Tests.Unit/UndoManagers/AddOriginTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using NUnit.Framework;
using YDotNet.Document;
using YDotNet.Document.UndoManagers;

namespace YDotNet.Tests.Unit.UndoManagers;

public class AddOriginTests
{
[Test]
public void TrackChangesFromEmptyOriginsByDefault()
{
// Arrange
var doc = new Doc();
var text = doc.Text("text");
var undoManager = new UndoManager(doc, text, new UndoManagerOptions { CaptureTimeoutMilliseconds = 0 });

var called = 0;
undoManager.ObserveAdded(_ => called++);

// Act
var transaction = doc.WriteTransaction();
text.Insert(transaction, index: 0, "Lucas");
transaction.Commit();

// Assert
Assert.That(called, Is.EqualTo(expected: 1));

// Act
transaction = doc.WriteTransaction(new byte[] { 0 });
text.Insert(transaction, index: 5, " Viana");
transaction.Commit();

// Assert
Assert.That(called, Is.EqualTo(expected: 1));
}

[Test]
public void TrackChangesFromDefinedOriginsAfterAdding()
{
// Arrange
var doc = new Doc();
var text = doc.Text("text");
var undoManager = new UndoManager(doc, text, new UndoManagerOptions { CaptureTimeoutMilliseconds = 0 });

var called = 0;
undoManager.ObserveAdded(_ => called++);

// Act
undoManager.AddOrigin(new byte[] { 0 });
var transaction = doc.WriteTransaction();
text.Insert(transaction, index: 0, "Lucas");
transaction.Commit();

// Assert
Assert.That(called, Is.EqualTo(expected: 0));

// Act
transaction = doc.WriteTransaction(new byte[] { 0 });
text.Insert(transaction, index: 5, " Viana");
transaction.Commit();

// Assert
Assert.That(called, Is.EqualTo(expected: 1));

// Act
transaction = doc.WriteTransaction(new byte[] { 1 });
text.Insert(transaction, index: 5, " Viana");
transaction.Commit();

// Assert
Assert.That(called, Is.EqualTo(expected: 1));
}
}
42 changes: 42 additions & 0 deletions Tests/YDotNet.Tests.Unit/UndoManagers/AddScopeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using NUnit.Framework;
using YDotNet.Document;
using YDotNet.Document.Cells;
using YDotNet.Document.UndoManagers;
using YDotNet.Document.UndoManagers.Events;

namespace YDotNet.Tests.Unit.UndoManagers;

public class AddScopeTests
{
[Test]
public void StartsTrackingAfterAddingScope()
{
// Arrange
var doc = new Doc();
var text = doc.Text("text");
var array = doc.Array("array");
var undoManager = new UndoManager(doc, text, new UndoManagerOptions { CaptureTimeoutMilliseconds = 0 });

UndoEvent? undoEvent = null;
undoManager.ObserveAdded(e => undoEvent = e);

// Act
undoEvent = null;
var transaction = doc.WriteTransaction();
array.InsertRange(transaction, index: 0, new[] { Input.Long(value: 2469L) });
transaction.Commit();

// Assert
Assert.That(undoEvent, Is.Null);

// Act
undoEvent = null;
undoManager.AddScope(array);
transaction = doc.WriteTransaction();
array.InsertRange(transaction, index: 1, new[] { Input.Boolean(value: false) });
transaction.Commit();

// Assert
Assert.That(undoEvent, Is.Not.Null);
}
}
49 changes: 49 additions & 0 deletions Tests/YDotNet.Tests.Unit/UndoManagers/CanRedoTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using NUnit.Framework;
using YDotNet.Document;
using YDotNet.Document.UndoManagers;

namespace YDotNet.Tests.Unit.UndoManagers;

public class CanRedoTests
{
[Test]
public void CanNotRedoInitially()
{
// Arrange
var doc = new Doc();
var text = doc.Text("text");
var undoManager = new UndoManager(doc, text, new UndoManagerOptions { CaptureTimeoutMilliseconds = 0 });

// Act
var canUndo = undoManager.CanUndo();

// Assert
Assert.That(canUndo, Is.False);
}

[Test]
public void CanRedoAfterUndoingChanges()
{
// Arrange
var doc = new Doc();
var text = doc.Text("text");
var undoManager = new UndoManager(doc, text, new UndoManagerOptions { CaptureTimeoutMilliseconds = 0 });

// Act
var transaction = doc.WriteTransaction();
text.Insert(transaction, index: 0, "Lucas");
transaction.Commit();
var canRedo = undoManager.CanRedo();

// Assert
Assert.That(canRedo, Is.False);

// Act
var undo = undoManager.Undo();
canRedo = undoManager.CanRedo();

// Assert
Assert.That(undo, Is.True);
Assert.That(canRedo, Is.True);
}
}
49 changes: 49 additions & 0 deletions Tests/YDotNet.Tests.Unit/UndoManagers/CanUndoTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using NUnit.Framework;
using YDotNet.Document;
using YDotNet.Document.UndoManagers;

namespace YDotNet.Tests.Unit.UndoManagers;

public class CanUndoTests
{
[Test]
public void CanNotUndoInitially()
{
// Arrange
var doc = new Doc();
var text = doc.Text("text");
var undoManager = new UndoManager(doc, text, new UndoManagerOptions { CaptureTimeoutMilliseconds = 0 });

// Act
var canUndo = undoManager.CanUndo();

// Assert
Assert.That(canUndo, Is.False);
}

[Test]
public void CanUndoAfterApplyingChanges()
{
// Arrange
var doc = new Doc();
var text = doc.Text("text");
var undoManager = new UndoManager(doc, text, new UndoManagerOptions { CaptureTimeoutMilliseconds = 0 });

// Act
var transaction = doc.WriteTransaction();
text.Insert(transaction, index: 0, "Lucas");
transaction.Commit();
var canUndo = undoManager.CanUndo();

// Assert
Assert.That(canUndo, Is.True);

// Act
var undo = undoManager.Undo();
canUndo = undoManager.CanUndo();

// Assert
Assert.That(undo, Is.True);
Assert.That(canUndo, Is.False);
}
}
77 changes: 77 additions & 0 deletions Tests/YDotNet.Tests.Unit/UndoManagers/ClearTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using NUnit.Framework;
using YDotNet.Document;
using YDotNet.Document.UndoManagers;

namespace YDotNet.Tests.Unit.UndoManagers;

public class ClearTests
{
[Test]
public void ClearDisablesUndoing()
{
// Arrange
var doc = new Doc();
var text = doc.Text("text");
var undoManager = new UndoManager(doc, text, new UndoManagerOptions { CaptureTimeoutMilliseconds = 0 });

var transaction = doc.WriteTransaction();
text.Insert(transaction, index: 0, "Lucas");
transaction.Commit();

// Act
var canUndo1 = undoManager.CanUndo();
var clear = undoManager.Clear();
var canUndo2 = undoManager.CanUndo();

// Assert
Assert.That(canUndo1, Is.True);
Assert.That(clear, Is.True);
Assert.That(canUndo2, Is.False);

// Act
undoManager.Undo();
transaction = doc.ReadTransaction();
var value = text.String(transaction);
transaction.Commit();

// Assert
Assert.That(value, Is.EqualTo("Lucas"));
}

[Test]
public void ClearDisablesRedoing()
{
// Arrange
var doc = new Doc();
var text = doc.Text("text");
var undoManager = new UndoManager(doc, text, new UndoManagerOptions { CaptureTimeoutMilliseconds = 0 });

var transaction = doc.WriteTransaction();
text.Insert(transaction, index: 0, "Lucas");
transaction.Commit();

transaction = doc.WriteTransaction();
text.Insert(transaction, index: 5, " Viana");
transaction.Commit();

// Act
undoManager.Undo();
var canRedo1 = undoManager.CanRedo();
var clear = undoManager.Clear();
var canRedo2 = undoManager.CanRedo();

// Assert
Assert.That(canRedo1, Is.True);
Assert.That(clear, Is.True);
Assert.That(canRedo2, Is.False);

// Act
undoManager.Redo();
transaction = doc.ReadTransaction();
var value = text.String(transaction);
transaction.Commit();

// Assert
Assert.That(value, Is.EqualTo("Lucas"));
}
}
40 changes: 40 additions & 0 deletions Tests/YDotNet.Tests.Unit/UndoManagers/NewTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using NUnit.Framework;
using YDotNet.Document;
using YDotNet.Document.UndoManagers;

namespace YDotNet.Tests.Unit.UndoManagers;

public class NewTests
{
[Test]
public void CreateWithOptions()
{
// Arrange
var doc = new Doc();
var text = doc.Text("text");

// Act
var undoManager = new UndoManager(
doc, text, new UndoManagerOptions
{
CaptureTimeoutMilliseconds = 1000
});

// Assert
Assert.That(undoManager.Handle, Is.GreaterThan(nint.Zero));
}

[Test]
public void CreateWithoutOptions()
{
// Arrange
var doc = new Doc();
var text = doc.Text("text");

// Act
var undoManager = new UndoManager(doc, text);

// Assert
Assert.That(undoManager.Handle, Is.GreaterThan(nint.Zero));
}
}
Loading

0 comments on commit 68f1b7a

Please sign in to comment.