The CompositePatternAttribute class is a base class that can be used to define "composite" patterns.

Namespace:  Gallio.Framework.Pattern
Assembly:  Gallio (in Gallio.dll) Version: 3.3.0.0 (3.3.610.0)

Syntax

C#
public abstract class CompositePatternAttribute : PatternAttribute
Visual Basic (Declaration)
Public MustInherit Class CompositePatternAttribute _
	Inherits PatternAttribute

Remarks

A composite pattern is a single pattern that is composed of (and equivelent to) a defined set of "component" patterns.

Composite patterns allow arbitrary sets of individual IPatterns to be grouped together and encapsulated so that the entire group can be treated as a single pattern.

Grouping patterns that are commonly used together into a composite pattern offers multiple benefits vs. using the component patterns separately. Benefits of composite patterns include:

  • Composite patterns are easier to apply than the equivelent set of component patterns.
  • Composite patterns ensure that the entire set of component patterns is applied consistently.
  • Composite patterns can reduce code duplication (e.g. for declaratively applied PatternAttributes).
  • Composite patterns encapsulate and centralize the definition of the pattern group. (This makes it easier and safer to maintain test code, such as when adding, removing, or modifying the component patterns of the composite pattern.)
  • Composite patterns allow meaningful names to be assigned to pattern groups. (This often improves test code readability, understandability, and maintainability.)

Examples

This C# example shows a derived class that encapsulates 3 separate PatternAttributes as a named composite attribute.

CopyC#
/// <summary>
/// This attribute composites 3 separate <see cref="MetadataPatternAttribute"/>s into a single attribute.
/// </summary>
public class SampleCompositePatternAttribute: CompositePatternAttribute
{
    private readonly PatternAttribute[] _componentPatterns = new[]
    {
        new CategoryAttribute("Samples"),
        new AuthorAttribute("John"),
        new ImportanceAttribute(Importance.Critical)
    };

    protected override IEnumerable<IPattern> GetPatterns()
    {
        return _componentPatterns;
    }
}

This C# example shows 2 derived classes that each encapsulates multiple separate MbUnit.Framework.CatchExceptionAttributes as a named composite attribute.

CopyC#
/// <summary>
/// This attribute composites 4 separate <see cref="MbUnit.Framework.CatchExceptionAttributes"/>s into a single attribute.
/// </summary>
public class CompositeCatchArgumentExceptionsAttribute : CompositePatternAttribute
{
    //NOTE: The Order properties of the attributes are intentionally different than the array sequence.
    private readonly PatternAttribute[] _componentPatterns = new PatternAttribute[]
    {
        new CatchExceptionAttribute(typeof(ArgumentNullException)) { StandardOutcome = "TestOutcome.Skipped", Order = 1},
        new CatchExceptionAttribute(typeof(ArgumentOutOfRangeException)) { StandardOutcome = "TestOutcome.Passed", Order = 4},
        new CatchExceptionAttribute(typeof(ArgumentOutOfRangeException)) { StandardOutcome = "TestOutcome.Ignored", ExceptionMessage = "expectedmessage", Order = 2},
        new CatchExceptionAttribute(typeof(ArgumentException)) { StandardOutcome = "TestOutcome.Inconclusive", Order = 3}
    };

    protected override IEnumerable<IPattern> GetPatterns()
    {
        return _componentPatterns;
    }
}

/// <summary>
/// This attribute composites 2 separate <see cref="MbUnit.Framework.CatchExceptionAttributes"/>s into a single attribute.
/// </summary>
public class CompositeCatchMiscExceptionsAttribute : CompositePatternAttribute
{
    private readonly PatternAttribute[] _componentPatterns = new PatternAttribute[]
    {
        new CatchExceptionAttribute(typeof(InvalidCastException)) { StandardOutcome = "TestOutcome.Ignored", Order = 1},
        new CatchExceptionAttribute(typeof(Exception)) { StandardOutcome = "TestOutcome.Pending", ExceptionMessage = "expectedmessage", Order = 2}
    };

    protected override IEnumerable<IPattern> GetPatterns()
    {
        return _componentPatterns;
    }
}

/// <summary>
/// This test illustrates a potential usage scenario for the 2 composite attributes above.
/// </summary>
[Test]
[CompositeCatchArgumentExceptions]
[CompositeCatchMiscExceptions]
public void ExpectInconclusiveFromFirstOfMultipleCompositeCatchAttributesWhenThrowArgEx()
{
    throw new ArgumentException();
}

Inheritance Hierarchy

System..::.Object
  System..::.Attribute
    Gallio.Framework.Pattern..::.PatternAttribute
      Gallio.Framework.Pattern..::.CompositePatternAttribute

See Also