Skip to content

Commit

Permalink
Associativity tests, slight refactoring and formatting fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
jwaliszko committed Jul 6, 2016
1 parent 12498a7 commit 1db7a2f
Show file tree
Hide file tree
Showing 18 changed files with 985 additions and 769 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ A small .NET and JavaScript library which provides annotation-based conditional
- [Declarative vs. imperative programming - what is it about?](#declarative-vs-imperative-programming---what-is-it-about)
- [EA expressions specification](#expressions-specification)
- [Grammar definition](#grammar-definition)
- [Operators precedence](#operators-precedence)
- [Operators precedence and associativity](#operators-precedence)
- [Built-in functions (methods ready to be used by expressions)](#built-in-functions)
- [How to construct conditional validation attributes?](#how-to-construct-conditional-validation-attributes)
- [Signatures description](#signatures)
Expand Down Expand Up @@ -165,7 +165,7 @@ array-lit => '[' [exp-list] ']'
exp-list => exp (',' exp)*
```
Terminals are expressed in quotes. Each nonterminal is defined by a rule in the grammar except for *int-lit*, *float-lit*, *bin-lit*, *hex-lit*, *string-lit* and *identifier*, which are assumed to be implicitly defined (*identifier* specifies names of functions, properties, constants and enums).
Terminals are expressed in quotes. Each nonterminal is defined by a rule in the grammar except for *dec-lit*, *bin-lit*, *hex-lit*, *float-lit*, *string-lit* and *identifier*, which are assumed to be implicitly defined (*identifier* specifies names of functions, properties, constants and enums).

Expressions are built of unicode letters and numbers (i.e. `[L*]` and `[N*]` [categories](https://en.wikipedia.org/wiki/Unicode_character_property) respectively) with the usage of following components:

Expand All @@ -176,15 +176,15 @@ Expressions are built of unicode letters and numbers (i.e. `[L*]` and `[N*]` [ca
* literals:
* null, i.e. `null`,
* boolean, i.e. `true` and `false`,
* integer, e.g. `123`,
* decimal integer, e.g. `123`,
* binary integer (with `0b` prefix), e.g. `0b1010`,
* hexadecimal integer (with `0x` prefix), e.g. `0xFF`,
* float, e.g. `1.5` or `0.3e-2`,
* binary (with `0b` prefix), e.g. `0b1010`,
* hexadecimal (with `0x` prefix), e.g. `0xFF`,
* string, e.g. `'in single quotes'` (internal quote escape sequence is `\'`, character representing new line is `\n`),
* array (comma separated items within square brackets), e.g. `[1,2,3]`,
* id, i.e. names of functions, properties, constants and enums.
* identifier, i.e. names of functions, properties, constants and enums.

#####<a id="operators-precedence">Operators precedence</a>
#####<a id="operators-precedence">Operators precedence and associativity</a>

The following table lists the precedence and associativity of operators (listed top to bottom, in descending precedence):

Expand Down Expand Up @@ -287,7 +287,7 @@ The following table lists the precedence and associativity of operators (listed
<tr>
<td>10</td>
<td><code>|</code></td>
<td>Bitwise OR (incluseve OR)</td>
<td>Bitwise OR (inclusive OR)</td>
</tr>
<tr>
<td>11</td>
Expand Down
4 changes: 2 additions & 2 deletions src/ExpressiveAnnotations.MvcUnobtrusive.Tests/BaseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public BaseTest()

protected ModelMetadata GetModelMetadata<TModel, TProp>(TModel model, Expression<Func<TModel, TProp>> expression)
{
var property = ((MemberExpression)expression.Body).Member.Name;
return new ModelMetadata(ModelMetadataProviders.Current, typeof(TModel), () => model, typeof(TProp), property);
var property = ((MemberExpression) expression.Body).Member.Name;
return new ModelMetadata(ModelMetadataProviders.Current, typeof (TModel), () => model, typeof (TProp), property);
}

protected ControllerContext GetControllerContext()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public class ExpressiveAnnotationsModelValidatorProvider : DataAnnotationsModelV
/// </summary>
public ExpressiveAnnotationsModelValidatorProvider()
{
RegisterAdapter(typeof(RequiredIfAttribute), typeof(RequiredIfValidator));
RegisterAdapter(typeof(AssertThatAttribute), typeof(AssertThatValidator));
RegisterAdapter(typeof (RequiredIfAttribute), typeof (RequiredIfValidator));
RegisterAdapter(typeof (AssertThatAttribute), typeof (AssertThatValidator));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected ExpressiveValidator(ModelMetadata metadata, ControllerContext context,
{ // (by design, no reason to recompile once compiled expressions)
var parser = new Parser();
parser.RegisterToolchain();
parser.Parse(metadata.ContainerType, attribute.Expression);
parser.Parse<bool>(metadata.ContainerType, attribute.Expression);

FieldsMap = parser.GetFields().ToDictionary(x => x.Key, x => Helper.GetCoarseType(x.Value));
ConstsMap = parser.GetConsts();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,12 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using ExpressiveAnnotations.Attributes;
using Xunit;

namespace ExpressiveAnnotations.MvcWebSample.UITests
{
public static class Helper
{
public static IEnumerable<ExpressiveAttribute> CompileExpressiveAttributes(this Assembly assembly)
{
return assembly.GetTypes().SelectMany(t => t.CompileExpressiveAttributes());
}

public static IEnumerable<ExpressiveAttribute> CompileExpressiveAttributes(this Type type)
{
var properties = type.GetProperties()
.Where(p => Attribute.IsDefined(p, typeof(ExpressiveAttribute)));

var attributes = new List<ExpressiveAttribute>();
foreach (var prop in properties)
{
var attribs = prop.GetCustomAttributes<ExpressiveAttribute>().ToList();
attribs.ForEach(x => x.Compile(prop.DeclaringType));
attributes.AddRange(attribs);
}
return attributes;
}
}

public class AttributesCompilationTest
{
private static string GetAssemblyLocation(string assemblyName) // looks inside bin folder of sample project
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
<Compile Include="ClientModeTest.cs" />
<Compile Include="AttributesCompilationTest.cs" />
<Compile Include="DriverFixture.cs" />
<Compile Include="Helper.cs" />
<Compile Include="HomePage.cs" />
<Compile Include="ServerModeTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
31 changes: 31 additions & 0 deletions src/ExpressiveAnnotations.MvcWebSample.UITests/Helper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using ExpressiveAnnotations.Attributes;

namespace ExpressiveAnnotations.MvcWebSample.UITests
{
public static class Helper
{
public static IEnumerable<ExpressiveAttribute> CompileExpressiveAttributes(this Assembly assembly)
{
return assembly.GetTypes().SelectMany(CompileExpressiveAttributes);
}

public static IEnumerable<ExpressiveAttribute> CompileExpressiveAttributes(this Type type)
{
var properties = type.GetProperties()
.Where(p => Attribute.IsDefined(p, typeof (ExpressiveAttribute)));

var attributes = new List<ExpressiveAttribute>();
foreach (var prop in properties)
{
var attribs = prop.GetCustomAttributes<ExpressiveAttribute>().ToList();
attribs.ForEach(x => x.Compile(prop.DeclaringType));
attributes.AddRange(attribs);
}
return attributes;
}
}
}
4 changes: 2 additions & 2 deletions src/ExpressiveAnnotations.MvcWebSample/Global.asax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ protected void Application_Start()
//private static void RegisterExpressiveAttributes()
//{
// DataAnnotationsModelValidatorProvider.RegisterAdapter(
// typeof(RequiredIfAttribute), typeof(RequiredIfValidator));
// typeof (RequiredIfAttribute), typeof (RequiredIfValidator));
// DataAnnotationsModelValidatorProvider.RegisterAdapter(
// typeof(AssertThatAttribute), typeof(AssertThatValidator));
// typeof (AssertThatAttribute), typeof (AssertThatValidator));
//}

//private static void RegisterExpressiveModelValidatorProvider()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ public class CustomExpressiveAnnotationsModelValidatorProvider : ExpressiveAnnot
{
public CustomExpressiveAnnotationsModelValidatorProvider()
{
RegisterAdapter(typeof(CustomRequiredIfAttribute), typeof(CustomRequiredIfValidator));
RegisterAdapter(typeof(CustomAssertThatAttribute), typeof(CustomAssertThatValidator));
RegisterAdapter(typeof (CustomRequiredIfAttribute), typeof (CustomRequiredIfValidator));
RegisterAdapter(typeof (CustomAssertThatAttribute), typeof (CustomAssertThatValidator));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public CustomRequiredIfAttribute(string expression)
: base(expression, "The {0} field is conditionally required.") // this default message will be overriden by resources
{
AllowEmptyStrings = false;
ErrorMessageResourceType = typeof(Resources);
ErrorMessageResourceType = typeof (Resources);
ErrorMessageResourceName = "CustomizedRequiredIfDefaultError";
}

Expand Down
32 changes: 1 addition & 31 deletions src/ExpressiveAnnotations.Tests/AttribsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public void custom_error_message_tolerates_null_value()
Assert.Equal("Input string was not in a correct format.", e.InnerException.Message);

IDictionary<string, Guid> errFieldsMap;
e = Assert.Throws<FormatException>(() => attrib.FormatErrorMessage("asd", "true", typeof(object), out errFieldsMap));
e = Assert.Throws<FormatException>(() => attrib.FormatErrorMessage("asd", "true", typeof (object), out errFieldsMap));
Assert.Equal($"Problem with error message processing. The message is following: {msg}", e.Message);
Assert.IsType<FormatException>(e.InnerException);
Assert.Equal("Input string was not in a correct format.", e.InnerException.Message);
Expand Down Expand Up @@ -499,34 +499,4 @@ private class DisplayDuplicateModel
public int? Value2 { get; set; }
}
}

public static class Helper
{
public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
var seenKeys = new HashSet<TKey>();
foreach (var element in source)
{
if (seenKeys.Add(keySelector(element)))
{
yield return element;
}
}
}

public static IEnumerable<ExpressiveAttribute> CompileExpressiveAttributes(this Type type)
{
var properties = type.GetProperties()
.Where(p => Attribute.IsDefined(p, typeof (ExpressiveAttribute)));
var attributes = new List<ExpressiveAttribute>();

foreach (var prop in properties)
{
var attribs = prop.GetCustomAttributes<ExpressiveAttribute>().ToList();
attribs.ForEach(x => x.Compile(prop.DeclaringType));
attributes.AddRange(attribs);
}
return attributes;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
</Choose>
<ItemGroup>
<Compile Include="AttribsTest.cs" />
<Compile Include="Helper.cs" />
<Compile Include="LexerTest.cs" />
<Compile Include="LocationComparer.cs" />
<Compile Include="ParserTest.cs" />
Expand Down
Loading

0 comments on commit 1db7a2f

Please sign in to comment.