コード例 #1
0
        public void CreateWillCorrectlyRaiseSpecimenRequestedInCompositeRequest()
        {
            // Fixture setup
            object requestedObject = "The request";
            object subRequest      = "Some sub request";

            var spy      = new List <RequestTraceEventArgs>();
            var builder2 = new DelegatingSpecimenBuilder {
                OnCreate = (r, c) => r == requestedObject?c.Resolve(subRequest) : new NoSpecimen()
            };
            var builder3 = new DelegatingSpecimenBuilder {
                OnCreate = (r, c) => r == subRequest ? new object() : new NoSpecimen()
            };
            var compBuilder = new CompositeSpecimenBuilder(builder2, builder3);

            var sut = new DelegatingTracingBuilder(compBuilder);

            sut.SpecimenRequested += (sender, e) => spy.Add(e);

            var container = new SpecimenContext(sut);

            // Exercise system
            sut.Create(requestedObject, container);
            // Verify outcome
            Assert.Equal(2, spy.Count);
            Assert.Equal(subRequest, spy[1].Request);
            Assert.Equal(2, spy[1].Depth);
            // Teardown
        }
コード例 #2
0
        public void CreateOnMultipleThreadsConcurrentlyWorks()
        {
            // Fixture setup
            var tracer = new DelegatingTracingBuilder(new DelegatingSpecimenBuilder
            {
                OnCreate = (r, c) => 99
            });
            var sut          = new TerminatingWithPathSpecimenBuilder(tracer);
            var dummyContext = new DelegatingSpecimenContext()
            {
                OnResolve = (r) => 99
            };

            // Exercise system
            int[] specimens = Enumerable.Range(0, 1000)
                              .AsParallel()
                              .WithDegreeOfParallelism(8)
                              .WithExecutionMode(ParallelExecutionMode.ForceParallelism)
                              .Select(x => (int)sut.Create(typeof(int), dummyContext))
                              .ToArray();
            // Verify outcome
            Assert.Equal(1000, specimens.Length);
            Assert.True(specimens.All(s => s == 99));
            // Teardown
        }
コード例 #3
0
        public void CreateThrowsWithAutoMockingHintOnInterfaceOrAbcRequest(
            object request,
            string requestType)
        {
            var tracer =
                new DelegatingTracingBuilder(
                    new DelegatingSpecimenBuilder
            {
                OnCreate = (r, c) => new NoSpecimen(request)
            });
            var sut = new TerminatingWithPathSpecimenBuilder(tracer);

            var context = new SpecimenContext(sut);
            var e       = Assert.Throws <ObjectCreationException>(
                () => sut.Create(request, context));

            Assert.Contains(
                requestType,
                e.Message,
                StringComparison.CurrentCultureIgnoreCase);
            Assert.Contains(
                "auto-mocking",
                e.Message,
                StringComparison.CurrentCultureIgnoreCase);
            Assert.Contains(
                "request path",
                e.Message,
                StringComparison.CurrentCultureIgnoreCase);
        }
コード例 #4
0
        public void IgnoredTypeWillNotTrackCreatedSpecimen()
        {
            // Fixture setup
            object tracked         = null;
            object requestedObject = Guid.NewGuid();

            var decoratedBuilder = new DelegatingSpecimenBuilder {
                OnCreate = (r, c) => r
            };
            var sut = new DelegatingTracingBuilder(decoratedBuilder);

            var ignoredTypes = new List <Type> {
                typeof(Guid)
            };

            sut.Filter = new DelegatingRequestSpecification {
                OnIsSatisfiedBy = r => !ignoredTypes.Contains(r.GetType())
            };

            // Exercise system
            var    dummyContainer = new DelegatingSpecimenContext();
            object res            = sut.Create(requestedObject, dummyContainer);

            // Verify outcome
            Assert.Null(tracked);

            // Teardown
        }
コード例 #5
0
        public void CreateWillNotRaiseSpecimenCreatedForIgnoredType()
        {
            // Fixture setup
            var eventRaised = false;

            var request = Guid.NewGuid();
            var sut     = new DelegatingTracingBuilder();

            sut.SpecimenCreated += (sender, e) => eventRaised = true;

            var ignoredTypes = new List <Type> {
                typeof(Guid)
            };

            sut.Filter = new DelegatingRequestSpecification {
                OnIsSatisfiedBy = r => !ignoredTypes.Contains(r.GetType())
            };
            // Exercise system
            var dummyContainer = new DelegatingSpecimenContext();

            sut.Create(request, dummyContainer);
            // Verify outcome
            Assert.False(eventRaised, "Event raised");
            // Teardown
        }
