Пример #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
        }
        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
        }
        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
        }
        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
 }
        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
 }
        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
 }
        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
        }
        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
        }
        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
 }
        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
        }
        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
        }
        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
        }