<commit_before>﻿using NUnit.Framework.Constraints;
using System;

namespace Vanara.PInvoke.Tests
{
	public abstract class ResultIs // : NUnit.Framework.Is
	{
		public static FailureConstraint Failure => new FailureConstraint();
		public static MyConstraintExpression Not => MyConstraintExpression.Not;
		public static SuccessfulConstraint Successful => new SuccessfulConstraint();
		public static ValidHandleConstraint ValidHandle => new ValidHandleConstraint();

		public static FailureConstraint FailureCode(object expectedError) => new FailureConstraint(expectedError);
		public static ValueConstraint Value(object value) => new ValueConstraint(value);
	}

	public class MyConstraintExpression
	{
		private OpConstraint.Op op;

		private MyConstraintExpression(OpConstraint.Op _op) => op = _op;

		public static MyConstraintExpression Not => new MyConstraintExpression(OpConstraint.Op.Not);

		public ValidHandleConstraint ValidHandle => new ValidHandleConstraint(op);

		public ValueConstraint Value(object value) => new ValueConstraint(value, op);
	}

	public class FailureConstraint : Constraint
	{
		public FailureConstraint(object expected = null)
		{
			switch (expected)
			{
				case null:
					break;

				case int i:
					Expected = new Win32Error(i);
					break;

				case uint i:
					Expected = new HRESULT(i);
					break;

				case IErrorProvider iep:
					Expected = iep;
					break;

				default:
					throw new ArgumentException();
			}
		}

		public object Expected { get; }

		public override ConstraintResult ApplyTo<TActual>(TActual actual)
		{
			var success = false;
			object updActual = actual;
			switch (actual)
			{
				case bool b:
					success = b;
					Description = nameof(Win32Error.ERROR_SUCCESS);
					if (!b)
					{
						var le = Win32Error.GetLastError();
						if (Expected != null) Description = Expected.ToString();
						success = Expected is null ? le.Failed : le.Failed && ((IErrorProvider)Expected).ToHRESULT().Equals(le.ToHRESULT());
						updActual = le;
					}
					break;

				case HRESULT hr:
					success = Expected is null ? hr.Failed : hr.Failed && ((IErrorProvider)Expected).ToHRESULT().Equals(hr);
					Description = Expected?.ToString() ?? nameof(HRESULT.S_OK);
					break;

				case Win32Error err:
					success = Expected is null ? err.Failed : err.Failed && ((IErrorProvider)Expected).ToHRESULT().Equals(err.ToHRESULT());
					Description = Expected?.ToString() ?? nameof(Win32Error.ERROR_SUCCESS);
					break;

				case NTStatus st:
					success = Expected is null ? st.Failed : st.Failed && ((IErrorProvider)Expected).ToHRESULT().Equals(((IErrorProvider)st).ToHRESULT());
					Description = Expected?.ToString() ?? nameof(NTStatus.STATUS_SUCCESS);
					break;

				case int i:
					var e = new Win32Error(i);
					success = Expected is null ? e.Failed : e.Failed && ((IErrorProvider)Expected).ToHRESULT().Equals(e.ToHRESULT());
					updActual = e;
					Description = Expected?.ToString() ?? nameof(Win32Error.ERROR_SUCCESS);
					break;

				case uint ui:
					var h = new HRESULT(ui);
					success = Expected is null ? h.Failed : h.Failed && ((IErrorProvider)Expected).ToHRESULT().Equals(h);
					updActual = h;
					Description = Expected?.ToString() ?? nameof(HRESULT.S_OK);
					break;

				default:
					break;
			}
			return new ConstraintResult(this, updActual, success);
		}
	}

	public class SuccessfulConstraint : Constraint
	{
		public SuccessfulConstraint()
		{
		}

