Ejemplo n.º 1
0
 public static IMatcher <byte[]> StartsWith(byte[] startsWith)
 {
     return(Matchers.Function((byte[] actual, IMatchDiagnostics diag) =>
     {
         if (actual == null || actual.Length < startsWith.Length)
         {
             return false;
         }
         for (int i = 0; i < startsWith.Length; i++)
         {
             if (actual[i] != startsWith[i])
             {
                 diag.MisMatched("mismatched at byte[{0}], expected {1}, but got [{2}],\nexpect bytes\n{3}\nbut got bytes\n{4}"
                                 , i
                                 , startsWith[i]
                                 , actual[i]
                                 , GetSegmentWithOffsetAndLength(startsWith, i, 30)
                                 , GetSegmentWithOffsetAndLength(actual, i, 30)
                                 );
                 return false;
             }
         }
         return true;
     },
                              "a non null or empty byte array"));
 }
Ejemplo n.º 2
0
 public static IMatcher <double?> EqualTo(double?expect)
 {
     if (expect == null)
     {
         return(Null());
     }
     return(Matchers.Function((double?actual) => actual.IsValidDouble() && expect.Value.SafeCompareTo(actual) == 0, "a double == " + expect.ToPrettyString()));
 }
Ejemplo n.º 3
0
 public static IMatcher <decimal?> EqualTo(decimal?expect)
 {
     if (expect == null)
     {
         return(Null());
     }
     return(Matchers.Function((decimal? actual) => actual == expect, "a decimal == " + expect.ToPrettyString()));
 }
Ejemplo n.º 4
0
 public static IMatcher <Guid?> NotEmpty()
 {
     return(Matchers.Function((Guid? actual) =>
     {
         return actual != null && !Guid.Empty.Equals(actual);
     },
                              "a non empty or null Guid"
                              ));
 }
Ejemplo n.º 5
0
 public static IMatcher <string> TrimmedLength(IMatcher <int?> intMatcher)
 {
     return(Matchers.Function((string actual, IMatchDiagnostics diagnostics) =>
     {
         actual = actual == null?"":actual.Trim();
         return intMatcher.Matches(actual.Length, diagnostics);
     },
                              "string length " + intMatcher));
 }
Ejemplo n.º 6
0
 public static IMatcher <KeyValuePair <K, V> > EqualTo <K, V>(IMatcher <K> keyMatcher, IMatcher <V> valueMatcher)
 {
     return(Matchers.Function((KeyValuePair <K, V> actual, IMatchDiagnostics diag) => {
         return diag.TryMatch(actual.Key, keyMatcher) && diag.TryMatch(actual.Value, valueMatcher);
     },
                              (desc) => {
         desc.Text("KeyValuePair");
         desc.Value("Key", keyMatcher);
         desc.Text("Value", valueMatcher);
     }));
 }
Ejemplo n.º 7
0
        /// <summary>
        /// Add a matcher which will match on the result of the given extractor
        /// </summary>
        /// <typeparam name="TType">the type of the value extracted from the instance matched against (i.e. the proeprty type)</typeparam>
        /// <param name="valueDescription">the label given to the expression in case of a non match (e.g. "Foo.Bar.GetSomeValue()")</param>
        /// <param name="valueExtractor">the function which extracts the given value (e.g. (T instance)=>instance.Foo.Bar.GetSomeValue())</param>
        /// <param name="valueMatcher">the matcher used to validate the extracted value</param>
        /// <returns></returns>
        protected PropertyMatcher <T> WithMatcher <TType>(string valueDescription, Func <T, TType> valueExtractor, IMatcher <TType> valueMatcher)
        {
            var instanceMatcher = Matchers.Function((T instance, IMatchDiagnostics diag) =>
            {
                TType valueFromInstance = valueExtractor.Invoke(instance);
                return(diag.TryMatch(valueFromInstance, valueMatcher));
            },
                                                    "'" + valueDescription + "' is " + valueMatcher.ToString()
                                                    );

            WithMatcher(instanceMatcher);
            return(this);
        }
Ejemplo n.º 8
0
 public static IMatcher <string> WithForwardSlashes(IMatcher <string> matcher)
 {
     return(Matchers.Function((string actual, IMatchDiagnostics diag) =>
     {
         if (actual != null)
         {
             actual = actual.Replace("\\", "/");
         }
         return matcher.Matches(actual, diag);
     },
                              "ignoring slash type, " + matcher
                              ));
 }
Ejemplo n.º 9
0
 public static IMatcher <byte[]> As <T>(Func <byte[], T> transformFunc, IMatcher <T> matcher)
 {
     return(Matchers.Function((byte[] actual, IMatchDiagnostics diag) =>
     {
         if (actual == null)
         {
             return false;
         }
         T converted = transformFunc.Invoke(actual);
         return diag.TryMatch(converted, matcher);
     },
                              "a non null byte array which is converted to Utf8 and matches " + matcher
                              ));
 }
