예제 #1
0
        public override bool TryGetMember(string name, out object result)
        {
            if (!base.TryGetMember(name, out result))
                SetMember(name, result = new MockBuilder(Settings));

            return true;
        }
        public void SetIndexesTests()
        {
            dynamic mocks = new MockBuilder();

            C1 key1 = new C1(), val1 = new C1();
            C2 key2 = new C2(), val2 = new C2();
            C1 key3 = null, val3 = null;
            mocks[key1, 33] = val1;
            mocks[key2, 55] = val2;
            mocks[key3, 66] = val3;

            mocks["hello"] = new { };
            mocks["goodbye"] = 989898;

            var copiled = COMPILER.Compile(typeof(SetIndexes));
            var subject = (SetIndexes)
                COMPILER.Compile(typeof(SetIndexes)).GetConstructors()[0]
                    .Invoke(new object[] { new ObjectBase(new DxSettings { TestForInvalidMocks = false }, mocks) });

            Assert.AreEqual(subject.Get1(key1, 33), val1);
            Assert.AreEqual(subject.Get1(key2, 55), val2);
            Assert.AreEqual(subject.Get1(key3, 66), val3);

            Assert.AreEqual(subject["goodbye"], 989898);
        }
예제 #3
0
파일: Mock.cs 프로젝트: ShaneGH/Dynamox
        public Mock(Type mockType, MockBuilder builder, DxSettings settings, IEnumerable<object> constructorArgs = null)
        {
            if (mockType.IsSealed && !settings.CreateSealedClasses)
                throw new InvalidOperationException("Cannot mock a sealed class" + mockType.Name);

            ConstructorArgs = constructorArgs ?? Enumerable.Empty<object>();
            Settings = settings;
            MockType = mockType;
            MockInfo = builder;
        }
예제 #4
0
        public void GetInvalidPropertyType()
        {
            // arrange
            var vaues = new MockBuilder();
            ((dynamic)vaues).abc = new object();
            var subject = new ObjectBase(DxSettings.GlobalSettings, vaues);

            // act
            // assert
            Assert.Throws(typeof(InvalidMockException), () => subject.GetProperty<string>("abc", false));
        }
        public void InterfaceAndClassTests()
        {
            dynamic builder = new MockBuilder(Dx.Settings);
            builder.Prop = 77;
            builder.Method().DxReturns(88);

            var subject = (InterfaceAndClass2)COMPILER.Compile(typeof(InterfaceAndClass2))
                    .GetConstructors()[0]
                        .Invoke(new object[] { new ObjectBase(Dx.Settings, builder) });

            Assert.AreEqual(subject.Prop, 77);
            Assert.AreEqual(subject.Method(), 88);
        }
예제 #6
0
        public void GetInvalidIndexeType()
        {
            var key = new[] { new MethodArg<string>("asdsadas", string.Empty) };

            // arrange
            var values = new MockBuilder();
            ((dynamic)values)[key[0].Arg] = new object();
            var subject = new ObjectBase(DxSettings.GlobalSettings, values);

            // act
            // assert
            Assert.Throws(typeof(InvalidMockException), () => subject.GetIndex<string>(key, false));
        }
        public void SetFieldsTests()
        {
            dynamic mocks = new MockBuilder();
            mocks.F1 = "Hi";
            mocks.F2 = "Hi";
            mocks.F3 = "Hi";

            var subject = (SetFields)
                COMPILER.Compile(typeof(SetFields)).GetConstructors()[0]
                    .Invoke(new object[] { new ObjectBase(new DxSettings { TestForInvalidMocks = false }, mocks) });

            Assert.AreEqual(subject.GetF1(), "Hi");
            Assert.AreEqual(subject.GetF2(), 0);
            Assert.AreEqual(subject.F3, null);
        }
예제 #8
0
        public void IndexesTests()
        {
            var values = new MockBuilder();
            ((dynamic)values)["hello"] = 22;
            ((dynamic)values)[true] = 44;

            var subject = (Indexes)
                COMPILER.Compile(typeof(Indexes)).GetConstructors()[0].Invoke(new object[]
                {
                    new ObjectBase(Dx.Settings, values)
                });

            Assert.AreEqual(subject["hello"], 22);
            subject["hello"] = 33;
            Assert.AreEqual(subject["hello"], 33);
            Assert.AreEqual(subject[true], 44);
        }
