Skip to content

Commit

Permalink
Color picker tests and call ApplyStyleChanges as part of codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
tznind committed Aug 24, 2024
1 parent 1d2db5a commit 4d2b8e7
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/ToCode/ColorPickerToCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.CodeDom;
using Terminal.Gui;

namespace TerminalGuiDesigner.ToCode
{
internal class ColorPickerToCode : ToCodeBase
{
private readonly Design design;
private readonly ColorPicker colorPicker;

/// <summary>
/// Initializes a new instance of the <see cref="ColorPickerToCode"/> class.
/// </summary>
/// <param name="design">Wrapper for a <see cref="ColorPicker"/>.</param>
public ColorPickerToCode(Design design)
{
this.design = design;

if (design.View is not ColorPicker cp)
{
throw new ArgumentException(nameof(design),
$"{nameof(ColorPickerToCode)} can only be used with {nameof(TerminalGuiDesigner.Design)} that wrap {nameof(ColorPicker)}");
}

this.colorPicker = cp;
}

/// <summary>
/// Adds code to .Designer.cs to call <see cref="ColorPicker.ApplyStyleChanges"/>
/// </summary>
/// <param name="args">State object for the .Designer.cs file being generated.</param>
public void ToCode(CodeDomArgs args)
{
var colorPickerFieldExpression = new CodeFieldReferenceExpression(
new CodeThisReferenceExpression(), design.FieldName);

AddMethodCall(args,colorPickerFieldExpression,nameof(ColorPicker.ApplyStyleChanges));
}
}
}
7 changes: 7 additions & 0 deletions src/ToCode/DesignToCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ internal void ToCode(CodeDomArgs args, CodeExpression parentView)
designItems.ToCode(args);
}

if (this.Design.View is ColorPicker)
{
var designItems = new ColorPickerToCode(this.Design);
designItems.ToCode(args);
}


// call this.Add(someView)
this.AddAddToViewStatement(args, this.Design, parentView);
}
Expand Down
2 changes: 2 additions & 0 deletions src/UI/Windows/ColorPicker.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions tests/ColorPickerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using static Terminal.Gui.SpinnerStyle;

namespace UnitTests;

[TestFixture]
[Category("Core")]
internal class ColorPickerTests : Tests
{
[Test]
public void ColorPickerHeightStaysAuto()
{
Assume.That(ViewFactory.SupportedViewTypes, Does.Contain(typeof(ColorPicker)));

using (var cp = ViewFactory.Create<ColorPicker>())
{
Assert.That(cp.Height?.IsAuto(out _,out _, out _),Is.True);
}
}


[Test]
[Category("Code Generation")]
public void ColorPickerCanSerializeStyle()
{
using var backIn = RoundTrip<View, ColorPicker>(static (d, v) =>
{
Assume.That(v.Style.ColorModel,Is.Not.EqualTo(ColorModel.RGB));
var prop = d.GetDesignableProperty(nameof(ColorPicker.Style) + "." + nameof(ColorPickerStyle.ColorModel))
?? throw new("Property was unexpectedly not designable");
// We change to RGB
prop.SetValue(ColorModel.RGB);
Assert.That(v.Style.ColorModel, Is.EqualTo(ColorModel.RGB));
}, out _);

// Reloaded code from .Designer.cs should match
Assert.That(backIn.Style.ColorModel, Is.EqualTo(ColorModel.RGB));
}
}

0 comments on commit 4d2b8e7

Please sign in to comment.