Added nullability to unit tests for Imm32, User32, and Usp10

nullableenabled
David Hall 2023-09-24 17:01:23 -06:00
parent 3f5dbc6bb0
commit 70a5838202
6 changed files with 66 additions and 181 deletions

View File

@ -1199,7 +1199,7 @@ public static partial class User32
// nBuff, HKL *lpList ); // nBuff, HKL *lpList );
[DllImport(Lib.User32, SetLastError = true, ExactSpelling = true)] [DllImport(Lib.User32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("winuser.h", MSDNShortId = "")] [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);
/// <summary>Retrieves the name of the active input locale identifier (formerly called the keyboard layout) for the system.</summary> /// <summary>Retrieves the name of the active input locale identifier (formerly called the keyboard layout) for the system.</summary>
/// <param name="pwszKLID"> /// <param name="pwszKLID">

View File

@ -42,7 +42,7 @@ public class MSImmTests
[Test] [Test]
public void PlugInDictDictionaryListTest() 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); Assert.That(() => idlist.GetDictionariesInUse(out var guids, out var dt, out var enc), Throws.Nothing);
} }

View File

@ -9,22 +9,13 @@ namespace Vanara.PInvoke.Tests;
public partial class User32Tests public partial class User32Tests
{ {
[Test()] [Test()]
public void CallNextHookExTest() public void CallNextHookExTest() => throw new NotImplementedException();
{
throw new NotImplementedException();
}
[Test()] [Test()]
public void ChildWindowFromPointExTest() public void ChildWindowFromPointExTest() => throw new NotImplementedException();
{
throw new NotImplementedException();
}
[Test()] [Test()]
public void DestroyIconTest() public void DestroyIconTest() => throw new NotImplementedException();
{
throw new NotImplementedException();
}
[Test] [Test]
public void EnumDisplayDevicesTest() public void EnumDisplayDevicesTest()

View File

@ -44,52 +44,28 @@ public partial class User32Tests
} }
[Test()] [Test()]
public void GetWindowLongTest() public void GetWindowLongTest() => throw new NotImplementedException();
{
throw new NotImplementedException();
}
[Test()] [Test()]
public void GetWindowLong32Test() public void GetWindowLong32Test() => throw new NotImplementedException();
{
throw new NotImplementedException();
}
[Test()] [Test()]
public void GetWindowLongPtrTest() public void GetWindowLongPtrTest() => throw new NotImplementedException();
{
throw new NotImplementedException();
}
[Test()] [Test()]
public void LockWorkStationTest() public void LockWorkStationTest() => throw new NotImplementedException();
{
throw new NotImplementedException();
}
[Test()] [Test()]
public void RealGetWindowClassTest() public void RealGetWindowClassTest() => throw new NotImplementedException();
{
throw new NotImplementedException();
}
[Test()] [Test()]
public void RegisterHotKeyTest() public void RegisterHotKeyTest() => throw new NotImplementedException();
{
throw new NotImplementedException();
}
[Test()] [Test()]
public void RegisterWindowMessageTest() public void RegisterWindowMessageTest() => throw new NotImplementedException();
{
throw new NotImplementedException();
}
[Test()] [Test()]
public void ScreenToClientTest() public void ScreenToClientTest() => throw new NotImplementedException();
{
throw new NotImplementedException();
}
[Test()] [Test()]
public void SendMessageTest() public void SendMessageTest()
@ -150,167 +126,86 @@ public partial class User32Tests
TestContext.WriteLine(sb); TestContext.WriteLine(sb);
} }
[Test()] [Test()]
public void SendMessageTest2() public void SendMessageTest2() => throw new NotImplementedException();
{
throw new NotImplementedException();
}
[Test()] [Test()]
public void SendMessageTest3() public void SendMessageTest3() => throw new NotImplementedException();
{
throw new NotImplementedException();
}
[Test()] [Test()]
public void SendMessageTest4() public void SendMessageTest4() => throw new NotImplementedException();
{
throw new NotImplementedException();
}
[Test()] [Test()]
public void SendMessageTest5() public void SendMessageTest5() => throw new NotImplementedException();
{
throw new NotImplementedException();
}
[Test()] [Test()]
public void SetWindowLongTest() public void SetWindowLongTest() => throw new NotImplementedException();
{
throw new NotImplementedException();
}
[Test()] [Test()]
public void SetWindowPosTest() public void SetWindowPosTest() => throw new NotImplementedException();
{
throw new NotImplementedException();
}
[Test()] [Test()]
public void SetWindowTextTest() public void SetWindowTextTest() => throw new NotImplementedException();
{
throw new NotImplementedException();
}
[Test()] [Test()]
public void ShutdownBlockReasonCreateTest() public void ShutdownBlockReasonCreateTest() => throw new NotImplementedException();
{
throw new NotImplementedException();
}
[Test()] [Test()]
public void ShutdownBlockReasonDestroyTest() public void ShutdownBlockReasonDestroyTest() => throw new NotImplementedException();
{
throw new NotImplementedException();
}
[Test()] [Test()]
public void ShutdownBlockReasonQueryTest() public void ShutdownBlockReasonQueryTest() => throw new NotImplementedException();
{
throw new NotImplementedException();
}
[Test()] [Test()]
public void ShutdownBlockReasonQueryTest1() public void ShutdownBlockReasonQueryTest1() => throw new NotImplementedException();
{
throw new NotImplementedException();
}
[Test()] [Test()]
public void UnhookWindowsHookExTest() public void UnhookWindowsHookExTest() => throw new NotImplementedException();
{
throw new NotImplementedException();
}
[Test()] [Test()]
public void UnregisterHotKeyTest() public void UnregisterHotKeyTest() => throw new NotImplementedException();
{
throw new NotImplementedException();
}
[Test()] [Test()]
public void WindowFromPointTest() public void WindowFromPointTest() => throw new NotImplementedException();
{
throw new NotImplementedException();
}
[Test()] [Test()]
public void ExitWindowsExTest() public void ExitWindowsExTest() => throw new NotImplementedException();
{
throw new NotImplementedException();
}
[Test()] [Test()]
public void DrawTextTest() public void DrawTextTest() => throw new NotImplementedException();
{
throw new NotImplementedException();
}
[Test()] [Test()]
public void GetClientRectTest() public void GetClientRectTest() => throw new NotImplementedException();
{
throw new NotImplementedException();
}
[Test()] [Test()]
public void GetWindowRectTest() public void GetWindowRectTest() => throw new NotImplementedException();
{
throw new NotImplementedException();
}
[Test()] [Test()]
public void InvalidateRectTest() public void InvalidateRectTest() => throw new NotImplementedException();
{
throw new NotImplementedException();
}
[Test()] [Test()]
public void MapWindowPointsTest() public void MapWindowPointsTest() => throw new NotImplementedException();
{
throw new NotImplementedException();
}
[Test()] [Test()]
public void MapWindowPointsTest1() public void MapWindowPointsTest1() => throw new NotImplementedException();
{
throw new NotImplementedException();
}
[Test()] [Test()]
public void MapWindowPointsTest2() public void MapWindowPointsTest2() => throw new NotImplementedException();
{
throw new NotImplementedException();
}
[Test()] [Test()]
public void SendMessageTest6() public void SendMessageTest6() => throw new NotImplementedException();
{
throw new NotImplementedException();
}
[Test()] [Test()]
public void LoadImageTest() public void LoadImageTest() => throw new NotImplementedException();
{
throw new NotImplementedException();
}
[Test()] [Test()]
public void LoadStringTest() public void LoadStringTest() => throw new NotImplementedException();
{
throw new NotImplementedException();
}
[Test()] [Test()]
public void LoadStringTest1() public void LoadStringTest1() => throw new NotImplementedException();
{
throw new NotImplementedException();
}
[Test()] [Test()]
public void SetWindowsHookExTest() public void SetWindowsHookExTest() => throw new NotImplementedException();
{
throw new NotImplementedException();
}
[Test] [Test]
public void SystemParametersInfoGetTest() public void SystemParametersInfoGetTest()
@ -371,35 +266,35 @@ public partial class User32Tests
var gmi = X(e, mi, CorrespondingAction.Get); var gmi = X(e, mi, CorrespondingAction.Get);
if (gmi is null) if (gmi is null)
continue; continue;
var param = new object[] { e, null }; var param = new object?[] { e, null };
TestContext.Write($"{e}: "); TestContext.Write($"{e}: ");
if ((bool)gmi.Invoke(null, param)) if ((bool)gmi.Invoke(null, param)!)
TestContext.WriteLine($"{param[1]}"); TestContext.WriteLine($"{param[1]}");
else else
TestContext.Write($"ERROR: {Win32Error.GetLastError()}"); 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; continue;
gmi = X(se, smi, CorrespondingAction.Set); gmi = X(se, smi, CorrespondingAction.Set);
if (gmi is null) if (gmi is null)
continue; continue;
var sparam = new object[] { se, param[1], false, false }; var sparam = new object?[] { se, param[1], false, false };
TestContext.Write($"{se}: "); TestContext.Write($"{se}: ");
if ((bool)gmi.Invoke(null, sparam)) if ((bool)gmi.Invoke(null, sparam)!)
TestContext.WriteLine("Pass"); TestContext.WriteLine("Pass");
else else
TestContext.Write($"Fail: {Win32Error.GetLastError()}"); 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<ObsoleteAttribute>().Any()) if (!e.GetType().GetField(e.ToString())!.GetCustomAttributes<ObsoleteAttribute>().Any())
{ {
var typeAttrs = CorrespondingTypeAttribute.GetAttrForEnum(e).ToArray(); var typeAttrs = CorrespondingTypeAttribute.GetAttrForEnum(e).ToArray();
if (typeAttrs.Length > 0 && typeAttrs[0].Action == a) if (typeAttrs.Length > 0 && typeAttrs[0].Action == a)
{ {
var genType = typeAttrs[0].TypeRef; var genType = typeAttrs[0].TypeRef;
if (genType.IsValueType) if (genType!.IsValueType)
return mi.MakeGenericMethod(genType); return mi.MakeGenericMethod(genType);
} }
} }

