예제 #1
0
        public static RequireContract <T> NotNull <T>(this T obj, string name = null)
        {
            var r = new RequireContract <T>(obj, name);

            r.Conditions.Add(new RequireCondition <T>(o => !EqualityComparer <T> .Default.Equals(o, default), $"{name} is null", typeof(ArgumentNullException)));
            return(r);
        }
예제 #2
0
 public static void Requires <T0, T1, T2>(RequireContract <T0> arg0, RequireContract <T1> arg1, RequireContract <T2> arg2, [CallerMemberName] string callerName = "",
                                          [CallerFilePath] string filePath = "", [CallerLineNumber] int callerLineNumber = 0)
 {
     Requires(arg0, arg0.Name ?? "params[0]", callerName, filePath, callerLineNumber);
     Requires(arg1, arg1.Name ?? "params[1]", callerName, filePath, callerLineNumber);
     Requires(arg2, arg2.Name ?? "params[2]", callerName, filePath, callerLineNumber);
 }
예제 #3
0
        private static void Requires <T>(RequireContract <T> arg, string info = "", [CallerMemberName] string callerName = "", [CallerFilePath] string filePath = "", [CallerLineNumber] int callerLineNumber = 0)
        {
            var fileName = string.IsNullOrEmpty(filePath) ? "" : Path.GetFileName(filePath);

            foreach (var c in arg.Conditions)
            {
                if (!c.Condition(arg.Arg))
                {
                    if (c.ExceptionType != null)
                    {
                        throw (Exception)Activator.CreateInstance(c.ExceptionType, $"Contract: {info} in {callerName} - {fileName}:{callerLineNumber} {c.ErrorMessage}");
                    }
                    throw new ContractsException($"Contract: {info} in {callerName} - {fileName}:{callerLineNumber} {c.ErrorMessage}");
                }
            }
        }
예제 #4
0
 public static RequireContract <int> InRange(this RequireContract <int> r, Func <int> min, Func <int> max, string name = null)
 {
     r.Conditions.Add(new RequireCondition <int>(o => min() <= o && o < max(), $"{name} not in range {min()}<{name ?? "x"}<{max()}", typeof(ArgumentOutOfRangeException)));
     return(r);
 }
예제 #5
0
 public static void Requires <T>(RequireContract <T> arg, [CallerMemberName] string callerName = "", [CallerFilePath] string filePath = "", [CallerLineNumber] int callerLineNumber = 0)
 {
     Requires(arg, arg.Name ?? "params[0]", callerName, filePath, callerLineNumber);
 }
예제 #6
0
 public static RequireContract <T> IsTrue <T>(this RequireContract <T> r, Predicate <T> pred, string name = null)
 {
     r.Conditions.Add(new RequireCondition <T>(pred, $"{name} IsTrue condition failed"));
     return(r);
 }