Skip to content

Commit

Permalink
Merge pull request andrebaltieri#11 from douglasoliveirabh/master
Browse files Browse the repository at this point in the history
Create IsNullOrNullable and IsDigit Implementations
andrebaltieri authored Aug 29, 2018

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents bc7991d + c267f1a commit ef814b5
Showing 12 changed files with 239 additions and 7 deletions.
34 changes: 34 additions & 0 deletions Flunt.Tests/DateTimeValidationContractTests.cs
Original file line number Diff line number Diff line change
@@ -132,5 +132,39 @@ public void IsBetween()

Assert.AreEqual(true, right.Valid);
}


[TestMethod]
[TestCategory("DateTimeValidation")]
public void IsNullOrNullable()
{
var date = new Nullable<DateTime>();

var wrong = new Contract()
.Requires()
.IsNullOrNullable(date, "datetime", "The date is required");

Assert.AreEqual(false, wrong.Valid);
Assert.AreEqual(1, wrong.Notifications.Count);

Nullable<DateTime> dateNull = null;

var wrongNull = new Contract()
.Requires()
.IsNullOrNullable(dateNull, "datetime", "The date is required");

Assert.AreEqual(false, wrongNull.Valid);
Assert.AreEqual(1, wrongNull.Notifications.Count);


var right = new Contract()
.Requires()
.IsNullOrNullable(new DateTime(2017, 10, 30), "datetime", "The date is required");

Assert.AreEqual(true, right.Valid);
}



}
}
36 changes: 34 additions & 2 deletions Flunt.Tests/DecimalValidationContractTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Flunt.Validations;
using System;
using Flunt.Validations;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Flunt.Tests
@@ -115,6 +116,37 @@ public void IsBetweenDecimal()
.IsBetween(value, from, to, "decimal", "The value 1.015 is between 1.01 and 1.02");

Assert.AreEqual(true, right.Valid);
}
}

[TestMethod]
[TestCategory("DecimalValidation")]
public void IsNullOrNullable()
{
var value = new Nullable<decimal>();

var wrong = new Contract()
.Requires()
.IsNullOrNullable(value, "decimal", "The decimal is required");

Assert.AreEqual(false, wrong.Valid);
Assert.AreEqual(1, wrong.Notifications.Count);

Nullable<DateTime> valueNull = null;

var wrongNull = new Contract()
.Requires()
.IsNullOrNullable(valueNull, "decimal", "The decimal is required");

Assert.AreEqual(false, wrongNull.Valid);
Assert.AreEqual(1, wrongNull.Notifications.Count);


var right = new Contract()
.Requires()
.IsNullOrNullable(10m, "decimal", "The decimal is required");

Assert.AreEqual(true, right.Valid);
}

}
}
38 changes: 36 additions & 2 deletions Flunt.Tests/DoubleValidationContractTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Flunt.Validations;
using System;
using Flunt.Validations;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Flunt.Tests
@@ -30,6 +31,39 @@ public void IsBetweenDouble()
.IsBetween(value, from, to, "double", "The value 1250.01 is between 1000.01 and 1299.99");

Assert.AreEqual(true, right.Valid);
}
}


[TestMethod]
[TestCategory("DoubleValidation")]
public void IsNullOrNullable()
{
var value = new Nullable<double>();

var wrong = new Contract()
.Requires()
.IsNullOrNullable(value, "double", "The double is required");

Assert.AreEqual(false, wrong.Valid);
Assert.AreEqual(1, wrong.Notifications.Count);

Nullable<double> valueNull = null;

var wrongNull = new Contract()
.Requires()
.IsNullOrNullable(valueNull, "double", "The double is required");

Assert.AreEqual(false, wrongNull.Valid);
Assert.AreEqual(1, wrongNull.Notifications.Count);


var right = new Contract()
.Requires()
.IsNullOrNullable(1.7E+3, "double", "The decimal is required");

Assert.AreEqual(true, right.Valid);
}


}
}
36 changes: 34 additions & 2 deletions Flunt.Tests/FloatValidationContractTests.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using Flunt.Validations;
using System;
using Flunt.Validations;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Flunt.Tests
{
[TestClass]
public class FloatContractTests
{
{
[TestMethod]
[TestCategory("FloatValidation")]
public void IsBetweenFloat()
@@ -31,5 +32,36 @@ public void IsBetweenFloat()

Assert.AreEqual(true, right.Valid);
}


[TestMethod]
[TestCategory("FloatValidation")]
public void IsNullOrNullable()
{
var value = new Nullable<float>();

var wrong = new Contract()
.Requires()
.IsNullOrNullable(value, "float", "The float is required");

Assert.AreEqual(false, wrong.Valid);
Assert.AreEqual(1, wrong.Notifications.Count);

Nullable<float> valueNull = null;

var wrongNull = new Contract()
.Requires()
.IsNullOrNullable(valueNull, "float", "The float is required");

Assert.AreEqual(false, wrongNull.Valid);
Assert.AreEqual(1, wrongNull.Notifications.Count);


var right = new Contract()
.Requires()
.IsNullOrNullable(3.5F, "float", "The float is required");

Assert.AreEqual(true, right.Valid);
}
}
}
33 changes: 32 additions & 1 deletion Flunt.Tests/IntValidationContractTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Flunt.Validations;
using System;
using Flunt.Validations;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Flunt.Tests
@@ -31,5 +32,35 @@ public void IsBetweenInt()

