public void VisitLocalVariableInfoElementsReturnsItself()
        {
            var sut = new Mock<MemberReferenceCollector> { CallBase = true }.Object;
            sut.ToMock().Setup(x => x.Visit(It.IsAny<LocalVariableInfoElement>())).Throws<InvalidOperationException>();
            var localVariableInfoElement = new Methods<ClassForCollectingReference>()
                .Select(x => x.ReturnMethod()).GetMethodBody()
                .LocalVariables.First().ToElement();

            var actual = sut.Visit(new[] { localVariableInfoElement });

            Assert.Same(sut, actual);
        }
        public void VisitAssemblyElementCallsBaseMethod()
        {
            var assembly = new DelegatingAssembly
            {
                OnGetTypes = () => new[] { typeof(object), typeof(int) }
            };
            var sut = new Mock<ReferenceCollector> { CallBase = true }.Object;
            var visitor1 = new Mock<ReferenceCollector>().Object;
            var visitor2 = new Mock<ReferenceCollector>().Object;
            sut.ToMock().Setup(x => x.Visit(typeof(object).ToElement())).Returns(visitor1);
            visitor1.ToMock().Setup(x => x.Visit(typeof(int).ToElement())).Returns(visitor2);

            var actual = sut.Visit(assembly.ToElement());

            Assert.Equal(visitor2, actual);
        }
 public void VisitThrowsOnNullResults()
 {
     ParserVisitor target = new Mock<ParserVisitor>().Object;
     Assert.Throws<ArgumentNullException>("result", () => target.Visit(null));
 }
 public void VisitThrowsOnNullResults()
 {
     ParserVisitor target = new Mock<ParserVisitor>().Object;
     Assert.ThrowsArgumentNull(() => target.Visit(null), "result");
 }
        public void VisitConstructorInfoElementCollectsCorrectAssembliesForMethodBody()
        {
            var visitor = new DelegatingReflectionVisitor<IEnumerable<Assembly>>();
            var sut = new Mock<ReferenceCollector> { CallBase = true }.Object;
            sut.ToMock().Setup(x => x.Visit(It.IsAny<ParameterInfoElement>())).Returns(visitor);
            sut.ToMock().Setup(x => x.Visit(It.IsAny<LocalVariableInfoElement>())).Returns(visitor);
            var expected = new[]
            {
                typeof(IDisposable).Assembly,
                GetType().Assembly,
                typeof(Fixture).Assembly
            };
            var constructorInfoElement = Constructors.Select(() => new ClassForCollectingReference(0)).ToElement();

            var actual = sut.Visit(constructorInfoElement);

            Assert.Equal(visitor, actual);
            Assert.Equal(expected.OrderBy(x => x.ToString()), sut.Value.OrderBy(x => x.ToString()));
        }
        public void VisitTypeElementCollectsCorrectAssemblies()
        {
            var type = typeof(List<ClassImplementingHierarchical>);
            var expected = new[]
            {
                typeof(ClassImplementingMultiple).Assembly,
                typeof(IDisposable).Assembly,
                typeof(IReflectionElement).Assembly,
                typeof(Fixture).Assembly
            };
            var visitor = new DelegatingReflectionVisitor<IEnumerable<Assembly>>();
            var sut = new Mock<ReferenceCollector> { CallBase = true }.Object;
            sut.ToMock().Setup(x => x.Visit(It.IsAny<FieldInfoElement>())).Returns(visitor);
            sut.ToMock().Setup(x => x.Visit(It.IsAny<ConstructorInfoElement>())).Returns(visitor);
            sut.ToMock().Setup(x => x.Visit(It.IsAny<PropertyInfoElement>())).Returns(visitor);
            sut.ToMock().Setup(x => x.Visit(It.IsAny<MethodInfoElement>())).Returns(visitor);
            sut.ToMock().Setup(x => x.Visit(It.IsAny<EventInfoElement>())).Returns(visitor);

            var actual = sut.Visit(type.ToElement());

            Assert.Equal(visitor, actual);
            Assert.Equal(expected.OrderBy(x => x.ToString()), sut.Value.OrderBy(x => x.ToString()));
        }
        public void VisitPropertyInfoElementsCallsBaseMethod()
        {
            var sut = new Mock<ReferenceCollector> { CallBase = true }.Object;
            var visitor = new DelegatingReflectionVisitor<IEnumerable<Assembly>>();
            sut.ToMock().Setup(x => x.Visit(It.IsAny<PropertyInfoElement>())).Returns(visitor);
            var propertyInfoElements = typeof(ClassWithMembers).GetProperties(Bindings)
                .Select(x => x.ToElement()).ToArray();

            var actual = sut.Visit(propertyInfoElements);

            Assert.Equal(visitor, actual);
        }
        public void VisitMethodInfoElementCollectsCorrectAssembliesForMethodCallInMethodBody()
        {
            var visitor = new DelegatingReflectionVisitor<IEnumerable<Assembly>>();
            var sut = new Mock<ReferenceCollector> { CallBase = true }.Object;
            sut.ToMock().Setup(x => x.Visit(It.IsAny<ParameterInfoElement>())).Returns(visitor);
            sut.ToMock().Setup(x => x.Visit(It.IsAny<LocalVariableInfoElement>())).Returns(visitor);
            var expected = new[]
            {
                typeof(IDisposable).Assembly,
                typeof(Enumerable).Assembly
            };
            var methodInfoElement = new Methods<ClassForCollectingReference>()
                .Select(x => x.MethodCallInMethodBody()).ToElement();

            sut.Visit(methodInfoElement);

            Assert.Equal(expected.OrderBy(x => x.ToString()), sut.Value.OrderBy(x => x.ToString()));
        }
        public void VisitMethodInfoElementCollectsAssembliesFromBaseMethod()
        {
            var references = new[] { typeof(ISet<>).Assembly };
            var sut = new Mock<MemberReferenceCollector> { CallBase = true }.Object;
            sut.ToMock().Setup(x => x.Visit(It.IsAny<ParameterInfoElement[]>())).Returns(sut);
            sut.ToMock().Setup(x => x.Visit(It.IsAny<LocalVariableInfoElement[]>()))
                .Returns(new MemberReferenceCollector(references));
            var expected = new[]
            {
                typeof(ClassImplementingMultiple).Assembly,
                typeof(IDisposable).Assembly,
                typeof(ISpecimenContext).Assembly,
                references[0]
            };
            var dummyMethodInfoElement = new Methods<ClassForCollectingReference>()
                .Select(x => x.ReturnMethod()).ToElement();

            var actual = sut.Visit(dummyMethodInfoElement);

            sut.ToMock().VerifyAll();
            Assert.Equal(
                expected.OrderBy(x => x.ToString()),
                actual.Value.OrderBy(x => x.ToString()));
        }
        public void VisitTypeElementCollectsCorrectAssemblies(
            Type type, Assembly[] expected)
        {
            var refereces = new[] { typeof(ISet<>).Assembly };
            var sut = new Mock<MemberReferenceCollector>(new object[] { refereces }) { CallBase = true }.Object;
            sut.ToMock().Setup(x => x.Visit(It.IsAny<FieldInfoElement>())).Throws<InvalidOperationException>();
            sut.ToMock().Setup(x => x.Visit(It.IsAny<ConstructorInfoElement>())).Throws<InvalidOperationException>();
            sut.ToMock().Setup(x => x.Visit(It.IsAny<PropertyInfoElement>())).Throws<InvalidOperationException>();
            sut.ToMock().Setup(x => x.Visit(It.IsAny<MethodInfoElement>())).Throws<InvalidOperationException>();
            sut.ToMock().Setup(x => x.Visit(It.IsAny<EventInfoElement>())).Throws<InvalidOperationException>();

            var actual = sut.Visit(type.ToElement());

            Assert.NotSame(sut, actual);
            Assert.Equal(
                refereces.Concat(expected).OrderBy(x => x.ToString()),
                actual.Value.OrderBy(x => x.ToString()));
        }