コード例 #6
0
        public void DepthWillBeResetAfterDecoratedBuilderThrows()
        {
            // Fixture setup
            int createCallNumber = 0;
            int lastRequestDepth = 0;
            var firstRequest     = Guid.NewGuid();
            var secondRequest    = Guid.NewGuid();
            var dummyContainer   = new DelegatingSpecimenContext();
            var builder          = new DelegatingSpecimenBuilder {
                OnCreate = (r, c) =>
                {
                    createCallNumber++;
                    if (createCallNumber == 1)
                    {
                        throw new PrivateException();
                    }
                    return(c.Resolve(r));
                }
            };
            var sut = new DelegatingTracingBuilder(builder);

            sut.SpecimenRequested += (sender, e) => lastRequestDepth = e.Depth;

            // Exercise system and verify outcome
            Assert.Throws <PrivateException>(() => sut.Create(firstRequest, dummyContainer));
            Assert.Equal(1, lastRequestDepth);

            sut.Create(secondRequest, dummyContainer);
            Assert.Equal(1, lastRequestDepth);
        }
コード例 #7
0
        public void CreateWillTrackCreatedSpecimensComposite()
        {
            // Fixture setup
            object requestedObject = "The request";
            object subRequest      = "Some sub request";
            object createdSpecimen = Guid.NewGuid();
            var    spy             = new List <object>();
            var    builder2        = new DelegatingSpecimenBuilder {
                OnCreate = (r, c) => r == requestedObject?c.Resolve(subRequest) : new NoSpecimen()
            };
            var builder3 = new DelegatingSpecimenBuilder {
                OnCreate = (r, c) => r == subRequest ? createdSpecimen : new NoSpecimen()
            };
            var compBuilder = new CompositeSpecimenBuilder(builder2, builder3);

            var sut = new DelegatingTracingBuilder(compBuilder);

            sut.SpecimenCreated += (sender, e) => spy.Add(e.Specimen);
            var container = new SpecimenContext(sut);

            // Exercise system
            sut.Create(requestedObject, container);

            // Verify outcome
            Assert.Equal(2, spy.Count);
            Assert.Equal(createdSpecimen, spy[0]);
            Assert.Equal(createdSpecimen, spy[1]);

            // Teardown
        }
コード例 #8
0
 public void SutIsSpecimenBuilder()
 {
     // Fixture setup
     // Exercise system
     var sut = new DelegatingTracingBuilder();
     // Verify outcome
     Assert.IsAssignableFrom<ISpecimenBuilder>(sut);
     // Teardown
 }
コード例 #9
0
 public void InitializeWithNullWriterWillThrow()
 {
     // Fixture setup
     var dummyBuilder = new DelegatingTracingBuilder();
     // Exercise system and verify outcome
     Assert.Throws<ArgumentNullException>(() =>
         new TraceWriter(null, dummyBuilder));
     // Teardown
 }
コード例 #10
0
        public void InitializeWithNullWriterWillThrow()
        {
            // Fixture setup
            var dummyBuilder = new DelegatingTracingBuilder();

            // Exercise system and verify outcome
            Assert.Throws <ArgumentNullException>(() =>
                                                  new TraceWriter(null, dummyBuilder));
            // Teardown
        }
コード例 #11
0
        public void SutIsSpecimenBuilder()
        {
            // Fixture setup
            // Exercise system
            var sut = new DelegatingTracingBuilder();

            // Verify outcome
            Assert.IsAssignableFrom <ISpecimenBuilder>(sut);
            // Teardown
        }
コード例 #12
0
        public void SpecimenRequestsWillInitiallyBeEmpty()
        {
            // Fixture setup
            var tracer = new DelegatingTracingBuilder();
            var sut    = new TerminatingWithPathSpecimenBuilder(tracer);

            // Exercise system and verify outcome
            Assert.Empty(sut.SpecimenRequests);
            // Teardown
        }
コード例 #13
0
        public void AssignNullFilterWillThrow()
        {
            // Fixture setup
            var sut = new DelegatingTracingBuilder();

            // Exercise system and verify outcome
            Assert.Throws <ArgumentNullException>(() =>
                                                  sut.Filter = null);
            // Teardown
        }
 public void TracerIsCorrect()
 {
     // Fixture setup
     var expected = new DelegatingTracingBuilder();
     // Exercise system
     var sut = new TerminatingWithPathSpecimenBuilder(expected);
     // Verify outcome
     Assert.Same(expected, sut.Tracer);
     // Teardown
 }
コード例 #15
0
 public void SutIsSpecimenBuilderNode()
 {
     // Fixture setup
     var dummyWriter = TextWriter.Null;
     var dummyBuilder = new DelegatingTracingBuilder();
     // Exercise system
     var sut = new TraceWriter(dummyWriter, dummyBuilder);
     // Verify outcome
     Assert.IsAssignableFrom<ISpecimenBuilderNode>(sut);
     // Teardown
 }