예제 #9
0
        public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
        {
            if (args.Length != 1)
                throw new InvalidOperationException("There can only be one argument to this method: the mock settings.");

            var terms = args[0] is IReservedTerms ? args[0] as IReservedTerms : new ReservedTerms(args[0]);
            if (!base.TryGetMember(binder.Name, out result))
            {
                SetMember(binder.Name, result = new MockBuilder(terms, Settings));
                return true;
            }

            if (!(result is MockBuilder))
                throw new InvalidOperationException("The member \"" + binder.Name + "\" has already been set as a property, and cannot be mocked");    //TODM

            (result as MockBuilder).MockSettings.Set(terms);

            return true;
        }
예제 #10
0
        public MethodMockBuilder(IReservedTerms terms, MockBuilder nextPiece, IEnumerable<Type> genericArgs, IEnumerable<object> args)
        {
            if (args.Count() == 1 && args.First() is IMethodAssert)
                ArgChecker = args.First() as IMethodAssert;
            else if (args.Any(a => a is IMethodAssert))
                throw new InvalidMockException("An IMethodAssert from Dx.Args<>(...) must be the first and only argument");
            else
                ArgChecker = new EqualityMethodApplicabilityChecker(args);

            ReservedTerms = new ReservedTermsContainer(terms);

            ArgChecker.OutParamValues = OutParamValues;

            GenericArguments = Array.AsReadOnly((genericArgs ?? Enumerable.Empty<Type>()).ToArray());
            ReturnValue = nextPiece;
            NextPiece = nextPiece;
            MustBeCalled = false;
            WasCalled = false;
        }
예제 #11
0
        public void Sealed()
        {
            // Arrange
            var subject = new Constructors(typeof(C2));
            var objBase1 = new ObjectBase(Dx.Settings);

            var values = new MockBuilder();
            ((dynamic)values).Prop = "bye";

            var objBase2 = new ObjectBase(Dx.Settings, values);

            // Act
            // Assert
            Assert.AreEqual("Hi", ((C2)subject.Construct(objBase1)).Prop);
            Assert.AreEqual("bla", ((C2)subject.Construct(objBase1, new[] { "bla" })).Prop);
            Assert.AreEqual(null, ((C2)subject.Construct(objBase1, new[] { 55 as object })).Prop);

            Assert.AreEqual("bye", ((C2)subject.Construct(objBase2)).Prop);
            Assert.AreEqual("bye", ((C2)subject.Construct(objBase2, new[] { "bla" })).Prop);
            Assert.AreEqual("bye", ((C2)subject.Construct(objBase2, new[] { 55 as object })).Prop);
        }
        public void SetPropertiesTests()
        {
            dynamic mocks = new MockBuilder();

            mocks.P1 = "Hi";
            mocks.P2 = "Hi";
            mocks.P3 = "Hi";
            mocks.P4 = "Hi";
            mocks.P5 = "Hi";
            mocks.P6 = "Hi";

            var subject = (SetProperties)
                COMPILER.Compile(typeof(SetProperties)).GetConstructors()[0]
                    .Invoke(new object[] { new ObjectBase(new DxSettings { TestForInvalidMocks = false }, mocks) });

            Assert.AreEqual(subject.GetP1(), "Hi");
            Assert.AreEqual(subject.GetP2(), 0);
            Assert.AreEqual(subject.P3, null);
            Assert.AreEqual(subject.GetP4(), "Hi");
            Assert.AreEqual(subject.P6, null);
        }
예제 #13
0
 public MethodMockBuilder(MockBuilder nextPiece, IEnumerable<object> args)
     : this(nextPiece, Enumerable.Empty<Type>(), args)
 {
 }
예제 #14
0
 public MethodMockBuilder(MockBuilder nextPiece, IEnumerable<Type> genericArgs, IEnumerable<object> args)
     : this(new ReservedTerms(), nextPiece, genericArgs, args)
 {
 }
