Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
210 changes: 210 additions & 0 deletions EvenBetterFastSim.Tests/NodeMillisecondsViewModelTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
using System.Collections.Generic;
using EvenBetterFastSim.Services;
using EvenBetterFastSim.Window_Helpers;
using EvenBetterFastSim.WPF.ViewModels;
using EvenBetterFastSim.WPF.ViewModels.Graph;
using SecsGemScenarioEngine.Models;
using Xunit;

namespace EvenBetterFastSim.Tests;

/// <summary>
/// The editor for a node's time in milliseconds (GitLab secsgem/evencoolerfastsim#18). It only lets a whole number in
/// digits through, because the engine takes everything else as "no wait" without an error.
/// </summary>
public class NodeMillisecondsViewModelTests
{
[Fact]
public void Ok_writes_the_new_time_into_the_node()
{
var node = WaitNode("1500");
var editor = NodeMillisecondsViewModel.ForWait(node);
var closed = 0;
editor.CloseAction = () => closed++;
Assert.Equal("Wait", editor.HeaderText);
Assert.Equal("1500", editor.Text);
Assert.Null(editor.Error);

editor.Text = " 2500 ";
Assert.True(editor.AcceptButtonClickCommand.CanExecute(null));
editor.AcceptButtonClickCommand.Execute(null);
editor.ApplyTo(node);

Assert.True(editor.Accepted);
Assert.Equal(2500, editor.Milliseconds);
Assert.Equal(1, closed);
Assert.Equal("2500", node.TransactionName);
Assert.Equal("Wait 2500 ms", node.Title);
}

[Theory]
[InlineData("")]
[InlineData("0")]
[InlineData("-1")]
[InlineData("+5")]
[InlineData("1.5")]
[InlineData("12,6")]
[InlineData("12.600")]
[InlineData("20 s")]
[InlineData("1 500")]
[InlineData("2147483648")]
[InlineData("99999999999")]
[InlineData("1500")]
public void Anything_but_a_whole_number_above_zero_cannot_be_confirmed(string text)
{
var node = WaitNode("1500");
var editor = NodeMillisecondsViewModel.ForWait(node);
var closed = 0;
editor.CloseAction = () => closed++;

editor.Text = text;

Assert.NotNull(editor.Error);
Assert.False(editor.AcceptButtonClickCommand.CanExecute(null));
// Enter in the dialog executes the command only if it can; a direct call must not get through either.
editor.AcceptButtonClickCommand.Execute(null);
editor.ApplyTo(node);
Assert.False(editor.Accepted);
Assert.Equal(0, closed);
Assert.Equal("1500", node.TransactionName);
}

[Theory]
[InlineData("1", 1)]
[InlineData("0001500", 1500)]
[InlineData("2147483647", int.MaxValue)]
public void Whole_numbers_above_zero_are_accepted(string text, int milliseconds)
{
var node = WaitNode("7");
var editor = NodeMillisecondsViewModel.ForWait(node);

editor.Text = text;
editor.AcceptButtonClickCommand.Execute(null);
editor.ApplyTo(node);

Assert.Equal(milliseconds, editor.Milliseconds);
Assert.Equal(milliseconds.ToString(System.Globalization.CultureInfo.InvariantCulture), node.TransactionName);
}

/// <summary>A node from a file with "20 s" opens with that text and says what is wrong; correcting it clears the error.</summary>
[Fact]
public void Invalid_time_from_a_file_is_shown_and_can_be_corrected()
{
var node = WaitNode("20 s");
var editor = NodeMillisecondsViewModel.ForWait(node);
var changed = new List<string?>();
editor.PropertyChanged += (_, e) => changed.Add(e.PropertyName);
var requeried = 0;
editor.AcceptButtonClickCommand.CanExecuteChanged += (_, _) => requeried++;
Assert.Equal("20 s", editor.Text);
Assert.NotNull(editor.Error);
Assert.False(editor.AcceptButtonClickCommand.CanExecute(null));

editor.Text = "20000";

Assert.Null(editor.Error);
Assert.Contains(nameof(NodeMillisecondsViewModel.Error), changed);
// The OK button asks CanExecute again only when the command says so; without it the button stays grey.
Assert.Equal(1, requeried);
Assert.True(editor.AcceptButtonClickCommand.CanExecute(null));
editor.AcceptButtonClickCommand.Execute(null);
editor.ApplyTo(node);
Assert.Equal("20000", node.TransactionName);
}

/// <summary>
/// A value from a file with characters nobody can see around the digits (a non-breaking space, a zero-width
/// space, a byte order mark). The editor opens with the bare digits, and OK repairs the value.
/// </summary>
[Theory]
[InlineData("\u00A01500\u00A0")]
[InlineData("1500\u200B")]
[InlineData("\uFEFF1500")]
[InlineData(" \t1500\r\n ")]
public void Time_with_characters_nobody_can_see_around_it_is_repaired_by_ok(string fromFile)
{
var node = WaitNode(fromFile);
var editor = NodeMillisecondsViewModel.ForWait(node);
Assert.Equal("1500", editor.Text);
Assert.Null(editor.Error);

editor.AcceptButtonClickCommand.Execute(null);
editor.ApplyTo(node);

Assert.Equal("1500", node.TransactionName);
Assert.Equal("Wait 1500 ms", node.Title);
}

/// <summary>The same characters typed or pasted into the box: OK writes the bare digits.</summary>
[Fact]
public void Pasted_time_with_characters_nobody_can_see_around_it_is_accepted()
{
var node = WaitNode("7");
var editor = NodeMillisecondsViewModel.ForWait(node);

editor.Text = "\u00A02500\u200B";
Assert.Null(editor.Error);
editor.AcceptButtonClickCommand.Execute(null);
editor.ApplyTo(node);

Assert.Equal("2500", node.TransactionName);
}

/// <summary>
/// Such a character inside the number: the box shows "1500", but it is not a number. The message has to say
/// why, "use digits only" would not help.
/// </summary>
[Fact]
public void Character_nobody_can_see_inside_the_number_is_named_in_the_message()
{
var node = WaitNode("15\u200B00");
var editor = NodeMillisecondsViewModel.ForWait(node);

Assert.Equal("15\u200B00", editor.Text);
Assert.False(editor.AcceptButtonClickCommand.CanExecute(null));
Assert.Equal(
"The text contains characters that cannot be seen: 15[U+200B]00. Type the number again, in digits only.",
editor.Error);

editor.Text = "1 500";
Assert.Equal("This is not a whole number from 1 to 2147483647. Use digits only, for example 1500.", editor.Error);

editor.Text = "1500";
Assert.Null(editor.Error);
Assert.True(editor.AcceptButtonClickCommand.CanExecute(null));
}

/// <summary>Without the mapping a double-click on a Wait node ends in an exception of WindowManager.</summary>
[Fact]
public void Editor_opens_in_the_shared_dialog_window()
{
var editor = NodeMillisecondsViewModel.ForWait(WaitNode("1500"));

Assert.Equal(typeof(DialogWindow), new WindowMapper().GetWindowTypeForViewModel(editor));
}

[Fact]
public void Cancel_leaves_the_node_as_it_is()
{
var node = WaitNode("1500");
var editor = NodeMillisecondsViewModel.ForWait(node);

editor.Text = "9000";
editor.ApplyTo(node);

Assert.False(editor.Accepted);
Assert.Equal("1500", node.TransactionName);
}

/// <summary>The minimum is a parameter: a later node type may allow 0.</summary>
[Fact]
public void Minimum_decides_whether_zero_is_allowed()
{
Assert.False(new NodeMillisecondsViewModel("t", "l", "0", minimum: 1).AcceptButtonClickCommand.CanExecute(null));
Assert.True(new NodeMillisecondsViewModel("t", "l", "0", minimum: 0).AcceptButtonClickCommand.CanExecute(null));
Assert.Contains(" 0 ", new NodeMillisecondsViewModel("t", "l", null, minimum: 0).Hint);
Assert.Contains(" 1 ", new NodeMillisecondsViewModel("t", "l", null, minimum: 1).Error);
}

private static ScenarioNodeViewModel WaitNode(string? time) => new() { Type = NodeType.Wait, TransactionName = time };
}
172 changes: 172 additions & 0 deletions EvenBetterFastSim.Tests/WaitNodeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
using System.Globalization;
using System.Linq;
using System.Text.Json;
using System.Windows;
using EvenBetterFastSim.WPF.ViewModels;
using EvenBetterFastSim.WPF.ViewModels.Graph;
using MessagePack;
using SecsGemScenarioEngine.Models;
using Xunit;