コード例 #16
0
        public void TracerIsCorrect()
        {
            // Fixture setup
            var expected = new DelegatingTracingBuilder();
            // Exercise system
            var sut = new TerminatingWithPathSpecimenBuilder(expected);

            // Verify outcome
            Assert.Same(expected, sut.Tracer);
            // Teardown
        }
コード例 #17
0
 public void TracerIsCorrect()
 {
     // Fixture setup
     var dummyWriter = TextWriter.Null;
     var expectedTracer = new DelegatingTracingBuilder();
     var sut = new TraceWriter(dummyWriter, expectedTracer);
     // Exercise system
     TracingBuilder result = sut.Tracer;
     // Verify outcome
     Assert.Equal(expectedTracer, result);
     // Teardown
 }
コード例 #18
0
        public void SutIsSpecimenBuilderNode()
        {
            // Fixture setup
            var dummyWriter  = TextWriter.Null;
            var dummyBuilder = new DelegatingTracingBuilder();
            // Exercise system
            var sut = new TraceWriter(dummyWriter, dummyBuilder);

            // Verify outcome
            Assert.IsAssignableFrom <ISpecimenBuilderNode>(sut);
            // Teardown
        }
 public void SutYieldsCorrectSequence()
 {
     // Fixture setup
     var expected = new DelegatingSpecimenBuilder();
     var tracer = new DelegatingTracingBuilder(expected);
     var sut = new TerminatingWithPathSpecimenBuilder(tracer);
     // Exercise system
     // Verify outcome
     Assert.Equal(expected, sut.Single());
     Assert.Equal(expected, sut.Cast<object>().Single());
     // Teardown
 }
コード例 #20
0
        public void SutYieldsCorrectSequence()
        {
            // Fixture setup
            var expected = new DelegatingSpecimenBuilder();
            var tracer   = new DelegatingTracingBuilder(expected);
            var sut      = new TerminatingWithPathSpecimenBuilder(tracer);

            // Exercise system
            // Verify outcome
            Assert.Equal(expected, sut.Single());
            Assert.Equal(expected, sut.Cast <object>().Single());
            // Teardown
        }
コード例 #21
0
        public void TracerIsCorrect()
        {
            // Fixture setup
            var dummyWriter    = TextWriter.Null;
            var expectedTracer = new DelegatingTracingBuilder();
            var sut            = new TraceWriter(dummyWriter, expectedTracer);
            // Exercise system
            TracingBuilder result = sut.Tracer;

            // Verify outcome
            Assert.Equal(expectedTracer, result);
            // Teardown
        }
コード例 #22
0
 public void SutYieldsCorrectSequence()
 {
     // Fixture setup
     var dummyWriter = TextWriter.Null;
     var expected = new DelegatingSpecimenBuilder();
     var tracer = new DelegatingTracingBuilder(expected);
     var sut = new TraceWriter(dummyWriter, tracer);
     // Exercise system
     // Verify outcome
     Assert.Equal(expected, sut.Single());
     Assert.Equal(expected, ((System.Collections.IEnumerable)sut).Cast<object>().Single());
     // Teardown
 }
コード例 #23
0
        public void ComposeSingleItemReturnsCorrectResult()
        {
            // Fixture setup
            var tracer = new DelegatingTracingBuilder();
            var sut    = new TerminatingWithPathSpecimenBuilder(tracer);
            // Exercise system
            var expected = new DelegatingSpecimenBuilder();
            var actual   = sut.Compose(new[] { expected });
            // Verify outcome
            var tw = Assert.IsAssignableFrom <TerminatingWithPathSpecimenBuilder>(actual);

            Assert.Equal(expected, tw.Tracer.Builder);
            // Teardown
        }
コード例 #24
0
        public void SpecimenRequestsRaisedFromTracerAreRecordedCorrectly()
        {
            // Fixture setup
            var tracer        = new DelegatingTracingBuilder();
            var specimens     = new[] { new object(), new object(), new object() };
            var requestEvents = specimens.Select((o, i) => new RequestTraceEventArgs(o, i)).ToList();
            var sut           = new TerminatingWithPathSpecimenBuilder(tracer);

            // Exercise system
            requestEvents.ForEach(tracer.RaiseSpecimenRequested);
            // Verify outcome
            Assert.True(specimens.SequenceEqual(sut.SpecimenRequests));
            // Teardown
        }
