Skip to content

Commit

Permalink
#112 Match verification method fails for regex with curly brackets; A…
Browse files Browse the repository at this point in the history
…dd Should_Match test
  • Loading branch information
YevgeniyShunevych committed Nov 16, 2017
1 parent 10892da commit 1d30ea8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/Atata.Tests/Components/ContentPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public class ContentPage : Page<_>
[FindById]
public Number<_> NumberNull { get; private set; }

[Term("Number")]
public Text<_> NumberAsText { get; private set; }

[Format("count: {0}")]
public Number<_> NumberWithFormat { get; private set; }

Expand Down
15 changes: 15 additions & 0 deletions src/Atata.Tests/ShouldTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,5 +154,20 @@ public void Should_Equal_Delayed_WithParentReset()
WaitAndUpdateValue.Click().
ValueContainer.ValueBlock.Should.Equal("New value");
}

[Test]
public void Should_Match()
{
var should = Go.To<ContentPage>().
NumberAsText.Should.AtOnce;

should.Match(@"^\d{3}.\d{2}$");

Assert.Throws<AssertionException>(() =>
should.Not.Match(@"^\d{3}.\d{2}$"));

Assert.Throws<AssertionException>(() =>
should.Match(@"^\d{4}.\d{2}$"));
}
}
}
29 changes: 21 additions & 8 deletions src/Atata/Verification/IDataVerificationProviderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,22 @@ private static string BuildVerificationConstraintMessage<TData, TOwner>(IDataVer
{
if (!string.IsNullOrWhiteSpace(message))
{
string[] convertedArgs = args?.
Select(x => "\"{0}\"".FormatWith(should.DataProvider.ConvertValueToString(x) ?? NullString)).
ToArray();
string formattedMessage;

return $"{should.GetShouldText()} {message.FormatWith(convertedArgs)}";
if (args != null && args.Any())
{
string[] convertedArgs = args.
Select(x => $"\"{should.DataProvider.ConvertValueToString(x) ?? NullString}\"").
ToArray();

formattedMessage = message.FormatWith(convertedArgs);
}
else
{
formattedMessage = message;
}

return $"{should.GetShouldText()} {formattedMessage}";
}
else
{
Expand All @@ -89,9 +100,11 @@ private static string BuildVerificationConstraintMessage<TData, TOwner>(IDataVer
private static Exception CreateAssertionException<TData, TOwner>(IDataVerificationProvider<TData, TOwner> should, TData actual, string message, params TData[] args)
where TOwner : PageObject<TOwner>
{
return should.CreateAssertionException(
message.FormatWith(args?.Select(x => ObjectToString(x)).ToArray()),
ObjectToString(actual));
string formattedMessage = args != null && args.Any()
? message.FormatWith(args.Select(x => ObjectToString(x)).ToArray())
: message;

return should.CreateAssertionException(formattedMessage, ObjectToString(actual));
}

internal static Exception CreateAssertionException<TData, TOwner>(this IDataVerificationProvider<TData, TOwner> should, string expected, string actual)
Expand Down Expand Up @@ -248,7 +261,7 @@ public static TOwner Match<TOwner>(this IDataVerificationProvider<string, TOwner
{
pattern.CheckNotNull(nameof(pattern));

return should.Satisfy(actual => actual != null && Regex.IsMatch(actual, pattern), "match pattern {0}".FormatWith(ObjectToString(pattern)));
return should.Satisfy(actual => actual != null && Regex.IsMatch(actual, pattern), $"match pattern \"{pattern}\"");
}

public static TOwner BeGreater<TData, TOwner>(this IDataVerificationProvider<TData, TOwner> should, TData expected)
Expand Down

0 comments on commit 1d30ea8

Please sign in to comment.