Skip to content

Commit

Permalink
Fix code generation and test expectations
Browse files Browse the repository at this point in the history
  • Loading branch information
tznind committed Jan 2, 2024
1 parent 49695c5 commit d5cb94f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
15 changes: 10 additions & 5 deletions src/ToCode/TreeObjectsProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ namespace TerminalGuiDesigner.ToCode;
public class TreeObjectsProperty<T> : Property where T : class
{
public List<T> Value { get; private set; } = new List<T>();
readonly TreeView<T> treeView;

public TreeObjectsProperty(Design design)
: base(
design,
typeof(TreeView<T>).GetProperty(nameof(TreeView<T>.Objects))
?? throw new MissingFieldException("Expected property was missing from TreeView"))
{
treeView = (TreeView<T>)design.View;
}

public override string GetHumanReadableName()
Expand All @@ -32,6 +34,10 @@ public override string ToString()
public override void SetValue(object? value)
{
this.Value = (List<T>)(value ?? new List<T>());

treeView.ClearObjects();
treeView.AddObjects(this.Value);

}

public override object GetValue()
Expand All @@ -44,18 +50,17 @@ public override void ToCode(CodeDomArgs args)
// Create statement like this
//tree1.AddObjects(new List<FileSystemInfo>() { new DirectoryInfo("c:\\") });


var call = new CodeMethodInvokeExpression();
call.Method.TargetObject = new CodeFieldReferenceExpression(
new CodeThisReferenceExpression(),
$"this.{this.Design.FieldName}");
this.Design.FieldName);

call.Method.MethodName = nameof(TreeView.AddObjects);

var newListStatement = new CodeObjectCreateExpression(typeof(T),
var newListStatement =
new CodeArrayCreateExpression(
new CodeTypeReference(typeof(string)),
Value.Select(v => TTypes.ToCode(args, Design, v)).ToArray()));
new CodeTypeReference(typeof(T[])),
Value.Select(v => TTypes.ToCode(args, Design, v)).ToArray());

call.Parameters.Add(newListStatement);

Expand Down
11 changes: 7 additions & 4 deletions tests/TreeViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,26 @@ public void TestRoundTrip_TreeView_FileSystemInfo()
{
var objectsProperty = d.GetDesignableProperty("Objects");
Assert.That(objectsProperty,Is.InstanceOf<TreeObjectsProperty<FileSystemInfo>>());

objectsProperty.SetValue(
new List<FileSystemInfo>(
new FileSystemInfo[] {
new DirectoryInfo("/"),
new FileInfo("/blah.txt")
}));
}, out var viewAfter);



Assert.That(v.Objects.Count, Is.EqualTo(2));

}, out var viewAfter);

Assert.That(viewAfter.Objects.Count, Is.EqualTo(2));

Assert.That(viewAfter.Objects.Count, Is.EqualTo(2));
Assert.That(viewAfter.Objects.Count, Is.EqualTo(2));

Assert.That(viewAfter.Objects.ElementAt(0), Is.EqualTo(new DirectoryInfo("/")));
Assert.That(viewAfter.Objects.ElementAt(1), Is.EqualTo(new FileInfo("/blah.txt")));
Assert.That(viewAfter.Objects.ElementAt(0).ToString(), Is.EqualTo(new DirectoryInfo("/").ToString()));
Assert.That(viewAfter.Objects.ElementAt(1).ToString(), Is.EqualTo(new FileInfo("/blah.txt").ToString()));
}

}
Expand Down

0 comments on commit d5cb94f

Please sign in to comment.