Skip to content

Commit 11af9a7

Browse files
committed
MAUI: Added unit tests shared with Windows.
1 parent 7d224e4 commit 11af9a7

File tree

9 files changed

+172
-12
lines changed

9 files changed

+172
-12
lines changed

CalculateX.MAUI.sln

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MathExpressions", "MAUI.Sou
99
EndProject
1010
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MathExpressions.UnitTests", "MAUI.Source\MathExpressions.UnitTests\MathExpressions.UnitTests.csproj", "{51B7C763-6C9B-477E-9056-CC348172AF7A}"
1111
EndProject
12+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CalculateX.UnitTests", "MAUI.Source\CalculateX.UnitTests\CalculateX.UnitTests.csproj", "{1255F73B-4EED-445C-B5DF-E13BE9C37C68}"
13+
EndProject
1214
Global
1315
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1416
Debug|Any CPU = Debug|Any CPU
@@ -28,6 +30,9 @@ Global
2830
{51B7C763-6C9B-477E-9056-CC348172AF7A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
2931
{51B7C763-6C9B-477E-9056-CC348172AF7A}.Debug|Any CPU.Build.0 = Debug|Any CPU
3032
{51B7C763-6C9B-477E-9056-CC348172AF7A}.Release|Any CPU.ActiveCfg = Release|Any CPU
33+
{1255F73B-4EED-445C-B5DF-E13BE9C37C68}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
34+
{1255F73B-4EED-445C-B5DF-E13BE9C37C68}.Debug|Any CPU.Build.0 = Debug|Any CPU
35+
{1255F73B-4EED-445C-B5DF-E13BE9C37C68}.Release|Any CPU.ActiveCfg = Release|Any CPU
3136
EndGlobalSection
3237
GlobalSection(SolutionProperties) = preSolution
3338
HideSolutionNode = FALSE
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<!--
5+
Need to run on Windows, so must build for Windows.
6+
But then how to distinguish between building WPF version and Android version.
7+
Looks like we might need Windows-specific and Android-specific unit test files.
8+
-->
9+
<TargetFramework>net8.0-windows10.0.20348.0</TargetFramework>
10+
11+
<Nullable>enable</Nullable>
12+
<ImplicitUsings>enable</ImplicitUsings>
13+
<IsPackable>false</IsPackable>
14+
</PropertyGroup>
15+
16+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
17+
<DefineConstants>$(DefineConstants);MAUI_UNITTESTS</DefineConstants>
18+
</PropertyGroup>
19+
20+
<ItemGroup>
21+
<PackageReference Include="CommunityToolkit.Maui" Version="7.0.1" />
22+
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
23+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
24+
<PackageReference Include="MSTest.TestAdapter" Version="3.2.0" />
25+
<PackageReference Include="MSTest.TestFramework" Version="3.2.0" />
26+
</ItemGroup>
27+
28+
<ItemGroup>
29+
<Compile Include="..\..\Shared.Source\CalculateX.UnitTests\**" />
30+
<Compile Include="..\..\Windows.Source\CalculateX.UnitTests\*.cs" />
31+
<Compile Include="..\..\MAUI.Source\CalculateX\Shared\Numbers.cs" LinkBase="Shared" />
32+
<Compile Include="..\..\MAUI.Source\CalculateX\Models\*.cs" LinkBase="Models" />
33+
<Compile Include="..\..\MAUI.Source\CalculateX\ViewModels\*.cs" Link="ViewModels\%(RecursiveDir)%(FileName)%(Extension)" />
34+
<!-- <Compile Include="..\..\MAUI.Source\CalculateX\Views\AboutPage.xaml.cs" LinkBase="Views" /> -->
35+
<!-- <Compile Include="..\..\MAUI.Source\CalculateX\Views\VariablesPage.xaml.cs" LinkBase="Views" /> -->
36+
<!-- <Compile Include="..\..\MAUI.Source\CalculateX\Views\WorkspacePage.xaml.cs" LinkBase="Views" /> -->
37+
</ItemGroup>
38+
39+
<ItemGroup>
40+
<ProjectReference Include="..\MathExpressions\MathExpressions.csproj" />
41+
</ItemGroup>
42+
43+
</Project>

MAUI.Source/CalculateX/ViewModels/WorkspaceViewModel.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ private async Task RenameWorkspaceAsync()
169169

170170
private async Task VariablesWorkspaceAsync()
171171
{
172+
#if !MAUI_UNITTESTS
172173
// Navigate to page that displays the variables from this workspace
173174
await Shell.Current.GoToAsync(nameof(Views.VariablesPage),
174175
new Dictionary<string, object>()
@@ -178,10 +179,13 @@ await Shell.Current.GoToAsync(nameof(Views.VariablesPage),
178179
_workspace.Variables
179180
}
180181
});
182+
#endif
181183
}
182184

