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
4 changes: 2 additions & 2 deletions Coder.Graph/AstGraphEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
/// sits beside the graph, and the names it draws — <c>Visibility</c>, <c>Parameters</c> — are
/// longer than most of the values beside them. It stays the user's to drag either way.
/// <para>
/// Fractions are spelled to fifteen significant digits rather than the widget's six decimal
/// Fractions are spelled to seventeen significant digits rather than the widget's six decimal
/// places. A literal is a value the generated source will carry, so the row has to show what the
/// document holds: 3.14 has to read as 3.14 rather than as 3.140000, and a value with more digits
/// than that has to keep them when the user opens the box to change something else about it.
Expand All @@ -59,7 +59,7 @@
private static readonly ImGuiWidgets.PropertyGridOptions InspectorGridOptions = new()
{
LabelColumnWeight = 0.95f,
DoubleFormat = "%.15g",
DoubleFormat = "%.17g",
};

private string statusMessage = string.Empty;
Expand Down Expand Up @@ -983,7 +983,7 @@
{
// Undone in the order the replacement was made: the original goes back into its place,
// then each child that moved goes back into the slot it came from.
Graph.Replace(replacement, existing);

Check warning on line 986 in Coder.Graph/AstGraphEditor.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Parameters to 'Replace' have the same names but not the same order as the method arguments.

Check warning on line 986 in Coder.Graph/AstGraphEditor.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Parameters to 'Replace' have the same names but not the same order as the method arguments.

Check warning on line 986 in Coder.Graph/AstGraphEditor.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Parameters to 'Replace' have the same names but not the same order as the method arguments.

Check warning on line 986 in Coder.Graph/AstGraphEditor.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Parameters to 'Replace' have the same names but not the same order as the method arguments.
Graph.MoveTo(existing, from);
foreach ((AstNode child, AstLocation origin) in moved)
{
Expand Down Expand Up @@ -1245,7 +1245,7 @@

// A category's operator entries are one submenu deep, so eighteen binary operators do not
// bury the four literals.
foreach (string group in AstNodeCatalog.GroupsIn(category))

Check warning on line 1248 in Coder.Graph/AstGraphEditor.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Loops should be simplified using the "Where" LINQ method

Check warning on line 1248 in Coder.Graph/AstGraphEditor.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Loops should be simplified using the "Where" LINQ method

Check warning on line 1248 in Coder.Graph/AstGraphEditor.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Loops should be simplified using the "Where" LINQ method

Check warning on line 1248 in Coder.Graph/AstGraphEditor.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Loops should be simplified using the "Where" LINQ method
{
if (ImGui.BeginMenu(group))
{
Expand Down
46 changes: 46 additions & 0 deletions Coder.Test/Graph/AstGraphEditorInspectorPanelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace ktsu.Coder.Test.Graph;

using System.Globalization;
using System.Reflection;
using System.Numerics;

using Hexa.NET.ImGui;
Expand Down Expand Up @@ -42,6 +44,14 @@ private static FunctionDeclaration SampleFunction()
return function;
}

private static (FunctionDeclaration Function, LiteralExpression<double> Literal) SampleFunctionWithSmallFractionLiteral()
{
LiteralExpression<double> literal = Literal.DecimalValue(1e-9);
FunctionDeclaration function = new("tiny") { ReturnType = "double" };
function.Body.Add(new ReturnStatement(literal));
return (function, literal);
}

/// <summary>
/// Draws the whole editor, because selecting a node is ImNodes' business and it faults inside
/// native code when its context is missing rather than throwing.
Expand Down Expand Up @@ -207,4 +217,40 @@ public void Inspector_AddsAndRemovesChildrenFromASlotRow()

Assert.HasCount(1, function.Parameters);
}

/// <summary>
/// Tests that a tiny floating-point literal keeps its value through inspector draws and edit
/// commits, rather than being collapsed to zero by the row's display format.
/// </summary>
[TestMethod]
public void Inspector_PreservesASmallFractionLiteralAcrossDrawAndEdit()
{
FieldInfo optionsField = typeof(AstGraphEditor).GetField("InspectorGridOptions", BindingFlags.NonPublic | BindingFlags.Static)
?? throw new AssertFailedException("Inspector grid options should exist.");

ktsu.ImGui.Widgets.ImGuiWidgets.PropertyGridOptions options =
(ktsu.ImGui.Widgets.ImGuiWidgets.PropertyGridOptions)optionsField.GetValue(null)!;
Assert.AreEqual("%.17g", options.DoubleFormat);

(FunctionDeclaration function, LiteralExpression<double> literal) = SampleFunctionWithSmallFractionLiteral();
AstGraphEditor editor = new(function);
double expected = literal.Value;

using ImGuiAppHarness harness = Inspecting(editor, literal);

// Rendering and selecting the node must leave the document value untouched.
Assert.AreEqual(expected, literal.Value);

harness.Click("Value");
harness.Step(2);

harness.Keyboard.Press(ImGuiKey.A, ctrl: true);
harness.Keyboard.Type(expected.ToString("G17", CultureInfo.InvariantCulture));
harness.Step(2);

harness.Keyboard.Press(ImGuiKey.Enter);
harness.Step(2);

Assert.AreEqual(expected, literal.Value);
}
}