public void Name_XmlAttributeAttribute_Defined() { // Arrange & Act & Assert var propertyInfo = TypeAssert.PropertyHasAttribute <Resx.DataNode>("Name", typeof(XmlAttributeAttribute)); AttributeAssert.ValidateProperty(propertyInfo, typeof(XmlAttributeAttribute), nameof(XmlAttributeAttribute.AttributeName), "name"); }
public void Debit() { #if !DEBUG Assert.Multiple(() => { #endif var assert = new TypeAssert<SavingsAccount>(); assert.Method<bool>( "Debit", BindingFlags.Instance | BindingFlags.Public, new Param<decimal>("amount") ).Override(); decimal rate = 0.035m; dynamic o; // new SavingsAccount(1000m, rate); o = assert.New(1000m, rate); Assert.IsTrue(o.Debit(491m)); Assert.AreEqual(509m, o.Balance); Assert.IsFalse(o.Debit(509.01m)); Assert.AreEqual(509m, o.Balance); Assert.IsTrue(o.Debit(509m)); Assert.AreEqual(0m, o.Balance); #if !DEBUG }); #endif }
public void Value_XmlElementAttribute_Defined() { // Arrange & Act & Assert var propertyInfo = TypeAssert.PropertyHasAttribute <Resx.DataNode>("Value", typeof(XmlElementAttribute)); AttributeAssert.ValidateProperty(propertyInfo, typeof(XmlElementAttribute), nameof(XmlElementAttribute.ElementName), "value"); }
public void TestParkInstanceMethods() { #if !DEBUG Assert.Multiple(() => { #endif var park = new TypeAssert<Park>(); dynamic actual = park.New( "Park 1", "123 Test Drive", (FacilityType)Enum.Parse(typeof(FacilityType), "National"), "444-432-9876", 5, 18, 12.34m); Assert.AreEqual("Park 1 National Park 5 AM to 6 PM $12.34", actual.Info); Assert.AreEqual("444-432-9876 123 Test Drive", actual.Contact); Assert.AreEqual(419.56m, actual.CalculateFee(34)); park.Catch<ArgumentOutOfRangeException>( () => actual = park.New( "Park 1", "123 Test Drive", (FacilityType)Enum.Parse(typeof(FacilityType), "National"), "444-432-9876", 6, 5, 12.34m) ); #if !DEBUG }); #endif }
public void IsAssignableFromFails() { var array10 = new int[10]; var array2 = new int[2, 2]; TypeAssert.IsAssignableFrom(array10, array2.GetType()); }
public void GPA() { #if !DEBUG Assert.Multiple(() => { #endif var assert = new TypeAssert<Student>(); assert.Catch<ArgumentOutOfRangeException>(() => assert.New("User", "Test", "7654321", 4.5m, "MIS")); dynamic student = assert.New("User", "Test", "7654321"); assert.Catch<ArgumentOutOfRangeException>( () => student.GPA = -0.01m ); assert.Catch<ArgumentOutOfRangeException>( () => student.GPA = 4.01m ); student.GPA = 4.0m; Assert.AreEqual(4.0m, student.GPA); student.GPA = 0.0m; Assert.AreEqual(0.0m, student.GPA); student.GPA = 2.0m; Assert.AreEqual(2.0m, student.GPA); student.GPA = 3.0m; Assert.AreEqual(3.0m, student.GPA); #if !DEBUG }); #endif }
public void TestAggregatorClass() { var aggregator = new TypeAssert <Aggregator>(); aggregator.Class(); aggregator.NonAbstract(); aggregator.PropertyOrField <int[]>("Numbers", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField | BindingFlags.GetProperty)?.Protected()?.ReadOnly(); aggregator.Constructor( BindingFlags.NonPublic | BindingFlags.Instance, new Param <int[]>("numbers"))?.Protected(); aggregator.Method <int>( "GetValue", BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty, new Param <int>("n") )?.Virtual(); aggregator.Method <string>( "ToString", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly ).Override(); }
public void TestSquareData() { var assert = new TypeAssert <Square>(); var dimension1 = assert.Field <int>( "Dimension1", BindingFlags.NonPublic | BindingFlags.Instance ); var dimension2 = assert.Field <int>( "Dimension2", BindingFlags.NonPublic | BindingFlags.Instance ); // var obj = new Square(1, 2 ,3); dynamic obj = assert.New(1, 2, 3); Assert.AreEqual("Square", obj.Name); Assert.AreEqual(1, obj.X); Assert.AreEqual(2, obj.Y); Assert.AreEqual(3, obj.Side); Assert.AreEqual(3, dimension1.GetValue(obj)); Assert.AreEqual(3, dimension2.GetValue(obj)); Assert.AreEqual(9.0, obj.Area, 1E-10); Assert.AreEqual("(1, 2) side: 3", obj.ToString()); obj.Side = 7; Assert.AreEqual(7, dimension1.GetValue(obj)); Assert.AreEqual(7, dimension2.GetValue(obj)); Assert.AreEqual(49.0, obj.Area, 1E-10); Assert.AreEqual("(1, 2) side: 7", obj.ToString()); }
public void IsNotAssignableFromFails() { int[] array10 = new int[10]; int[] array2 = new int[2]; TypeAssert.IsNotAssignableFrom(array10, array2.GetType()); }
public void TestTwoDimensionalShapeClass() { var assert = new TypeAssert <TwoDimensionalShape>(); assert.Extends <Shape>(); assert.Constructor( BindingFlags.NonPublic | BindingFlags.Instance, new Param <int>("x"), new Param <int>("y"), new Param <int>("dimension1"), new Param <int>("dimension2") ).Protected(); assert.Field <int>( "Dimension1", BindingFlags.Instance | BindingFlags.NonPublic ).Protected(); assert.Field <int>( "Dimension2", BindingFlags.Instance | BindingFlags.NonPublic ).Protected(); assert.Property <double>( "Area", BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty ).Virtual(); }
public void TestInvoiceObject() { #if !DEBUG Assert.Multiple(() => { #endif var invoiceClass = new TypeAssert<Invoice>(); dynamic invoice = invoiceClass.New("1234", "Hammer", 2, 14.95m); Assert.AreEqual("1234", invoice.PartNumber, "Initial Part Number"); Assert.AreEqual("Hammer", invoice.PartDescription, "Initial Part Description"); Assert.AreEqual(2, invoice.Quantity, "Initial Quantity"); Assert.AreEqual(14.95m, invoice.PricePerItem, "Initial Price Per Item"); Assert.AreEqual(29.90m, invoice.GetInvoiceAmount(), "Initial GetInvoiceAmount()"); invoice.PartNumber = "001234"; invoice.PartDescription = "Yellow Hammer"; invoice.Quantity = 3; invoice.PricePerItem = 19.49m; Assert.AreEqual("001234", invoice.PartNumber, "Updated Part Number"); Assert.AreEqual("Yellow Hammer", invoice.PartDescription, "Updated Part Description"); Assert.AreEqual(3, invoice.Quantity, "Updated Quantity"); Assert.AreEqual(19.49m, invoice.PricePerItem, "Updated Price Per Item"); Assert.AreEqual(58.47m, invoice.GetInvoiceAmount(), "Updated GetInvoiceAmount()"); invoice.Quantity = -4; invoice.PricePerItem = -0.01m; Assert.AreEqual(3, invoice.Quantity, "Updated Quantity with a negative value"); Assert.AreEqual(19.49m, invoice.PricePerItem, "Updated Price Per Item with a negative value"); Assert.AreEqual(58.47m, invoice.GetInvoiceAmount(), "Updated GetInvoiceAmount() after alterations"); #if !DEBUG }); #endif }
public void Debit() { #if !DEBUG Assert.Multiple(() => { #endif var assert = new TypeAssert<CheckingAccount>(); assert.Method<bool>( "Debit", BindingFlags.Instance | BindingFlags.Public, new Param<decimal>("amount") ).Override(); decimal fee = 3.5m; dynamic o; // new CheckingAccount(1000m, fee); o = assert.New(1000m, fee); Assert.IsTrue(o.Debit(500m)); Assert.AreEqual(496.5m, o.Balance); Assert.IsFalse(o.Debit(494.0m)); Assert.AreEqual(496.5m, o.Balance); #if !DEBUG }); #endif }
public void TestCountClass() { var count = new TypeAssert <Count>(); count.Class(); count.NonAbstract(); count.Extends <Aggregator>(); count.NonPropertyAndField <int[]>("Numbers", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField | BindingFlags.GetProperty); count.Constructor( BindingFlags.Public | BindingFlags.Instance, new Param <int[]>("numbers"))?.Public(); count.Method <int>( "GetValue", BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty, new Param <int>("n") )?.Override(); }
public void Constructor() { #if !DEBUG Assert.Multiple(() => { #endif var assert = new TypeAssert<CheckingAccount>(); assert.Extends<Account>(); assert.Constructor( BindingFlags.Instance | BindingFlags.Public, new Param<decimal>("initialBalance"), new Param<decimal>("transactionFee") ); assert.NonConstructor( BindingFlags.Instance | BindingFlags.Public, new Param<decimal>("initialBalance") ); // new CheckingAccount(11.5m, -0.05m); assert.Catch<ArgumentOutOfRangeException>(() => assert.New(11.5m, -0.05m)); #if !DEBUG }); #endif }
public void Data_XmlElementAttribute_Defined() { // Arrange & Act & Assert var propertyInfo = TypeAssert.PropertyHasAttribute <Resx>("Data", typeof(XmlElementAttribute)); AttributeAssert.ValidateProperty(propertyInfo, typeof(XmlElementAttribute), nameof(XmlElementAttribute.ElementName), "data"); }
public void XmlRootAttribute_Defined() { // Arrange & Act & Assert var attribute = TypeAssert.HasAttribute <Resx, XmlRootAttribute>(); Assert.Equal("root", attribute.ElementName); }
public void TestCondoData() { #if !DEBUG Assert.Multiple(() => { #endif var condo = new TypeAssert<Condo>(); dynamic o; Catch<ArgumentOutOfRangeException>(() => condo.New("One Main Street", 1799, 100_000m, "A", 1m, false)); Catch<ArgumentOutOfRangeException>(() => condo.New("Two Main Street", 2000, 0m, "A", 1m, true)); Catch<ArgumentOutOfRangeException>(() => condo.New("Two Main Street", 2000, 100m, "A", -1m, false)); Catch<ArgumentOutOfRangeException>(() => condo.New("Two Main Street", 2050, 100m, "A", 1m, false)); o = condo.New("Two Main Street", 1800, 100_000m, "A", 2000m, false); Assert.AreEqual(2000m, o.Fee); Assert.AreEqual(100_000m, o.Price); Assert.AreEqual(102_000m, o.TotalCost); Assert.AreEqual(1800, o.YearBuilt); Assert.AreEqual("A", o.UnitNumber); Assert.IsFalse(o.IsRental); Catch<InvalidOperationException>(() => o.GetMonthlyRate()); Assert.AreEqual("Two Main Street 1800 A $102,000.00", o.ToString()); o.IsRental = true; Assert.AreEqual(2000m, o.Fee); Assert.AreEqual(100_000m, o.Price); Assert.AreEqual(102_000m, o.TotalCost); Assert.AreEqual(1800, o.YearBuilt); Assert.AreEqual("A", o.UnitNumber); Assert.IsTrue(o.IsRental); Assert.AreEqual(425, o.GetMonthlyRate()); Assert.AreEqual("Two Main Street 1800 A $425.00", o.ToString()); #if !DEBUG }); #endif }
public void Debit() { #if !DEBUG Assert.Multiple(() => { #endif var assert = new TypeAssert<Account>(); var p = assert.Method<bool>( "Debit", BindingFlags.Instance | BindingFlags.Public, new Param<decimal>("amount") ).Virtual(); dynamic account; account = assert.New(100m); Assert.AreEqual(100m, account.Balance); bool debit = account.Debit(23.45m); Assert.AreEqual(76.55m, account.Balance); Assert.IsTrue(debit); debit = account.Debit(76.56m); Assert.IsFalse(debit); Assert.AreEqual(76.55m, account.Balance); assert.Catch<ArgumentOutOfRangeException>(() => account.Debit(-0.01m)); Assert.AreEqual(76.55m, account.Balance); #if !DEBUG }); #endif }
public void TestCircleData() { var assert = new TypeAssert <Circle>(); var dimension1 = assert.Field <int>( "Dimension1", BindingFlags.NonPublic | BindingFlags.Instance ); var dimension2 = assert.Field <int>( "Dimension2", BindingFlags.NonPublic | BindingFlags.Instance ); // var obj = new Circle(1, 2 ,3); dynamic obj = assert.New(1, 2, 3); Assert.AreEqual("Circle", obj.Name); Assert.AreEqual(1, obj.X); Assert.AreEqual(2, obj.Y); Assert.AreEqual(3, obj.Radius); Assert.AreEqual(3, dimension1.GetValue(obj)); Assert.AreEqual(3, dimension2.GetValue(obj)); Assert.AreEqual(28.274333882308138, obj.Area, 1E-10); Assert.AreEqual("(1, 2) radius: 3", obj.ToString()); obj.Radius = 7; Assert.AreEqual(7, dimension1.GetValue(obj)); Assert.AreEqual(7, dimension2.GetValue(obj)); Assert.AreEqual(153.93804002589985, obj.Area, 1E-10); Assert.AreEqual("(1, 2) radius: 7", obj.ToString()); }
public void Balance() { #if !DEBUG Assert.Multiple(() => { #endif var assert = new TypeAssert<Account>(); var p = assert.Property<decimal>( "Balance", BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.SetProperty ).NonVirtual(); dynamic account; account = assert.New(0m); Assert.AreEqual(0m, account.Balance); account.Balance = 123.45m; Assert.AreEqual(123.45m, account.Balance); assert.Catch<ArgumentOutOfRangeException>(() => account.Balance = -0.05m); Assert.AreEqual(123.45m, account.Balance); #if !DEBUG }); #endif }
public void TestAccountDepositWithdraw() { #if !DEBUG Assert.Multiple(() => { #endif var accountClass = new TypeAssert<Account>(); dynamic account = accountClass.New("John Smith", 20m); Assert.AreEqual(20m, account.Balance, "Initial balance"); account.Deposit(15.5m); Assert.AreEqual(35.5m, account.Balance, "20 + 15.5"); account = accountClass.New("John Smith", 20m); account.Withdraw(15.5m); Assert.AreEqual(4.5m, account.Balance, "20 - 15.5"); account = accountClass.New("John Smith", 20m); account.Deposit(-0.5m); Assert.AreEqual(20m, account.Balance, "cannot deposit negative amount."); account = accountClass.New("John Smith", 20m); Action tester = () => { account.Withdraw(25m); }; string output = tester.Run(); Assert.AreEqual(20m, account.Balance, "cannot deposit negative amount."); output.Assert("Withdrawal amount exceeded account balance."); #if !DEBUG }); #endif }
public void TestSingleFamily() { var assert = new TypeAssert <SingleFamily>(); assert.Extends <Home>(); assert.Property <string>( "Address", BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty ).DeclaredIn <Home>(); assert.Property <int>( "YearBuilt", BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty ).DeclaredIn <Home>(); assert.Property <decimal>( "Price", BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty ).DeclaredIn <Home>(); assert.Constructor( BindingFlags.Public | BindingFlags.Instance, new Param <string>("address"), new Param <int>("yearBuilt"), new Param <decimal>("price") ); assert.NonConstructor( BindingFlags.Public | BindingFlags.Instance ); assert.Property <decimal>( "TotalCost", BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty ).DeclaredIn <Home>().Override(); assert.Method <decimal>( "GetRate", BindingFlags.Public | BindingFlags.Instance, new Param <int>("numberOfPeriods") ).DeclaredIn <SingleFamily>().Override(); assert.Method <string>( "ToString", BindingFlags.Public | BindingFlags.Instance ).DeclaredIn <SingleFamily>().Override(); }
public void TestAccountClass() { #if !DEBUG Assert.Multiple(() => { #endif var accountClass = new TypeAssert<Account>(); var balanceField = accountClass.Field<decimal>("_balance", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField | BindingFlags.SetField ).NonPublic(); var nameProperty = accountClass.Property<string>("Name", BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty); var balanceProperty = accountClass.Property<decimal>("Balance", BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty); balanceProperty.NotAutoImplemented(); var constructor = accountClass.Constructor( BindingFlags.Public | BindingFlags.Instance, new Param<string>("name"), new Param<decimal>("balance")); accountClass.Method("Deposit", BindingFlags.Public | BindingFlags.Instance, new Param<decimal>("depositAmount")); accountClass.Method("Withdraw", BindingFlags.Public | BindingFlags.Instance, new Param<decimal>("withdrawAmount") ); // testing dynamic account; account = accountClass.New("Darth Vader", -1.1m); Assert.AreEqual(0m, balanceField.GetValue(account)); Assert.AreEqual("Darth Vader", account.Name); Assert.AreEqual(0m, account.Balance); account = accountClass.New("Padme Amidala", 1.49m); Assert.AreEqual(1.49m, balanceField.GetValue(account)); Assert.AreEqual("Padme Amidala", account.Name); Assert.AreEqual(1.49m, account.Balance); #if !DEBUG }); #endif }
public void DescriptionFor_Attribute_Defined(Type attributeType, string attributePropertyName, object attributePropertyValue) { // Arrange & Act & Assert var propertyInfo = TypeAssert.PropertyHasAttribute <DescriptionTagHelper>("DescriptionFor", attributeType); AttributeAssert.ValidateProperty(propertyInfo, attributeType, attributePropertyName, attributePropertyValue); }
public void PropertyHasAttribute_Success() { // Arrange & Act & Assert var propertyInfo = TypeAssert.PropertyHasAttribute <FakeClassModel>(nameof(FakeClassModel.Value), typeof(FakePropertyAttribute)); Assert.NotNull(propertyInfo); Assert.IsAssignableFrom <PropertyInfo>(propertyInfo); }
public void FindsDetailDefinedOnClass() { IList <IEditable> editables = explorer.Find <IEditable>(typeof(Items.DefinitionTwoColumnPage)); Assert.AreEqual(2, editables.Count); TypeAssert.AreEqual(typeof(N2.Details.WithEditableTitleAttribute), editables[0]); TypeAssert.AreEqual(typeof(N2.Details.WithEditableNameAttribute), editables[1]); }
public void Attribute_Defined(Type attributeType, string firstAttributePropertyName, object firstAttributePropertyValue, string secondAttributePropertyName, object secondAttributePropertyValue) { // Arrange & Act & Assert var typeInfo = TypeAssert.HasAttribute <DescriptionTagHelper>(attributeType); AttributeAssert.ValidateProperty(typeInfo, attributeType, firstAttributePropertyName, firstAttributePropertyValue); AttributeAssert.ValidateProperty(typeInfo, attributeType, secondAttributePropertyName, secondAttributePropertyValue); }
public void MethodHasAttribute_WithNullMethodParametersTypes_Success() { // Arrange & Act & Assert var methodInfo = TypeAssert.MethodHasAttribute <FakeClassModel>(nameof(FakeClassModel.Get), null, typeof(FakeMethodAttribute)); Assert.NotNull(methodInfo); Assert.IsAssignableFrom <MethodInfo>(methodInfo); }
public void HasAttributeGeneric_Success() { // Arrange & Act & Assert var attribute = TypeAssert.HasAttribute <FakeClassModel, FakeClassAttribute>(); Assert.NotNull(attribute); Assert.IsAssignableFrom <FakeClassAttribute>(attribute); }
public void PropertyHasAttributeGeneric_Success() { // Arrange & Act & Assert var attribute = TypeAssert.PropertyHasAttribute <FakeClassModel, FakePropertyAttribute>(nameof(FakeClassModel.Value)); Assert.NotNull(attribute); Assert.IsAssignableFrom <FakePropertyAttribute>(attribute); }