SubSpec / test / SubSpec.Tests / SpecificationPrimitiveFacts.cs

using System;
using Xunit;
using SubSpec;
using System.Threading;

public class SpecificationPrimitiveFacts
{
    [Fact]
    public void CreatePrimitive_WithNullAction_Throws()
    {
        Assert.Throws<ArgumentNullException>( () => new SpecificationPrimitive<Action>( "", null ) );
    }

    [Fact]
    public void CreatePrimitive_WithNullMessage_Throws()
    {
        Assert.Throws<ArgumentNullException>( () => new SpecificationPrimitive<Action>( null, () => { } ) );
    }

    [Fact]
    public void Execute_Action_WithTimeout_FailsCorrectly()
    {
        SpecificationPrimitive<Action> sut = new SpecificationPrimitive<Action>( "", () => Thread.Sleep( 200 ) );

        sut.WithTimeout( 10 );

        Assert.Throws<Xunit.Sdk.TimeoutException>( () => sut.Execute() );
    }

    [Fact]
    public void Execute_Action_Timeout_HappyPath()
    {
        SpecificationPrimitive<Action> sut = new SpecificationPrimitive<Action>( "", () => { } );

        sut.WithTimeout( 10 );

        Assert.DoesNotThrow( () => sut.Execute() );
    }

    [Fact]
    public void Execute_Action_NoTimeout_HappyPath()
    {
        SpecificationPrimitive<Action> sut = new SpecificationPrimitive<Action>( "", () => { } );

        Assert.DoesNotThrow( () => sut.Execute() );
    }

    [Fact]
    public void Execute_ContextDelegate_WithTimeout_FailsCorrectly()
    {
        SpecificationPrimitive<ContextDelegate> sut = new SpecificationPrimitive<ContextDelegate>( "",
            () =>
            {
                Thread.Sleep( 20 );
                return DisposableAction.None;
            } );

        sut.WithTimeout( 10 );

        Assert.Throws<Xunit.Sdk.TimeoutException>( () => sut.Execute() );
    }

    [Fact]
    public void Execute_ContextDelegate_WithTimeout_HappyPath()
    {
        SpecificationPrimitive<ContextDelegate> sut = new SpecificationPrimitive<ContextDelegate>( "", () => DisposableAction.None );

        sut.WithTimeout( 10 );

        var result = default( IDisposable );
        Assert.DoesNotThrow( () => result = sut.Execute() );

        Assert.Equal( DisposableAction.None, result );
    }

    [Fact]
    public void Execute_ContextDelegate_NoTimeout_HappyPath()
    {
        SpecificationPrimitive<ContextDelegate> sut = new SpecificationPrimitive<ContextDelegate>( "", () => DisposableAction.None );

        var result = default( IDisposable );
        Assert.DoesNotThrow( () => result = sut.Execute() );

        Assert.Equal( DisposableAction.None, result );
    }
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.