|
1 | | -using NTestDataBuilder.Tests.Builders; |
| 1 | +using System.Linq; |
| 2 | +using FizzWare.NBuilder; |
| 3 | +using NTestDataBuilder.Tests.Builders; |
| 4 | +using NTestDataBuilder.Tests.Entities; |
2 | 5 | using NTestDataBuilder.Tests.TestHelpers; |
3 | 6 | using NUnit.Framework; |
4 | 7 |
|
@@ -61,5 +64,41 @@ public void GivenNoValueHasBeenSetForAnIntProperty_WhenRetrievingTheValueForThat |
61 | 64 |
|
62 | 65 | Assert.That(val1, Is.Not.EqualTo(val2)); |
63 | 66 | } |
| 67 | + |
| 68 | + [Test] |
| 69 | + public void GivenGlobalValueSupplierSet_WhenGeneratingList_UseTheSupplierForTheRelevantPropertyExceptWhereItsOverridden() |
| 70 | + { |
| 71 | + AnonymousValueFixture.GlobalValueSuppliers.Add(new YearValueSupplier()); |
| 72 | + var customers = CustomerBuilder.CreateListOfSize(5) |
| 73 | + .TheLast(1).With(b => b.WhoJoinedIn(1990)) |
| 74 | + .BuildList<Customer, CustomerBuilder>(); |
| 75 | + |
| 76 | + Assert.That(customers[0].YearJoined, Is.EqualTo(2000)); |
| 77 | + Assert.That(customers[1].YearJoined, Is.EqualTo(2001)); |
| 78 | + Assert.That(customers[2].YearJoined, Is.EqualTo(2002)); |
| 79 | + Assert.That(customers[3].YearJoined, Is.EqualTo(2003)); |
| 80 | + Assert.That(customers[4].YearJoined, Is.EqualTo(1990)); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + internal class YearValueSupplier : IAnonymousValueSupplier |
| 85 | + { |
| 86 | + private int _year; |
| 87 | + |
| 88 | + public YearValueSupplier() |
| 89 | + { |
| 90 | + _year = 2000; |
| 91 | + } |
| 92 | + |
| 93 | + public bool CanSupplyValue<TObject, TValue>(string propertyName) |
| 94 | + { |
| 95 | + return typeof(TValue) == typeof(int) |
| 96 | + && propertyName.ToLower().StartsWith("year"); |
| 97 | + } |
| 98 | + |
| 99 | + public TValue GenerateAnonymousValue<TObject, TValue>(AnonymousValueFixture any, string propertyName) |
| 100 | + { |
| 101 | + return (TValue)(object)_year++; |
| 102 | + } |
64 | 103 | } |
65 | 104 | } |
0 commit comments