Unit tests getting up-to-par for StyleCop (#5364)

Co-authored-by: Clint Rutkas <crutkas@microsoft.com>
This commit is contained in:
Clint Rutkas 2020-08-04 14:01:37 -07:00 committed by GitHub
parent 48b816fb36
commit 1f590c45be
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 143 additions and 126 deletions

View file

@ -210,6 +210,21 @@
<Version>16.6.1</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\codeAnalysis\GlobalSuppressions.cs">
<Link>GlobalSuppressions.cs</Link>
</Compile>
<AdditionalFiles Include="..\..\codeAnalysis\StyleCop.json">
<Link>StyleCop.json</Link>
</AdditionalFiles>
</ItemGroup>
<ItemGroup>
<PackageReference Include="StyleCop.Analyzers">
<Version>1.1.118</Version>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\common\ManagedTelemetry\Telemetry\Telemetry.csproj">
<Project>{5d00d290-4016-4cfe-9e41-1e7c724509ba}</Project>

View file

@ -1,9 +1,13 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using Microsoft.PowerToys.Settings.UI.Lib;
using Microsoft.PowerToys.Settings.UnitTest;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Schema;
using System;
namespace CommonLibTest
{
@ -17,7 +21,7 @@ namespace CommonLibTest
[Obsolete]
public void ToJsonString_ShouldReturnValidJSONOfModel_WhenSuccessful()
{
// Arrange
// Arrange
string file_name = "test\\BasePTModuleSettingsTest";
string expectedSchemaText = @"
{
@ -46,4 +50,4 @@ namespace CommonLibTest
Assert.IsTrue(valid);
}
}
}
}

View file

@ -1,4 +1,8 @@
using Microsoft.PowerToys.Settings.UI.Lib;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Microsoft.PowerToys.Settings.UI.Lib;
namespace Microsoft.PowerToys.Settings.UnitTest
{
@ -10,4 +14,4 @@ namespace Microsoft.PowerToys.Settings.UnitTest
Version = string.Empty;
}
}
}
}

View file

@ -1,6 +1,10 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.PowerToys.Settings.UI.Lib.Utilities;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using Microsoft.PowerToys.Settings.UI.Lib.Utilities;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace CommonLibTest
{
@ -13,7 +17,6 @@ namespace CommonLibTest
Assert.IsTrue(res < 0);
}
public static void TestStringsAreEqual(string v1, string v2)
{
var res = Helper.CompareVersions(v1, v2);
@ -46,7 +49,7 @@ namespace CommonLibTest
[ExpectedException(typeof(FormatException))]
public void Helper_CompareVersions_ShouldThrowBadFormat_WhenNoVersionString()
{
Helper.CompareVersions("v0.0.1", "");
Helper.CompareVersions("v0.0.1", string.Empty);
}
[TestMethod]

View file

@ -1,12 +1,14 @@
using Microsoft.PowerToys.Settings.UI.Lib;
using Microsoft.PowerToys.Settings.UnitTest;
using Microsoft.VisualStudio.TestTools.UnitTesting;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.PowerToys.Settings.UI.Lib;
using Microsoft.PowerToys.Settings.UnitTest;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace CommonLibTest
{
@ -22,7 +24,7 @@ namespace CommonLibTest
}
}
[TestCleanup()]
[TestCleanup]
public void Cleanup()
{
string file_name = "\\test";
@ -50,7 +52,7 @@ namespace CommonLibTest
}
[TestMethod]
public async Task SaveSettings_ShouldCreateFile_WhenFilePathIsNotFoundAsync()
public void SaveSettings_ShouldCreateFile_WhenFilePathIsNotFound()
{
// Arrange
string file_name = "test\\Test Folder";
@ -113,6 +115,7 @@ namespace CommonLibTest
Random random = new Random();
int length = 20;
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[random.Next(s.Length)]).ToArray());
}

View file

