|
| 1 | +using System; |
| 2 | +using Shouldly; |
| 3 | +using Xunit; |
| 4 | + |
| 5 | +namespace NTestDataBuilder.Tests |
| 6 | +{ |
| 7 | + public class ChildBuilderTests |
| 8 | + { |
| 9 | + [Fact] |
| 10 | + public void WhenNotUsingChildBuilder_ThenTheAnonymousValueFixtureIsNotReused() |
| 11 | + { |
| 12 | + var parent = new ParentBuilder().Build(); |
| 13 | + |
| 14 | + parent.ParentEnum.ShouldBe(parent.Child.ChildEnum); |
| 15 | + } |
| 16 | + |
| 17 | + [Fact] |
| 18 | + public void WhenUsingChildBuilder_ThenTheAnonymousValueFixtureIsReused() |
| 19 | + { |
| 20 | + var parent = new ParentBuilder().WithChildBuilder().Build(); |
| 21 | + |
| 22 | + parent.ParentEnum.ShouldNotBe(parent.Child.ChildEnum); |
| 23 | + } |
| 24 | + |
| 25 | + [Fact] |
| 26 | + public void WhenUsingChildBuilderIncludingModifier_ThenTheModifierGetsApplied() |
| 27 | + { |
| 28 | + const int number = 2; |
| 29 | + var parent = new ParentBuilder() |
| 30 | + .WithChildBuilder(b => b.WithANumber(number)) |
| 31 | + .Build(); |
| 32 | + |
| 33 | + parent.Child.Number.ShouldBe(number); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + public enum AnEnum |
| 38 | + { |
| 39 | + One, |
| 40 | + Two |
| 41 | + } |
| 42 | + |
| 43 | + public class ParentObject |
| 44 | + { |
| 45 | + public ParentObject(AnEnum parentEnum, ChildObject child) |
| 46 | + { |
| 47 | + ParentEnum = parentEnum; |
| 48 | + Child = child; |
| 49 | + } |
| 50 | + |
| 51 | + public AnEnum ParentEnum { get; private set; } |
| 52 | + public ChildObject Child { get; private set; } |
| 53 | + } |
| 54 | + |
| 55 | + public class ChildObject |
| 56 | + { |
| 57 | + public ChildObject(AnEnum childEnum, int number) |
| 58 | + { |
| 59 | + ChildEnum = childEnum; |
| 60 | + Number = number; |
| 61 | + } |
| 62 | + |
| 63 | + public AnEnum ChildEnum { get; private set; } |
| 64 | + public int Number { get; private set; } |
| 65 | + } |
| 66 | + |
| 67 | + public class ParentBuilder : TestDataBuilder<ParentObject, ParentBuilder> |
| 68 | + { |
| 69 | + public ParentBuilder() |
| 70 | + { |
| 71 | + Set(x => x.Child, new ChildBuilder().Build()); |
| 72 | + } |
| 73 | + |
| 74 | + public ParentBuilder WithChildBuilder(Func<ChildBuilder, ChildBuilder> modifier = null) |
| 75 | + { |
| 76 | + return Set(x => x.Child, GetChildBuilder<ChildObject, ChildBuilder>(modifier).Build()); |
| 77 | + } |
| 78 | + |
| 79 | + protected override ParentObject BuildObject() |
| 80 | + { |
| 81 | + return new ParentObject(Get(x => x.ParentEnum), Get(x => x.Child)); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + public class ChildBuilder : TestDataBuilder<ChildObject, ChildBuilder> |
| 86 | + { |
| 87 | + public ChildBuilder() |
| 88 | + { |
| 89 | + WithANumber(1); |
| 90 | + } |
| 91 | + |
| 92 | + public ChildBuilder WithANumber(int number) |
| 93 | + { |
| 94 | + return Set(x => x.Number, number); |
| 95 | + } |
| 96 | + |
| 97 | + protected override ChildObject BuildObject() |
| 98 | + { |
| 99 | + return new ChildObject(Get(x => x.ChildEnum), Get(x => x.Number)); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments