Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions ArchUnitNET/Domain/Architecture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using ArchUnitNET.Fluent;

namespace ArchUnitNET.Domain
{
Expand Down Expand Up @@ -36,6 +35,8 @@ public Architecture(IEnumerable<Assembly> allAssemblies, IEnumerable<Namespace>
public IEnumerable<Class> Classes => Types.OfType<Class>();
public IEnumerable<Interface> Interfaces => Types.OfType<Interface>();
public IEnumerable<Attribute> Attributes => Types.OfType<Attribute>();
public IEnumerable<Struct> Structs => Types.OfType<Struct>();
public IEnumerable<Enum> Enums => Types.OfType<Enum>();
public IEnumerable<Class> ReferencedClasses => ReferencedTypes.OfType<Class>();
public IEnumerable<Interface> ReferencedInterfaces => ReferencedTypes.OfType<Interface>();
public IEnumerable<Attribute> ReferencedAttributes => ReferencedTypes.OfType<Attribute>();
Expand All @@ -62,7 +63,7 @@ public override bool Equals(object obj)
return true;
}

return obj.GetType() == GetType() && Equals((Architecture) obj);
return obj.GetType() == GetType() && Equals((Architecture)obj);
}

private bool Equals(Architecture other)
Expand Down
4 changes: 2 additions & 2 deletions ArchUnitNET/Domain/Attribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ namespace ArchUnitNET.Domain
public class Attribute : Class
{
public Attribute(IType type, bool? isAbstract, bool? isSealed) : base(type, isAbstract,
isSealed, false, false)
isSealed)
{
}