		public override ConstraintResult ApplyTo<TActual>(TActual actual)
		{
			var success = false;
			object updActual = actual;
			switch (actual)
			{
				case bool b:
					success = b;
					Description = nameof(Win32Error.ERROR_SUCCESS);
					if (!b)
					{
						var le = Win32Error.GetLastError();<commit_msg>Minor tweaks<commit_after>﻿using NUnit.Framework.Constraints;
using System;
using Vanara.PInvoke;

namespace Vanara.PInvoke.Tests
{
	public abstract class ResultIs // : NUnit.Framework.Is
	{
		public static FailureConstraint Failure => new FailureConstraint();
		public static MyConstraintExpression Not => MyConstraintExpression.Not;
		public static SuccessfulConstraint Successful => new SuccessfulConstraint();
		public static ValidHandleConstraint ValidHandle => new ValidHandleConstraint();

		public static FailureConstraint FailureCode(object expectedError) => new FailureConstraint(expectedError);
		public static ValueConstraint Value(object value) => new ValueConstraint(value);
	}

	public class MyConstraintExpression
	{
		private OpConstraint.Op op;

		private MyConstraintExpression(OpConstraint.Op _op) => op = _op;

		public static MyConstraintExpression Not => new MyConstraintExpression(OpConstraint.Op.Not);

		public ValidHandleConstraint ValidHandle => new ValidHandleConstraint(op);

		public ValueConstraint Value(object value) => new ValueConstraint(value, op);
	}

	public class FailureConstraint : Constraint
	{
		public FailureConstraint(object expected = null)
		{
			switch (expected)
			{
				case null:
					break;

				case int i:
					Expected = new Win32Error(i);
					break;

				case uint i:
					Expected = new HRESULT(i);
					break;

				case IErrorProvider iep:
					Expected = iep;
					break;

				default:
					throw new ArgumentException();
			}
		}

		public object Expected { get; }

		public override ConstraintResult ApplyTo<TActual>(TActual actual)
		{
			var success = false;
			object updActual = actual;
			switch (actual)
			{
				case bool b:
					success = b;
					Description = nameof(Win32Error.ERROR_SUCCESS);
					if (!b)
					{
						var le = Win32Error.GetLastError();
						if (Expected != null) Description = Expected.ToString();
						success = Expected is null ? le.Failed : le.Failed && ((IErrorProvider)Expected).ToHRESULT().Equals(le.ToHRESULT());
						updActual = le;
					}
					break;

				case HRESULT hr:
					success = Expected is null ? hr.Failed : hr.Failed && ((IErrorProvider)Expected).ToHRESULT().Equals(hr);
					Description = Expected?.ToString() ?? nameof(HRESULT.S_OK);
					break;

				case Win32Error err:
					success = Expected is null ? err.Failed : err.Failed && ((IErrorProvider)Expected).ToHRESULT().Equals(err.ToHRESULT());
					Description = Expected?.ToString() ?? nameof(Win32Error.ERROR_SUCCESS);
					break;

				case NTStatus st:
					success = Expected is null ? st.Failed : st.Failed && ((IErrorProvider)Expected).ToHRESULT().Equals(((IErrorProvider)st).ToHRESULT());
					Description = Expected?.ToString() ?? nameof(NTStatus.STATUS_SUCCESS);
					break;

				case int i:
					var e = new Win32Error(i);
					success = Expected is null ? e.Failed : e.Failed && ((IErrorProvider)Expected).ToHRESULT().Equals(e.ToHRESULT());
					updActual = e;
					Description = Expected?.ToString() ?? nameof(Win32Error.ERROR_SUCCESS);
					break;

				case uint ui:
					var h = new HRESULT(ui);
					success = Expected is null ? h.Failed : h.Failed && ((IErrorProvider)Expected).ToHRESULT().Equals(h);
					updActual = h;
					Description = Expected?.ToString() ?? nameof(HRESULT.S_OK);
					break;

				default:
					break;
			}
			return new ConstraintResult(this, updActual, success);
		}
	}

	public class SuccessfulConstraint : Constraint
	{
		public SuccessfulConstraint()
		{
		}

		public override ConstraintResult ApplyTo<TActual>(TActual actual)
		{
			var success = false;
			var updActual = actual as IErrorProvider;
			switch (actual)
			{
				case bool b:
					success = b;
					Description = nameof(Win32Error.ERROR_SUCCESS);
					if (!b)
					{
						var le = Win32Error.GetLastError();
{'fraction_non_alphanumeric': 0.10790031813361611, 'fraction_numerical': 0.004772004241781549, 'mean_word_length': 3.5922093730979916, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4033988', 'n_tokens_mistral': 2770, 'n_tokens_neox': 2157, 'n_words': 560}