@ -1,18 +1,12 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
namespace Microsoft.PowerToys.Settings.UnitTest
@ -20,16 +14,17 @@ namespace Microsoft.PowerToys.Settings.UnitTest
/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// </summary>
sealed partial class App : Application
public sealed partial class App : Application
{
/// <summary>
/// Initializes the singleton application object. This is the first line of authored code
/// Initializes a new instance of the <see cref="App"/> class.
/// This is the first line of authored code
/// executed, and as such is the logical equivalent of main() or WinMain().
/// </summary>
public App()
{
this.InitializeComponent();
this.Suspending += OnSuspending;
InitializeComponent();
Suspending += OnSuspending;
}
/// <summary>
@ -39,11 +34,10 @@ namespace Microsoft.PowerToys.Settings.UnitTest
/// <param name="e">Details about the launch request and process.</param>
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
#if DEBUG
if (System.Diagnostics.Debugger.IsAttached)
{
this.DebugSettings.EnableFrameRateCounter = true;
DebugSettings.EnableFrameRateCounter = true;
}
#endif
@ -60,7 +54,7 @@ namespace Microsoft.PowerToys.Settings.UnitTest
if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
//TODO: Load state from previously suspended application
// TODO: Load state from previously suspended application
}
// Place the frame in the current Window
@ -80,7 +74,7 @@ namespace Microsoft.PowerToys.Settings.UnitTest
/// </summary>
/// <param name="sender">The Frame which failed navigation</param>
/// <param name="e">Details about the navigation failure</param>
void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
private void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
{
throw new Exception("Failed to load Page " + e.SourcePageType.FullName);
}
@ -95,7 +89,8 @@ namespace Microsoft.PowerToys.Settings.UnitTest
private void OnSuspending(object sender, SuspendingEventArgs e)
{
var deferral = e.SuspendingOperation.GetDeferral();
//TODO: Save application state and stop any background activity
// TODO: Save application state and stop any background activity
deferral.Complete();
}
}

View file

@ -1,9 +1,13 @@
using Microsoft.PowerToys.Settings.UI.Lib;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.IO;
using System.Text.Json;
using Microsoft.PowerToys.Settings.UI.Lib;
using Microsoft.PowerToys.Settings.UI.ViewModels;
using Microsoft.PowerToys.Settings.UI.Views;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using System.Text.Json;
namespace ViewModelTests
{

View file

@ -1,14 +1,13 @@
using Microsoft.PowerToys.Settings.UI.Lib;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.IO;
using System.Text.Json;
using Microsoft.PowerToys.Settings.UI.Lib;
using Microsoft.PowerToys.Settings.UI.ViewModels;
using Microsoft.PowerToys.Settings.UI.Views;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Windows.UI;
namespace ViewModelTests
@ -326,7 +325,7 @@ namespace ViewModelTests
{
// arrange
FancyZonesViewModel viewModel = new FancyZonesViewModel();
Assert.AreEqual("", viewModel.ExcludedApps);
Assert.AreEqual(string.Empty, viewModel.ExcludedApps);
// Assert
ShellPage.DefaultSndMSGCallback = msg =>
@ -357,7 +356,7 @@ namespace ViewModelTests
viewModel.HighlightOpacity = 60;
}
private String ToRGBHex(Color color)
private string ToRGBHex(Color color)
{
return "#" + color.R.ToString("X2") + color.G.ToString("X2") + color.B.ToString("X2");
}

View file

@ -1,14 +1,13 @@
using Microsoft.PowerToys.Settings.UI.Lib;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.IO;
using System.Text.Json;
using Microsoft.PowerToys.Settings.UI.Lib;
using Microsoft.PowerToys.Settings.UI.ViewModels;
using Microsoft.PowerToys.Settings.UI.Views;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace ViewModelTests
{

View file

@ -1,14 +1,14 @@
using Microsoft.PowerToys.Settings.UI.Lib;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.IO;
using System.Linq;
using System.Text.Json;
using Microsoft.PowerToys.Settings.UI.Lib;
using Microsoft.PowerToys.Settings.UI.ViewModels;
using Microsoft.PowerToys.Settings.UI.Views;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace ViewModelTests
{
@ -16,6 +16,7 @@ namespace ViewModelTests
public class ImageResizer
{
public const string Module = "ImageResizer";
[TestInitialize]
public void Setup()
{
@ -138,7 +139,6 @@ namespace ViewModelTests
Assert.AreEqual(true, settings.Properties.ImageresizerKeepDateModified.Value);
}
[TestMethod]
public void Encoder_ShouldUpdateValue_WhenSuccessful()
{
@ -201,7 +201,7 @@ namespace ViewModelTests
double negativeWidth = -2.0;
double negativeHeight = -2.0;
// Act
// Act
imageSize.Width = negativeWidth;
imageSize.Height = negativeHeight;
@ -209,7 +209,7 @@ namespace ViewModelTests
Assert.AreEqual(0, imageSize.Width);
Assert.AreEqual(0, imageSize.Height);
// Act
// Act
imageSize.Width = 50;
imageSize.Height = 50;

View file

@ -1,11 +1,12 @@
using Microsoft.PowerToys.Settings.UI.Lib;
using Microsoft.PowerToys.Settings.UI.ViewModels;
using Microsoft.PowerToys.Settings.UI.Views;
using Microsoft.VisualStudio.TestTools.UnitTesting;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using Microsoft.PowerToys.Settings.UI.Lib;
using Microsoft.PowerToys.Settings.UI.ViewModels;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Windows.System;
namespace ViewModelTests
@ -17,11 +18,13 @@ namespace ViewModelTests
[TestInitialize]
public void Setup()
{ }
{
}
[TestCleanup]
public void CleanUp()
{ }
{
}
[TestMethod]
public void CombineShortcutLists_ShouldReturnEmptyList_WhenBothArgumentsAreEmptyLists()

View file

@ -1,31 +1,30 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.PowerToys.Settings.UI.ViewModels;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Microsoft.PowerToys.Settings.UI.Lib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.Json;
using Microsoft.PowerToys.Settings.UI.ViewModels;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace ViewModelTests
{
[TestClass]
public class PowerLauncher
public class PowerLauncherViewModelTest
{
class SendCallbackMock
private class SendCallbackMock
{
public int TimesSent { get; set; }
public void OnSend(PowerLauncherSettings settings)
{
TimesSent++;
}
}
private PowerLauncherViewModel viewModel;
private PowerLauncherSettings mockSettings;
private SendCallbackMock sendCallbackMock;
[TestInitialize]
public void Initialize()
{
@ -34,8 +33,7 @@ namespace ViewModelTests
viewModel = new PowerLauncherViewModel(
mockSettings,
new PowerLauncherViewModel.SendCallback(sendCallbackMock.OnSend)
);
new PowerLauncherViewModel.SendCallback(sendCallbackMock.OnSend));
}
[TestMethod]
@ -65,7 +63,6 @@ namespace ViewModelTests
openPowerLauncher.Win = true;
openPowerLauncher.Code = (int)Windows.System.VirtualKey.S;
var openFileLocation = new HotkeySettings();
openFileLocation.Ctrl = true;
openFileLocation.Code = (int)Windows.System.VirtualKey.A;
@ -91,32 +88,28 @@ namespace ViewModelTests
false,
false,
false,
(int)Windows.System.VirtualKey.S
);
(int)Windows.System.VirtualKey.S);
AssertHotkeySettings(
mockSettings.Properties.OpenFileLocation,
false,
true,
false,
false,
(int)Windows.System.VirtualKey.A
);
(int)Windows.System.VirtualKey.A);
AssertHotkeySettings(
mockSettings.Properties.OpenConsole,
false,
false,
true,
false,
(int)Windows.System.VirtualKey.D
);
(int)Windows.System.VirtualKey.D);
AssertHotkeySettings(
mockSettings.Properties.CopyPathLocation,
false,
false,
false,
true,
(int)Windows.System.VirtualKey.F
);
(int)Windows.System.VirtualKey.F);
}
[TestMethod]
@ -125,7 +118,6 @@ namespace ViewModelTests
viewModel.OverrideWinRKey = true;
viewModel.OverrideWinSKey = false;
Assert.AreEqual(1, sendCallbackMock.TimesSent);
Assert.IsTrue(mockSettings.Properties.OverrideWinkeyR);