public Attribute(Class cls) : base(cls.Type, cls.IsAbstract, cls.IsSealed, cls.IsValueType, cls.IsEnum)
public Attribute(Class cls) : base(cls.Type, cls.IsAbstract, cls.IsSealed)
{
}
}
Expand Down
25 changes: 2 additions & 23 deletions ArchUnitNET/Domain/Class.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@ namespace ArchUnitNET.Domain
{
public class Class : IType
{
public Class(IType type, bool? isAbstract = null, bool? isSealed = null, bool? isValueType = null,
bool? isEnum = null)
public Class(IType type, bool? isAbstract = null, bool? isSealed = null)
{
Type = type;
IsAbstract = isAbstract;
IsSealed = isSealed;
IsValueType = isValueType;
IsEnum = isEnum;
}

public IType Type { get; }
Expand All @@ -42,21 +39,6 @@ public Class(IType type, bool? isAbstract = null, bool? isSealed = null, bool? i
public IEnumerable<MethodMember> Constructors => Type.GetConstructors();
public bool? IsAbstract { get; }
public bool? IsSealed { get; }
public bool? IsValueType { get; }
public bool? IsEnum { get; }

public bool? IsStruct
{
get
{
if (IsValueType.HasValue && IsEnum.HasValue)
{
return IsValueType.Value && !IsEnum.Value;
}

return null;
}
}

public IEnumerable<Class> InheritedClasses => BaseClass == null
? Enumerable.Empty<Class>()
Expand Down Expand Up @@ -114,8 +96,7 @@ public override string ToString()
private bool Equals(Class other)
{
return Equals(Type, other.Type) && Equals(IsAbstract, other.IsAbstract) &&
Equals(IsSealed, other.IsSealed) && Equals(IsValueType, other.IsValueType) &&
Equals(IsEnum, other.IsEnum);
Equals(IsSealed, other.IsSealed);
}

public override bool Equals(object obj)
Expand All @@ -140,8 +121,6 @@ public override int GetHashCode()
var hashCode = Type != null ? Type.GetHashCode() : 0;
hashCode = (hashCode * 397) ^ IsAbstract.GetHashCode();
hashCode = (hashCode * 397) ^ IsSealed.GetHashCode();
hashCode = (hashCode * 397) ^ IsValueType.GetHashCode();
hashCode = (hashCode * 397) ^ IsEnum.GetHashCode();
return hashCode;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ namespace ArchUnitNET.Domain.Dependencies
{
public class InheritsBaseClassDependency : TypeInstanceDependency
{
// ReSharper disable SuggestBaseTypeForParameter
public InheritsBaseClassDependency(Class origin, ITypeInstance<Class> targetInstance)
public InheritsBaseClassDependency(IType origin, ITypeInstance<IType> targetInstance)
: base(origin, targetInstance)
{
}
Expand Down
105 changes: 105 additions & 0 deletions ArchUnitNET/Domain/Enum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright 2019 Florian Gather <florian.gather@tngtech.com>
// Copyright 2019 Paula Ruiz <paularuiz22@gmail.com>
// Copyright 2019 Fritz Brandhuber <fritz.brandhuber@tngtech.com>
//
// SPDX-License-Identifier: Apache-2.0

using System.Collections.Generic;
using System.Linq;
using ArchUnitNET.Domain.Dependencies;
using ArchUnitNET.Domain.Extensions;
using JetBrains.Annotations;

namespace ArchUnitNET.Domain
{
public class Enum : IType
{
public Enum(IType type)
{
Type = type;
}

public IType Type { get; }
public string Name => Type.Name;
public string FullName => Type.FullName;

[CanBeNull]
public Class BaseClass =>
(Class)Dependencies.OfType<InheritsBaseClassDependency>().FirstOrDefault()?.Target;

public IEnumerable<Class> InheritedClasses => BaseClass == null
? Enumerable.Empty<Class>()
: BaseClass.InheritedClasses.Concat(new[] { BaseClass });

public Visibility Visibility => Type.Visibility;
public bool IsNested => Type.IsNested;
public bool IsGeneric => Type.IsGeneric;
public bool IsGenericParameter => Type.IsGenericParameter;
public bool IsStub => Type.IsStub;
public bool IsCompilerGenerated => Type.IsCompilerGenerated;

public Namespace Namespace => Type.Namespace;
public Assembly Assembly => Type.Assembly;

public IEnumerable<Attribute> Attributes => AttributeInstances.Select(instance => instance.Type);
public List<AttributeInstance> AttributeInstances => Type.AttributeInstances;

public List<ITypeDependency> Dependencies => Type.Dependencies;
public List<ITypeDependency> BackwardsDependencies => Type.BackwardsDependencies;
public IEnumerable<IType> ImplementedInterfaces => Type.ImplementedInterfaces;

public MemberList Members => Type.Members;
public List<GenericParameter> GenericParameters => Type.GenericParameters;

public bool ImplementsInterface(Interface intf)
{
return Type.ImplementsInterface(intf);
}

public bool ImplementsInterface(string pattern, bool useRegularExpressions = false)
{
return Type.ImplementsInterface(pattern, useRegularExpressions);
}

public bool IsAssignableTo(IType assignableToType)
{
return this.GetAssignableTypes().Contains(assignableToType);
}

public bool IsAssignableTo(string pattern, bool useRegularExpressions = false)
{
return pattern != null && this.GetAssignableTypes()
.Any(type => type.FullNameMatches(pattern, useRegularExpressions));
}

public override string ToString()
{
return FullName;
}

private bool Equals(Enum other)
{
return Equals(Type, other.Type);
}

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
{
return false;
}

if (ReferenceEquals(this, obj))
{
return true;
}

return obj.GetType() == GetType() && Equals((Enum)obj);
}

public override int GetHashCode()
{
return Type != null ? Type.GetHashCode() : 0;
}
}
}
4 changes: 4 additions & 0 deletions ArchUnitNET/Domain/Extensions/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public static IEnumerable<IType> GetAssignableTypes(this IType type)
return intf.ImplementedInterfaces.Concat(new[] {intf});
case Class cls:
return cls.InheritedClasses.Concat(new[] {cls}).Concat(cls.ImplementedInterfaces);
case Struct str:
return str.InheritedClasses.Concat(new IType[] {str}).Concat(str.ImplementedInterfaces);
case Enum en:
return en.InheritedClasses.Concat(new IType[] {en}).Concat(en.ImplementedInterfaces);
default:
return Enumerable.Empty<IType>();
}
Expand Down
105 changes: 105 additions & 0 deletions ArchUnitNET/Domain/Struct.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright 2019 Florian Gather <florian.gather@tngtech.com>
// Copyright 2019 Paula Ruiz <paularuiz22@gmail.com>
// Copyright 2019 Fritz Brandhuber <fritz.brandhuber@tngtech.com>
//
// SPDX-License-Identifier: Apache-2.0

using System.Collections.Generic;
using System.Linq;
using ArchUnitNET.Domain.Dependencies;
using ArchUnitNET.Domain.Extensions;
using JetBrains.Annotations;

namespace ArchUnitNET.Domain
{
public class Struct : IType
{
public Struct(IType type)
{
Type = type;
}

public IType Type { get; }
public string Name => Type.Name;
public string FullName => Type.FullName;

[CanBeNull]
public Class BaseClass =>
(Class) Dependencies.OfType<InheritsBaseClassDependency>().FirstOrDefault()?.Target;

public IEnumerable<Class> InheritedClasses => BaseClass == null
? Enumerable.Empty<Class>()
: BaseClass.InheritedClasses.Concat(new[] {BaseClass});

public Visibility Visibility => Type.Visibility;
public bool IsNested => Type.IsNested;
public bool IsGeneric => Type.IsGeneric;
public bool IsGenericParameter => Type.IsGenericParameter;
public bool IsStub => Type.IsStub;
public bool IsCompilerGenerated => Type.IsCompilerGenerated;

public Namespace Namespace => Type.Namespace;
public Assembly Assembly => Type.Assembly;

public IEnumerable<Attribute> Attributes => AttributeInstances.Select(instance => instance.Type);
public List<AttributeInstance> AttributeInstances => Type.AttributeInstances;

public List<ITypeDependency> Dependencies => Type.Dependencies;
public List<ITypeDependency> BackwardsDependencies => Type.BackwardsDependencies;
public IEnumerable<IType> ImplementedInterfaces => Type.ImplementedInterfaces;

public MemberList Members => Type.Members;
public List<GenericParameter> GenericParameters => Type.GenericParameters;

public bool ImplementsInterface(Interface intf)
{
return Type.ImplementsInterface(intf);
}

public bool ImplementsInterface(string pattern, bool useRegularExpressions = false)
{
return Type.ImplementsInterface(pattern, useRegularExpressions);
}

public bool IsAssignableTo(IType assignableToType)
{
return this.GetAssignableTypes().Contains(assignableToType);
}

public bool IsAssignableTo(string pattern, bool useRegularExpressions = false)
{
return pattern != null && this.GetAssignableTypes()
.Any(type => type.FullNameMatches(pattern, useRegularExpressions));
}

public override string ToString()
{
return FullName;
}

private bool Equals(Struct other)
{
return Equals(Type, other.Type);
}

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
{
return false;
}

if (ReferenceEquals(this, obj))
{
return true;
}

return obj.GetType() == GetType() && Equals((Struct) obj);
}

public override int GetHashCode()
{
return Type != null ? Type.GetHashCode() : 0;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,6 @@ public static ICondition<Class> BeSealed()
"is not sealed");
}

public static ICondition<Class> BeValueTypes()
{
return new SimpleCondition<Class>(cls => !cls.IsValueType.HasValue || cls.IsValueType.Value,
"be value types", "is no value type");
}

public static ICondition<Class> BeEnums()
{
return new SimpleCondition<Class>(cls => !cls.IsEnum.HasValue || cls.IsEnum.Value, "be enums",
"is no enum");
}

public static ICondition<Class> BeStructs()
{
return new SimpleCondition<Class>(cls => !cls.IsStruct.HasValue || cls.IsStruct.Value, "be structs",
"is no struct");
}


//Negations

Expand All @@ -56,23 +38,5 @@ public static ICondition<Class> NotBeSealed()
return new SimpleCondition<Class>(cls => !cls.IsSealed.HasValue || !cls.IsSealed.Value, "not be sealed",
"is sealed");
}

public static ICondition<Class> NotBeValueTypes()
{
return new SimpleCondition<Class>(cls => !cls.IsValueType.HasValue || !cls.IsValueType.Value,
"not be value types", "is a value type");
}

public static ICondition<Class> NotBeEnums()
{
return new SimpleCondition<Class>(cls => !cls.IsEnum.HasValue || !cls.IsEnum.Value, "not be enums",
"is an enum");
}

public static ICondition<Class> NotBeStructs()
{
return new SimpleCondition<Class>(cls => !cls.IsStruct.HasValue || !cls.IsStruct.Value, "not be structs",
"is a struct");
}
}
}
Loading