183185
private async Task Help()
184186
{
187+
#if !MAUI_UNITTESTS
185188
await Shell.Current.GoToAsync(nameof(Views.HelpPage));
189+
#endif
186190
}
187191
}

MAUI.Source/CalculateX/ViewModels/WorkspacesViewModel.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ private void AddWorkspace()
5454

5555
private async Task About()
5656
{
57+
#if !MAUI_UNITTESTS
5758
await Shell.Current.GoToAsync(nameof(Views.AboutPage));
59+
#endif
5860
}
5961

6062
public void SaveWorkspaces() => _workspaces.SaveWorkspaces(_selectedWorkspaceID);
@@ -94,6 +96,7 @@ public async Task SelectWorkspaceAsync(WorkspaceViewModel? workspaceVM)
9496

9597
_selectedWorkspaceID = workspaceVM.ID;
9698

99+
#if !MAUI_UNITTESTS
97100
// Should navigate to "NotePage?ItemId=path\on\device\XYZ.notes.txt"
98101
//Shell.Current.GoToAsync($"{nameof(Views.WorkspacePage)}?select={workspace.ID}");
99102
await Shell.Current.GoToAsync(nameof(Views.WorkspacePage),
@@ -104,6 +107,7 @@ await Shell.Current.GoToAsync(nameof(Views.WorkspacePage),
104107
TheWorkspaceViewModels.First(workspaceView => workspaceView.ID == workspaceVM.ID)
105108
}
106109
});
110+
#endif
107111
}
108112

109113
private string FormWorkspaceName()

Windows.Source/CalculateX.UnitTests/Windows.WorkspaceViewModel.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,27 @@ public void TestConstructorDefault()
3333
{
3434
ViewModels.WorkspaceViewModel vm = new();
3535
Assert.IsTrue(vm.CanCloseTab);
36+
#if !MAUI_UNITTESTS
3637
Assert.AreEqual("Designer", vm.Name);
38+
#else
39+
Assert.AreEqual("temporary", vm.Name);
40+
#endif
3741
}
3842

3943
[TestMethod]
4044
public void TestConstructorCanCloseTab()
4145
{
46+
#if !MAUI_UNITTESTS
4247
Assert.ThrowsException<ArgumentException>(() => new ViewModels.WorkspaceViewModel(canCloseTab: true));
4348

4449
ViewModels.WorkspaceViewModel vm = new(canCloseTab: false);
4550
Assert.IsFalse(vm.CanCloseTab);
4651
Assert.AreEqual("+", vm.Name);
52+
#else
53+
ViewModels.WorkspaceViewModel vm = new();
54+
Assert.IsTrue(vm.CanCloseTab);
55+
Assert.AreEqual("temporary", vm.Name);
56+
#endif
4757
}
4858

4959
[TestMethod]
@@ -69,7 +79,11 @@ public void TestInput()
6979

7080
vm.EvaluateCommand.Execute(null);
7181

82+
#if !MAUI_UNITTESTS
7283
vm.ClearInput();
84+
#else
85+
vm.Input = string.Empty;
86+
#endif
7387
Assert.AreEqual(string.Empty, vm.Input);
7488
}
7589

@@ -92,6 +106,7 @@ public void TestInsertTextAtCursor()
92106
Assert.AreEqual(10, vm.InsertTextAtCursor(insertText, vm.Input.Length - insertText.Length, insertText.Length));
93107
}
94108

109+
#if !MAUI_UNITTESTS
95110
[TestMethod]
96111
public void TestHelp()
97112
{
@@ -124,4 +139,5 @@ public void TestClearHistory()
124139
vm.ClearHistoryCommand.Execute(null);
125140
Assert.IsFalse(vm.ClearHistoryCommand.CanExecute(null));
126141
}
142+
#endif
127143
}

Windows.Source/CalculateX.UnitTests/Windows.Workspaces.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,11 @@ public void TestManagement()
124124

125125
Assert.AreEqual(3, newWorkspaces.TheWorkspaces.Count);
126126
Assert.AreEqual(3, workspace3.Variables.Count);
127+
#if !MAUI_UNITTESTS
127128
Assert.AreEqual(workspace2.ID, newWorkspaces.LoadedSelectedWorkspaceID);
129+
#else
130+
Assert.AreEqual(workspace2.ID, newWorkspaces.SelectedWorkspaceID);
131+
#endif
128132

129133
Assert.IsNotNull(newWorkspaces.TheWorkspaces.SingleOrDefault(w => w.ID == workspace1.ID));
130134
Assert.IsNotNull(newWorkspaces.TheWorkspaces.SingleOrDefault(w => w.ID == workspace2.ID));

0 commit comments

Comments
 (0)