View file

@ -1,14 +1,13 @@
using Microsoft.PowerToys.Settings.UI.Lib;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.IO;
using System.Text.Json;
using Microsoft.PowerToys.Settings.UI.Lib;
using Microsoft.PowerToys.Settings.UI.ViewModels;
using Microsoft.PowerToys.Settings.UI.Views;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace ViewModelTests
{

View file

@ -1,14 +1,13 @@
using Microsoft.PowerToys.Settings.UI.Lib;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.IO;
using System.Text.Json;
using Microsoft.PowerToys.Settings.UI.Lib;
using Microsoft.PowerToys.Settings.UI.ViewModels;
using Microsoft.PowerToys.Settings.UI.Views;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace ViewModelTests
{
@ -16,7 +15,6 @@ namespace ViewModelTests
public class PowerRename
{
public const string ModuleName = "PowerRename";
public string schemaText = null;
[TestInitialize]
public void Setup()
@ -101,7 +99,6 @@ namespace ViewModelTests
viewModel.IsEnabled = false;
viewModel.MRUEnabled = true;
Assert.IsFalse(viewModel.GlobalAndMruEnabled);
}

View file

@ -1,14 +1,13 @@
using Microsoft.PowerToys.Settings.UI.Lib;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.IO;
using System.Text.Json;
using Microsoft.PowerToys.Settings.UI.Lib;
using Microsoft.PowerToys.Settings.UI.ViewModels;
using Microsoft.PowerToys.Settings.UI.Views;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Schema;
using System;
using System.Diagnostics;
using System.IO;
using System.Text.Json;
using Windows.UI.Popups;
namespace ViewModelTests
{
@ -119,6 +118,7 @@ namespace ViewModelTests
ShellPage.DefaultSndMSGCallback = msg =>
{
ShortcutGuideSettingsIPCMessage snd = JsonSerializer.Deserialize<ShortcutGuideSettingsIPCMessage>(msg);
// Serialisation not working as expected in the test project:
Assert.AreEqual(100, snd.Powertoys.ShortcutGuide.Properties.OverlayOpacity.Value);
};
@ -127,4 +127,4 @@ namespace ViewModelTests
viewModel.OverlayOpacity = 100;
}
}
}
}