mirror of https://github.com/dahall/Vanara.git
Updated dump and custom constraints test helpers
parent
4bb12ff369
commit
7746a986bf
|
@ -7,6 +7,7 @@ namespace Vanara.PInvoke.Tests
|
|||
public static FailureConstraint Failure => new FailureConstraint();
|
||||
public static SuccessfulConstraint Successful => new SuccessfulConstraint();
|
||||
public static ValueConstraint Value(object value) => new ValueConstraint(value);
|
||||
public static ValidHandleConstraint ValidHandle => new ValidHandleConstraint();
|
||||
}
|
||||
|
||||
public class FailureConstraint : Constraint
|
||||
|
@ -146,4 +147,35 @@ namespace Vanara.PInvoke.Tests
|
|||
return new ConstraintResult(this, actual, true);
|
||||
}
|
||||
}
|
||||
|
||||
public class ValidHandleConstraint : Constraint
|
||||
{
|
||||
public ValidHandleConstraint()
|
||||
{
|
||||
}
|
||||
|
||||
public override ConstraintResult ApplyTo<TActual>(TActual actual)
|
||||
{
|
||||
var success = true;
|
||||
switch (actual)
|
||||
{
|
||||
case System.Runtime.InteropServices.SafeHandle h:
|
||||
success = !h.IsInvalid;
|
||||
break;
|
||||
case IHandle ih:
|
||||
var l = ih.DangerousGetHandle().ToInt64();
|
||||
success = l != 0 && l != -1;
|
||||
break;
|
||||
case System.IntPtr p:
|
||||
l = p.ToInt64();
|
||||
success = l != 0 && l != -1;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
Description = $"Valid handle";
|
||||
return new ConstraintResult(this, success ? "Valid handle" : $"Invalid handle (Err: {Win32Error.GetLastError()})", success);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -36,8 +36,15 @@ namespace Vanara.PInvoke.Tests
|
|||
|
||||
public static void WriteValues(this object value)
|
||||
{
|
||||
var json = JsonConvert.SerializeObject(value, Formatting.Indented, new Newtonsoft.Json.Converters.StringEnumConverter());
|
||||
var json = JsonConvert.SerializeObject(value, Formatting.Indented, new Newtonsoft.Json.Converters.StringEnumConverter(), new SizeTConverter());
|
||||
TestContext.WriteLine(json);
|
||||
}
|
||||
|
||||
private class SizeTConverter : JsonConverter<SizeT>
|
||||
{
|
||||
public override void WriteJson(JsonWriter writer, SizeT value, JsonSerializer serializer) => writer.WriteValue(value.Value);
|
||||
|
||||
public override SizeT ReadJson(JsonReader reader, Type objectType, SizeT existingValue, bool hasExistingValue, JsonSerializer serializer) => reader.Value is ulong ul ? new SizeT(ul) : new SizeT(0);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -218,7 +218,7 @@ namespace Vanara.PInvoke.Tests
|
|||
var status = MEMORYSTATUS.Default;
|
||||
GlobalMemoryStatus(ref status);
|
||||
Assert.That(status.dwAvailVirtual, Is.Not.Zero);
|
||||
//status.WriteValues();
|
||||
status.WriteValues();
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
|
Loading…
Reference in New Issue