Beispiel #1
0
 private void WritePreamble()
 {
     if (IsReadOnly)
     {
         throw SpecFailure.Sealed();
     }
 }
Beispiel #2
0
 public void ValidateActual(object actual)
 {
     if (actual is null && !_allowNull)
     {
         throw SpecFailure.CannotAssertTypeOnNullActual();
     }
 }
Beispiel #3
0
 protected void ThrowIfDisposed()
 {
     if (IsDisposed)
     {
         throw SpecFailure.Disposed(GetType().ToString());
     }
 }
Beispiel #4
0
 private void ThrowIfSealed()
 {
     if (IsReadOnly)
     {
         throw SpecFailure.Sealed();
     }
 }
Beispiel #5
0
            static IEnumerable <TKey> Keys(IEnumerable actual)
            {
                var ng = actual as IDictionary;

                if (ng != null)
                {
                    return(ng.Keys.Cast <TKey>());
                }

                // Look for IDictionary<>, IReadOnlyDictionary<> using reflection
                var interfaces = actual.GetType().GetTypeInfo().GetInterfaces()
                                 .Where(t => t.GetTypeInfo().IsGenericType);

                {
                    var iface = interfaces.FirstOrDefault(IsGenericDictionary);
                    if (iface != null)
                    {
                        return(((IEnumerable)iface.GetTypeInfo().GetProperty("Keys").GetValue(actual)).Cast <TKey>());
                    }
                }

                // Look for IEnumerable<IGrouping<TKey, TValue>> and
                // IEnumerable<KeyValuePair<TKey, TValue>>
                foreach (var iface in interfaces)
                {
                    PropertyInfo kp;
                    if (IsKVP(iface, out kp))
                    {
                        var enumMethod = iface.GetTypeInfo().GetMethod("GetEnumerator");
                        return(LateBoundKVP(actual, enumMethod, kp));
                    }
                }

                throw SpecFailure.CannotTreatAsDictionaryOrGroupings(actual.GetType());
            }
Beispiel #6
0
 protected void ThrowIfReadOnly()
 {
     if (IsReadOnly)
     {
         throw SpecFailure.ReadOnlyCollection();
     }
 }
        private static Exception _TryParse(string text, out TestPlanFilterPattern result)
        {
            if (text == null)
            {
                result = null;
                return(new ArgumentNullException());
            }

            text = text.Trim();
            if (text.Length == 0)
            {
                result = null;
                return(SpecFailure.AllWhitespace(nameof(text)));
            }
            string[] nv = Array.ConvertAll(
                text.Split(new [] { ':' }, 3), t => t.Trim()
                );
            if (nv.Length == 2 && nv[0] == "regex")
            {
                return(_TryParseRegex(nv[1], out result));
            }
            else
            {
                result = TestPlanFilterPattern.Wildcard(text);
                return(null);
            }
        }
Beispiel #8
0
 public override bool Matches(object actual)
 {
     if (actual is ValueType)
     {
         throw SpecFailure.CannotUseNullOnValueType(actual.GetType());
     }
     return(actual == null);
 }
 protected override object CoreRunTest(TestExecutionContext context)
 {
     try {
         return(InvokeMethodHelper(context.TestObject, null));
     }
     catch (TargetParameterCountException) {
         throw SpecFailure.FactMethodParamCount(TypeName + "." + MethodName);
     }
 }
Beispiel #10
0
        public void AddSelfTests()
        {
            SpecLog.ActivatedSelfTestMode();

            if (!TestClass.HasSelfTests)
            {
                throw SpecFailure.NoSelfTestsAvailable();
            }
            AddAssembly(typeof(TestMatcher).GetTypeInfo().Assembly);
        }
Beispiel #11
0
        protected override object CoreRunTest(TestExecutionContext context)
        {
            try {
                var args = TestMethodArguments.Select(a => RebindDelegates(context.TestObject, a)).ToArray();

                return(InvokeMethodHelper(context.TestObject, args));
            }
            catch (TargetParameterCountException) {
                throw SpecFailure.WrongNumberOfTheoryArguments(TypeName, MethodName, _index);
            }
        }
Beispiel #12
0
 private void CompleteResult()
 {
     if (_opts.PassExplicitly)
     {
         _result.SetFailed(SpecFailure.ExplicitPassNotSet());
     }
     else
     {
         _result.SetSuccess();
     }
 }
Beispiel #13
0
 public override bool Matches(IEnumerable actual)
 {
     if (actual == null)
     {
         return(false);
     }
     if (actual is Array)
     {
         return(((Array)actual).Length == Expected);
     }
     if (actual is string)
     {
         return(((string)actual).Length == Expected);
     }
     throw SpecFailure.HaveLengthWorksWith(actual.GetType());
 }
Beispiel #14
0
        static TestUnit CreateCore(MethodInfo method, IEnumerable <Attribute> attrs)
        {
            TestUnit testCase = null;

            foreach (var attr in attrs.OfType <IReflectionTestUnitFactory>())
            {
                if (testCase == null)
                {
                    testCase = attr.CreateTestCase(method);
                }
                else
                {
                    throw SpecFailure.MultipleTestUnitFactories();
                }
            }
            return(testCase);
        }
Beispiel #15
0
        private bool DemandRetargetDelegates()
        {
            switch (RetargetDelegates)
            {
            case RetargetDelegates.Enabled:
                return(true);

            case RetargetDelegates.Disabled:
                return(false);

            case RetargetDelegates.Unspecified:
            default:
                if (Assert.UseStrictMode)
                {
                    throw SpecFailure.PossibleDelegateRetargeting();
                }
                return(true);
            }
        }
Beispiel #16
0
        internal Assembly LoadAssembly(string asmPath)
        {
            string fullPath = Path.GetFullPath(asmPath);

            if (!File.Exists(fullPath))
            {
                throw SpecFailure.FailedToLoadAssemblyPath(asmPath);
            }
            try {
                SpecLog.LoadAssembly(fullPath);

                return(LoadAssemblyFromPath(fullPath));
            } catch (BadImageFormatException) {
                throw SpecFailure.FailedToLoadAssembly(asmPath);
            } catch (FileNotFoundException ex) {
                throw SpecFailure.FailedToLoadAssemblyPath(asmPath + " -> " + ex.FileName);
            } catch (IOException ex) {
                throw SpecFailure.FailedToLoadAssemblyGeneralIO(asmPath, ex.Message);
            }
        }
Beispiel #17
0
        private static Exception _TryParse(string text, out TestId result)
        {
            result = default(TestId);

            if (text == null)
            {
                return(new ArgumentNullException(nameof(text)));
            }
            text = text.Trim();
            if ((text.Length % 2) == 0)
            {
                var bytes = new byte[text.Length / 2];
                int j     = 0;
                for (int i = 0; i < text.Length; i += 2)
                {
                    bytes[j++] = Convert.ToByte(text.Substring(i, 2), 16);
                }
                result = new TestId(bytes);
                return(null);
            }
            return(SpecFailure.NotParsable(nameof(text), typeof(TestId)));
        }
Beispiel #18
0
 public void RunTest(TestExecutionContext context, Action <TestExecutionContext> next)
 {
     throw SpecFailure.CouldNotLoadType(_type, _ex);
 }