Пример #1
0
        private bool InternalTryMatch(Object actual, IMatcher childMatcher, ISelfDescribing selfDescribing)
        {
            //so we can print child diagnostics after
            var childDiag = new MatchDiagnostics();
            var matched   = childMatcher.Matches(actual, childDiag);

            Text(matched ? "Match":"Mismatch!");

            var desc = Description.With();

            if (selfDescribing != null)
            {
                desc.Value(selfDescribing);
            }
            if (!matched)
            {
                Expect.PrintExpectButGot(desc, actual, childMatcher);
                desc.Value(childDiag);
            }
            else
            {
                desc.Value(childMatcher);
            }
            Child(desc);
            return(matched);
        }
Пример #2
0
        /// <summary>
        /// Performs a type check to ensure the object is of the expected type.
        /// </summary>
        /// <param name="actual"></param>
        /// <param name="diagnostics"></param>
        /// <returns></returns>
        public bool Matches(object actual, IMatchDiagnostics diagnostics)
        {
            if (actual == null)
            {
                switch (m_isNullable)
                {
                case Nulls.Allowed:
                    return(Matches((T)actual, diagnostics));

                case Nulls.NotAllowed:
                    diagnostics.MisMatched("Expected non null");
                    return(false);

                case Nulls.NonNullableType:
                    diagnostics.MisMatched("Wrong type, expected type {0} which is non nullable, but got null instead", typeof(T).FullName);
                    return(false);
                }
            }
            if (!isValidType(actual))
            {
                diagnostics.MisMatched(Description.With()
                                       .Text("Wrong type, expected {0} but got {1}", typeof(T).FullName, actual.GetType().FullName)
                                       .Value("actual", actual.GetType().FullName)
                                       );
                return(false);
            }

            T val;

            try {
                val = wrap(actual);
            } catch (InvalidCastException e) {
                throw new InvalidCastException(String.Format("Could not cast {0} to {1}", actual.GetType().FullName, typeof(T).FullName), e);
            }
            return(Matches(val, diagnostics));
        }
Пример #3
0
 public IMatchDiagnostics MisMatched(string text, params object[] args)
 {
     MisMatched(Description.With().Text(text, args));
     return(this);
 }
Пример #4
0
 public bool TryMatch <T>(T actual, String actualName, IMatcher <T> childMatcher)
 {
     return(InternalTryMatch(actual, childMatcher, Description.With().Value("named", actualName)));
 }
Пример #5
0
 public bool TryMatch <T>(T actual, int index, IMatcher <T> childMatcher)
 {
     return(InternalTryMatch(actual, childMatcher, Description.With().Value("at position", index)));
 }