Skip to content

Commit

Permalink
Unit tests related to eavalid event, enumerations and phone validatio…
Browse files Browse the repository at this point in the history
…n added.
  • Loading branch information
jwaliszko committed Nov 24, 2016
1 parent 140345f commit b61d54b
Show file tree
Hide file tree
Showing 9 changed files with 302 additions and 204 deletions.
6 changes: 5 additions & 1 deletion src/ExpressiveAnnotations.MvcWebSample/Models/Contact.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ public class Contact
{
public Contact()
{
Addresses = new List<Address> { new Address { Type = Resources.HomeAddress }, new Address { Type = Resources.OfficeAddress } };
Addresses = new List<Address>
{
new Address {Type = Resources.HomeAddress},
new Address {Type = Resources.OfficeAddress}
};
}

[RequiredIf("Phone == null",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
</li>
<li>
<pre class="code">
[RequiredIf("GoAbroad == true")]
[RequiredIf("GoAbroad == true")]
</pre>
<pre class="action">[show attribute]</pre>
<div>
Expand Down
352 changes: 180 additions & 172 deletions src/ExpressiveAnnotations.Tests/ParserTest.cs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/ExpressiveAnnotations/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ public static Type GetNullableEquivalent(this Type type)
{
{typeof(sbyte), typeof(sbyte?)},
{typeof(byte), typeof(byte?)},
{typeof(char), typeof(char?)},
{typeof(short), typeof(short?)},
{typeof(ushort), typeof(ushort?)},
{typeof(int), typeof(int?)},
Expand Down
4 changes: 2 additions & 2 deletions src/expressive.annotations.validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ var
value = modelHelper.adjustGivenValue(value, element, params); // preprocess given value (here basically we are concerned about determining if such a value is null or not, to determine if the attribute
// logic should be invoked or not - full type-detection parsing is not required at this stage, but we may have to extract such a value using
// value parser, e.g. for an array which values are distracted among multiple fields)
if (!(value === undefined || value === null || value === '')) { // check if the field value is set (continue if so, otherwise skip condition verification)
if (value !== undefined && value !== null && value !== '') { // check if the field value is set (continue if so, otherwise skip condition verification)
var model = modelHelper.deserializeObject(params.form, params.fieldsMap, params.constsMap, params.enumsMap, params.parsersMap, params.prefix);
toolchain.registerMethods(model);
logger.dump(typeHelper.string.format('AssertThat expression of {0} field:\n{1}\nwill be executed within following context (methods hidden):\n{2}', element.name, params.expression, model));
Expand Down Expand Up @@ -732,7 +732,7 @@ var
}
return {
valid: true,
condition: exprVal
condition: exprVal // undefined when optimize flag is on
}
},

Expand Down
22 changes: 16 additions & 6 deletions src/expressive.annotations.validate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
qunit.testDone(function() { // reset state for further tests
ea.settings.apply({
debug: false,
optimize: true,
enumsAsNumbers: true,
dependencyTriggers: 'change keyup'
});
});
Expand Down Expand Up @@ -104,7 +106,7 @@
assert.equal(actual, expected, "custom value parser properly handles non-standard dd/mm/yyyy format");
});

qunit.test("verify_non_standard_object_format_parsing", function(assert) {
qunit.test("verify_non_standard_object_format_parsing", function(assert) {
var result = eapriv.typeHelper.tryParse("<rss version='2.0'><channel><title>RSS Title</title></channel></rss>", "object", undefined);
assert.ok(result.error, "default object parse fails to understand non-json format");
var expected = "Given value was not recognized as a valid JSON.";
Expand Down Expand Up @@ -292,7 +294,7 @@
assert.ok(!eapriv.typeHelper.isBool("false"), "'false' string not recognized as bool");
assert.ok(!eapriv.typeHelper.isBool("False"), "'False' string not recognized as bool");
assert.ok(!eapriv.typeHelper.isBool(""), "empty string not recognized as bool");
assert.ok(!eapriv.typeHelper.isBool(0), "0 integer not recognized as bool");
assert.ok(!eapriv.typeHelper.isBool(0), "0 integer not recognized as bool");
assert.ok(!eapriv.typeHelper.isBool(1), "positive integer not recognized as bool");
assert.ok(!eapriv.typeHelper.isBool(-1), "negative integer not recognized as bool");
assert.ok(!eapriv.typeHelper.isBool({}), "empty object not recognized as bool");
Expand Down Expand Up @@ -396,7 +398,7 @@
assert.ok(eapriv.typeHelper.isNumeric(m.ToDate('2016-04-27')), "ToDate() returns number (of milliseconds)");

assert.ok(m.Now() > m.Today());
assert.ok(m.Date(1985, 2, 20) < m.Date(1985, 2, 20, 0, 0, 1));
assert.ok(m.Date(1985, 2, 20) < m.Date(1985, 2, 20, 0, 0, 1));
assert.equal(m.Date(1, 1, 1), new Date(new Date(1, 0, 1).setFullYear(1)).getTime());
assert.equal(m.Date(1, 1, 1), m.Date(1, 1, 1, 0, 0, 0));
assert.equal(m.ToDate('2016-04-27'), Date.parse('2016-04-27'));
Expand Down Expand Up @@ -451,7 +453,7 @@
assert.equal(m.CompareOrdinalIgnoreCase('', null), 1);
assert.equal(m.CompareOrdinalIgnoreCase(null, null), 0);
assert.equal(m.CompareOrdinalIgnoreCase('', ''), 0);

assert.ok(!m.StartsWith(' ab c', ' A'));
assert.ok(m.StartsWith(' ab c', ' a'));
assert.ok(m.StartsWith(' ', ' '));
Expand All @@ -467,7 +469,7 @@
assert.ok(!m.StartsWithIgnoreCase(null, ''));
assert.ok(!m.StartsWithIgnoreCase('', null));
assert.ok(!m.StartsWithIgnoreCase(null, null));

assert.ok(!m.EndsWith(' ab c', ' C'));
assert.ok(m.EndsWith(' ab c', ' c'));
assert.ok(m.EndsWith(' ', ' '));
Expand All @@ -483,7 +485,7 @@
assert.ok(!m.EndsWithIgnoreCase(null, ''));
assert.ok(!m.EndsWithIgnoreCase('', null));
assert.ok(!m.EndsWithIgnoreCase(null, null));

assert.ok(!m.Contains(' ab c', 'B '));
assert.ok(m.Contains(' ab c', 'b '));
assert.ok(m.Contains(' ', ' '));
Expand Down Expand Up @@ -535,6 +537,14 @@
assert.ok(!m.IsEmail(null));
assert.ok(!m.IsEmail(''));

assert.ok(m.IsPhone('+48 999 888 777'));
assert.ok(m.IsPhone('(0048) 999 888 777'));
assert.ok(m.IsPhone('(+48) 999888777'));
assert.ok(m.IsPhone('112'));
assert.ok(!m.IsPhone('11 22 333xx'));
assert.ok(!m.IsPhone(null));
assert.ok(!m.IsPhone(''));

assert.ok(m.IsUrl('http://www.github.com/'));
assert.ok(!m.IsUrl(null));
assert.ok(!m.IsUrl(''));
Expand Down
32 changes: 23 additions & 9 deletions src/form.tests.harness.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,38 @@
<div id="qunit-fixture">
<form id="basic_test_form">
<input data-val="true"
data-val-assertthat="Provided email: 02b4c617-f4a4-4820-9624-6bc4cd0922c5 (yes 02b4c617-f4a4-4820-9624-6bc4cd0922c5) cannot be accepted."
data-val-assertthat-constsmap="{&quot;Stability.Low&quot;:0}"
data-val-assertthat-expression="&quot;Whoami() == 'root' && IsEmail(Email) && ArrayContains('a', Letters) && PoliticalStability == Stability.Low&quot;"
data-val-assertthat-fieldsmap="{&quot;Email&quot;:&quot;string&quot;,&quot;Letters&quot;:&quot;mytype&quot;,&quot;PoliticalStability&quot;:&quot;enumeration&quot;}"
data-val-assertthat="Provided email 02b4c617-f4a4-4820-9624-6bc4cd0922c5 (yes 02b4c617-f4a4-4820-9624-6bc4cd0922c5) cannot be accepted {0}{1}."
data-val-assertthat-constsmap="{&quot;LetterA&quot;:&quot;a&quot;}"
data-val-assertthat-enumsmap="{&quot;Stability.Low&quot;:0}"
data-val-assertthat-expression="&quot;Whoami() == 'root' && ArrayContains(LetterA, Letters) && PoliticalStabilityA == Stability.High && IsEmail(Email)&quot;"
data-val-assertthat-fieldsmap="{&quot;Letters&quot;:&quot;unknowntype&quot;,&quot;PoliticalStabilityA&quot;:&quot;enumeration&quot;,&quot;Email&quot;:&quot;string&quot;}"
data-val-assertthat-parsersmap="{&quot;Letters&quot;:&quot;ArrayParser&quot;}"
data-val-assertthat-errfieldsmap="{&quot;Email&quot;:&quot;02b4c617-f4a4-4820-9624-6bc4cd0922c5&quot;}"
name="ContactDetails.Email"
type="text"
value="[email protected]">
<input name="ContactDetails.Letters"
value="ea{at}home.com">
<input data-val="true"
data-val-requiredif="Field required."
data-val-requiredif-expression="&quot;true&quot;"
name="ContactDetails.Letters"
type="text"
value="a,b,c">
<input data-val="true"
data-val-assertthat="{0}{1}"
data-val-assertthat-expression="&quot;false&quot;"
name="ContactDetails.PoliticalStability"
data-val-assertthat="Assertion not satisfied."
data-val-assertthat-enumsmap="{&quot;Stability.High&quot;:1}"
data-val-assertthat-expression="&quot;PoliticalStabilityA == Stability.High&quot;"
data-val-assertthat-fieldsmap="{&quot;PoliticalStabilityA&quot;:&quot;enumeration&quot;}"
name="ContactDetails.PoliticalStabilityA"
type="text"
value="1">
<input data-val="true"
data-val-assertthat="Assertion not satisfied."
data-val-assertthat-enumsmap="{&quot;Stability.High&quot;:1}"
data-val-assertthat-expression="&quot;PoliticalStabilityB == Stability.High&quot;"
data-val-assertthat-fieldsmap="{&quot;PoliticalStabilityB&quot;:&quot;enumeration&quot;}"
name="ContactDetails.PoliticalStabilityB"
type="text"
value="High">
</form>
</div>

Expand Down
32 changes: 23 additions & 9 deletions src/form.tests.harness.latestdeps.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,38 @@
<div id="qunit-fixture">
<form id="basic_test_form">
<input data-val="true"
data-val-assertthat="Provided email: 02b4c617-f4a4-4820-9624-6bc4cd0922c5 (yes 02b4c617-f4a4-4820-9624-6bc4cd0922c5) cannot be accepted."
data-val-assertthat-constsmap="{&quot;Stability.Low&quot;:0}"
data-val-assertthat-expression="&quot;Whoami() == 'root' && IsEmail(Email) && ArrayContains('a', Letters) && PoliticalStability == Stability.Low&quot;"
data-val-assertthat-fieldsmap="{&quot;Email&quot;:&quot;string&quot;,&quot;Letters&quot;:&quot;mytype&quot;,&quot;PoliticalStability&quot;:&quot;enumeration&quot;}"
data-val-assertthat="Provided email 02b4c617-f4a4-4820-9624-6bc4cd0922c5 (yes 02b4c617-f4a4-4820-9624-6bc4cd0922c5) cannot be accepted {0}{1}."
data-val-assertthat-constsmap="{&quot;LetterA&quot;:&quot;a&quot;}"
data-val-assertthat-enumsmap="{&quot;Stability.Low&quot;:0}"
data-val-assertthat-expression="&quot;Whoami() == 'root' && ArrayContains(LetterA, Letters) && PoliticalStabilityA == Stability.High && IsEmail(Email)&quot;"
data-val-assertthat-fieldsmap="{&quot;Letters&quot;:&quot;unknowntype&quot;,&quot;PoliticalStabilityA&quot;:&quot;enumeration&quot;,&quot;Email&quot;:&quot;string&quot;}"
data-val-assertthat-parsersmap="{&quot;Letters&quot;:&quot;ArrayParser&quot;}"
data-val-assertthat-errfieldsmap="{&quot;Email&quot;:&quot;02b4c617-f4a4-4820-9624-6bc4cd0922c5&quot;}"
name="ContactDetails.Email"
type="text"
value="[email protected]">
<input name="ContactDetails.Letters"
value="ea{at}home.com">
<input data-val="true"
data-val-requiredif="Field required."
data-val-requiredif-expression="&quot;true&quot;"
name="ContactDetails.Letters"
type="text"
value="a,b,c">
<input data-val="true"
data-val-assertthat="{0}{1}"
data-val-assertthat-expression="&quot;false&quot;"
name="ContactDetails.PoliticalStability"
data-val-assertthat="Assertion not satisfied."
data-val-assertthat-enumsmap="{&quot;Stability.High&quot;:1}"
data-val-assertthat-expression="&quot;PoliticalStabilityA == Stability.High&quot;"
data-val-assertthat-fieldsmap="{&quot;PoliticalStabilityA&quot;:&quot;enumeration&quot;}"
name="ContactDetails.PoliticalStabilityA"
type="text"
value="1">
<input data-val="true"
data-val-assertthat="Assertion not satisfied."
data-val-assertthat-enumsmap="{&quot;Stability.High&quot;:1}"
data-val-assertthat-expression="&quot;PoliticalStabilityB == Stability.High&quot;"
data-val-assertthat-fieldsmap="{&quot;PoliticalStabilityB&quot;:&quot;enumeration&quot;}"
name="ContactDetails.PoliticalStabilityB"
type="text"
value="High">
</form>
</div>

Expand Down
55 changes: 51 additions & 4 deletions src/form.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,62 @@

var validator = $('#basic_test_form').validate();

var triggered = false;
var element = $('#basic_test_form').find('[name="ContactDetails.Email"]');
$(element).on('eavalid', function(e, type, valid, expr, cond) {
triggered = true;

assert.equal(e.currentTarget.name, 'ContactDetails.Email');
assert.equal(type, 'assertthat');
assert.equal(valid, false);
assert.equal(expr, "Whoami() == 'root' && ArrayContains(LetterA, Letters) && PoliticalStabilityA == Stability.High && IsEmail(Email)");
assert.equal(cond, undefined);
});
var result = element.valid(); // trigger wait for result (all is synchronous)
assert.ok(triggered);
assert.ok(!result);
assert.equal(validator.errorList[0].message, 'Provided email: ea@home.com (yes ea@home.com) cannot be accepted.');
assert.equal(validator.errorList[0].message, 'Provided email ea{at}home.com (yes ea{at}home.com) cannot be accepted {0}{1}.');

element = $('#basic_test_form').find('[name="ContactDetails.PoliticalStability"]');
triggered = false;
element = $('#basic_test_form').find('[name="ContactDetails.Letters"]');
$(element).on('eavalid', function(e, type, valid, expr, cond) {
triggered = true;

assert.equal(e.currentTarget.name, 'ContactDetails.Letters');
assert.equal(type, 'requiredif');
assert.equal(valid, true);
assert.equal(expr, "true");
assert.equal(cond, undefined); // optimization on - no redundant expression evaluation is performed
});
result = element.valid();
assert.ok(!result);
assert.equal(validator.errorList[0].message, '{0}{1}');
assert.ok(triggered);
assert.ok(result);

triggered = false;
$(element).off('eavalid');
ea.settings.optimize = false;
$(element).on('eavalid', function(e, type, valid, expr, cond) {
triggered = true;

assert.equal(e.currentTarget.name, 'ContactDetails.Letters');
assert.equal(type, 'requiredif');
assert.equal(valid, true);
assert.equal(expr, "true");
assert.equal(cond, true);
});
result = element.valid();
assert.ok(triggered);
assert.ok(result);

element = $('#basic_test_form').find('[name="ContactDetails.PoliticalStabilityA"]');
result = element.valid();
assert.ok(result);

ea.settings.enumsAsNumbers = false;

element = $('#basic_test_form').find('[name="ContactDetails.PoliticalStabilityB"]');
result = element.valid();
assert.ok(result);
});

}($, QUnit, window.ea));

0 comments on commit b61d54b

Please sign in to comment.