Assert.AreEqual(true, right.Valid);
}

[TestMethod]
[TestCategory("IntValidation")]
public void IsNullOrNullable()
{
var value = new Nullable<int>();

var wrong = new Contract()
.Requires()
.IsNullOrNullable(value, "int", "The int is required");

Assert.AreEqual(false, wrong.Valid);
Assert.AreEqual(1, wrong.Notifications.Count);

Nullable<int> valueNull = null;

var wrongNull = new Contract()
.Requires()
.IsNullOrNullable(valueNull, "int", "The int is required");

Assert.AreEqual(false, wrongNull.Valid);
Assert.AreEqual(1, wrongNull.Notifications.Count);


var right = new Contract()
.Requires()
.IsNullOrNullable(3.5F, "int", "The int is required");

Assert.AreEqual(true, right.Valid);
}
}
}
20 changes: 20 additions & 0 deletions Flunt.Tests/StringValidationContractTests.cs
Original file line number Diff line number Diff line change
@@ -191,5 +191,25 @@ public void Match()

Assert.AreEqual(true, right.Valid);
}

[TestMethod]
[TestCategory("StringValidation")]
public void IsDigit()
{
var wrong = new Contract()
.Requires()
.IsDigit("asdfa989798", "string", "String must have digits only");

Assert.AreEqual(false, wrong.Valid);
Assert.AreEqual(1, wrong.Notifications.Count);

var right = new Contract()
.Requires()
.IsDigit("1234567890", "string", "String must have digits only");

Assert.AreEqual(true, right.Valid);
}


}
}
8 changes: 8 additions & 0 deletions Flunt/Validations/DateTimeValidationContract.cs
Original file line number Diff line number Diff line change
@@ -44,5 +44,13 @@ public Contract IsBetween(DateTime val, DateTime from, DateTime to, string prope
return this;
}

public Contract IsNullOrNullable(DateTime? val, string property, string message)
{
if (val== null || !val.HasValue)
AddNotification(property, message);

return this;
}

}
}
9 changes: 9 additions & 0 deletions Flunt/Validations/DecimalValidationContract.cs
Original file line number Diff line number Diff line change
@@ -215,5 +215,14 @@ public Contract IsBetween(decimal val, decimal from, decimal to, string property
return this;
}
#endregion

public Contract IsNullOrNullable(decimal? val, string property, string message)
{
if (val== null || !val.HasValue)
AddNotification(property, message);

return this;
}

}
}
8 changes: 8 additions & 0 deletions Flunt/Validations/DoubleValidationContract.cs
Original file line number Diff line number Diff line change
@@ -215,5 +215,13 @@ public Contract IsBetween(double val, double from, double to, string property, s
return this;
}
#endregion

public Contract IsNullOrNullable(double? val, string property, string message)
{
if (val== null || !val.HasValue)
AddNotification(property, message);

return this;
}
}
}
8 changes: 8 additions & 0 deletions Flunt/Validations/FloatValidationContract.cs
Original file line number Diff line number Diff line change
@@ -215,5 +215,13 @@ public Contract IsBetween(float val, float from, float to, string property, stri
return this;
}
#endregion

public Contract IsNullOrNullable(float? val, string property, string message)
{
if (val== null || !val.HasValue)
AddNotification(property, message);

return this;
}
}
}
8 changes: 8 additions & 0 deletions Flunt/Validations/IntValidationContract.cs
Original file line number Diff line number Diff line change
@@ -215,5 +215,13 @@ public Contract IsBetween(int val, int from, int to, string property, string mes
return this;
}
#endregion

public Contract IsNullOrNullable(int? val, string property, string message)
{
if (val== null || !val.HasValue)
AddNotification(property, message);

return this;
}
}
}
8 changes: 8 additions & 0 deletions Flunt/Validations/StringValidationContract.cs
Original file line number Diff line number Diff line change
@@ -106,5 +106,13 @@ public Contract Matchs(string text, string pattern, string property, string mess

return this;
}

public Contract IsDigit(string text, string property, string message)
{
const string pattern = @"^\d+$";
return Matchs(text, pattern, property, message);
}


}
}

0 comments on commit ef814b5

Please sign in to comment.