コード例 #1
0
 public static Assertable <T> True <T>(this Assertable <T> a, Func <T, bool> shouldBeTrue, string errorMsg)
 {
     if (!shouldBeTrue(a.Item))
     {
         throw a.Exception($"Condition '{errorMsg}' is false, Expected=true");
     }
     return(a);
 }
コード例 #2
0
 public static Assertable <T> Null <T>(this Assertable <T> a)
 {
     if (a.Item != null)
     {
         throw a.Exception("Object is not null, Expected=null");
     }
     return(a);
 }
コード例 #3
0
        public static Assertable <T> NotEqual <T>(this Assertable <T> a, T notExpected)
        {
            if (object.Equals(a.Item, notExpected))
            {
                throw a.Exception($"Objects are equal: Actual({a.Item?.ToString()}) == Expected({notExpected?.ToString()})");
            }

            return(a);
        }
コード例 #4
0
        public static Assertable <T> Equal <T>(this Assertable <T> a, T expected)
        {
            if (!object.Equals(a.Item, expected))
            {
                throw a.Exception($"Objects are not equal: Actual({a.Item?.ToString()}) <> Expected({expected?.ToString()})");
            }

            return(a);
        }
コード例 #5
0
        public static Assertable <T> Single <T>(this Assertable <IEnumerable <T> > a)
        {
            var sample = a.Item
                         .Select((v, i) => new { v, i })
                         .TakeWhile(x => x.i <= 1)
                         .Select(x => x.v)
                         .ToArray();

            return(SingleImpl(a, sample));
        }
コード例 #6
0
        private static Assertable <T> SingleImpl <T, TT>(Assertable <TT> a, IReadOnlyList <T> sample)
            where TT : IEnumerable <T>
        {
            if (sample.Count == 0)
            {
                throw a.Exception("Collection is empty, expected exactly one item");
            }

            if (sample.Count > 1)
            {
                var coll = string.Join(", ", a.Item.Select(x => x.ToString()));
                throw a.Exception($"Collection has more than one element, expected exactly one item. Collection: \n{coll}");
            }

            return(new Assertable <T>(sample[0], a.UseAssertionException));
        }
コード例 #7
0
 public static Assertable <T> Single <T>(this Assertable <T[]> a)
 {
     return(SingleImpl(a, a.Item));
 }