-
Notifications
You must be signed in to change notification settings - Fork 84
Supporting complex types in examples, and expanded expression parser to ... #122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cb9db3d
Supporting complex types in examples, and expanded expression parser …
e09a23b
Trying to fix failing approval test on build server
eb2f42f
Tollerate null references in Argument Expression
cf0991a
Switched to normal assertion
1b3b135
Fixing review feedback
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| using ApprovalTests; | ||
| using System.Runtime.CompilerServices; | ||
| using ApprovalTests; | ||
| using NUnit.Framework; | ||
| using Shouldly; | ||
| using TestStack.BDDfy.Reporters; | ||
|
|
@@ -31,6 +32,22 @@ public void FluentCanBeUsedWithExamples() | |
| Approvals.Verify(textReporter.ToString()); | ||
| } | ||
|
|
||
| [Test] | ||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| public void ExampleTypeMismatch() | ||
| { | ||
| var ex = Should.Throw<UnassignableExampleException>( | ||
| () => this.Given(() => WrongType.ShouldBe(1), "Given i use an example") | ||
| .WithExamples(new ExampleTable("Wrong type") | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hard to diagnose before. Was an invalid cast exception |
||
| { | ||
| new object(), | ||
| new object[] { null } | ||
| }) | ||
| .BDDfy()); | ||
|
|
||
| ex.Message.ShouldBe("System.Object cannot be assigned to Int32 (Column: 'Wrong type', Row: 1)"); | ||
| } | ||
|
|
||
| private void AndIUseA(string multiWordHeading) | ||
| { | ||
| multiWordHeading.ShouldBeOneOf("", "val2"); | ||
|
|
@@ -44,7 +61,7 @@ private void GivenADifferentMethodWith(string prop2) | |
|
|
||
| private void GivenADifferentMethodWithRandomArg(int foo) | ||
| { | ||
|
|
||
| } | ||
|
|
||
| private void ThenAllIsGood() | ||
|
|
@@ -63,6 +80,7 @@ private void GivenMethodTaking__ExampleInt__(int exampleInt) | |
| exampleInt.ShouldBeInRange(1, 2); | ||
| } | ||
|
|
||
| public int WrongType { get; set; } | ||
| public int Prop1 { get; set; } | ||
| private string _prop2 = null; | ||
| private string multiWordHeading = null; | ||
|
|
||
18 changes: 18 additions & 0 deletions
18
TestStack.BDDfy.Tests/Scanner/StepScanners/Examples/ExampleValueTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| using System; | ||
| using NUnit.Framework; | ||
| using Shouldly; | ||
|
|
||
| namespace TestStack.BDDfy.Tests.Scanner.StepScanners.Examples | ||
| { | ||
| public class ExampleValueTests | ||
| { | ||
| [Test] | ||
| public void CanFormatAsStringTests() | ||
| { | ||
| new ExampleValue("Header", null, () => 0).GetValueAsString().ShouldBe("'null'"); | ||
| new ExampleValue("Header", 1, () => 0).GetValueAsString().ShouldBe("1"); | ||
| new ExampleValue("Header", new Object(), () => 0).GetValueAsString().ShouldBe("System.Object"); | ||
| new ExampleValue("Header", new[] {1, 2}, () => 0).GetValueAsString().ShouldBe("1, 2"); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
TestStack.BDDfy/Scanners/StepScanners/ArgumentCleaningExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| using System; | ||
| using System.Linq; | ||
|
|
||
| namespace TestStack.BDDfy | ||
| { | ||
| internal static class ArgumentCleaningExtensions | ||
| { | ||
| internal static object[] FlattenArrays(this object[] inputs) | ||
| { | ||
| return inputs.Select(FlattenArray).ToArray(); | ||
| } | ||
|
|
||
| public static object FlattenArray(this object input) | ||
| { | ||
| var inputArray = input as Array; | ||
| if (inputArray != null) | ||
| { | ||
| var temp = (from object arrElement in inputArray select GetSafeString(arrElement)).ToArray(); | ||
| return string.Join(", ", temp); | ||
| } | ||
|
|
||
| if (input == null) return "'null'"; | ||
|
|
||
| return input; | ||
| } | ||
|
|
||
| static string GetSafeString(object input) | ||
| { | ||
| if (input == null) | ||
| return "'null'"; | ||
|
|
||
| return input.ToString(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
TestStack.BDDfy/Scanners/StepScanners/Examples/UnassignableExampleException.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| using System; | ||
| using System.Runtime.Serialization; | ||
|
|
||
| namespace TestStack.BDDfy | ||
| { | ||
| [Serializable] | ||
| public class UnassignableExampleException : Exception | ||
| { | ||
| public UnassignableExampleException(string message, Exception inner, ExampleValue exampleValue) : base(message, inner) | ||
| { | ||
| ExampleValue = exampleValue; | ||
| } | ||
|
|
||
| protected UnassignableExampleException( | ||
| SerializationInfo info, | ||
| StreamingContext context) : base(info, context) | ||
| { | ||
| } | ||
|
|
||
| public ExampleValue ExampleValue { get; private set; } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -152,7 +152,6 @@ private static IEnumerable<object> ExtractConvertibleTypeArrayConstants(NewArray | |
|
|
||
| private static IEnumerable<object> ExtractArguments<T>(ConstantExpression constantExpression, T value) | ||
| { | ||
|
|
||
| var expression = constantExpression.Value as Expression; | ||
| if (expression != null) | ||
| { | ||
|
|
@@ -220,7 +219,26 @@ private static IEnumerable<object> ExtractConstant(MemberExpression memberExpres | |
|
|
||
| private static IEnumerable<object> ExtractPropertyValue<T>(MemberExpression expression, PropertyInfo member, T value) | ||
| { | ||
| return new[] { member.GetValue(value, null) }; | ||
| var memberExpression = expression.Expression as MemberExpression; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good addition. |
||
| if (memberExpression != null) | ||
| { | ||
| var extractArguments = ExtractArguments(memberExpression, value); | ||
| try | ||
| { | ||
| return new[] | ||
| { | ||
| member.GetValue(extractArguments.Single(), null) | ||
| }; | ||
| } | ||
| catch (TargetException) | ||
| { | ||
| return new object[] {null}; | ||
| } | ||
| } | ||
| return new[] | ||
| { | ||
| member.GetValue(value, null) | ||
| }; | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This blew up before. Much nicer now :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice. Man I had to read the code again to understand how these tests worked!! Thanks for adding the tests.