예제 #1
0
        public void ReachAllInnerClasses()
        {
            //  #   Arrange.
            var     pr  = new PseudoRandom(nameof(ReachAllInnerClasses));
            var     obj = new MyPublicClass();
            dynamic sut = new ReachIn(obj);
            int     res;
            int     val;

            //  #   Act and Assert.
            val = pr.Int();

            //  We don't support finding privates in inherited classes
            //  so here we look for a field overloaded in the child class
            //  just as a sanity check.
            sut._myInnerPrivateClass._myPrivateField = val;
            res = sut._myInnerPrivateClass._myPrivateField;
            res.Should().Be(val);

            //  Keep looking for a visible (e.g. internal) field that comes
            //  from the base class.
            sut._myInnerPrivateClass._myInternalField = val;
            res = sut._myInnerPrivateClass._myInternalField;
            res.Should().Be(val);
            sut._myInnerInternalClass._myInternalField = val;
            res = sut._myInnerInternalClass._myInternalField;
            res.Should().Be(val);
            sut._myInnerProtectedClass._myInternalField = val;
            res = sut._myInnerProtectedClass._myInternalField;
            res.Should().Be(val);
            sut._myInnerPublicClass._myInternalField = val;
            res = sut._myInnerPublicClass._myInternalField;
            res.Should().Be(val);
        }
예제 #2
0
        public void ThrowExceptionOverloadedMember()
        {
            //  #   Arrange.
            dynamic sut = new ReachIn(new MyOverloadedMethodClass());

            //  #   Act and Assert.
            Assert.Throws <Exception>(
                () => sut.MyMethod);
        }
예제 #3
0
        public void ThrowExceptionIfStaticFieldOrPropetyNotFound()
        {
            //  #   Arrange.
            var     pr  = new PseudoRandom(nameof(ThrowExceptionIfStaticFieldOrPropetyNotFound));
            dynamic sut = new ReachIn(typeof(MyBaseClass));

            //  #   Act and Assert.
            //  A setter does not exist.
            Assert.Throws <Exception>(
                () => { sut.ThisPropertyOrFieldIsNotFound = pr.Int(); });

            //  A getter does not exist.
            Assert.Throws <Exception>(
                () => { var res = sut.ThisPropertyOrFieldIsNotFound; });
        }
예제 #4
0
        public void ThrowExceptionIfMethodNotFound()
        {
            //  #   Arrange.
            var     pr  = new PseudoRandom(nameof(ThrowExceptionIfMethodNotFound));
            var     obj = new MyBaseClass();
            dynamic sut = new ReachIn(obj);

            //  #   Act and Assert.
            //  The method does not exist.
            Assert.Throws <Exception>(
                () => { sut.ThisMethodIsNotFound(pr.Int()); });

            //  There is a property by the same name.
            Assert.Throws <RuntimeBinderException>(
                () => { sut.MyPrivateProperty(pr.Int()); });
        }
예제 #5
0
        public void ThrowExceptionIfStaticMethodNotFound()
        {
            //  #   Arrange.
            var     pr  = new PseudoRandom(nameof(ThrowExceptionIfStaticMethodNotFound));
            dynamic sut = new ReachIn(typeof(MyBaseClass));

            //  #   Act and Assert.
            //  The method does not exist.
            Assert.Throws <Exception>(
                () => { sut.ThisMethodIsNotFound(pr.Int()); });

            //  There is a property by the same name.
            //  Trying to call it fails.
            Assert.Throws <RuntimeBinderException>(
                () => { sut.MyStaticPrivateProperty(pr.Int()); });
        }
예제 #6
0
        public void ReachAllStaticFieldsAndProperties()
        {
            //  #   Arrange.
            var     pr = new PseudoRandom(nameof(ReachAllStaticFieldsAndProperties));
            dynamic sut = new ReachIn(typeof(MyBaseClass));
            int     value, res;

            //  #   Act and Assert.

            //  ##  Fields.
            value = pr.Int();
            sut._myStaticPrivateField = value;
            res = sut._myStaticPrivateField;
            res.Should().Be(value);
            value = pr.Int();
            sut._myStaticInternalField = value;
            res = sut._myStaticInternalField;
            res.Should().Be(value);
            value = pr.Int();
            sut._myStaticProtectedField = value;
            res = sut._myStaticProtectedField;
            res.Should().Be(value);
            value = pr.Int();
            sut._myStaticPublicField = value;
            res = sut._myStaticPublicField;
            res.Should().Be(value);

            //  ##   Properties.
            value = pr.Int();
            sut.MyStaticPrivateProperty = value;
            res = sut.MyStaticPrivateProperty;
            res.Should().Be(value);
            value = pr.Int();
            sut.MyStaticInternalProperty = value;
            res = sut.MyStaticInternalProperty;
            res.Should().Be(value);
            value = pr.Int();
            sut.MyStaticProtectedProperty = value;
            res = sut.MyStaticProtectedProperty;
            res.Should().Be(value);
            value = pr.Int();
            sut.MyStaticPublicProperty = value;
            res = sut.MyStaticPublicProperty;
            res.Should().Be(value);
        }