예제 #15
0
        public void GetMockedIndexKeys_Inheritance()
        {
            var mod = Dx.Module();

            mod.Add("Happy Path")
                .Arrange(bag =>
                {
                    bag.key = new C1();
                    dynamic mock = new MockBuilder();
                    mock[4, bag.key] = new C1();

                    bag.subject = new ObjectBase(DxSettings.GlobalSettings, mock);
                })
                .Act(bag =>
                    (IEnumerable<IEnumerable<MethodArg>>)bag.subject.GetMockedIndexKeys<C1>(new[] { typeof(int), typeof(C1) }))
                .Assert((bag, result) =>
                {
                    Assert.AreEqual(result.Count(), 1);
                    Assert.AreEqual(result.ElementAt(0).Count(), 2);
                    Assert.AreEqual(result.ElementAt(0).ElementAt(0).Arg, 4);
                    Assert.AreEqual(result.ElementAt(0).ElementAt(0).ArgType, typeof(int));
                    Assert.AreEqual(result.ElementAt(0).ElementAt(1).Arg, bag.key);
                    Assert.AreEqual(result.ElementAt(0).ElementAt(1).ArgType, typeof(C1));
                });

            mod.Add("inheritance, parent key type")
                .BasedOn("Happy Path")
                .UseParentArrange(false)
                .Arrange(bag =>
                {
                    bag.key = new C2();
                    dynamic mock = new MockBuilder();
                    mock[4, bag.key] = new C1();

                    bag.subject = new ObjectBase(DxSettings.GlobalSettings, mock);
                })
                .UseParentAct(true)
                .SkipParentAssert(false);

            mod.Add("inheritance, parent value type")
                .BasedOn("Happy Path")
                .UseParentArrange(false)
                .Arrange(bag =>
                {
                    bag.key = new C1();
                    dynamic mock = new MockBuilder();
                    mock[4, bag.key] = new C2();

                    bag.subject = new ObjectBase(DxSettings.GlobalSettings, mock);
                })
                .UseParentAct(true)
                .SkipParentAssert(false);

            mod.Add("inheritance, child key type")
                .BasedOn("Happy Path")
                .UseParentArrange(false)
                .Arrange(bag =>
                {
                    bag.key = new object();
                    dynamic mock = new MockBuilder();
                    mock[4, bag.key] = new C1();

                    bag.subject = new ObjectBase(DxSettings.GlobalSettings, mock);
                })
                .UseParentAct<IEnumerable<IEnumerable<MethodArg>>>(true)
                .Assert((bag, result) => Assert.IsEmpty(result));

            mod.Add("inheritance, child value type")
                .BasedOn("Happy Path")
                .UseParentArrange(false)
                .Arrange(bag =>
                {
                    bag.key = new C1();
                    dynamic mock = new MockBuilder();
                    mock[4, bag.key] = new object();

                    bag.subject = new ObjectBase(DxSettings.GlobalSettings, mock);
                })
                .UseParentAct<IEnumerable<IEnumerable<MethodArg>>>(true)
                .Assert((bag, result) => Assert.IsEmpty(result));

            mod.Add("null key")
                .BasedOn("Happy Path")
                .UseParentArrange(false)
                .Arrange(bag =>
                {
                    bag.key = null;
                    dynamic mock = new MockBuilder();
                    mock[4, bag.key] = new C1();

                    bag.subject = new ObjectBase(DxSettings.GlobalSettings, mock);
                })
                .UseParentAct(true)
                .SkipParentAssert(false);

            mod.Add("null val")
                .BasedOn("Happy Path")
                .UseParentArrange(false)
                .Arrange(bag =>
                {
                    bag.key = new C1();
                    dynamic mock = new MockBuilder();
                    mock[4, bag.key] = null;

                    bag.subject = new ObjectBase(DxSettings.GlobalSettings, mock);
                })
                .UseParentAct(true)
                .SkipParentAssert(false);

            mod.Add("null key is value type")
                .BasedOn("Happy Path")
                .UseParentArrange(false)
                .Arrange(bag =>
                {
                    bag.key = new C1();
                    dynamic mock = new MockBuilder();
                    mock[null, bag.key] = new C1();

                    bag.subject = new ObjectBase(DxSettings.GlobalSettings, mock);
                })
                .UseParentAct<IEnumerable<IEnumerable<MethodArg>>>(true)
                .Assert((bag, result) => Assert.IsEmpty(result));

            mod.Add("null value is value type")
                .Arrange(bag =>
                {
                    dynamic mock = new MockBuilder();
                    mock[4, new C1()] = null;

                    bag.subject = new ObjectBase(DxSettings.GlobalSettings, mock);
                })
                .Act(bag =>
                    (IEnumerable<IEnumerable<MethodArg>>)bag.subject.GetMockedIndexKeys<int>(new[] { typeof(int), typeof(C1) }))
                .Assert((bag, result) => Assert.IsEmpty(result));

            Dx.Run(mod);
        }
