-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from LSViana/27-create-the-undomanager-bindings
Create the UndoManager bindings
- Loading branch information
Showing
33 changed files
with
2,357 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
Oops, something went wrong.