Ejemplo n.º 10
0
 public static IMatcher <T> EqualTo <T>(T expect, Func <String> mismatchMessageFactory)
 {
     return(Matchers.Function((T actual) =>
     {
         if (actual == null && expect == null)
         {
             return true;
         }
         if (expect == null)
         {
             return false;
         }
         return expect.Equals(actual);
     },
                              mismatchMessageFactory));
 }
Ejemplo n.º 11
0
 /// <summary>
 /// Equal by reference
 /// </summary>
 public static IMatcher <T> SameAs <T>(T expect)
 {
     return(Matchers.Function((T actual) =>
     {
         if (actual == null && expect == null)
         {
             return true;
         }
         if (expect == null)
         {
             return false;
         }
         return Object.ReferenceEquals(expect, actual);
     },
                              () => expect.ToPrettyString()));
 }
Ejemplo n.º 12
0
 //TODO:convert to use a generic type converter? Put into CoreMatchers?
 /// <summary>
 /// Attempt to parse the string to an int and apply the given int matcher
 /// </summary>
 public static IMatcher <string> As(IMatcher <int?> intMatcher)
 {
     return(Matchers.Function((string actual, IMatchDiagnostics diagnostics) =>
     {
         int intActual;
         if (int.TryParse(actual, out intActual))
         {
             return intMatcher.Matches(intActual, diagnostics);
         }
         else
         {
             diagnostics.MisMatched("Couldn't parse the string '{0}' as an int", actual);
         }
         return false;
     },
                              "string of int matching " + intMatcher));
 }
Ejemplo n.º 13
0
 public static IMatcher <char?> EqualTo(char?expect)
 {
     return(Matchers.Function((char?actual) =>
     {
         if (expect == null && actual == null)
         {
             return true;
         }
         if (expect == null || actual == null)
         {
             return false;
         }
         return expect.Value.Equals(actual.Value);
     },
                              expect == null?"a null char":"a char == '" + expect + "'"
                              ));
 }
Ejemplo n.º 14
0
 public static IMatcher <string> EqualTo(string expect)
 {
     return(Matchers.Function((string actual) =>
     {
         if (expect == null && actual == null)
         {
             return true;
         }
         if (expect != null)
         {
             return expect.Equals(actual);
         }
         return false;
     },
                              "the string '" + expect + "'"
                              ));
 }
Ejemplo n.º 15
0
 public static IMatcher <Guid?> EqualTo(Guid?expect)
 {
     return(Matchers.Function((Guid? actual) =>
     {
         if (expect == null && actual == null)
         {
             return true;
         }
         if (expect == null || actual == null)
         {
             return false;
         }
         return expect.Value.Equals(actual.Value);
     },
                              expect == null?"a null Guid":"a Guid == '" + expect + "'"
                              ));
 }
Ejemplo n.º 16
0
        public static IMatcher <T> EqualTo <T>(T expect)
        {
            if (expect != null)
            {
                if (expect is String)
                {
                    return((IMatcher <T>)AString.EqualTo(expect as String));
                }
                Type t = typeof(T);
                if (t.IsPrimitive || t.IsValueType)
                {
                    return(Matchers.Function((T actual) => actual.Equals(expect), typeof(T).Name + " == " + expect));
                }
                if (expect is TimeSpan)
                {
                    return((IMatcher <T>)ATimeSpan.EqualTo(expect as TimeSpan?));
                }
                if (expect is Uri)
                {
                    return((IMatcher <T>)AnUri.EqualTo(expect as Uri));
                }
                if (expect is DateTime)
                {
                    return((IMatcher <T>)ADateTime.EqualTo(expect as DateTime?));
                }
                if (expect is DateTimeOffset)
                {
                    return((IMatcher <T>)ADateTimeOffset.EqualTo(expect as DateTimeOffset?));
                }
            }

            return(EqualTo(expect, () =>
            {
                if (expect != null)
                {
                    return "The instance " + expect.GetHashCode() + "@" + typeof(T).FullName + " => " + expect.ToPrettyString();
                }
                else
                {
                    return "Null instance of type " + typeof(T).FullName;
                }
            }));
        }
Ejemplo n.º 17
0
            private IMatcher <DateTimeOffset> GetOrBuild()
            {
                var cached = m_cachedMatcher;

                if (cached == null)
                {
                    cached = Matchers.Function((DateTimeOffset actual, IMatchDiagnostics diagnostics) =>
                    {
                        var expectVal = m_expect.Value;
                        var plus      = m_inclusive ? 1 : 0;
                        var maxMinus  = Math.Truncate((m_maxMinus ?? TimeSpan.FromMilliseconds(0)).TotalMilliseconds + plus);
                        var maxPlus   = Math.Truncate((m_maxPlus ?? TimeSpan.FromMilliseconds(0)).TotalMilliseconds + plus);

                        IMatcher <double?> diffMatcher = ADouble.BetweenIncluding(-maxMinus, maxPlus);
                        double diff = Math.Truncate((actual - expectVal).TotalMilliseconds);
                        return(diffMatcher.Matches(diff, diagnostics));
                    }, "");
                    m_cachedMatcher = cached;
                }
                return(cached);
            }
