Skip to content

Commit

Permalink
✨ add .With.Required dangler
Browse files Browse the repository at this point in the history
  • Loading branch information
fluffynuts committed Aug 3, 2021
1 parent 1509366 commit d53b7e8
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 27 deletions.
49 changes: 47 additions & 2 deletions src/NExpect.Tests/DanglingPrepositions/With.cs
Expand Up @@ -26,7 +26,7 @@ public void ShouldBeAbleToDangleWith()
}, Throws.Exception.InstanceOf<UnmetExpectationException>()
.With.Message.Contains("to have name 'Spot'")
);

Assert.That(() =>
{
Expect(dog)
Expand Down Expand Up @@ -75,10 +75,55 @@ public void ShouldAllowContinuationsFromAssignableType()
}, Throws.Nothing);
// Assert
}

[TestCase(".With.Required")]
public void ShouldBeAbleToDangle_(string moo)
{
// Arrange
var passingAnimal = new Dog()
{
Name = "Rex"
};
var failingAnimal = new Dog()
{
Name = "Spot"
};
// Act
Assert.That(() =>
{
Expect(passingAnimal)
.To.Be.An.Instance.Of<Dog>()
.With.Required.Name("Rex");
}, Throws.Nothing);

Assert.That(() =>
{
Expect(failingAnimal)
.To.Be.An.Instance.Of<Dog>()
.With.Required.Name("Rex");
}, Throws.Exception.InstanceOf<UnmetExpectationException>());
// Assert
}
}

public static class WithExtensions
{
public static IMore<Dog> Name(
this IRequired<Dog> required,
string requiredName
)
{
return required.AddMatcher(actual =>
{
var name = actual.Name; // ?.GetOrDefault(nameof(Animal.Name), null as string);
var passed = name == requiredName;
return new MatcherResult(
passed,
() => $"Expected {actual} to have name '{requiredName}'"
);
});
}

public static IMore<Dog> Name(
this IWith<Dog> with,
string expected)
Expand All @@ -95,7 +140,7 @@ public static class WithExtensions
}
}

public abstract class Animal: IAnimal
public abstract class Animal : IAnimal
{
public string Name { get; set; }
}
Expand Down
17 changes: 17 additions & 0 deletions src/NExpect/Implementations/Required.cs
@@ -0,0 +1,17 @@
using System;
using NExpect.Implementations.Strings;
using NExpect.Interfaces;

