| | | 1 | | // Copyright (c) .NET Foundation. All rights reserved. |
| | | 2 | | // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Diagnostics; |
| | | 7 | | using System.Linq; |
| | | 8 | | using System.Reflection; |
| | | 9 | | using JetBrains.Annotations; |
| | | 10 | | |
| | | 11 | | // Copied from https://github.com/aspnet/EntityFramework/blob/dev/src/Shared/Check.cs |
| | | 12 | | namespace WireMock.Validation |
| | | 13 | | { |
| | | 14 | | // [ExcludeFromCodeCoverage] |
| | | 15 | | [DebuggerStepThrough] |
| | | 16 | | internal static class Check |
| | | 17 | | { |
| | | 18 | | [ContractAnnotation("value:null => halt")] |
| | | 19 | | public static T Condition<T>([NoEnumeration] T value, [NotNull] Predicate<T> condition, [InvokerParameterName] [ |
| | 24 | 20 | | { |
| | 24 | 21 | | NotNull(condition, nameof(condition)); |
| | 24 | 22 | | NotNull(value, nameof(value)); |
| | | 23 | | |
| | 24 | 24 | | if (!condition(value)) |
| | 1 | 25 | | { |
| | 1 | 26 | | NotNullOrEmpty(parameterName, nameof(parameterName)); |
| | | 27 | | |
| | 1 | 28 | | throw new ArgumentOutOfRangeException(parameterName); |
| | | 29 | | } |
| | | 30 | | |
| | 23 | 31 | | return value; |
| | 23 | 32 | | } |
| | | 33 | | |
| | | 34 | | [ContractAnnotation("value:null => halt")] |
| | | 35 | | public static T NotNull<T>([NoEnumeration] T value, [InvokerParameterName] [NotNull] string parameterName) |
| | 4052 | 36 | | { |
| | 4053 | 37 | | if (ReferenceEquals(value, null)) |
| | 2 | 38 | | { |
| | 2 | 39 | | NotNullOrEmpty(parameterName, nameof(parameterName)); |
| | | 40 | | |
| | 2 | 41 | | throw new ArgumentNullException(parameterName); |
| | | 42 | | } |
| | | 43 | | |
| | 4050 | 44 | | return value; |
| | 4051 | 45 | | } |
| | | 46 | | |
| | | 47 | | [ContractAnnotation("value:null => halt")] |
| | | 48 | | public static T NotNull<T>([NoEnumeration] T value, [InvokerParameterName] [NotNull] string parameterName, [NotN |
| | 0 | 49 | | { |
| | 0 | 50 | | if (ReferenceEquals(value, null)) |
| | 0 | 51 | | { |
| | 0 | 52 | | NotNullOrEmpty(parameterName, nameof(parameterName)); |
| | 0 | 53 | | NotNullOrEmpty(propertyName, nameof(propertyName)); |
| | | 54 | | |
| | 0 | 55 | | throw new ArgumentException(CoreStrings.ArgumentPropertyNull(propertyName, parameterName)); |
| | | 56 | | } |
| | | 57 | | |
| | 0 | 58 | | return value; |
| | 0 | 59 | | } |
| | | 60 | | |
| | | 61 | | [ContractAnnotation("value:null => halt")] |
| | | 62 | | public static IList<T> NotNullOrEmpty<T>(IList<T> value, [InvokerParameterName] [NotNull] string parameterName) |
| | 455 | 63 | | { |
| | 455 | 64 | | NotNull(value, parameterName); |
| | | 65 | | |
| | 455 | 66 | | if (value.Count == 0) |
| | 0 | 67 | | { |
| | 0 | 68 | | NotNullOrEmpty(parameterName, nameof(parameterName)); |
| | | 69 | | |
| | 0 | 70 | | throw new ArgumentException(CoreStrings.CollectionArgumentIsEmpty(parameterName)); |
| | | 71 | | } |
| | | 72 | | |
| | 456 | 73 | | return value; |
| | 456 | 74 | | } |
| | | 75 | | |
| | | 76 | | [ContractAnnotation("value:null => halt")] |
| | | 77 | | public static string NotNullOrEmpty(string value, [InvokerParameterName] [NotNull] string parameterName) |
| | 36 | 78 | | { |
| | 36 | 79 | | Exception e = null; |
| | 36 | 80 | | if (ReferenceEquals(value, null)) |
| | 2 | 81 | | { |
| | 2 | 82 | | e = new ArgumentNullException(parameterName); |
| | 2 | 83 | | } |
| | 34 | 84 | | else if (value.Trim().Length == 0) |
| | 0 | 85 | | { |
| | 0 | 86 | | e = new ArgumentException(CoreStrings.ArgumentIsEmpty(parameterName)); |
| | 0 | 87 | | } |
| | | 88 | | |
| | 36 | 89 | | if (e != null) |
| | 2 | 90 | | { |
| | 2 | 91 | | NotNullOrEmpty(parameterName, nameof(parameterName)); |
| | | 92 | | |
| | 2 | 93 | | throw e; |
| | | 94 | | } |
| | | 95 | | |
| | 34 | 96 | | return value; |
| | 34 | 97 | | } |
| | | 98 | | |
| | | 99 | | public static string NullButNotEmpty(string value, [InvokerParameterName] [NotNull] string parameterName) |
| | 0 | 100 | | { |
| | 0 | 101 | | if (!ReferenceEquals(value, null) |
| | 0 | 102 | | && (value.Length == 0)) |
| | 0 | 103 | | { |
| | 0 | 104 | | NotNullOrEmpty(parameterName, nameof(parameterName)); |
| | | 105 | | |
| | 0 | 106 | | throw new ArgumentException(CoreStrings.ArgumentIsEmpty(parameterName)); |
| | | 107 | | } |
| | | 108 | | |
| | 0 | 109 | | return value; |
| | 0 | 110 | | } |
| | | 111 | | |
| | | 112 | | public static IList<T> HasNoNulls<T>(IList<T> value, [InvokerParameterName] [NotNull] string parameterName) |
| | | 113 | | where T : class |
| | 39 | 114 | | { |
| | 39 | 115 | | NotNull(value, parameterName); |
| | | 116 | | |
| | 78 | 117 | | if (value.Any(e => e == null)) |
| | 0 | 118 | | { |
| | 0 | 119 | | NotNullOrEmpty(parameterName, nameof(parameterName)); |
| | | 120 | | |
| | 0 | 121 | | throw new ArgumentException(parameterName); |
| | | 122 | | } |
| | | 123 | | |
| | 39 | 124 | | return value; |
| | 39 | 125 | | } |
| | | 126 | | |
| | | 127 | | public static Type ValidEntityType(Type value, [InvokerParameterName] [NotNull] string parameterName) |
| | 0 | 128 | | { |
| | 0 | 129 | | if (!value.GetTypeInfo().IsClass) |
| | 0 | 130 | | { |
| | 0 | 131 | | NotNullOrEmpty(parameterName, nameof(parameterName)); |
| | | 132 | | |
| | 0 | 133 | | throw new ArgumentException(CoreStrings.InvalidEntityType(value, parameterName)); |
| | | 134 | | } |
| | | 135 | | |
| | 0 | 136 | | return value; |
| | 0 | 137 | | } |
| | | 138 | | } |
| | | 139 | | } |