Skip to content

Commit

Permalink
Merge pull request #412 from joecare99/CSharpBible
Browse files Browse the repository at this point in the history
C sharp bible
  • Loading branch information
joecare99 authored Jun 12, 2024
2 parents 0e9abb1 + 445c101 commit 6ed7dae
Show file tree
Hide file tree
Showing 32 changed files with 1,329 additions and 112 deletions.
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);
}

}
}
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);
}
}
}
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);
}
}
}
Loading

0 comments on commit 6ed7dae

Please sign in to comment.