View File

@ -10,10 +10,10 @@ public partial class User32Tests
[Test] [Test]
public void WindowClassCtorTest() public void WindowClassCtorTest()
{ {
WindowClass wc = null; WindowClass? wc = null;
Assert.DoesNotThrow(() => wc = new()); Assert.DoesNotThrow(() => wc = new());
Assert.AreEqual(wc.wc.hInstance, (HINSTANCE)GetModuleHandle()); Assert.AreEqual(wc!.wc.hInstance, (HINSTANCE)GetModuleHandle());
Assert.True(wc.Unregister()); Assert.True(wc.Unregister());
Assert.DoesNotThrow(() => wc = new("MyCustomName")); Assert.DoesNotThrow(() => wc = new("MyCustomName"));
@ -32,13 +32,13 @@ public partial class User32Tests
{ {
using BasicMessageWindow wnd = new(); using BasicMessageWindow wnd = new();
WindowClass wc3 = null; WindowClass? wc3 = null;
Assert.NotNull(wc3 = WindowClass.GetNamedInstance(wnd.ClassName, GetModuleHandle())); Assert.NotNull(wc3 = WindowClass.GetNamedInstance(wnd!.ClassName!, GetModuleHandle()));
Assert.AreEqual(wnd.ClassName, wc3.ClassName); Assert.AreEqual(wnd.ClassName, wc3!.ClassName);
WindowClass wc = null; WindowClass? wc = null;
Assert.NotNull(wc = WindowClass.GetInstanceFromWindow(wnd.Handle)); Assert.NotNull(wc = WindowClass.GetInstanceFromWindow(wnd.Handle));
Assert.AreEqual(wc.ClassName, wnd.ClassName); Assert.AreEqual(wc!.ClassName, wnd.ClassName);
} }
[Test] [Test]
@ -67,10 +67,7 @@ public partial class User32Tests
} }
[Test] [Test]
public void WindowRunTest() public void WindowRunTest() => VisibleWindow.Run<MyWin>(null, "Hello");
{
VisibleWindow.Run<MyWin>(null, "Hello");
}
public class MyWin : VisibleWindow public class MyWin : VisibleWindow
{ {

View File

@ -10,9 +10,11 @@ namespace Vanara.PInvoke.Tests;
public class Usp10Tests public class Usp10Tests
{ {
private const string str = "Hello, world!\u064A\u064F\u0633\u0627\u0648\u0650\u064A How are you?"; 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 SafeHDC dc;
private SafeHFONT fnt; private SafeHFONT fnt;
private SafeSCRIPT_CACHE sc; 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] [OneTimeSetUp]
public void _Setup() public void _Setup()
@ -137,8 +139,8 @@ public class Usp10Tests
[Test] [Test]
public void ScriptGetPropertiesTest() public void ScriptGetPropertiesTest()
{ {
Assert.That(ScriptGetProperties(out SCRIPT_PROPERTIES[] sp), ResultIs.Successful); Assert.That(ScriptGetProperties(out SCRIPT_PROPERTIES[]? sp), ResultIs.Successful);
sp.WriteValues(); sp?.WriteValues();
} }
[Test] [Test]
@ -148,10 +150,10 @@ public class Usp10Tests
public void ScriptItemizeTest() public void ScriptItemizeTest()
{ {
const int max = 50; 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]; 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(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)) foreach (SCRIPT_ITEM i in pItems.Take(cItems))
{ {
i.WriteValues(); i.WriteValues();
@ -162,7 +164,7 @@ public class Usp10Tests
public void ScriptItemizeTest2() public void ScriptItemizeTest2()
{ {
const int max = 50; 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); Assert.That(ScriptRecordDigitSubstitution(LCID.LOCALE_CUSTOM_DEFAULT, out SCRIPT_DIGITSUBSTITUTE sub), ResultIs.Successful);
sub.WriteValues(); sub.WriteValues();
@ -173,7 +175,7 @@ public class Usp10Tests
SCRIPT_ITEM[] pItems = new SCRIPT_ITEM[max]; 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(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)) foreach (SCRIPT_ITEM i in pItems.Take(cItems))
{ {
i.WriteValues(); i.WriteValues();
@ -208,6 +210,6 @@ public class Usp10Tests
ssa.WriteValues(); ssa.WriteValues();
Assert.That(ssa.Out(POINT.Empty), ResultIs.Successful); Assert.That(ssa.Out(POINT.Empty), ResultIs.Successful);
Assert.That(ssa.CPtoX(5, true), Is.GreaterThan(0)); 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));
} }
} }