namespace EvenBetterFastSim.Tests;

/// <summary>
/// A Wait node can be made on the canvas and shows its time (GitLab secsgem/evencoolerfastsim#18). The time is text in
/// TransactionName; the engine waits only for a whole number greater than 0 and takes anything else as "no wait"
/// without an error, so the title has to say when the value is not a time.
/// </summary>
public class WaitNodeTests
{
[Theory]
[InlineData("1500", "Wait 1500 ms")]
[InlineData("12600", "Wait 12600 ms")]
[InlineData(" 250 ", "Wait 250 ms")]
[InlineData("0", "Wait 0 ms")]
[InlineData("20 s", "Wait (invalid: 20 s)")]
[InlineData("12,6", "Wait (invalid: 12,6)")]
[InlineData("12.600", "Wait (invalid: 12.600)")]
[InlineData("-5", "Wait (invalid: -5)")]
[InlineData("99999999999", "Wait (invalid: 99999999999)")]
[InlineData("2147483647", "Wait 2147483647 ms")]
[InlineData("2147483648", "Wait (invalid: 2147483648)")]
[InlineData("\t250\r\n", "Wait 250 ms")]
[InlineData("\t20 s\r\n", "Wait (invalid: 20 s)")]
[InlineData("", "Wait (no time set)")]
[InlineData(" ", "Wait (no time set)")]
[InlineData(null, "Wait (no time set)")]
public void Title_shows_the_time_or_what_is_wrong_with_it(string? text, string title)
{
var node = new ScenarioNodeViewModel { Type = NodeType.Wait, TransactionName = text };

Assert.Equal(title, node.Title);
}

/// <summary>
/// The engine skips only space, tab and line ends around the number. Any other character nobody can see (a
/// non-breaking space from a hand-edited file) makes the value invalid, so the title has to show it:
/// "Wait (invalid: 1500)" would look like a valid time.
/// </summary>
[Theory]
[InlineData("\u00A01500", "Wait (invalid: [U+00A0]1500)")]
[InlineData("1500\u200B", "Wait (invalid: 1500[U+200B])")]
[InlineData("\uFEFF1500", "Wait (invalid: [U+FEFF]1500)")]
[InlineData("12\t600", "Wait (invalid: 12[U+0009]600)")]
[InlineData(" 1 500 ", "Wait (invalid: 1 500)")]
public void Title_shows_characters_nobody_can_see(string text, string title)
{
var node = new ScenarioNodeViewModel { Type = NodeType.Wait, TransactionName = text };

Assert.Equal(title, node.Title);
}

/// <summary>
/// A leading sign is the one thing the engine versions read differently: up to package 1.0.0-build-0027 with
/// the culture of the computer, later with the invariant culture. The culture decides which characters are the
/// signs: Windows takes them from the regional settings of the user, and ICU data write the plus sign of Persian
/// or Hebrew with a direction mark in front (U+200E U+002B). On a computer with such a plus sign "+5" is a
/// number for one engine and not for the other, so the title must not promise a wait. The culture is made by
/// hand, so the test does not depend on the culture data of the computer it runs on.
/// </summary>
[Fact]
public void Title_promises_a_wait_only_if_every_engine_version_reads_the_time()
{
var withMark = (CultureInfo)CultureInfo.InvariantCulture.Clone();
withMark.NumberFormat.PositiveSign = "\u200E+";
var before = CultureInfo.CurrentCulture;
try
{
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
Assert.Equal("Wait 5 ms", WaitTime.Title("+5"));

CultureInfo.CurrentCulture = withMark;
// The culture is in effect: a read with the culture of the computer takes the other spelling.
Assert.False(int.TryParse("+5", out _));
Assert.True(int.TryParse("\u200E+5", out _));

Assert.Equal("Wait (invalid: +5)", WaitTime.Title("+5"));
Assert.Equal("Wait (invalid: [U+200E]+5)", WaitTime.Title("\u200E+5"));
Assert.Equal("Wait 12600 ms", WaitTime.Title("12600"));
}
finally
{
CultureInfo.CurrentCulture = before;
}
}

/// <summary>Until #18 a DisplayName replaced the number, so the node did not show how long it waits.</summary>
[Fact]
public void Title_follows_the_time_and_is_not_replaced_by_a_display_name()
{
var node = new ScenarioNodeViewModel { Type = NodeType.Wait, TransactionName = "1500", DisplayName = "Pause" };
Assert.Equal("Wait 1500 ms", node.Title);

node.TransactionName = "250";

Assert.Equal("Wait 250 ms", node.Title);
}

[Fact]
public void Wait_node_from_the_palette_starts_with_one_second()
{
var node = ScenarioNodeViewModel.CreateStructural(NodeType.Wait, new Point(120, 80));

Assert.Equal(NodeType.Wait, node.Type);
Assert.Equal("1000", node.TransactionName);
Assert.Equal("Wait 1000 ms", node.Title);
Assert.Equal(new Point(120, 80), node.Location);
Assert.Single(node.Input);
Assert.Equal(["Done"], node.Output.Select(connector => connector.Title));
Assert.True(node.IsDeletable);
}

[Fact]
public void And_node_from_the_palette_has_no_parameter()
{
var node = ScenarioNodeViewModel.CreateStructural(NodeType.And, new Point(10, 20));

Assert.Equal(NodeType.And, node.Type);
Assert.Null(node.TransactionName);
Assert.Equal("AND", node.Title);
Assert.Equal(new Point(10, 20), node.Location);
}

/// <summary>The time survives saving and loading as a MessagePack file (<c>.msgpack</c>).</summary>
[Fact]
public void Time_survives_saving_and_loading_as_MessagePack()
{
var node = ScenarioNodeViewModel.CreateStructural(NodeType.Wait, new Point(5, 6));
node.TransactionName = "12600";
var graph = new ScenarioGraph { Name = "s", Nodes = [node.ToModel()] };

var cancellation = TestContext.Current.CancellationToken;
var bytes = MessagePackSerializer.Serialize(ScenarioGraphDto.From(graph), cancellationToken: cancellation);
var loaded = MessagePackSerializer.Deserialize<ScenarioGraphDto>(bytes, cancellationToken: cancellation).ToModel();
var reloaded = ScenarioNodeViewModel.FromModel(loaded.Nodes.Single());

Assert.Equal(NodeType.Wait, reloaded.Type);
Assert.Equal("12600", reloaded.TransactionName);
Assert.Equal("Wait 12600 ms", reloaded.Title);
Assert.Equal(node.Id, reloaded.Id);
}

/// <summary>
/// The same as a JSON file, written and read with the calls of ScenariosViewModel (every other extension than
/// <c>.msgpack</c>, and the export).
/// </summary>
[Fact]
public void Time_survives_saving_and_loading_as_JSON()
{
var node = ScenarioNodeViewModel.CreateStructural(NodeType.Wait, new Point(5, 6));
node.TransactionName = "12600";
var graph = new ScenarioGraph { Name = "s", Nodes = [node.ToModel()] };

var json = JsonSerializer.Serialize(graph, new JsonSerializerOptions { WriteIndented = true });
var loaded = JsonSerializer.Deserialize<ScenarioGraph>(json)!;
var reloaded = ScenarioNodeViewModel.FromModel(loaded.Nodes.Single());

Assert.Equal(NodeType.Wait, reloaded.Type);
Assert.Equal("12600", reloaded.TransactionName);
Assert.Equal("Wait 12600 ms", reloaded.Title);
Assert.Equal(node.Id, reloaded.Id);
}
}
1 change: 1 addition & 0 deletions EvenBetterFastSim/Services/WindowMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public WindowMapper()
RegisterMapping<InspectSecsGemItemViewModel, DialogWindow>();
RegisterMapping<SecsGemTransactionViewModel, DialogWindow>();
RegisterMapping<WPF.ViewModels.Responders.NodeResponderViewModel, DialogWindow>();
RegisterMapping<NodeMillisecondsViewModel, DialogWindow>();
}

/// <summary>
Expand Down
Loading