public void VisitorShouldResolveMethodName() { // arrange Expression <Func <Entity, object> > expression = o => o.AuditInfo("test"); var visitor = new PropertyPathVisitor(); // act visitor.Visit(expression); // assert Assert.Equal(nameof(Entity.AuditInfo), visitor.Name); }
public void VisitorShouldResolvePropertyNameWithIndexer() { // arrange Expression <Func <Entity, object> > expression = o => o.Items[0]; var visitor = new PropertyPathVisitor(); // act visitor.Visit(expression); // assert Assert.Equal(nameof(Entity.Items), visitor.Name); }
public void VisitorShouldResolvePropertyNameFromPath() { // arrange Expression <Func <Entity, object> > expression = o => o.Items.Count; var visitor = new PropertyPathVisitor(); // act visitor.Visit(expression); // assert Assert.Equal(nameof(IList <object> .Count), visitor.Name); }
public void VisitorShouldGenerateKeyWithIndexer() { // arrange Expression <Func <Entity, object> > expression = o => o.Items[0]; var visitor = new PropertyPathVisitor(); var expected = $"{typeof( Entity ).FullName}.{nameof( Entity.Items )}_0"; // act visitor.Visit(expression); var actual = visitor.GenerateKey(typeof(Entity).FullName); // assert Assert.Equal(expected, actual); }
public void VisitorShouldGenerateKeyWithParameter() { // arrange Expression <Func <Entity, object> > expression = o => o.AuditInfo("test"); var visitor = new PropertyPathVisitor(); var expected = $"{typeof( Entity ).FullName}.{nameof( Entity.AuditInfo )}_test"; // act visitor.Visit(expression); var actual = visitor.GenerateKey(typeof(Entity).FullName); // assert Assert.Equal(expected, actual); }
internal static string GetInstanceAnnotationKey <TObject, TProperty>(this Expression <Func <TObject, TProperty> > propertyExpression, out string name) { Contract.Requires(propertyExpression != null); Contract.Ensures(!string.IsNullOrEmpty(Contract.Result <string>())); var visitor = new PropertyPathVisitor(); // visit the expression and get the name of the first member (e.g. property or field) visitor.Visit(propertyExpression); // the expression wasn't valid; throw meaningful exception if (string.IsNullOrEmpty(name = visitor.Name)) { throw new ArgumentException(string.Format(CurrentCulture, InvalidAnnotationPropertyExpression, propertyExpression), nameof(propertyExpression)); } // build unique key return(visitor.GenerateKey(typeof(TObject).FullName)); }