mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-12-01 06:41:42 -05:00
Moves the Drawings tab's row content (quantity, size, area, highlight and quantity bar, the hidden-list note), Delete Drawing, Remove Unused Drawings and the Drawing Properties dialog's values and color rules into OpenNest.Desktop so the Avalonia app can share them. The WinForms list, nest window and Edit Drawing form delegate to them unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
136 lines
4.7 KiB
C#
136 lines
4.7 KiB
C#
using System.Drawing;
|
|
using OpenNest.Undo;
|
|
|
|
namespace OpenNest.Desktop.Tests.Drawings;
|
|
|
|
public class DrawingCommandsTests
|
|
{
|
|
/// <summary>
|
|
/// A nest window's state: "square" (two required) with one part on each of two plates,
|
|
/// "unused" with none, the trailing empty plate and a history started on it.
|
|
/// </summary>
|
|
private sealed class Env
|
|
{
|
|
public Env()
|
|
{
|
|
var nest = TestNests.OneSquare();
|
|
Square = nest.Drawings.First();
|
|
Square.Quantity.Required = 2;
|
|
Unused = new Drawing("unused", (CNC.Program)Square.Program.Clone()) { Color = Color.Coral };
|
|
nest.Drawings.Add(Unused);
|
|
nest.Plates[0].Quantity = 1;
|
|
var second = new Plate(new Geometry.Size(48, 96)) { Quantity = 1 };
|
|
second.Parts.Add(new Part(Square));
|
|
nest.Plates.Add(second);
|
|
nest.UpdateDrawingQuantities();
|
|
Document = new Document { Nest = nest };
|
|
Plates = new PlateManager(nest);
|
|
Plates.EnsureSentinel();
|
|
Undo = new NestUndoRecorder(Document);
|
|
Undo.Reset();
|
|
}
|
|
|
|
public Drawing Square { get; }
|
|
public Drawing Unused { get; }
|
|
public Document Document { get; }
|
|
public Nest Nest => Document.Nest;
|
|
public PlateManager Plates { get; }
|
|
public NestUndoRecorder Undo { get; }
|
|
|
|
public ChangeScope BeginChange(string label) => Undo.BeginChange(label);
|
|
|
|
public int Steps => Undo.History.UndoCount;
|
|
|
|
public int SquareParts => Nest.Plates.Sum(p => p.Parts.Count(part => part.BaseDrawing.Name == "square"));
|
|
}
|
|
|
|
[Fact]
|
|
public void DeleteDrawing_RemovesItsPartsAndTheDrawing_AsOneStep()
|
|
{
|
|
var env = new Env();
|
|
Assert.Equal(2, env.SquareParts);
|
|
|
|
DrawingCommands.DeleteDrawing(env.Nest, env.Square, env.BeginChange);
|
|
|
|
Assert.Equal(0, env.SquareParts);
|
|
Assert.DoesNotContain(env.Square, env.Nest.Drawings);
|
|
Assert.Single(env.Nest.Drawings);
|
|
Assert.Equal(1, env.Steps);
|
|
Assert.Equal("Undo Delete Drawing", env.Undo.UndoText);
|
|
|
|
env.Undo.Undo();
|
|
Assert.Equal(2, env.SquareParts);
|
|
Assert.Equal(2, env.Nest.Drawings.Count);
|
|
Assert.Equal(2, env.Nest.Drawings.Single(d => d.Name == "square").Quantity.Nested);
|
|
}
|
|
|
|
[Fact]
|
|
public void DeletePrompt_NamesTheDrawing() =>
|
|
Assert.Equal("Delete drawing 'square' and all its parts from every plate?",
|
|
DrawingCommands.DeletePrompt(new Drawing("square")));
|
|
|
|
[Fact]
|
|
public void CleanUnusedDrawings_RemovesOnlyDrawingsWithNoParts_AsOneStep()
|
|
{
|
|
var env = new Env();
|
|
Assert.True(DrawingCommands.HasUnusedDrawings(env.Nest));
|
|
|
|
Assert.True(DrawingCommands.CleanUnusedDrawings(env.Nest, env.BeginChange));
|
|
|
|
Assert.Equal(new[] { "square" }, env.Nest.Drawings.Select(d => d.Name));
|
|
Assert.Equal(1, env.Steps);
|
|
Assert.Equal("Undo Clean Unused Drawings", env.Undo.UndoText);
|
|
|
|
env.Undo.Undo();
|
|
Assert.Equal(2, env.Nest.Drawings.Count);
|
|
}
|
|
|
|
[Fact]
|
|
public void CleanUnusedDrawings_WithNoneUnused_RecordsNothing()
|
|
{
|
|
var env = new Env();
|
|
DrawingCommands.CleanUnusedDrawings(env.Nest, env.BeginChange);
|
|
|
|
Assert.False(DrawingCommands.CleanUnusedDrawings(env.Nest, env.BeginChange));
|
|
|
|
Assert.False(DrawingCommands.HasUnusedDrawings(env.Nest));
|
|
Assert.Equal(1, env.Steps);
|
|
}
|
|
|
|
[Fact]
|
|
public void ApplyProperties_SavesTheValues_AsOneStep()
|
|
{
|
|
var env = new Env();
|
|
var properties = new DrawingProperties(env.Square, env.Nest)
|
|
{
|
|
Name = "bracket",
|
|
Customer = "Acme",
|
|
Quantity = 7,
|
|
Priority = -3,
|
|
CutFirst = true,
|
|
AllowCommonLine = true,
|
|
Formed = true,
|
|
};
|
|
|
|
DrawingCommands.ApplyProperties(properties, env.BeginChange);
|
|
|
|
Assert.Equal("bracket", env.Square.Name);
|
|
Assert.Equal("Acme", env.Square.Customer);
|
|
Assert.Equal(7, env.Square.Quantity.Required);
|
|
Assert.Equal(2, env.Square.Quantity.Nested);
|
|
Assert.Equal(-3, env.Square.Priority);
|
|
Assert.True(env.Square.CutFirst);
|
|
Assert.True(env.Square.AllowCommonLine);
|
|
Assert.True(env.Square.IsFormed);
|
|
Assert.False(env.Square.HasCustomColor);
|
|
Assert.Equal(1, env.Steps);
|
|
Assert.Equal("Undo Drawing Properties", env.Undo.UndoText);
|
|
|
|
env.Undo.Undo();
|
|
var restored = env.Nest.Drawings.Single(d => d.Quantity.Nested == 2);
|
|
Assert.Equal("square", restored.Name);
|
|
Assert.Equal(2, restored.Quantity.Required);
|
|
Assert.False(restored.CutFirst);
|
|
}
|
|
}
|