コード例 #25
0
        public void SutYieldsCorrectSequence()
        {
            // Fixture setup
            var dummyWriter = TextWriter.Null;
            var expected    = new DelegatingSpecimenBuilder();
            var tracer      = new DelegatingTracingBuilder(expected);
            var sut         = new TraceWriter(dummyWriter, tracer);

            // Exercise system
            // Verify outcome
            Assert.Equal(expected, sut.Single());
            Assert.Equal(expected, ((System.Collections.IEnumerable)sut).Cast <object>().Single());
            // Teardown
        }
コード例 #26
0
 public void CreateWillCorrectlyRaiseSpecimenRequested()
 {
     // Fixture setup
     var verified = false;
     var request = new object();
     var sut = new DelegatingTracingBuilder();
     sut.SpecimenRequested += (sender, e) => verified = e.Request == request && e.Depth == 1;
     // Exercise system
     var dummyContainer = new DelegatingSpecimenContext();
     sut.Create(request, dummyContainer);
     // Verify outcome
     Assert.True(verified, "Event raised");
     // Teardown
 }
コード例 #27
0
        public void FilterIsProperWritableProperty()
        {
            // Fixture setup
            var sut = new DelegatingTracingBuilder();
            IRequestSpecification expectedFilter = new DelegatingRequestSpecification();

            // Exercise system
            sut.Filter = expectedFilter;
            IRequestSpecification result = sut.Filter;

            // Verify outcome
            Assert.Equal(expectedFilter, result);
            // Teardown
        }
コード例 #28
0
        public void CreateWillPassThroughToDecoratedBuilder()
        {
            // Fixture setup
            object expectedSpecimen = Guid.NewGuid();
            var decoratedBuilder = new DelegatingSpecimenBuilder { OnCreate = (r, c) => r };
            var sut = new DelegatingTracingBuilder(decoratedBuilder);

            // Exercise system
            var dummyContainer = new DelegatingSpecimenContext();
            object result = sut.Create(expectedSpecimen, dummyContainer);

            // Verify outcome
            Assert.Equal(expectedSpecimen, result);

            // Teardown
        }
コード例 #29
0
        public void CreateWillCorrectlyRaiseSpecimenRequested()
        {
            // Fixture setup
            var verified = false;
            var request  = new object();
            var sut      = new DelegatingTracingBuilder();

            sut.SpecimenRequested += (sender, e) => verified = e.Request == request && e.Depth == 1;
            // Exercise system
            var dummyContainer = new DelegatingSpecimenContext();

            sut.Create(request, dummyContainer);
            // Verify outcome
            Assert.True(verified, "Event raised");
            // Teardown
        }
コード例 #30
0
        public void CreateWillTrackCreatedSpecimen()
        {
            // Fixture setup
            object tracked         = null;
            object createdSpecimen = Guid.NewGuid();
            var    container       = new DelegatingSpecimenContext {
                OnResolve = r => createdSpecimen
            };
            var sut = new DelegatingTracingBuilder();

            // Exercise system
            object res = sut.Create(new object(), container);

            // Verify outcome
            Assert.Equal(res, tracked);

            // Teardown
        }
コード例 #31
0
        public void SpecimenRequestsAreEmptyWhenAllThatWereRequestedAreCreated()
        {
            // Fixture setup
            var tracer        = new DelegatingTracingBuilder();
            var specimens     = new[] { new object(), new object(), new object() };
            var requestEvents = specimens.Select(
                (o, i) => new RequestTraceEventArgs(o, i)).ToList();
            var createdEvents = specimens.Reverse().Select(
                (o, i) => new SpecimenCreatedEventArgs(o, null, i)).ToList();
            var sut = new TerminatingWithPathSpecimenBuilder(tracer);

            // Exercise system
            requestEvents.ForEach(tracer.RaiseSpecimenRequested);
            createdEvents.ForEach(tracer.RaiseSpecimenCreated);
            // Verify outcome
            Assert.Empty(sut.SpecimenRequests);
            // Teardown
        }
コード例 #32
0
        public void CreateWillNotTrackFilteredRequest()
        {
            // Fixture setup
            object tracked         = null;
            object requestedObject = new object();
            var    sut             = new DelegatingTracingBuilder();

            sut.Filter = new DelegatingRequestSpecification {
                OnIsSatisfiedBy = r => false
            };
            // Exercise system
            var dummyContainer = new DelegatingSpecimenContext();

            sut.Create(requestedObject, dummyContainer);
            // Verify outcome
            Assert.Null(tracked);
            // Teardown
        }
コード例 #33
0
        public void CreateWillPassThroughToDecoratedBuilder()
        {
            // Fixture setup
            object expectedSpecimen = Guid.NewGuid();
            var    decoratedBuilder = new DelegatingSpecimenBuilder {
                OnCreate = (r, c) => r
            };
            var sut = new DelegatingTracingBuilder(decoratedBuilder);

            // Exercise system
            var    dummyContainer = new DelegatingSpecimenContext();
            object result         = sut.Create(expectedSpecimen, dummyContainer);

            // Verify outcome
            Assert.Equal(expectedSpecimen, result);

            // Teardown
        }