Ejemplo n.º 18
0
        public static IMatcher <string> CanReadFile()
        {
            return(Matchers.Function((string actualPath, IMatchDiagnostics diag) =>
            {
                if (File.Exists(actualPath))
                {
                    diag.Matched(Description.With().Value("FileExists", true));

                    try
                    {
                        using (var fs = File.OpenRead(actualPath))
                        {
                            if (fs.CanRead)
                            {
                                diag.Matched(Description.With().Value("CanReadFile", true).Value("path", actualPath).Value("exists", true));
                                return true;
                            }
                            else
                            {
                                diag.MisMatched(Description.With().Value("CanReadFile", false).Value("path", actualPath).Value("exists", true));
                                return false;
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        diag.MisMatched(Description.With().Value("CanReadFile", false).Value("path", actualPath).Value("error", e.Message));
                        return false;
                    }
                }
                diag.MisMatched(Description.With().Value("FileExists", false).Value("path", actualPath).Value("CanRead", false));
                return false;
            },
                                     "CanReadFile"
                                     ));
        }
Ejemplo n.º 19
0
 public static IMatcher <string> FileExists()
 {
     return(Matchers.Function((string actualPath, IMatchDiagnostics diag) =>
     {
         if (File.Exists(actualPath))
         {
             diag.Matched(Description.With()
                          .Value("expect", "file to exist")
                          .Value("path", actualPath)
                          .Value("exists", true));
             return true;
         }
         else
         {
             diag.MisMatched(Description.With()
                             .Value("expect", "file to exist")
                             .Value("path", actualPath)
                             .Value("exists", false));
             return false;
         }
     },
                              "FileExists exists"
                              ));
 }
Ejemplo n.º 20
0
 public static IMatcher <decimal?> BetweenIncluding(decimal expectFrom, decimal expectTo)
 {
     return(Matchers.Function((decimal? actual) => actual != null && expectFrom.SafeCompareTo(actual) <= 0 && expectTo.SafeCompareTo(actual) >= 0, "a decimal where " + expectFrom.ToPrettyString() + " <= value <= " + expectTo.ToPrettyString()));
 }
Ejemplo n.º 21
0
 public static IMatcher <decimal?> Not(decimal expect)
 {
     return(Matchers.Function((decimal? actual) => actual != null && !Equals(actual.Value, expect), "a decimal != " + expect.ToPrettyString()));
 }
Ejemplo n.º 22
0
 public static IMatcher <decimal?> LessThanOrEqualTo(decimal expect)
 {
     return(Matchers.Function((decimal? actual) => actual != null && expect.SafeCompareTo(actual) >= 0, "a decimal <= " + expect.ToPrettyString()));
 }
Ejemplo n.º 23
0
 public static IMatcher <decimal?> GreaterThan(decimal expect)
 {
     return(Matchers.Function((decimal? actual) => actual != null && expect.SafeCompareTo(actual) < 0, "a decimal > " + expect.ToPrettyString()));
 }
Ejemplo n.º 24
0
 public static IMatcher <TimeSpan?> BetweenIncluding(TimeSpan expectFrom, TimeSpan expectTo)
 {
     return(Matchers.Function((TimeSpan? actual) => actual.IsValidTimeSpan() && expectFrom.SafeCompareTo(actual) <= 0 && expectTo.SafeCompareTo(actual) >= 0, "a TimeSpan where " + expectFrom.ToPrettyString() + " <= value <= " + expectTo.ToPrettyString()));
 }
Ejemplo n.º 25
0
 public static IMatcher <TimeSpan?> Not(TimeSpan expect)
 {
     return(Matchers.Function((TimeSpan? actual) => actual.IsValidTimeSpan() && actual != expect, "a TimeSpan != " + expect.ToPrettyString()));
 }
Ejemplo n.º 26
0
 public static IMatcher <TimeSpan?> GreaterThan(TimeSpan expect)
 {
     return(Matchers.Function((TimeSpan? actual) => actual.IsValidTimeSpan() && expect.SafeCompareTo(actual) < 0, "a TimeSpan > " + expect.ToPrettyString()));
 }
Ejemplo n.º 27
0
 public static IMatcher <TimeSpan?> NotNull()
 {
     return(Matchers.Function((TimeSpan? actual) => actual != null, "a non null TimeSpan"));
 }
Ejemplo n.º 28
0
 public static IMatcher <decimal?> NotNull()
 {
     return(Matchers.Function((decimal? actual) => actual != null, "a non null decimal"));
 }
Ejemplo n.º 29
0
 public static IMatcher <TimeSpan?> EqualTo(TimeSpan expect)
 {
     return(Matchers.Function((TimeSpan? actual) => actual == expect, "a TimeSpan == " + expect.ToPrettyString()));
 }
Ejemplo n.º 30
0
 public static IMatcher <TimeSpan?> LessThanOrEqualTo(TimeSpan expect)
 {
     return(Matchers.Function((TimeSpan? actual) => actual.IsValidTimeSpan() && expect.SafeCompareTo(actual) >= 0, "a TimeSpan <= " + expect.ToPrettyString()));
 }