public static AndConstraint <AssemblyAssertions> NotHaveHiddenEvents(this AssemblyAssertions assertions) { Assembly assembly = assertions.Subject; var nonPublicEvents = new List <IAmEvent>(); foreach (var type in assembly.GetTypes().Select(SmartType.For)) { nonPublicEvents.AddRange(type.GetAllNonPublicEventsWithoutExplicitlyImplemented()); } nonPublicEvents.Should() .BeEmpty("assembly " + assembly + " should not contain non-public events, but: " + Environment.NewLine + ReflectionElementsList.NonPublicEventsFoundMessage(nonPublicEvents)); return(new AndConstraint <AssemblyAssertions>(assertions)); }
public static AndConstraint <AssemblyAssertions> NotHaveStaticFields(this AssemblyAssertions assertions) { Assembly assembly = assertions.Subject; var staticFields = new List <IAmField>(); foreach (var type in assembly.GetTypes()) { staticFields.AddRange(SmartType.For(type).GetAllStaticFields()); } staticFields.Should() .BeEmpty( "assembly " + assembly + " should not contain static fields, but: " + Environment.NewLine + ReflectionElementsList.Format(staticFields)); return(new AndConstraint <AssemblyAssertions>(assertions)); }
/// <summary>Asserts the <see cref="Assembly"/> to have a specific public key.</summary> public static AndConstraint <AssemblyAssertions> HavePublicKey(this AssemblyAssertions assertions, string publicKey, string because = "", params object[] becauseArgs) { Guard.NotNull(assertions, nameof(assertions)); var bytes = assertions.Subject.GetName().GetPublicKey() ?? Array.Empty <byte>(); var assemblyKey = BitConverter.ToString(bytes).Replace("-", ""); Execute.Assertion .BecauseOf(because, becauseArgs) .ForCondition(assemblyKey == publicKey) .FailWith( $"Expected '{assertions.Subject}' to have public key: {publicKey},{Environment.NewLine}" + $"but got: {assemblyKey} instead."); return(new AndConstraint <AssemblyAssertions>(assertions)); }
/// <summary> /// Asserts that the Assembly contains a type of kind <paramref name="typeKind"/> with the expected <paramref name="accessModifier"/>, <paramref name="name"/> and <paramref name="namespace"/>. /// </summary> /// <param name="assemblyAssertions">The AssemblyAssertions we are extending.</param> /// <param name="accessModifier">The C# access modifier of the class.</param> /// <param name="typeKind">The expected kind of the type.</param> /// <param name="namespace">The namespace of the class.</param> /// <param name="name">The name of the class.</param> /// <param name="because">A formatted phrase as is supported by <see cref="M:System.String.Format(System.String,System.Object[])"/> explaining why the assertion /// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.</param> /// <param name="reasonArgs">Zero or more objects to format using the placeholders in <see cref="!:because"/>.</param> public static AndConstraint <AssemblyAssertions> DefineType(this AssemblyAssertions assemblyAssertions, CSharpAccessModifiers accessModifier, TypeKinds typeKind, string @namespace, string name, string because = "", params object[] reasonArgs) { var isNull = assemblyAssertions.Subject == null; var definesType = assemblyAssertions.Subject != null && assemblyAssertions.Subject.DefinedTypes.Any( t => t.GetCSharpAccessModifier() == accessModifier && t.GetTypeKind() == typeKind && t.Namespace == @namespace && t.Name == name); Execute.Assertion.ForCondition(definesType) .BecauseOf(because, reasonArgs) .FailWith("Expected assembly {0} to define a {1} {2} {3}.{4}.", assemblyAssertions.Subject.FullName, accessModifier, typeKind, @namespace, name); return(new AndConstraint <AssemblyAssertions>(assemblyAssertions)); }
public static AndConstraint <AssemblyAssertions> NotReferenceAssemblyWith(this AssemblyAssertions assertions, Type type) { assertions.Subject.Should().NotReference(type.Assembly); return(new AndConstraint <AssemblyAssertions>(assertions)); }
public static AndConstraint <AssemblyAssertions> HaveOnlyTypesWithSingleConstructor(this AssemblyAssertions assertions) { Assembly assembly = assertions.Subject; var constructorLimitsExceeded = new List <Tuple <Type, int> >(); foreach (var type in assembly.GetTypes()) { var constructorCount = SmartType.For(type).GetAllPublicConstructors().Count(); if (constructorCount > 1) { constructorLimitsExceeded.Add(Tuple.Create(type, constructorCount)); } } constructorLimitsExceeded.Any().Should() .BeFalse("assembly " + assembly + " should not contain types with more than one constructor, but: " + Environment.NewLine + ReflectionElementsList.Format(constructorLimitsExceeded)); return(new AndConstraint <AssemblyAssertions>(assertions)); }