-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #412 from joecare99/CSharpBible
C sharp bible
- Loading branch information
Showing
32 changed files
with
1,329 additions
and
112 deletions.
There are no files selected for viewing
117 changes: 117 additions & 0 deletions
117
CSharpBible/MVVM_Tutorial/MVVM_09a_CTDialogBoxesTests/View/DialogWindowTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// *********************************************************************** | ||
// Assembly : MVVM_00a_CTTemplate_netTests | ||
// Author : Mir | ||
// Created : 05-14-2023 | ||
// | ||
// Last Modified By : Mir | ||
// Last Modified On : 05-14-2023 | ||
// *********************************************************************** | ||
// <copyright file="DialogWindowTests.cs" company="JC-Soft"> | ||
// Copyright © JC-Soft 2023 | ||
// </copyright> | ||
// <summary></summary> | ||
// *********************************************************************** | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using MVVM.ViewModel; | ||
using MVVM_09a_CTDialogBoxes.ViewModel; | ||
using System.ComponentModel; | ||
using System.Threading; | ||
using System.Windows; | ||
|
||
/// <summary> | ||
/// The Tests namespace. | ||
/// </summary> | ||
/// <autogeneratedoc /> | ||
namespace MVVM_09a_CTDialogBoxes.View.Tests | ||
{ | ||
/// <summary> | ||
/// Defines test class DialogWindowTests. | ||
/// </summary> | ||
/// <autogeneratedoc /> | ||
[TestClass()] | ||
public class DialogWindowTests : BaseTestViewModel | ||
{ | ||
#pragma warning disable CS8618 // Ein Non-Nullable-Feld muss beim Beenden des Konstruktors einen Wert ungleich NULL enthalten. Erwägen Sie die Deklaration als Nullable. | ||
/// <summary> | ||
/// The test view | ||
/// </summary> | ||
/// <autogeneratedoc /> | ||
DialogWindow testView; | ||
DialogWindowViewModel vm; | ||
private MessageBoxResult mbResult; | ||
private bool? xResult; | ||
private string sNewName = "NewName"; | ||
private string sNewEmail = "NewEmail"; | ||
#pragma warning restore CS8618 // Ein Non-Nullable-Feld muss beim Beenden des Konstruktors einen Wert ungleich NULL enthalten. Erwägen Sie die Deklaration als Nullable. | ||
|
||
/// <summary> | ||
/// Initializes this instance. | ||
/// </summary> | ||
/// <autogeneratedoc /> | ||
[TestInitialize] | ||
public void Init() | ||
{ | ||
var t = new Thread(() => { testView = new(); vm = (DialogWindowViewModel)testView.DataContext; testView.Show(); }); | ||
t.SetApartmentState(ApartmentState.STA); //Set the thread to STA | ||
t.Start(); | ||
t.Join(); //Wait for the thread to end | ||
vm.PropertyChanged += OnVMPropertyChanged; | ||
if (vm is INotifyPropertyChanging npchgn) | ||
npchgn.PropertyChanging += OnVMPropertyChanging; | ||
} | ||
|
||
/// <summary> | ||
/// Defines the test method DialogWindowTest. | ||
/// </summary> | ||
/// <autogeneratedoc /> | ||
[TestMethod()] | ||
public void DialogWindowTest() | ||
{ | ||
Assert.IsNotNull(testView); | ||
Assert.IsInstanceOfType(testView, typeof(DialogWindow)); | ||
Assert.IsNotNull(vm); | ||
Assert.IsInstanceOfType(vm, typeof(DialogWindowViewModel)); | ||
} | ||
|
||
[DataTestMethod] | ||
public void DoCancelTest() | ||
{ | ||
var t = new Thread(() => { testView = new(); vm = (DialogWindowViewModel)testView.DataContext; testView.Show(); vm.CancelCommand.Execute(null); }); | ||
t.SetApartmentState(ApartmentState.STA); //Set the thread to STA | ||
t.Start(); | ||
t.Join(); //Wait for the thread to end | ||
Assert.IsFalse(testView.IsVisible); | ||
} | ||
|
||
|
||
[TestMethod] | ||
public void DoOKTest() | ||
{ | ||
bool? xRes = null; | ||
bool xVisible = false; | ||
var t = new Thread(() => | ||
{ | ||
testView = new(); | ||
vm = (DialogWindowViewModel)testView.DataContext; | ||
ExecOK(); | ||
xRes = testView.ShowDialog(); | ||
// xResult = testView.ShowDialog(); | ||
xVisible = testView.IsVisible; | ||
async void ExecOK() | ||
{ | ||
await System.Threading.Tasks.Task.Delay(50); | ||
testView.Dispatcher.Invoke(() => vm.OKCommand.Execute(null)); | ||
} | ||
}); | ||
t.SetApartmentState(ApartmentState.STA); //Set the thread to STA | ||
t.Start(); | ||
t.Join(); //Wait for the thread to end | ||
Assert.IsFalse(xVisible); | ||
Assert.IsTrue(xRes.HasValue); | ||
Assert.IsTrue(xRes.Value); | ||
} | ||
|
||
} | ||
} |
136 changes: 136 additions & 0 deletions
136
CSharpBible/MVVM_Tutorial/MVVM_09a_CTDialogBoxesTests/View/MainWindowTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
// *********************************************************************** | ||
// Assembly : MVVM_00a_CTTemplate_netTests | ||
// Author : Mir | ||
// Created : 05-14-2023 | ||
// | ||
// Last Modified By : Mir | ||
// Last Modified On : 05-14-2023 | ||
// *********************************************************************** | ||
// <copyright file="MainWindowTests.cs" company="JC-Soft"> | ||
// Copyright © JC-Soft 2023 | ||
// </copyright> | ||
// <summary></summary> | ||
// *********************************************************************** | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using MVVM.ViewModel; | ||
using MVVM_09a_CTDialogBoxes.ViewModel; | ||
using NSubstitute; | ||
using System.ComponentModel; | ||
using System.Threading; | ||
using System.Windows; | ||
|
||
/// <summary> | ||
/// The Tests namespace. | ||
/// </summary> | ||
/// <autogeneratedoc /> | ||
namespace MVVM_09a_CTDialogBoxes.View.Tests | ||
{ | ||
/// <summary> | ||
/// Defines test class MainWindowTests. | ||
/// </summary> | ||
/// <autogeneratedoc /> | ||
[TestClass()] | ||
public class MainWindowTests : BaseTestViewModel | ||
{ | ||
#pragma warning disable CS8618 // Ein Non-Nullable-Feld muss beim Beenden des Konstruktors einen Wert ungleich NULL enthalten. Erwägen Sie die Deklaration als Nullable. | ||
/// <summary> | ||
/// The test view | ||
/// </summary> | ||
/// <autogeneratedoc /> | ||
MainWindow testView; | ||
MainWindowViewModel vm; | ||
private MessageBoxResult mbResult; | ||
private IDialogWindow dw; | ||
private bool? xResult; | ||
private string sNewName = "NewName"; | ||
private string sNewEmail = "NewEmail"; | ||
#pragma warning restore CS8618 // Ein Non-Nullable-Feld muss beim Beenden des Konstruktors einen Wert ungleich NULL enthalten. Erwägen Sie die Deklaration als Nullable. | ||
|
||
/// <summary> | ||
/// Initializes this instance. | ||
/// </summary> | ||
/// <autogeneratedoc /> | ||
[TestInitialize] | ||
public void Init() | ||
{ | ||
var t = new Thread(() => { | ||
testView = new(); | ||
vm = (MainWindowViewModel)testView.DataContext; | ||
testView.Window_Loaded(this,null!); | ||
}); | ||
t.SetApartmentState(ApartmentState.STA); //Set the thread to STA | ||
t.Start(); | ||
t.Join(); //Wait for the thread to end | ||
vm.PropertyChanged += OnVMPropertyChanged; | ||
if (vm is INotifyPropertyChanging npchgn) | ||
npchgn.PropertyChanging += OnVMPropertyChanging; | ||
Assert.IsNotNull(testView.NewDialogWindow); | ||
try { Assert.IsNotNull(testView.NewDialogWindow()); } | ||
catch { } | ||
Assert.IsNotNull(testView.MessageBoxShow); | ||
testView.MessageBoxShow = MyMBShow; | ||
testView.NewDialogWindow = () => | ||
{ | ||
var dw = Substitute.For<IDialogWindow>(); | ||
dw.ShowDialog().Returns((c) => | ||
{ | ||
DoLog($"ShowDialog()=>{xResult}"); | ||
var vm = dw.DataContext as DialogWindowViewModel; | ||
vm!.Name = sNewName; | ||
vm!.Email = sNewEmail; | ||
return xResult; | ||
}); | ||
dw.DataContext.Returns(new DialogWindowViewModel()); | ||
return dw; | ||
}; | ||
} | ||
|
||
private MessageBoxResult MyMBShow(string arg1, string arg2, MessageBoxButton button) | ||
{ | ||
DoLog($"MyMBShow({arg1},{arg2},{button})=>{mbResult}"); | ||
return mbResult; | ||
} | ||
|
||
/// <summary> | ||
/// Defines the test method MainWindowTest. | ||
/// </summary> | ||
/// <autogeneratedoc /> | ||
[TestMethod()] | ||
public void MainWindowTest() | ||
{ | ||
Assert.IsNotNull(testView); | ||
Assert.IsInstanceOfType(testView, typeof(MainWindow)); | ||
Assert.IsNotNull(vm); | ||
Assert.IsInstanceOfType(vm, typeof(MainWindowViewModel)); | ||
Assert.IsNotNull(vm.DoOpenDialog); | ||
Assert.IsNotNull(vm.DoOpenMessageBox); | ||
} | ||
|
||
[DataTestMethod] | ||
[DataRow("OKTitle", "OKLine", MessageBoxResult.OK, new[] { "MyMBShow(OKTitle,OKLine,YesNo)=>OK\r\n" })] | ||
[DataRow("CancelTitle", "CancelLine", MessageBoxResult.Cancel, new[] { "MyMBShow(CancelTitle,CancelLine,YesNo)=>Cancel\r\n" })] | ||
[DataRow("YesTitle", "YesLine", MessageBoxResult.Yes, new[] { "MyMBShow(YesTitle,YesLine,YesNo)=>Yes\r\n" })] | ||
public void DoOpenMessageBoxTest(string sAct1,string sAct2, MessageBoxResult xRes, string[] asExp) | ||
{ | ||
mbResult = xRes; | ||
Assert.AreEqual(xRes, vm.DoOpenMessageBox!.Invoke(sAct1, sAct2)); | ||
Assert.AreEqual(asExp[0], DebugLog); | ||
} | ||
|
||
|
||
[DataTestMethod] | ||
[DataRow("OKName", "OKMail", "OKName1", "OKMail1", true, new[] { "ShowDialog()=>True\r\n", "OKName1", "OKMail1" })] | ||
[DataRow("NoName", "NoMail", "NoName1", "NoMail1", false, new[] { "ShowDialog()=>False\r\n", "NoName", "NoMail" })] | ||
[DataRow("Name", "Mail", "Name1", "Mail1", null, new[] { "ShowDialog()=>\r\n", "Name", "Mail" })] | ||
public void DoDialogWindowTest(string sAct1, string sAct2, string sAct3, string sAct4, bool? xRes, string[] asExp) | ||
{ | ||
xResult = xRes; | ||
sNewName = sAct3; | ||
sNewEmail = sAct4; | ||
var tRes = vm.DoOpenDialog!.Invoke(sAct1, sAct2); | ||
Assert.AreEqual(asExp[1], tRes.name); | ||
Assert.AreEqual(asExp[2], tRes.email); | ||
Assert.AreEqual(asExp[0], DebugLog); | ||
} | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
...rpBible/MVVM_Tutorial/MVVM_09a_CTDialogBoxesTests/ViewModel/DialogWindowViewModelTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
using CommunityToolkit.Mvvm.Input; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using MVVM.ViewModel; | ||
using System; | ||
using System.ComponentModel; | ||
|
||
namespace MVVM_09a_CTDialogBoxes.ViewModel.Tests | ||
{ | ||
[TestClass()] | ||
public class DialogWindowViewModelTests : BaseTestViewModel | ||
{ | ||
#pragma warning disable CS8618 // Ein Non-Nullable-Feld muss beim Beenden des Konstruktors einen Wert ungleich NULL enthalten. Erwägen Sie die Deklaration als Nullable. | ||
/// <summary> | ||
/// The test model | ||
/// </summary> | ||
/// <autogeneratedoc /> | ||
DialogWindowViewModel testModel; | ||
#pragma warning restore CS8618 // Ein Non-Nullable-Feld muss beim Beenden des Konstruktors einen Wert ungleich NULL enthalten. Erwägen Sie die Deklaration als Nullable. | ||
|
||
/// <summary> | ||
/// Initializes this instance. | ||
/// </summary> | ||
/// <autogeneratedoc /> | ||
[TestInitialize] | ||
public void Init() | ||
{ | ||
testModel = new(); | ||
testModel.PropertyChanged += OnVMPropertyChanged; | ||
if (testModel is INotifyPropertyChanging npchgn) | ||
npchgn.PropertyChanging += OnVMPropertyChanging; | ||
testModel.DoCancel += DoCancelTest; | ||
testModel.DoOK += DoOKTest; | ||
ClearLog(); | ||
} | ||
|
||
private void DoOKTest(object o, EventArgs e) | ||
{ | ||
DoLog($"DoOK({o},{e})"); | ||
} | ||
|
||
private void DoCancelTest(object o, EventArgs e) | ||
{ | ||
DoLog($"DoCancel({o},{e})"); | ||
} | ||
|
||
/// <summary> | ||
/// Defines the test method SetupTest. | ||
/// </summary> | ||
/// <autogeneratedoc /> | ||
[TestMethod()] | ||
public void SetupTest() | ||
{ | ||
Assert.IsNotNull(testModel); | ||
Assert.IsInstanceOfType(testModel, typeof(DialogWindowViewModel)); | ||
Assert.IsInstanceOfType(testModel, typeof(BaseViewModelCT)); | ||
Assert.IsInstanceOfType(testModel, typeof(INotifyPropertyChanged)); | ||
Assert.IsNotNull(testModel.CancelCommand); | ||
Assert.IsInstanceOfType(testModel.CancelCommand, typeof(IRelayCommand)); | ||
Assert.IsNotNull(testModel.OKCommand); | ||
Assert.IsInstanceOfType(testModel.OKCommand, typeof(IRelayCommand)); | ||
} | ||
|
||
[DataTestMethod()] | ||
[DataRow(true, new[] { @"DoCancel(MVVM_09a_CTDialogBoxes.ViewModel.DialogWindowViewModel,System.EventArgs) | ||
" })] | ||
[DataRow(false, new[] { @"" })] | ||
[DataRow(null, new[] { @"DoCancel(MVVM_09a_CTDialogBoxes.ViewModel.DialogWindowViewModel,System.EventArgs) | ||
" })] | ||
public void CancelCommandTest(bool? oAct, string[] asExp) | ||
{ | ||
Assert.IsTrue(testModel.CancelCommand.CanExecute(oAct)); | ||
if (oAct == false) testModel.DoCancel -= DoCancelTest; | ||
testModel.CancelCommand.Execute(oAct); | ||
Assert.AreEqual(asExp[0], DebugLog); | ||
} | ||
|
||
[DataTestMethod()] | ||
[DataRow(true, new[] { @"DoOK(MVVM_09a_CTDialogBoxes.ViewModel.DialogWindowViewModel,System.EventArgs) | ||
" })] | ||
[DataRow(false, new[] { @"" })] | ||
[DataRow(null, new[] { @"DoOK(MVVM_09a_CTDialogBoxes.ViewModel.DialogWindowViewModel,System.EventArgs) | ||
" })] | ||
public void OKCommandTest(bool? oAct, string[] asExp) | ||
{ | ||
Assert.IsTrue(testModel.OKCommand.CanExecute(oAct)); | ||
if (oAct == false) testModel.DoOK -= DoOKTest; | ||
testModel.OKCommand.Execute(oAct); | ||
Assert.AreEqual(asExp[0], DebugLog); | ||
} | ||
} | ||
} |
Oops, something went wrong.