예제 #16
0
        public void Invoke_Void_NoValidMock_Strict()
        {
            // arrange
            var arg = new object();
            var method = new MethodMockBuilder(null, new[]
            {
                Dx.Args<object>(a =>
                {
                    Assert.AreEqual(a, arg);
                    return false;
                })
            });

            var values = new MockBuilder();
            ((dynamic)values).abc = new MethodGroup(method);

            var subject = new ObjectBase(DxSettings.GlobalSettings, values, true);

            // act
            // assert
            Assert.Throws(typeof(InvalidMockException), () =>
                subject.Invoke("abc", new[] { new MethodArg<object>(arg, string.Empty) }));
        }
예제 #17
0
        public void Invoke_Void_NoValidMock_NonStrict()
        {
            // arrange
            var ok = false;
            var arg = new object();
            var method = new MethodMockBuilder(null, new[]
            {
                Dx.Args<object>(a =>
                {
                    ok = true;
                    Assert.AreEqual(a, arg);
                    return false;
                })
            });

            var values = new MockBuilder();
            ((dynamic)values).abc = new MethodGroup(method);

            var subject = new ObjectBase(DxSettings.GlobalSettings, values);

            // act
            // assert
            subject.Invoke("abc", new[] { new MethodArg<object>(arg, string.Empty) });
            Assert.IsTrue(ok);
        }
예제 #18
0
        public void Invoke_Void_NoMock_Strict()
        {
            // arrange
            var arg = new object();
            var values = new MockBuilder();
            ((dynamic)values).abc = new MethodGroup();

            var subject = new ObjectBase(DxSettings.GlobalSettings, values, true);

            // act
            // assert
            Assert.Throws(typeof(InvalidMockException), () =>
                subject.Invoke("abc", new[] { new MethodArg<object>(arg, string.Empty) }));
        }
예제 #19
0
 public MethodMockBuilder(IReservedTerms settings, MockBuilder nextPiece, IEnumerable<object> args)
     : this(settings, nextPiece, Enumerable.Empty<Type>(), args)
 {
 }
예제 #20
0
        public void Invoke_Void_NoMock_NonStrict()
        {
            // arrange
            var arg = new object();
            var values = new MockBuilder();
            ((dynamic)values).abc = new MethodGroup();

            var subject = new ObjectBase(DxSettings.GlobalSettings, values);

            // act
            // assert
            subject.Invoke("abc", new[] { new MethodArg<object>(arg, string.Empty) });
        }
예제 #21
0
        public void Invoke_NonVoid_VoidMethod_RefType()
        {
            // arrange
            var ok = false;
            var arg = new object();
            var method = new MethodMockBuilder(null, new[]
            {
                Dx.Args<object>(a =>
                {
                    ok = true;
                    return true;
                })
            });

            var values = new MockBuilder();
            ((dynamic)values).abc = new MethodGroup(method);

            var subject = new ObjectBase(DxSettings.GlobalSettings, values);

            // act
            // assert
            var output = subject.Invoke<int>("abc", new[] { new MethodArg<object>(arg, string.Empty) });
            Assert.AreEqual(output, 0);
            Assert.IsTrue(ok);
        }
