From 70a58382028c66d68cc59c8427ecff31611fc30e Mon Sep 17 00:00:00 2001 From: David Hall Date: Sun, 24 Sep 2023 17:01:23 -0600 Subject: [PATCH] Added nullability to unit tests for Imm32, User32, and Usp10 --- PInvoke/User32/WinUser.Keyboard.cs | 2 +- UnitTests/PInvoke/Imm32/MSImeTests.cs | 2 +- UnitTests/PInvoke/User32/User32GdiTests.cs | 15 +-- UnitTests/PInvoke/User32/User32Tests.cs | 193 +++++++---------------------- UnitTests/PInvoke/User32/WrapperTests.cs | 19 ++- UnitTests/PInvoke/Usp10/Usp10Tests.cs | 16 +-- 6 files changed, 66 insertions(+), 181 deletions(-) diff --git a/PInvoke/User32/WinUser.Keyboard.cs b/PInvoke/User32/WinUser.Keyboard.cs index 73219584..804ce107 100644 --- a/PInvoke/User32/WinUser.Keyboard.cs +++ b/PInvoke/User32/WinUser.Keyboard.cs @@ -1199,7 +1199,7 @@ public static partial class User32 // nBuff, HKL *lpList ); [DllImport(Lib.User32, SetLastError = true, ExactSpelling = true)] [PInvokeData("winuser.h", MSDNShortId = "")] - public static extern int GetKeyboardLayoutList(int nBuff, [In, Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] HKL[] lpList); + public static extern int GetKeyboardLayoutList(int nBuff, [In, Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] HKL[]? lpList); /// Retrieves the name of the active input locale identifier (formerly called the keyboard layout) for the system. /// diff --git a/UnitTests/PInvoke/Imm32/MSImeTests.cs b/UnitTests/PInvoke/Imm32/MSImeTests.cs index 07fd2085..b29dd57d 100644 --- a/UnitTests/PInvoke/Imm32/MSImeTests.cs +++ b/UnitTests/PInvoke/Imm32/MSImeTests.cs @@ -42,7 +42,7 @@ public class MSImmTests [Test] public void PlugInDictDictionaryListTest() { - var idlist = new ImePlugInDictDictionaryList1041() as IImePlugInDictDictionaryList; + var idlist = (IImePlugInDictDictionaryList)new ImePlugInDictDictionaryList1041(); Assert.That(() => idlist.GetDictionariesInUse(out var guids, out var dt, out var enc), Throws.Nothing); } diff --git a/UnitTests/PInvoke/User32/User32GdiTests.cs b/UnitTests/PInvoke/User32/User32GdiTests.cs index 78dce273..fcc2c21e 100644 --- a/UnitTests/PInvoke/User32/User32GdiTests.cs +++ b/UnitTests/PInvoke/User32/User32GdiTests.cs @@ -9,22 +9,13 @@ namespace Vanara.PInvoke.Tests; public partial class User32Tests { [Test()] - public void CallNextHookExTest() - { - throw new NotImplementedException(); - } + public void CallNextHookExTest() => throw new NotImplementedException(); [Test()] - public void ChildWindowFromPointExTest() - { - throw new NotImplementedException(); - } + public void ChildWindowFromPointExTest() => throw new NotImplementedException(); [Test()] - public void DestroyIconTest() - { - throw new NotImplementedException(); - } + public void DestroyIconTest() => throw new NotImplementedException(); [Test] public void EnumDisplayDevicesTest() diff --git a/UnitTests/PInvoke/User32/User32Tests.cs b/UnitTests/PInvoke/User32/User32Tests.cs index acaff110..ee665713 100644 --- a/UnitTests/PInvoke/User32/User32Tests.cs +++ b/UnitTests/PInvoke/User32/User32Tests.cs @@ -44,52 +44,28 @@ public partial class User32Tests } [Test()] - public void GetWindowLongTest() - { - throw new NotImplementedException(); - } + public void GetWindowLongTest() => throw new NotImplementedException(); [Test()] - public void GetWindowLong32Test() - { - throw new NotImplementedException(); - } + public void GetWindowLong32Test() => throw new NotImplementedException(); [Test()] - public void GetWindowLongPtrTest() - { - throw new NotImplementedException(); - } + public void GetWindowLongPtrTest() => throw new NotImplementedException(); [Test()] - public void LockWorkStationTest() - { - throw new NotImplementedException(); - } + public void LockWorkStationTest() => throw new NotImplementedException(); [Test()] - public void RealGetWindowClassTest() - { - throw new NotImplementedException(); - } + public void RealGetWindowClassTest() => throw new NotImplementedException(); [Test()] - public void RegisterHotKeyTest() - { - throw new NotImplementedException(); - } + public void RegisterHotKeyTest() => throw new NotImplementedException(); [Test()] - public void RegisterWindowMessageTest() - { - throw new NotImplementedException(); - } + public void RegisterWindowMessageTest() => throw new NotImplementedException(); [Test()] - public void ScreenToClientTest() - { - throw new NotImplementedException(); - } + public void ScreenToClientTest() => throw new NotImplementedException(); [Test()] public void SendMessageTest() @@ -150,167 +126,86 @@ public partial class User32Tests TestContext.WriteLine(sb); } - [Test()] - public void SendMessageTest2() - { - throw new NotImplementedException(); - } + [Test()] + public void SendMessageTest2() => throw new NotImplementedException(); [Test()] - public void SendMessageTest3() - { - throw new NotImplementedException(); - } + public void SendMessageTest3() => throw new NotImplementedException(); [Test()] - public void SendMessageTest4() - { - throw new NotImplementedException(); - } + public void SendMessageTest4() => throw new NotImplementedException(); [Test()] - public void SendMessageTest5() - { - throw new NotImplementedException(); - } + public void SendMessageTest5() => throw new NotImplementedException(); [Test()] - public void SetWindowLongTest() - { - throw new NotImplementedException(); - } + public void SetWindowLongTest() => throw new NotImplementedException(); [Test()] - public void SetWindowPosTest() - { - throw new NotImplementedException(); - } + public void SetWindowPosTest() => throw new NotImplementedException(); [Test()] - public void SetWindowTextTest() - { - throw new NotImplementedException(); - } + public void SetWindowTextTest() => throw new NotImplementedException(); [Test()] - public void ShutdownBlockReasonCreateTest() - { - throw new NotImplementedException(); - } + public void ShutdownBlockReasonCreateTest() => throw new NotImplementedException(); [Test()] - public void ShutdownBlockReasonDestroyTest() - { - throw new NotImplementedException(); - } + public void ShutdownBlockReasonDestroyTest() => throw new NotImplementedException(); [Test()] - public void ShutdownBlockReasonQueryTest() - { - throw new NotImplementedException(); - } + public void ShutdownBlockReasonQueryTest() => throw new NotImplementedException(); [Test()] - public void ShutdownBlockReasonQueryTest1() - { - throw new NotImplementedException(); - } + public void ShutdownBlockReasonQueryTest1() => throw new NotImplementedException(); [Test()] - public void UnhookWindowsHookExTest() - { - throw new NotImplementedException(); - } + public void UnhookWindowsHookExTest() => throw new NotImplementedException(); [Test()] - public void UnregisterHotKeyTest() - { - throw new NotImplementedException(); - } + public void UnregisterHotKeyTest() => throw new NotImplementedException(); [Test()] - public void WindowFromPointTest() - { - throw new NotImplementedException(); - } + public void WindowFromPointTest() => throw new NotImplementedException(); [Test()] - public void ExitWindowsExTest() - { - throw new NotImplementedException(); - } + public void ExitWindowsExTest() => throw new NotImplementedException(); [Test()] - public void DrawTextTest() - { - throw new NotImplementedException(); - } + public void DrawTextTest() => throw new NotImplementedException(); [Test()] - public void GetClientRectTest() - { - throw new NotImplementedException(); - } + public void GetClientRectTest() => throw new NotImplementedException(); [Test()] - public void GetWindowRectTest() - { - throw new NotImplementedException(); - } + public void GetWindowRectTest() => throw new NotImplementedException(); [Test()] - public void InvalidateRectTest() - { - throw new NotImplementedException(); - } + public void InvalidateRectTest() => throw new NotImplementedException(); [Test()] - public void MapWindowPointsTest() - { - throw new NotImplementedException(); - } + public void MapWindowPointsTest() => throw new NotImplementedException(); [Test()] - public void MapWindowPointsTest1() - { - throw new NotImplementedException(); - } + public void MapWindowPointsTest1() => throw new NotImplementedException(); [Test()] - public void MapWindowPointsTest2() - { - throw new NotImplementedException(); - } + public void MapWindowPointsTest2() => throw new NotImplementedException(); [Test()] - public void SendMessageTest6() - { - throw new NotImplementedException(); - } + public void SendMessageTest6() => throw new NotImplementedException(); [Test()] - public void LoadImageTest() - { - throw new NotImplementedException(); - } + public void LoadImageTest() => throw new NotImplementedException(); [Test()] - public void LoadStringTest() - { - throw new NotImplementedException(); - } + public void LoadStringTest() => throw new NotImplementedException(); [Test()] - public void LoadStringTest1() - { - throw new NotImplementedException(); - } + public void LoadStringTest1() => throw new NotImplementedException(); [Test()] - public void SetWindowsHookExTest() - { - throw new NotImplementedException(); - } + public void SetWindowsHookExTest() => throw new NotImplementedException(); [Test] public void SystemParametersInfoGetTest() @@ -371,35 +266,35 @@ public partial class User32Tests var gmi = X(e, mi, CorrespondingAction.Get); if (gmi is null) continue; - var param = new object[] { e, null }; + var param = new object?[] { e, null }; TestContext.Write($"{e}: "); - if ((bool)gmi.Invoke(null, param)) + if ((bool)gmi.Invoke(null, param)!) TestContext.WriteLine($"{param[1]}"); else TestContext.Write($"ERROR: {Win32Error.GetLastError()}"); - if (!Enum.TryParse(Enum.GetName(typeof(SPI), e).Replace("SPI_GET", "SPI_SET"), out SPI se) || se == SPI.SPI_SETSHOWSOUNDS) + if (!Enum.TryParse(Enum.GetName(typeof(SPI), e)!.Replace("SPI_GET", "SPI_SET"), out SPI se) || se == SPI.SPI_SETSHOWSOUNDS) continue; gmi = X(se, smi, CorrespondingAction.Set); if (gmi is null) continue; - var sparam = new object[] { se, param[1], false, false }; + var sparam = new object?[] { se, param[1], false, false }; TestContext.Write($"{se}: "); - if ((bool)gmi.Invoke(null, sparam)) + if ((bool)gmi.Invoke(null, sparam)!) TestContext.WriteLine("Pass"); else TestContext.Write($"Fail: {Win32Error.GetLastError()}"); } - static MethodInfo X(SPI e, MethodInfo mi, CorrespondingAction a) + static MethodInfo? X(SPI e, MethodInfo mi, CorrespondingAction a) { - if (!e.GetType().GetField(e.ToString()).GetCustomAttributes().Any()) + if (!e.GetType().GetField(e.ToString())!.GetCustomAttributes().Any()) { var typeAttrs = CorrespondingTypeAttribute.GetAttrForEnum(e).ToArray(); if (typeAttrs.Length > 0 && typeAttrs[0].Action == a) { var genType = typeAttrs[0].TypeRef; - if (genType.IsValueType) + if (genType!.IsValueType) return mi.MakeGenericMethod(genType); } } diff --git a/UnitTests/PInvoke/User32/WrapperTests.cs b/UnitTests/PInvoke/User32/WrapperTests.cs index d6d1598d..5f3a65ca 100644 --- a/UnitTests/PInvoke/User32/WrapperTests.cs +++ b/UnitTests/PInvoke/User32/WrapperTests.cs @@ -10,10 +10,10 @@ public partial class User32Tests [Test] public void WindowClassCtorTest() { - WindowClass wc = null; + WindowClass? wc = null; Assert.DoesNotThrow(() => wc = new()); - Assert.AreEqual(wc.wc.hInstance, (HINSTANCE)GetModuleHandle()); + Assert.AreEqual(wc!.wc.hInstance, (HINSTANCE)GetModuleHandle()); Assert.True(wc.Unregister()); Assert.DoesNotThrow(() => wc = new("MyCustomName")); @@ -32,13 +32,13 @@ public partial class User32Tests { using BasicMessageWindow wnd = new(); - WindowClass wc3 = null; - Assert.NotNull(wc3 = WindowClass.GetNamedInstance(wnd.ClassName, GetModuleHandle())); - Assert.AreEqual(wnd.ClassName, wc3.ClassName); + WindowClass? wc3 = null; + Assert.NotNull(wc3 = WindowClass.GetNamedInstance(wnd!.ClassName!, GetModuleHandle())); + Assert.AreEqual(wnd.ClassName, wc3!.ClassName); - WindowClass wc = null; + WindowClass? wc = null; Assert.NotNull(wc = WindowClass.GetInstanceFromWindow(wnd.Handle)); - Assert.AreEqual(wc.ClassName, wnd.ClassName); + Assert.AreEqual(wc!.ClassName, wnd.ClassName); } [Test] @@ -67,10 +67,7 @@ public partial class User32Tests } [Test] - public void WindowRunTest() - { - VisibleWindow.Run(null, "Hello"); - } + public void WindowRunTest() => VisibleWindow.Run(null, "Hello"); public class MyWin : VisibleWindow { diff --git a/UnitTests/PInvoke/Usp10/Usp10Tests.cs b/UnitTests/PInvoke/Usp10/Usp10Tests.cs index 03a955d5..b0e51c84 100644 --- a/UnitTests/PInvoke/Usp10/Usp10Tests.cs +++ b/UnitTests/PInvoke/Usp10/Usp10Tests.cs @@ -10,9 +10,11 @@ namespace Vanara.PInvoke.Tests; public class Usp10Tests { private const string str = "Hello, world!\u064A\u064F\u0633\u0627\u0648\u0650\u064A How are you?"; +#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. private SafeHDC dc; private SafeHFONT fnt; private SafeSCRIPT_CACHE sc; +#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. [OneTimeSetUp] public void _Setup() @@ -137,8 +139,8 @@ public class Usp10Tests [Test] public void ScriptGetPropertiesTest() { - Assert.That(ScriptGetProperties(out SCRIPT_PROPERTIES[] sp), ResultIs.Successful); - sp.WriteValues(); + Assert.That(ScriptGetProperties(out SCRIPT_PROPERTIES[]? sp), ResultIs.Successful); + sp?.WriteValues(); } [Test] @@ -148,10 +150,10 @@ public class Usp10Tests public void ScriptItemizeTest() { const int max = 50; - Assert.That(ScriptGetProperties(out SCRIPT_PROPERTIES[] sp), ResultIs.Successful); + Assert.That(ScriptGetProperties(out SCRIPT_PROPERTIES[]? sp), ResultIs.Successful); SCRIPT_ITEM[] pItems = new SCRIPT_ITEM[max]; Assert.That(ScriptItemize(str, str.Length, pItems.Length, default, default, pItems, out int cItems), ResultIs.Successful); - Assert.That(pItems.Take(cItems).Select(i => i.a.eScript).Select(s => sp[s].fComplex), Has.Some.True); + Assert.That(pItems.Take(cItems).Select(i => i.a.eScript).Select(s => sp?[s].fComplex), Has.Some.True); foreach (SCRIPT_ITEM i in pItems.Take(cItems)) { i.WriteValues(); @@ -162,7 +164,7 @@ public class Usp10Tests public void ScriptItemizeTest2() { const int max = 50; - Assert.That(ScriptGetProperties(out SCRIPT_PROPERTIES[] sp), ResultIs.Successful); + Assert.That(ScriptGetProperties(out SCRIPT_PROPERTIES[]? sp), ResultIs.Successful); Assert.That(ScriptRecordDigitSubstitution(LCID.LOCALE_CUSTOM_DEFAULT, out SCRIPT_DIGITSUBSTITUTE sub), ResultIs.Successful); sub.WriteValues(); @@ -173,7 +175,7 @@ public class Usp10Tests SCRIPT_ITEM[] pItems = new SCRIPT_ITEM[max]; Assert.That(ScriptItemize(str, str.Length, pItems.Length, sc, ss, pItems, out int cItems), ResultIs.Successful); - Assert.That(pItems.Take(cItems).Select(i => i.a.eScript).Select(s => sp[s].fComplex), Has.Some.True); + Assert.That(pItems.Take(cItems).Select(i => i.a.eScript).Select(s => sp?[s].fComplex), Has.Some.True); foreach (SCRIPT_ITEM i in pItems.Take(cItems)) { i.WriteValues(); @@ -208,6 +210,6 @@ public class Usp10Tests ssa.WriteValues(); Assert.That(ssa.Out(POINT.Empty), ResultIs.Successful); Assert.That(ssa.CPtoX(5, true), Is.GreaterThan(0)); - Assert.That(ssa.XtoCP(ssa.Size.Value.Width).cp, Is.GreaterThan(30)); + Assert.That(ssa.XtoCP(ssa.Size!.Value.Width).cp, Is.GreaterThan(30)); } } \ No newline at end of file