コード例 #34
0
        public void SpecimenRequestedWillWriteCorrectMessageToWriter()
        {
            // Fixture setup
            var writer  = new StringWriter();
            var builder = new DelegatingTracingBuilder();

            var depth   = new Random().Next(1, 10);
            var request = new object();

            var sut = new TraceWriter(writer, builder);

            // Exercise system
            builder.RaiseSpecimenRequested(new RequestTraceEventArgs(request, depth));
            // Verify outcome
            var expected = new string(' ', depth * 2) + "Requested: " + request + Environment.NewLine;

            Assert.Equal(expected, writer.ToString());
            // Teardown
        }
 public void ComposeReturnsCorrectResult()
 {
     // Fixture setup
     var tracer = new DelegatingTracingBuilder();
     var sut = new TerminatingWithPathSpecimenBuilder(tracer);
     // Exercise system
     var expectedBuilders = new[]
     {
         new DelegatingSpecimenBuilder(),
         new DelegatingSpecimenBuilder(),
         new DelegatingSpecimenBuilder()
     };
     var actual = sut.Compose(expectedBuilders);
     // Verify outcome
     var tw = Assert.IsAssignableFrom<TerminatingWithPathSpecimenBuilder>(actual);
     var composite = Assert.IsAssignableFrom<CompositeSpecimenBuilder>(tw.Tracer.Builder);
     Assert.True(expectedBuilders.SequenceEqual(composite));
     // Teardown
 }
コード例 #36
0
        public void CreateThrowsWhenNoSpecimenIsReturnedFromTheDecoratedGraph()
        {
            // Fixture setup
            var requests = new[] { new object(), new object(), new object() };
            var tracer   = new DelegatingTracingBuilder(new DelegatingSpecimenBuilder
            {
                // Returns NoSpecimen only on the last specimen request
                OnCreate = (r, c) => (r == requests[2]) ? new NoSpecimen() : new object(),
            });

            var sut       = new TerminatingWithPathSpecimenBuilder(tracer);
            var container = new SpecimenContext(sut);

            // Exercise system and verify outcome
            Assert.DoesNotThrow(() => sut.Create(requests[0], container));
            Assert.DoesNotThrow(() => sut.Create(requests[1], container));
            Assert.Throws <ObjectCreationException>(() => sut.Create(requests[2], container));
            // Teardown
        }
コード例 #37
0
        public void ComposeReturnsCorrectResult()
        {
            // Fixture setup
            var tracer = new DelegatingTracingBuilder();
            var sut    = new TerminatingWithPathSpecimenBuilder(tracer);
            // Exercise system
            var expectedBuilders = new[]
            {
                new DelegatingSpecimenBuilder(),
                new DelegatingSpecimenBuilder(),
                new DelegatingSpecimenBuilder()
            };
            var actual = sut.Compose(expectedBuilders);
            // Verify outcome
            var tw        = Assert.IsAssignableFrom <TerminatingWithPathSpecimenBuilder>(actual);
            var composite = Assert.IsAssignableFrom <CompositeSpecimenBuilder>(tw.Tracer.Builder);

            Assert.True(expectedBuilders.SequenceEqual(composite));
            // Teardown
        }
コード例 #38
0
        public void CreateWillNotRaiseSpecimeCreatedForFilteredRequest()
        {
            // Fixture setup
            var eventRaised = false;

            var request = new object();
            var sut     = new DelegatingTracingBuilder();

            sut.SpecimenCreated += (sender, e) => eventRaised = true;
            sut.Filter           = new DelegatingRequestSpecification {
                OnIsSatisfiedBy = r => false
            };
            // Exercise system
            var dummyContainer = new DelegatingSpecimenContext();

            sut.Create(request, dummyContainer);
            // Verify outcome
            Assert.False(eventRaised, "Event raised");
            // Teardown
        }
