Skip to content

Commit

Permalink
added tests, added code to allow regex rule to handle null values. ad…
Browse files Browse the repository at this point in the history
…ded logging in case of number parse error.
  • Loading branch information
MikePohatu committed Apr 17, 2017
1 parent 0bc88ab commit 969fe88
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
6 changes: 6 additions & 0 deletions TsGui.Tests/Validation/MatchingRuleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,14 @@ class MatchingRuleTests
[TestCase(null, "*NULL", "Equals", false, ExpectedResult = true)]
[TestCase(null, "", "Equals", false, ExpectedResult = false)]
[TestCase(null, "Test", "StartsWith", false, ExpectedResult = false)]
[TestCase(null, "Test", "Characters", false, ExpectedResult = false)]
[TestCase(null, "Test", "EndsWith", false, ExpectedResult = false)]
[TestCase(null, "Test", "Contains", false, ExpectedResult = false)]
[TestCase(null, "0", "GreaterThan", false, ExpectedResult = false)]
[TestCase(null, "0", "LessThan", false, ExpectedResult = false)]
[TestCase(null, "0", "GreaterThanOrEqualTo", false, ExpectedResult = false)]
[TestCase(null, "0", "LessThanOrEqualTo", false, ExpectedResult = false)]
[TestCase(null, "^[a-z0-9_-]{6,18}$", "RegEx", false, ExpectedResult = false)]
public bool RuleTest(string TestString, string RuleString, string SearchType, bool CaseSensitive)
{
XElement xrule = new XElement("Rule", RuleString);
Expand Down
14 changes: 12 additions & 2 deletions TsGui/Validation/StringMatching/BaseNumberMatchingRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

using System;
using System.Xml.Linq;
using TsGui.Diagnostics.Logging;

namespace TsGui.Validation.StringMatching
{
Expand All @@ -26,8 +28,16 @@ public bool DoesMatch(string input)
double inputnum;
double rulenum;

if (!double.TryParse(input, out inputnum)) { return false; }
if (!double.TryParse(this.Content, out rulenum)) { return false; }
if (!double.TryParse(input, out inputnum))
{
LoggerFacade.Warn("Failed to convert input to number: " + input);
return false;
}
if (!double.TryParse(this.Content, out rulenum))
{
LoggerFacade.Warn("Failed to convert rule content to number: " + this.Content);
return false;
}

return this.Compare(inputnum, rulenum);
}
Expand Down
7 changes: 5 additions & 2 deletions TsGui/Validation/StringMatching/RegEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ public RegEx(XElement inputxml) : base(inputxml) { }

public bool DoesMatch(string input)
{
string s;
if (input == null) { s = string.Empty; }
else { s = input; }
try
{
if (this.IsCaseSensitive == true) { return Regex.IsMatch(input, this.Content); }
else { return Regex.IsMatch(input, this.Content, RegexOptions.IgnoreCase); }
if (this.IsCaseSensitive == true) { return Regex.IsMatch(s, this.Content); }
else { return Regex.IsMatch(s, this.Content, RegexOptions.IgnoreCase); }
}
catch (Exception e) { throw new TsGuiKnownException("Error processing RegEx: " + this.Content, e.Message); }
}
Expand Down

0 comments on commit 969fe88

Please sign in to comment.