|
| 1 | +using CommunityToolkit.Mvvm.ComponentModel; |
| 2 | +using CommunityToolkit.Mvvm.Input; |
| 3 | +using SquidStd.Tui.Binding; |
| 4 | +using Terminal.Gui.ViewBase; |
| 5 | +using Terminal.Gui.Views; |
| 6 | + |
| 7 | +namespace SquidStd.Tests.Tui.Binding; |
| 8 | + |
| 9 | +public partial class ViewBinderAutoBindTests |
| 10 | +{ |
| 11 | + private sealed partial class AutoBindViewModel : ObservableObject |
| 12 | + { |
| 13 | + [ObservableProperty] |
| 14 | + private string _title = string.Empty; |
| 15 | + |
| 16 | + [ObservableProperty] |
| 17 | + private string _name = string.Empty; |
| 18 | + |
| 19 | + // Wrong type on purpose — used by the "wrong type" skip-path test. |
| 20 | + public int Count { get; } = 42; |
| 21 | + |
| 22 | + private bool _canSave; |
| 23 | + |
| 24 | + [RelayCommand(CanExecute = nameof(CanSave))] |
| 25 | + private void Save() { } |
| 26 | + |
| 27 | + private bool CanSave() => _canSave; |
| 28 | + |
| 29 | + public void SetCanSave(bool value) |
| 30 | + { |
| 31 | + _canSave = value; |
| 32 | + SaveCommand.NotifyCanExecuteChanged(); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + // ------------------------------------------------------------------- |
| 37 | + // Label one-way |
| 38 | + // ------------------------------------------------------------------- |
| 39 | + |
| 40 | + [Fact] |
| 41 | + public void AutoBind_Label_AppliesInitialValue() |
| 42 | + { |
| 43 | + var vm = new AutoBindViewModel { Title = "Hello" }; |
| 44 | + var parent = new View(); |
| 45 | + var label = new Label { Id = "TitleLabel" }; |
| 46 | + parent.Add(label); |
| 47 | + |
| 48 | + var binder = new ViewBinder(); |
| 49 | + binder.AutoBind(parent, vm); |
| 50 | + |
| 51 | + Assert.Equal("Hello", label.Text); |
| 52 | + } |
| 53 | + |
| 54 | + [Fact] |
| 55 | + public void AutoBind_Label_UpdatesWhenVmPropertyChanges() |
| 56 | + { |
| 57 | + var vm = new AutoBindViewModel { Title = "initial" }; |
| 58 | + var parent = new View(); |
| 59 | + var label = new Label { Id = "TitleLabel" }; |
| 60 | + parent.Add(label); |
| 61 | + |
| 62 | + var binder = new ViewBinder(); |
| 63 | + binder.AutoBind(parent, vm); |
| 64 | + |
| 65 | + vm.Title = "updated"; |
| 66 | + |
| 67 | + Assert.Equal("updated", label.Text); |
| 68 | + } |
| 69 | + |
| 70 | + // ------------------------------------------------------------------- |
| 71 | + // TextField two-way (vm→field direction; writeback omitted headless) |
| 72 | + // ------------------------------------------------------------------- |
| 73 | + |
| 74 | + [Fact] |
| 75 | + public void AutoBind_TextField_AppliesInitialValue() |
| 76 | + { |
| 77 | + var vm = new AutoBindViewModel { Name = "squid" }; |
| 78 | + var parent = new View(); |
| 79 | + var field = new TextField { Id = "NameField" }; |
| 80 | + parent.Add(field); |
| 81 | + |
| 82 | + var binder = new ViewBinder(); |
| 83 | + binder.AutoBind(parent, vm); |
| 84 | + |
| 85 | + Assert.Equal("squid", field.Text); |
| 86 | + } |
| 87 | + |
| 88 | + [Fact] |
| 89 | + public void AutoBind_TextField_UpdatesFieldWhenVmPropertyChanges() |
| 90 | + { |
| 91 | + var vm = new AutoBindViewModel { Name = "before" }; |
| 92 | + var parent = new View(); |
| 93 | + var field = new TextField { Id = "NameField" }; |
| 94 | + parent.Add(field); |
| 95 | + |
| 96 | + var binder = new ViewBinder(); |
| 97 | + binder.AutoBind(parent, vm); |
| 98 | + |
| 99 | + vm.Name = "after"; |
| 100 | + |
| 101 | + Assert.Equal("after", field.Text); |
| 102 | + } |
| 103 | + |
| 104 | + // ------------------------------------------------------------------- |
| 105 | + // Button command — CanExecute drives Enabled |
| 106 | + // ------------------------------------------------------------------- |
| 107 | + |
| 108 | + [Fact] |
| 109 | + public void AutoBind_Button_TracksCanExecute() |
| 110 | + { |
| 111 | + var vm = new AutoBindViewModel(); // CanSave == false initially |
| 112 | + var parent = new View(); |
| 113 | + var button = new Button { Id = "SaveButton" }; |
| 114 | + parent.Add(button); |
| 115 | + |
| 116 | + var binder = new ViewBinder(); |
| 117 | + binder.AutoBind(parent, vm); |
| 118 | + |
| 119 | + Assert.False(button.Enabled); // initially disabled because CanSave == false |
| 120 | + |
| 121 | + vm.SetCanSave(true); |
| 122 | + |
| 123 | + Assert.True(button.Enabled); |
| 124 | + |
| 125 | + vm.SetCanSave(false); |
| 126 | + |
| 127 | + Assert.False(button.Enabled); |
| 128 | + } |
| 129 | + |
| 130 | + // ------------------------------------------------------------------- |
| 131 | + // Skip paths — no throw, no unintended side-effects |
| 132 | + // ------------------------------------------------------------------- |
| 133 | + |
| 134 | + [Fact] |
| 135 | + public void AutoBind_UnknownId_DoesNotThrow() |
| 136 | + { |
| 137 | + // "Unknown" maps to no VM member — should silently skip. |
| 138 | + var vm = new AutoBindViewModel(); |
| 139 | + var parent = new View(); |
| 140 | + var label = new Label { Id = "UnknownLabel" }; |
| 141 | + parent.Add(label); |
| 142 | + |
| 143 | + var binder = new ViewBinder(); |
| 144 | + var ex = Record.Exception(() => binder.AutoBind(parent, vm)); |
| 145 | + |
| 146 | + Assert.Null(ex); |
| 147 | + } |
| 148 | + |
| 149 | + [Fact] |
| 150 | + public void AutoBind_WrongPropertyType_DoesNotThrow() |
| 151 | + { |
| 152 | + // "Count" is int, not string — BindStringByName returns early without throwing. |
| 153 | + var vm = new AutoBindViewModel(); |
| 154 | + var parent = new View(); |
| 155 | + var label = new Label { Id = "CountLabel" }; |
| 156 | + parent.Add(label); |
| 157 | + |
| 158 | + var binder = new ViewBinder(); |
| 159 | + var ex = Record.Exception(() => binder.AutoBind(parent, vm)); |
| 160 | + |
| 161 | + Assert.Null(ex); |
| 162 | + Assert.Equal(string.Empty, label.Text); // left untouched |
| 163 | + } |
| 164 | +} |
0 commit comments