Skip to content

Commit

Permalink
🐛 resolve some issues
Browse files Browse the repository at this point in the history
- chained .To.Contain(...) assertions on strings won't try to maintain assertion order any more
- better help when throwing for incomplete assertions (include hint about exception assertions and pulling properties
  off with `.With.Property(...)`
  • Loading branch information
fluffynuts committed Jan 9, 2024
1 parent bd3f18d commit cc23737
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 6 deletions.
78 changes: 78 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
TODO
---

Some ideas for assertions which I'd like to implement when I get time (also
ideal candidates for a juicy PR!)


Params assertions for arbitrary groups
---

```csharp
Expect(a, b, c)
.To.All.Be.Instances.Of<T>();

Expect(a, b, c)
.To.All.Equal(d);
```

Better intersection equality testing
---
```csharp
// intersection equality usually tests properties
// which can be matched from _both_ sides, but
// that leaves the potential for an error when,
// eg, a property on the tested system is renamed
// as the rename will not follow back up to the anon
// object used for comparison, so I'd like to
// be able to:
// properties on left _must_ exist on right
Expect(left)
.To.Left.Intersection.Equal(right);
// properties on the right _must_ exist on the left:
Expect(left)
.To.Right.Intersection.Equal(right);
```

Easier collection assertions
---
```csharp
// for collections of numbers, DateTime, TimeSpan
Expect(collection)
.To.Contain.All
.Greater.Than(x);
Expect(collection)
.To.Contain.All
.Greater.Than.Or.Equal.To(x);
Expect(collection)
.To.Contain.All
.Less.Than(x);
Expect(collection)
.To.Contain.All
.Less.Than.Or.Equal.To(x);

Expect(someValue)
.Not.To.Be.In(someCollection);
```

Easier file assertions
---
```csharp
Expect(someFilePath)
.To.Be.A.Copy.Of(someOtherFilePath);
Expect(someFolderPath)
.To.Contain.Files();
Expect(someFolderPath)
.To.Contain.Folders();
Expect(someFilePath)
.To.Have.Data(someBytes);
```

Read-only property assertions
---
```csharp
Expect(obj)
.To.Have.Property("SomeProperty")
.Which.Is.ReadOnly();
```
37 changes: 33 additions & 4 deletions src/NExpect.Tests/Issues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,42 @@ public void ComparingTypes_ShouldNotStall()
// Pre-Assert

// Act
Assert.That(() =>
{
Expect(new Object().GetType()).To.Equal(typeof(Object));
}, Throws.Nothing);
Assert.That(
() =>
{
Expect(new Object().GetType()).To.Equal(typeof(Object));
},
Throws.Nothing
);

// Assert
}

[TestFixture]
public class MultipleChainedStringContains
{
[Test]
public void ShouldNotCarryStartMarkerThroughContains()
{
// Arrange
var data =
"SomeEntity does not have a parameterless constructor or is not a class Type. You must override SomeBuilder.CreateInstance for this type to provide an instance to work with";
// Act
Assert.That(
() =>
{
Expect(data)
.To.Contain("SomeBuilder")
.And.To.Contain("SomeEntity")
.And.To.Contain("parameterless constructor")
.And.To.Contain("override SomeBuilder.CreateInstance");
},
Throws.Nothing
);
// Assert
}
}

[TestFixture]
public class Issue30
{
Expand Down Expand Up @@ -97,6 +125,7 @@ public override string ToString()
return $"{nameof(_text)}: {_text}";
}
}

public struct MyStruct2
{
private readonly string _text;
Expand Down
3 changes: 3 additions & 0 deletions src/NExpect/Implementations/IncompleteExpectationException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ both matched by the provided matcher
provided value
- `.Intersection.Equal.To` // asserts 2 items, both intersection
equal to the provided value (only tests matching properties)
- `.With.Property(ex => ...)` after `Expect(func).To.Throw<TException>()`
- this is for switching context to testing the property, _NOT_ an
assertion against the property
- or perhaps some other collection matcher
The following list will tell you the type of item which wasn't
Expand Down
3 changes: 2 additions & 1 deletion src/NExpect/StringMatchers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ public static IStringMore Contain(
Func<string> customMessageGenerator
)
{
continuation.DeleteMetadata(SEARCH_OFFSET);
var result = new StringContainContinuation(continuation);
AddContainsMatcherTo(
continuation,
Expand Down Expand Up @@ -1502,7 +1503,7 @@ private static void AddContainsMatcherTo(
StringContainContinuation next
)
{
Assertions.Forget(next);
Forget(next);
continuation.AddMatcher(
s =>
{
Expand Down
2 changes: 1 addition & 1 deletion src/PeanutButter
Submodule PeanutButter updated 208 files

0 comments on commit cc23737

Please sign in to comment.