예제 #7
0
        public void ReachAllStaticMethods()
        {
            //  # Arrange.
            var     pr  = new PseudoRandom(nameof(ReachAllStaticMethods));
            dynamic sut = new ReachIn(typeof(MyBaseClass));
            var     val = pr.Int();

            //  #   Act and Assert.
            // We have a problem here with FluentAssertions where it, runtime, throws an exception even though the parameters are equal.
            //sut.MyStaticPrivateMethod(val).Should().Be(val + 1);
            //sut.MyStaticInternalMethod(val).Should().Be(val+1);
            //sut.MyStaticProtectedMethod(val).Should().Be(val+1);
            //sut.MyStaticPublicMethod(val).Should().Be(val+1);
            Assert.Equal(val + 1, sut.MyStaticPrivateMethod(val));
            Assert.Equal(val + 1, sut.MyStaticInternalMethod(val));
            Assert.Equal(val + 1, sut.MyStaticProtectedMethod(val));
            Assert.Equal(val + 1, sut.MyStaticPublicMethod(val));
        }
    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        if (type == null)
        {
            result = new ReachIn(Type.GetType($"{@namespace}.{binder.Name}".Trim('.')));
            return(true);
        }
        var member = type.GetMember(binder.Name).Single();

        if (member.MemberType == MemberTypes.NestedType)
        {
            result = new ReachIn((Type)member);
        }
        else
        {
            result = null;
            return(false);
        }
        return(true);
    }
예제 #9
0
        public void ReachAllFieldsOfAllTypes()
        {
            //  # Arrange.
            var     pr  = new PseudoRandom(nameof(ReachAllFieldsOfAllTypes));
            dynamic sut = new ReachIn(typeof(MyStaticBaseClassWithTypes));

            //  #   Act and Assert.
            var anyIntValue = pr.Int();

            sut._myPrivateStaticIntField = anyIntValue;
            // We have a problem here with FluentAssertions where it, runtime, throws an exception even though the parameters are equal.
            //sut._myPrivateStaticIntField.Should().be(anyIntValue);
            Assert.Equal(anyIntValue, sut._myPrivateStaticIntField);

            var anyStringValue = pr.String();

            sut._myPrivateStaticStringField = anyStringValue;
            // We have a problem here with FluentAssertions where it, runtime, throws an exception even though the parameters are equal.
            //sut._myPrivateStaticStringField.Should().Be(anyStringValue);
            Assert.Equal(anyStringValue, sut._myPrivateStaticStringField);

            var anyLongValue = pr.PositiveLong();

            sut._myPrivateStaticLongField = anyLongValue;
            // We have a problem here with FluentAssertions where it, runtime, throws an exception even though the parameters are equal.
            //sut._myPrivateStaticLongField.Should().be(anyLongValue);
            Assert.Equal(anyLongValue, sut._myPrivateStaticLongField);

            var anyObject =
                new
            {
                FieldOne = pr.Int(),
            };

            sut._myPrivateStaticObjectField = anyObject;
            // We have a problem here with FluentAssertions where it, runtime, throws an exception even though the parameters are equal.
            //sut._myPrivateStaticObjectField.FieldOne.Should().Be(anyObject.FieldOne);
            Assert.Equal(anyObject.FieldOne, sut._myPrivateStaticObjectField.FieldOne);

            // If there is any other type to test, this is the place to add.
        }
예제 #10
0
        public void ObjAndSutUseTheSameInstance()
        {
            //  # Arrange.
            var     pr    = new PseudoRandom(nameof(ObjAndSutUseTheSameInstance));
            var     obj   = new MyBaseClass();
            dynamic sut   = new ReachIn(obj);
            dynamic sut2  = new ReachIn(obj);
            var     value = pr.Int();

            value.Should().NotBe(default(int), "Sanity check we haven't randomised the default 0 value since all tests then would be moot.");

            //  #   Act.
            sut.MyPublicProperty  = value;
            sut2.MyPublicProperty = value;

            //  #   Assert.
            value.Should().Be(obj.MyPublicProperty);
            // We have a problem here with FluentAssertions where it, runtime, throws an exception even though the parameters are equal.
            //sut2.MyPublicProperty.Should().Be(sut.MyPublicProperty);
            Assert.Equal(sut.MyPublicProperty, sut2.MyPublicProperty);
        }
        //  Static methods.
        //[InlineData("MyStaticPrivateMethod")]
        //[InlineData("MyStaticInternalMethod")]
        //[InlineData("MyStaticProtectedMethod")]
        //[InlineData("MyStaticPublicMethod")]
        public void GetMemberOrThrow_ValidNameInParent_ReturnMember(string memberName)
        {
            var sut = new ReachIn(typeof(ReachIn));

            Assert_CanFind(sut, typeof(MyChildClass), memberName);
        }
        public void GetMemberOrThrow_ValidName_ReturnMember(string memberName)
        {
            dynamic sut = new ReachIn(typeof(ReachIn));

            Assert_CanFind(sut, typeof(MyBaseClass), memberName);
        }