コード例 #39
0
        public void SpecimenRequestsAreEmptyAfterThrowing()
        {
            // Fixture setup
            var requests     = new[] { new object(), new object(), new object() };
            var requestQueue = new Queue <object>(requests);
            var firstRequest = requestQueue.Dequeue();
            var tracer       = new DelegatingTracingBuilder(new DelegatingSpecimenBuilder
            {
                OnCreate = (r, c) => requestQueue.Count > 0
                    ? c.Resolve(requestQueue.Dequeue())
                    : new NoSpecimen()
            });
            var sut       = new TerminatingWithPathSpecimenBuilder(tracer);
            var container = new SpecimenContext(sut);

            // Exercise system and verify outcome
            Assert.Throws <ObjectCreationException>(() =>
                                                    sut.Create(firstRequest, container));

            Assert.Empty(sut.SpecimenRequests);
        }
        public void CreateThrowsWithAutoMockingHintOnInterfaceOrAbcRequest(
            object request,
            string requestType)
        {
            var tracer =
                new DelegatingTracingBuilder(
                    new DelegatingSpecimenBuilder
                    {
                        OnCreate = (r, c) => new NoSpecimen(request)
                    });
            var sut = new TerminatingWithPathSpecimenBuilder(tracer);

            var context = new SpecimenContext(sut);
            var e = Assert.Throws<ObjectCreationException>(
                () => sut.Create(request, context));

            Assert.Contains(
                requestType,
                e.Message,
                StringComparison.CurrentCultureIgnoreCase);
            Assert.Contains(
                "auto-mocking",
                e.Message,
                StringComparison.CurrentCultureIgnoreCase);
            Assert.Contains(
                "request path",
                e.Message,
                StringComparison.CurrentCultureIgnoreCase);
        }
 public void CreateOnMultipleThreadsConcurrentlyWorks()
 {
     // Fixture setup
     var tracer = new DelegatingTracingBuilder(new DelegatingSpecimenBuilder
     {
         OnCreate = (r, c) => 99
     });
     var sut = new TerminatingWithPathSpecimenBuilder(tracer);
     var dummyContext = new DelegatingSpecimenContext()
     {
         OnResolve = (r) => 99
     };
     // Exercise system
     int[] specimens = Enumerable.Range(0, 1000)
         .AsParallel()
             .WithDegreeOfParallelism(8)
             .WithExecutionMode(ParallelExecutionMode.ForceParallelism)
         .Select(x => (int)sut.Create(typeof(int), dummyContext))
         .ToArray();
     // Verify outcome
     Assert.Equal(1000, specimens.Length);
     Assert.True(specimens.All(s => s == 99));
     // Teardown
 }
コード例 #42
0
 public void ComposeSingleItemReturnsCorrectResult()
 {
     // Fixture setup
     var writer = TextWriter.Null;
     var tracer = new DelegatingTracingBuilder();
     var sut = new TraceWriter(writer, tracer);
     // Exercise system
     var expected = new DelegatingSpecimenBuilder();
     var actual = sut.Compose(new[] { expected });
     // Verify outcome
     var tw = Assert.IsAssignableFrom<TraceWriter>(actual);
     Assert.Equal(expected, tw.Tracer.Builder);
     // Teardown
 }
コード例 #43
0
        public void IgnoredTypeWillNotTrackCreatedSpecimen()
        {
            // Fixture setup
            object tracked = null;
            object requestedObject = Guid.NewGuid();

            var decoratedBuilder = new DelegatingSpecimenBuilder { OnCreate = (r, c) => r };
            var sut = new DelegatingTracingBuilder(decoratedBuilder);

            var ignoredTypes = new List<Type> { typeof(Guid) };
            sut.Filter = new DelegatingRequestSpecification { OnIsSatisfiedBy = r => !ignoredTypes.Contains(r.GetType()) };

            // Exercise system
            var dummyContainer = new DelegatingSpecimenContext();
            object res = sut.Create(requestedObject, dummyContainer);

            // Verify outcome
            Assert.Null(tracked);

            // Teardown
        }
 public void SpecimenRequestsWillInitiallyBeEmpty()
 {
     // Fixture setup
     var tracer = new DelegatingTracingBuilder();
     var sut = new TerminatingWithPathSpecimenBuilder(tracer);
     // Exercise system and verify outcome
     Assert.Empty(sut.SpecimenRequests);
     // Teardown
 }
 public void SpecimenRequestsAreEmptyWhenAllThatWereRequestedAreCreated()
 {
     // Fixture setup
     var tracer = new DelegatingTracingBuilder();
     var specimens = new[] { new object(), new object(), new object() };
     var requestEvents = specimens.Select(
         (o, i) => new RequestTraceEventArgs(o, i)).ToList();
     var createdEvents = specimens.Reverse().Select(
         (o, i) => new SpecimenCreatedEventArgs(o, null, i)).ToList();
     var sut = new TerminatingWithPathSpecimenBuilder(tracer);
     // Exercise system
     requestEvents.ForEach(tracer.RaiseSpecimenRequested);
     createdEvents.ForEach(tracer.RaiseSpecimenCreated);
     // Verify outcome
     Assert.Empty(sut.SpecimenRequests);
     // Teardown
 }
