public void Fail_if_properties_has_bad_key() { var propsWithNullKey = new List <KeyValuePair <string, Type> > { new KeyValuePair <string, Type>(null, typeof(string)) }; Assert.Throws <ArgumentException>( () => AnonymousTypeBuilder.Create(propsWithNullKey) ); var propsWithEmptyKey = new List <KeyValuePair <string, Type> > { new KeyValuePair <string, Type>(String.Empty, typeof(string)) }; Assert.Throws <ArgumentException>( () => AnonymousTypeBuilder.Create(propsWithEmptyKey) ); var propsWithWhitespaceKey = new List <KeyValuePair <string, Type> > { new KeyValuePair <string, Type>(" ", typeof(string)) }; Assert.Throws <ArgumentException>( () => AnonymousTypeBuilder.Create(propsWithWhitespaceKey) ); }
public void Create() { var properties = new List <KeyValuePair <string, Type> > { new KeyValuePair <string, Type>("Name", typeof(string)), new KeyValuePair <string, Type>("HowManyHotdogs", typeof(int)), new KeyValuePair <string, Type>("IsDiningOut", typeof(bool)) }; var anonymous = AnonymousTypeBuilder.Create(properties); var parameters = anonymous .GetConstructors() .First() .GetParameters() .Select(p => p.ParameterType); Assert.True( parameters .SequenceEqual(properties.Select(p => p.Value)) ); ConstructorInfo ctor = anonymous .GetConstructor(properties.Select(p => p.Value).ToArray()); object me = ctor.Invoke(new object[] { "Mary", 3, true }); Assert.NotNull(me); Assert.Equal("Mary", anonymous.GetProperty("Name").GetValue(me, null)); Assert.Equal(3, anonymous.GetProperty("HowManyHotdogs").GetValue(me, null)); Assert.Equal(true, anonymous.GetProperty("IsDiningOut").GetValue(me, null)); }
public void Fail_if_no_properties() { var properties = new List <KeyValuePair <string, Type> > { }; Assert.Throws <InvalidOperationException>( () => AnonymousTypeBuilder.Create(properties) ); }
public void Fail_if_properties_has_bad_value() { var propsWithNullValue = new List <KeyValuePair <string, Type> > { new KeyValuePair <string, Type>("Name", null) }; Assert.Throws <ArgumentException>( () => AnonymousTypeBuilder.Create(propsWithNullValue) ); }
public void Fail_if_properties_has_duplicates() { var properties = new List <KeyValuePair <string, Type> > { new KeyValuePair <string, Type>("Name", typeof(string)), new KeyValuePair <string, Type>("Name", typeof(int)) }; Assert.Throws <InvalidOperationException>( () => AnonymousTypeBuilder.Create(properties) ); }
public void Fail_if_properties_null() { Assert.Throws <ArgumentNullException>( () => AnonymousTypeBuilder.Create(null) ); }