예제 #22
0
        public void HasFieldOrProperty()
        {
            // arrange
            dynamic mock = new MockBuilder();
            mock.Prop1 = new C1();
            mock.Prop2 = Dx.Property(new C1());
            mock.Prop3.Prop = 7;

            var subject = new ObjectBase(DxSettings.GlobalSettings, mock);

            // act
            //assert
            Assert.True(subject.HasMockedFieldOrProperty<C1>("Prop1"));
            Assert.True(subject.HasMockedFieldOrProperty<object>("Prop1"));
            Assert.False(subject.HasMockedFieldOrProperty<C2>("Prop1"));

            Assert.True(subject.HasMockedFieldOrProperty<C1>("Prop2"));
            Assert.True(subject.HasMockedFieldOrProperty<object>("Prop2"));
            Assert.False(subject.HasMockedFieldOrProperty<C2>("Prop2"));

            Assert.True(subject.HasMockedFieldOrProperty<C1>("Prop3"));
            Assert.True(subject.HasMockedFieldOrProperty<int>("Prop3"));
            Assert.True(subject.HasMockedFieldOrProperty<string>("Prop3"));
        }
예제 #23
0
        public void GetSetProperties()
        {
            // arrange
            var prop1 = new object();
            var prop2 = new object();
            var prop3 = new object();
            var values = new MockBuilder();
            ((dynamic)values).abc = prop1;
            var subject = new ObjectBase(DxSettings.GlobalSettings, values);

            // act
            //
            Assert.AreEqual(prop1, subject.GetProperty<object>("abc", false));
            subject.SetProperty("abc", prop2);
            Assert.AreEqual(prop2, subject.GetProperty<object>("abc", false));
            subject.SetProperty("cde", prop3);
            Assert.AreEqual(prop3, subject.GetProperty<object>("cde", false));
        }
예제 #24
0
        public void GetSetIndexes()
        {
            // arrange
            var key1 = new MethodArg[] { new MethodArg<object>(new object(), string.Empty), new MethodArg<string>("asdsadoihasoid", string.Empty) };
            var key2 = new MethodArg[] { new MethodArg<int>(4, string.Empty), new MethodArg<List>(new List(), string.Empty) };
            var val1 = new object();
            var val2 = new object();
            var val3 = new object();
            var values = new MockBuilder();
            ((dynamic)values)[key1[0].Arg, key1[1].Arg] = val1;
            var subject = new ObjectBase(DxSettings.GlobalSettings, values);

            // act
            Assert.AreEqual(val1, subject.GetIndex<object>(key1, false));
            subject.SetIndex(key1, val2);
            Assert.AreEqual(val2, subject.GetIndex<object>(key1, false));
            subject.SetIndex(key2, val3);
            Assert.AreEqual(val3, subject.GetIndex<object>(key2, false));
        }
예제 #25
0
        protected object GetOrMockProperty(string name)
        {
            object result;

            // get
            if (base.TryGetMember(name, out result))
            {
                if (result is MockBuilder)
                    (result as MockBuilder).MockSettings.Set(MockSettings);

                return result;
            }

            // or mock
            SetMember(name, result = new MockBuilder(MockSettings.Next(), TestSettings));
            return result;
        }
예제 #26
0
        public void Invoke_NonVoid_Null()
        {
            // arrange
            var ok = false;
            var arg = new object();
            object returnVal = null;
            var method = new MethodMockBuilder(null, new[]
            {
                Dx.Args<object>(a =>
                {
                    ok = true;
                    return a == arg;
                })
            });
            (method as dynamic).DxReturns(returnVal);

            var values = new MockBuilder();
            ((dynamic)values).abc = new MethodGroup(method);

            var subject = new ObjectBase(DxSettings.GlobalSettings, values);

            // act
            // assert
            var output = subject.Invoke<object>("abc", new[] { new MethodArg<object>(arg, string.Empty) });
            Assert.AreEqual(returnVal, output);
            Assert.IsTrue(ok);
        }