コード例 #46
0
 public void AssignNullFilterWillThrow()
 {
     // Fixture setup
     var sut = new DelegatingTracingBuilder();
     // Exercise system and verify outcome
     Assert.Throws<ArgumentNullException>(() =>
         sut.Filter = null);
     // Teardown
 }
コード例 #47
0
 public void FilterIsProperWritableProperty()
 {
     // Fixture setup
     var sut = new DelegatingTracingBuilder();
     IRequestSpecification expectedFilter = new DelegatingRequestSpecification();
     // Exercise system
     sut.Filter = expectedFilter;
     IRequestSpecification result = sut.Filter;
     // Verify outcome
     Assert.Equal(expectedFilter, result);
     // Teardown
 }
コード例 #48
0
        public void CreateWillNotRaiseSpecimenCreatedForFilteredRequest()
        {
            // Fixture setup
            var eventRaised = false;

            var request = new object();
            var sut = new DelegatingTracingBuilder();
            sut.SpecimenCreated += (sender, e) => eventRaised = true;
            sut.Filter = new DelegatingRequestSpecification { OnIsSatisfiedBy = r => false };
            // Exercise system
            var dummyContainer = new DelegatingSpecimenContext();
            sut.Create(request, dummyContainer);
            // Verify outcome
            Assert.False(eventRaised, "Event raised");
            // Teardown
        }
コード例 #49
0
 public void CreateWillNotTrackFilteredSpecimen()
 {
     // Fixture setup
     object tracked = null;
     object requestedObject = new object();
     var decoratedBuilder = new DelegatingSpecimenBuilder { OnCreate = (r, c) => r };
     var sut = new DelegatingTracingBuilder(decoratedBuilder);
     sut.Filter = new DelegatingRequestSpecification { OnIsSatisfiedBy = r => false };
     // Exercise system
     var dummyContainer = new DelegatingSpecimenContext();
     sut.Create(requestedObject, dummyContainer);
     // Verify outcome
     Assert.Null(tracked);
     // Teardown
 }
コード例 #50
0
        public void DepthWillBeResetAfterDecoratedBuilderThrows()
        {
            // Fixture setup
            int createCallNumber = 0;
            int lastRequestDepth = 0;
            var firstRequest = Guid.NewGuid();
            var secondRequest = Guid.NewGuid();
            var dummyContainer = new DelegatingSpecimenContext();
            var builder = new DelegatingSpecimenBuilder { OnCreate = (r, c) =>
            {
                createCallNumber++;
                if (createCallNumber == 1) throw new PrivateException();
                return c.Resolve(r);
            }};
            var sut = new DelegatingTracingBuilder(builder);
            sut.SpecimenRequested += (sender, e) => lastRequestDepth = e.Depth;

            // Exercise system and verify outcome
            Assert.Throws<PrivateException>(() => sut.Create(firstRequest, dummyContainer));
            Assert.Equal(1, lastRequestDepth);

            sut.Create(secondRequest, dummyContainer);
            Assert.Equal(1, lastRequestDepth);
        }
        public void CreateThrowsWhenNoSpecimenIsReturnedFromTheDecoratedGraph()
        {
            // Fixture setup
            var requests = new[] {new object(), new object(), new object()};
            var tracer = new DelegatingTracingBuilder(new DelegatingSpecimenBuilder
            {
                // Returns NoSpecimen only on the last specimen request
                OnCreate = (r, c) => (r == requests[2]) ? new NoSpecimen() : new object(),
            });

            var sut = new TerminatingWithPathSpecimenBuilder(tracer);
            var container = new SpecimenContext(sut);
            // Exercise system and verify outcome
            Assert.DoesNotThrow(() => sut.Create(requests[0], container));
            Assert.DoesNotThrow(() => sut.Create(requests[1], container));
            Assert.Throws<ObjectCreationException>(() => sut.Create(requests[2], container));
            // Teardown
        }