namespace NExpect.Implementations
{
// ReSharper disable once ClassNeverInstantiated.Global
internal class Required<T>
: ExpectationContextWithLazyActual<T>,
IHasActual<T>,
IRequired<T>
{
public Required(Func<T> actualFetcher) : base(actualFetcher)
{
}
}
}
3 changes: 3 additions & 0 deletions src/NExpect/Implementations/With.cs
Expand Up @@ -9,6 +9,9 @@ internal class With<T>
IHasActual<T>,
IWith<T>
{
public IRequired<T> Required
=> ContinuationFactory.Create<T, Required<T>>(ActualFetcher, this);

public With(Func<T> actualFetcher) : base(actualFetcher)
{
}
Expand Down
9 changes: 9 additions & 0 deletions src/NExpect/Interfaces/IRequired.cs
@@ -0,0 +1,9 @@
namespace NExpect.Interfaces
{
/// <summary>
/// Provides the .Required dangling grammar type
/// </summary>
public interface IRequired<T> : ICanAddMatcher<T>
{
}
}
6 changes: 5 additions & 1 deletion src/NExpect/Interfaces/IWith.cs
Expand Up @@ -4,7 +4,11 @@
/// Provides the .With dangling grammar type
/// </summary>
/// <typeparam name="T"></typeparam>
public interface IWith<T>: ICanAddMatcher<T>
public interface IWith<T> : ICanAddMatcher<T>
{
/// <summary>
/// Provides the .Required dangling grammar type
/// </summary>
public IRequired<T> Required { get; }
}
}
48 changes: 24 additions & 24 deletions src/NExpect/NExpect.csproj
Expand Up @@ -36,37 +36,37 @@
<DefineConstants>BUILD_PEANUTBUTTER_INTERNAL</DefineConstants>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All"/>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\PeanutButter\source\Utils\PeanutButter.Utils\ArrayExtensions.cs" Link="Imported\ArrayExtensions.cs"/>
<Compile Include="..\PeanutButter\source\Utils\PeanutButter.Utils\AutoLocker.cs" Link="Imported\AutoLocker.cs"/>
<Compile Include="..\PeanutButter\source\Utils\PeanutButter.Utils\AutoResetter.cs" Link="Imported\AutoResetter.cs"/>
<Compile Include="..\PeanutButter\source\Utils\PeanutButter.Utils\ByteArrayExtensions.cs" Link="Imported\ByteArrayExtensions.cs"/>
<Compile Include="..\PeanutButter\source\Utils\PeanutButter.Utils\DeepEqualityTester.cs" Link="Imported\DeepEqualityTester.cs"/>
<Compile Include="..\PeanutButter\source\Utils\PeanutButter.Utils\EnumerableWrapper.cs" Link="Imported\EnumerableWrapper.cs"/>
<Compile Include="..\PeanutButter\source\Utils\PeanutButter.Utils\ObjectExtensions.cs" Link="Imported\ObjectExtensions.cs"/>
<Compile Include="..\PeanutButter\source\Utils\PeanutButter.Utils\ExtensionsForIEnumerables.cs" Link="Imported\ExtensionsForIEnumerables.cs"/>
<Compile Include="..\PeanutButter\source\Utils\PeanutButter.Utils\MetadataExtensions.cs" Link="Imported\MetadataExtensions.cs"/>
<Compile Include="..\PeanutButter\source\Utils\PeanutButter.Utils\MemberNotFoundException.cs" Link="Imported\MemberNotFoundException.cs"/>
<Compile Include="..\PeanutButter\source\Utils\PeanutButter.Utils\PropertyOrField.cs" Link="Imported\PropertyOrField.cs"/>
<Compile Include="..\PeanutButter\source\Utils\PeanutButter.Utils\PyLike.cs" Link="Imported\PyLike.cs"/>
<Compile Include="..\PeanutButter\source\Utils\PeanutButter.Utils\StringExtensions.cs" Link="Imported\StringExtensions.cs"/>
<Compile Include="..\PeanutButter\source\Utils\PeanutButter.Utils\RandomNumber.cs" Link="Imported\RandomNumber.cs"/>
<Compile Include="..\PeanutButter\source\Utils\PeanutButter.Utils\Stringifier.cs" Link="Imported\Stringifier.cs"/>
<Compile Include="..\PeanutButter\source\Utils\PeanutButter.Utils\TypeExtensions.cs" Link="Imported\TypeExtensions.cs"/>
<Compile Include="..\PeanutButter\source\Utils\PeanutButter.Utils\Types.cs" Link="Imported\Types.cs"/>
<Compile Include="..\PeanutButter\source\Utils\PeanutButter.Utils\CannotZipNullException.cs" Link="Imported\CannotZipNullException.cs"/>
<Compile Include="..\PeanutButter\source\Utils\PeanutButter.Utils\UnevenZipException.cs" Link="Imported\UnevenZipException.cs"/>
<Compile Include="..\PeanutButter\source\Utils\PeanutButter.Utils\ArrayExtensions.cs" Link="Imported\ArrayExtensions.cs" />
<Compile Include="..\PeanutButter\source\Utils\PeanutButter.Utils\AutoLocker.cs" Link="Imported\AutoLocker.cs" />
<Compile Include="..\PeanutButter\source\Utils\PeanutButter.Utils\AutoResetter.cs" Link="Imported\AutoResetter.cs" />
<Compile Include="..\PeanutButter\source\Utils\PeanutButter.Utils\ByteArrayExtensions.cs" Link="Imported\ByteArrayExtensions.cs" />
<Compile Include="..\PeanutButter\source\Utils\PeanutButter.Utils\DeepEqualityTester.cs" Link="Imported\DeepEqualityTester.cs" />
<Compile Include="..\PeanutButter\source\Utils\PeanutButter.Utils\EnumerableWrapper.cs" Link="Imported\EnumerableWrapper.cs" />
<Compile Include="..\PeanutButter\source\Utils\PeanutButter.Utils\ObjectExtensions.cs" Link="Imported\ObjectExtensions.cs" />
<Compile Include="..\PeanutButter\source\Utils\PeanutButter.Utils\ExtensionsForIEnumerables.cs" Link="Imported\ExtensionsForIEnumerables.cs" />
<Compile Include="..\PeanutButter\source\Utils\PeanutButter.Utils\MetadataExtensions.cs" Link="Imported\MetadataExtensions.cs" />
<Compile Include="..\PeanutButter\source\Utils\PeanutButter.Utils\MemberNotFoundException.cs" Link="Imported\MemberNotFoundException.cs" />
<Compile Include="..\PeanutButter\source\Utils\PeanutButter.Utils\PropertyOrField.cs" Link="Imported\PropertyOrField.cs" />
<Compile Include="..\PeanutButter\source\Utils\PeanutButter.Utils\PyLike.cs" Link="Imported\PyLike.cs" />
<Compile Include="..\PeanutButter\source\Utils\PeanutButter.Utils\StringExtensions.cs" Link="Imported\StringExtensions.cs" />
<Compile Include="..\PeanutButter\source\Utils\PeanutButter.Utils\RandomNumber.cs" Link="Imported\RandomNumber.cs" />
<Compile Include="..\PeanutButter\source\Utils\PeanutButter.Utils\Stringifier.cs" Link="Imported\Stringifier.cs" />
<Compile Include="..\PeanutButter\source\Utils\PeanutButter.Utils\TypeExtensions.cs" Link="Imported\TypeExtensions.cs" />
<Compile Include="..\PeanutButter\source\Utils\PeanutButter.Utils\Types.cs" Link="Imported\Types.cs" />
<Compile Include="..\PeanutButter\source\Utils\PeanutButter.Utils\CannotZipNullException.cs" Link="Imported\CannotZipNullException.cs" />
<Compile Include="..\PeanutButter\source\Utils\PeanutButter.Utils\UnevenZipException.cs" Link="Imported\UnevenZipException.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='netstandard1.6'">
<Reference Include="System.Diagnostics.StackTrace"/>
<Reference Include="System.Diagnostics.StackTrace" />
</ItemGroup>
<ItemGroup>
<None Include="icon.png" Pack="true" PackagePath=""/>
<None Include="icon.png" Pack="true" PackagePath="" />
</ItemGroup>
<ItemGroup>
<Folder Include="Impl"/>
<Folder Include="Impl" />
</ItemGroup>
<Import Project="..\MonoForFramework.targets"/>
<Import Project="..\MonoForFramework.targets" />
</Project>

0 comments on commit d53b7e8

Please sign in to comment.