コード例 #52
0
        public void CreateWillTrackCreatedSpecimensComposite()
        {
            // Fixture setup
            object requestedObject = "The request";
            object subRequest = "Some sub request";
            object createdSpecimen = Guid.NewGuid();
            var spy = new List<object>();
            var builder2 = new DelegatingSpecimenBuilder { OnCreate = (r, c) => r == requestedObject ? c.Resolve(subRequest) : new NoSpecimen() };
            var builder3 = new DelegatingSpecimenBuilder { OnCreate = (r, c) => r == subRequest ? createdSpecimen : new NoSpecimen() };
            var compBuilder = new CompositeSpecimenBuilder(builder2, builder3);

            var sut = new DelegatingTracingBuilder(compBuilder);
            sut.SpecimenCreated += (sender, e) => spy.Add(e.Specimen);
            var container = new SpecimenContext(sut);
            // Exercise system
            sut.Create(requestedObject, container);

            // Verify outcome
            Assert.Equal(2, spy.Count);
            Assert.Equal(createdSpecimen, spy[0]);
            Assert.Equal(createdSpecimen, spy[1]);

            // Teardown
        }
        public void SpecimenRequestsAreEmptyAfterThrowing()
        {
            // Fixture setup
            var requests = new[] {new object(), new object(), new object() };
            var requestQueue = new Queue<object>(requests);
            var firstRequest = requestQueue.Dequeue();
            var tracer = new DelegatingTracingBuilder(new DelegatingSpecimenBuilder
            {
                OnCreate = (r, c) => requestQueue.Count > 0
                    ? c.Resolve(requestQueue.Dequeue())
                    : new NoSpecimen()
            });
            var sut = new TerminatingWithPathSpecimenBuilder(tracer);
            var container = new SpecimenContext(sut);

            // Exercise system and verify outcome
            Assert.Throws<ObjectCreationException>(() =>
                sut.Create(firstRequest, container));

            Assert.Empty(sut.SpecimenRequests);
        }
コード例 #54
0
        public void CreateWillTrackCreatedSpecimen()
        {
            // Fixture setup
            object tracked = null;
            object createdSpecimen = Guid.NewGuid();
            var container = new DelegatingSpecimenContext { OnResolve = r => createdSpecimen };
            var sut = new DelegatingTracingBuilder();

            // Exercise system
            object res = sut.Create(new object(), container);

            // Verify outcome
            Assert.Equal(res, tracked);

            // Teardown
        }
 public void SpecimenRequestsRaisedFromTracerAreRecordedCorrectly()
 {
     // Fixture setup
     var tracer = new DelegatingTracingBuilder();
     var specimens = new[] { new object(), new object(), new object() };
     var requestEvents = specimens.Select((o, i) => new RequestTraceEventArgs(o, i)).ToList();
     var sut = new TerminatingWithPathSpecimenBuilder(tracer);
     // Exercise system
     requestEvents.ForEach(tracer.RaiseSpecimenRequested);
     // Verify outcome
     Assert.True(specimens.SequenceEqual(sut.SpecimenRequests));
     // Teardown
 }
コード例 #56
0
        public void SpecimenCreatedWillWriteCorrectMessageToWriter()
        {
            // Fixture setup
            var writer = new StringWriter();
            var builder = new DelegatingTracingBuilder();

            var depth = new Random().Next(1, 10);
            var specimen = new object();

            var sut = new TraceWriter(writer, builder);
            // Exercise system
            var dummyRequest = new object();
            builder.RaiseSpecimenCreated(new SpecimenCreatedEventArgs(dummyRequest, specimen, depth));
            // Verify outcome
            var expected = new string(' ', depth * 2) + "Created: " + specimen + Environment.NewLine;
            Assert.Equal(expected, writer.ToString());
            // Teardown
        }
コード例 #57
0
        public void CreateWillNotRaiseSpecimenCreatedForIgnoredType()
        {
            // Fixture setup
            var eventRaised = false;

            var request = Guid.NewGuid();
            var sut = new DelegatingTracingBuilder();
            sut.SpecimenCreated += (sender, e) => eventRaised = true;

            var ignoredTypes = new List<Type> { typeof(Guid) };
            sut.Filter = new DelegatingRequestSpecification { OnIsSatisfiedBy = r => !ignoredTypes.Contains(r.GetType()) };
            // Exercise system
            var dummyContainer = new DelegatingSpecimenContext();
            sut.Create(request, dummyContainer);
            // Verify outcome
            Assert.False(eventRaised, "Event raised");
            // Teardown
        }
コード例 #58
0
        public void CreateWillCorrectlyRaiseSpecimenRequestedInCompositeRequest()
        {
            // Fixture setup
            object requestedObject = "The request";
            object subRequest = "Some sub request";

            var spy = new List<RequestTraceEventArgs>();
            var builder2 = new DelegatingSpecimenBuilder { OnCreate = (r, c) => r == requestedObject ? c.Resolve(subRequest) : new NoSpecimen() };
            var builder3 = new DelegatingSpecimenBuilder { OnCreate = (r, c) => r == subRequest ? new object() : new NoSpecimen() };
            var compBuilder = new CompositeSpecimenBuilder(builder2, builder3);

            var sut = new DelegatingTracingBuilder(compBuilder);
            sut.SpecimenRequested += (sender, e) => spy.Add(e);

            var container = new SpecimenContext(sut);
            // Exercise system
            sut.Create(requestedObject, container);
            // Verify outcome
            Assert.Equal(2, spy.Count);
            Assert.Equal(subRequest, spy[1].Request);
            Assert.Equal(2, spy[1].Depth);
            // Teardown
        }