Ejemplo n.º 1
0
                public void Should_Configure_Binder_With_Instance()
                {
                    var binder = new TestBinder();

                    AutoFaker.SetBinder(binder);

                    AutoFaker.DefaultConfig.Binder.Should().Be(binder);
                }
Ejemplo n.º 2
0
            public void bind_handles_null_configuration()
            {
                Configuration configuration = new Configuration();
                TestBinder    binder        = new TestBinder(configuration);
                ITypeAccessor typeAccessor  = TypeAccessorFactory.Create(typeof(Test));
                IMember       member        = typeAccessor.GetMembers().First();

                binder.Bind(typeAccessor, member, CreateContext(null));
            }
Ejemplo n.º 3
0
        public void TestCustomFactoryWrapper()
        {
            IDIContext context    = ContextHelper.CreateContext();
            TestBinder testBinder = new TestBinder(context);

            testBinder.Bind <IMyClass>(() => new MyClass());

            IMyClass obj = context.Resolve <IMyClass>();

            Assert.IsInstanceOf <AnotherClass>(obj);
            Assert.IsInstanceOf <MyClass>((obj as AnotherClass).data);
        }
Ejemplo n.º 4
0
        public void IgnoresPrivatesSetters()
        {
            var model = new TestBinder().Deserialize <ModelClass>(new Dictionary <string, object>()
            {
                { "PropertyWithPrivateSetter", "modified value" },
                { "PrivateField", "modified value" },
                { "PrivateProperty", "modified value" },
            });

            Assert.AreEqual(model.PropertyWithPrivateSetter, "private value");
            Assert.AreEqual(model.GetPrivateField(), "private value");
            Assert.AreEqual(model.GetPrivateProperty(), "private value");
        }
Ejemplo n.º 5
0
        public void CanDeserializeStructFromDictionary()
        {
            var model = new TestBinder().Deserialize <ModelClass>(new Dictionary <string, object>()
            {
                { "IntProperty", 1 },
                { "IntField", 2 },
                { "StringProperty", "1" },
                { "StringField", "2" }
            });

            Assert.AreEqual(1, model.IntProperty);
            Assert.AreEqual(2, model.IntField);
            Assert.AreEqual("1", model.StringProperty);
            Assert.AreEqual("2", model.StringField);
        }
Ejemplo n.º 6
0
        static void StartTestBinder()
        {
            string managedDir = "Managed";

            TestBinder.Init(@"test\");

            foreach (var filePath in Directory.GetFiles(managedDir))
            {
                var file = Path.GetFileName(filePath);

                if (file.EndsWith(".dll") && !IgnoreAssemblySet.Contains(file))
                {
                    TestBinder.TestBind(filePath);
                }
            }
            TestBinder.End();
        }
Ejemplo n.º 7
0
        public void CanDeserializeFromDictionary()
        {
            var model = new TestBinder().Deserialize <ModelClass>(new Dictionary <string, object>()
            {
                { "IntProperty", 1 },
                { "IntField", 2 },
                { "StringProperty", "1" },
                { "StringField", "2" },
                { "IgnoredProperty", "5" },
                { "renamed_property", "renamed value" }
            });

            Assert.AreEqual(1, model.IntProperty);
            Assert.AreEqual(2, model.IntField);
            Assert.AreEqual("1", model.StringProperty);
            Assert.AreEqual("2", model.StringField);
            Assert.AreEqual("Ignore me", model.IgnoredProperty);
            Assert.AreEqual("renamed value", model.RenamedProperty);
        }
Ejemplo n.º 8
0
    public void GlobalSetup()
    {
        switch (HubProtocol)
        {
        case Protocol.MsgPack:
            _hubProtocol = new MessagePackHubProtocol();
            break;

        case Protocol.Json:
            _hubProtocol = new JsonHubProtocol();
            break;

        case Protocol.NewtonsoftJson:
            _hubProtocol = new NewtonsoftJsonHubProtocol();
            break;
        }

        switch (Input)
        {
        case Message.NoArguments:
            _hubMessage = new InvocationMessage("Target", Array.Empty <object>());
            break;

        case Message.FewArguments:
            _hubMessage = new InvocationMessage("Target", new object[] { 1, "Foo", 2.0f });
            break;

        case Message.ManyArguments:
            _hubMessage = new InvocationMessage("Target", new object[] { 1, "string", 2.0f, true, (byte)9, new byte[] { 5, 4, 3, 2, 1 }, 'c', 123456789101112L });
            break;

        case Message.LargeArguments:
            _hubMessage = new InvocationMessage("Target", new object[] { new string('F', 10240), new byte[10240] });
            break;
        }

        _binaryInput = _hubProtocol.GetMessageBytes(_hubMessage);
        _binder      = new TestBinder(_hubMessage);
    }
Ejemplo n.º 9
0
        public void Operations()
        {
            // Verify that document link list operations work and also raise the correct
            // change events.

            using (var test = new TestDatabase())
            {
                var db                = test.Database;
                var doc               = db.GetBinderDocument <TestBinder>("0");
                var prop              = string.Empty;
                var changed           = false;
                var collectionChanged = false;

                doc.Changed +=
                    (s, a) =>
                {
                    changed = true;
                };

                doc.Content.PropertyChanged +=
                    (s, a) =>
                {
                    prop = a.PropertyName;
                };

                Assert.Null(doc.Content.DocList);

                //---------------------
                // Assignment

                var doc1 = db.GetBinderDocument <TestBinder>("1");
                var doc2 = db.GetBinderDocument <TestBinder>("2");
                var doc3 = db.GetBinderDocument <TestBinder>("3");

                doc1.Content.String = "1";
                doc2.Content.String = "2";
                doc3.Content.String = "3";

                doc1.Save();
                doc2.Save();
                doc3.Save();

                changed = false;
                prop    = null;

                var item1 = doc1;
                var item2 = doc2;
                var item3 = doc3;

                doc.Content.DocList = new TestBinder[] { item1, item2 };

                Assert.True(changed);
                Assert.Equal("DocList", prop);
                Assert.NotNull(doc.Content.DocList);
                Assert.True(Match(new string[] { "1", "2" }, doc.Content.DocList));
                Assert.Equal(2, doc.Content.DocList.Count);

                //---------------------
                // IndexOf

                collectionChanged = false;
                changed           = false;
                prop = null;

                ((INotifyCollectionChanged)doc.Content.DocList).CollectionChanged +=
                    (s, a) =>
                {
                    collectionChanged = true;
                };

                Assert.Equal(0, doc.Content.DocList.IndexOf(item1));
                Assert.Equal(1, doc.Content.DocList.IndexOf(item2));
                Assert.Equal(-1, doc.Content.DocList.IndexOf(item3));
                Assert.Equal(-1, doc.Content.DocList.IndexOf(null));

                Assert.False(changed);
                Assert.False(collectionChanged);
                Assert.Null(prop);

                //---------------------
                // Contains

                ((INotifyCollectionChanged)doc.Content.DocList).CollectionChanged +=
                    (s, a) =>
                {
                    collectionChanged = true;
                };

                Assert.True(doc.Content.DocList.Contains(item1));
                Assert.True(doc.Content.DocList.Contains(item2));
                Assert.False(doc.Content.DocList.Contains(item3));

                Assert.False(changed);
                Assert.False(collectionChanged);
                Assert.Null(prop);

                //---------------------
                // Indexing

                ((INotifyCollectionChanged)doc.Content.DocList).CollectionChanged +=
                    (s, a) =>
                {
                    collectionChanged = true;
                };

                Assert.Equal("1", doc.Content.DocList[0].Content.String);
                Assert.Equal("2", doc.Content.DocList[1].Content.String);

                doc.Content.DocList[0] = item3;

                Assert.True(changed);
                Assert.True(collectionChanged);
                Assert.Null(prop);
                Assert.Equal("3", doc.Content.DocList[0].Content.String);
                Assert.Equal("2", doc.Content.DocList[1].Content.String);

                Assert.True(Match(new string[] { "3", "2" }, doc.Content.DocList));

                doc.Content.DocList[0] = null;

                Assert.Null(doc.Content.DocList[0]);

                //---------------------
                // Insert

                doc.Content.DocList = new TestBinder[] { item1, item2 };

                ((INotifyCollectionChanged)doc.Content.DocList).CollectionChanged +=
                    (s, a) =>
                {
                    collectionChanged = true;
                };

                prop              = null;
                changed           = false;
                collectionChanged = false;

                doc.Content.DocList.Insert(0, item3);
                doc.Content.DocList.Insert(3, null);

                Assert.True(changed);
                Assert.True(collectionChanged);
                Assert.Null(prop);
                Assert.Equal(4, doc.Content.DocList.Count);
                Assert.Equal("3", doc.Content.DocList[0].Content.String);
                Assert.Equal("1", doc.Content.DocList[1].Content.String);
                Assert.Equal("2", doc.Content.DocList[2].Content.String);
                Assert.Null(doc.Content.DocList[3]);

                //---------------------
                // Remove

                doc.Content.DocList = new TestBinder[] { item1, item2 };

                ((INotifyCollectionChanged)doc.Content.DocList).CollectionChanged +=
                    (s, a) =>
                {
                    collectionChanged = true;
                };

                prop              = null;
                changed           = false;
                collectionChanged = false;

                Assert.True(doc.Content.DocList.Remove(item1));
                Assert.False(doc.Content.DocList.Remove(item3));
                Assert.False(doc.Content.DocList.Remove(null));

                Assert.True(changed);
                Assert.True(collectionChanged);
                Assert.Null(prop);
                Assert.Equal(1, doc.Content.DocList.Count);
                Assert.True(Match(new string[] { "2" }, doc.Content.DocList));

                //---------------------
                // RemoveAt

                doc.Content.DocList = new TestBinder[] { item1, item2 };

                ((INotifyCollectionChanged)doc.Content.DocList).CollectionChanged +=
                    (s, a) =>
                {
                    collectionChanged = true;
                };

                prop              = null;
                changed           = false;
                collectionChanged = false;

                doc.Content.DocList.RemoveAt(1);

                Assert.True(changed);
                Assert.True(collectionChanged);
                Assert.Null(prop);
                Assert.Equal(1, doc.Content.DocList.Count);
                Assert.True(Match(new string[] { "1" }, doc.Content.DocList));

                //---------------------
                // Add

                doc.Content.DocList = new TestBinder[] { item1, item2 };

                ((INotifyCollectionChanged)doc.Content.DocList).CollectionChanged +=
                    (s, a) =>
                {
                    collectionChanged = true;
                };

                prop              = null;
                changed           = false;
                collectionChanged = false;

                doc.Content.DocList.Add(item3);

                Assert.True(changed);
                Assert.True(collectionChanged);
                Assert.Null(prop);
                Assert.Equal(3, doc.Content.DocList.Count);
                Assert.True(Match(new string[] { "1", "2", "3" }, doc.Content.DocList));

                doc.Content.DocList.Add(null);

                Assert.Equal(4, doc.Content.DocList.Count);
                Assert.Null(doc.Content.DocList[3]);

                //---------------------
                // CopyTo

                doc.Content.DocList = new TestBinder[] { item1, item2, null };

                ((INotifyCollectionChanged)doc.Content.DocList).CollectionChanged +=
                    (s, a) =>
                {
                    collectionChanged = true;
                };

                prop              = null;
                changed           = false;
                collectionChanged = false;

                var copy = new TestBinder[3];

                doc.Content.DocList.CopyTo(copy, 0);
                Assert.Equal("1", copy[0].Content.String);
                Assert.Equal("2", copy[1].Content.String);
                Assert.Null(copy[2]);

                Assert.False(changed);
                Assert.False(collectionChanged);
                Assert.Null(prop);
                Assert.Equal(3, doc.Content.DocList.Count);
                Assert.Equal("1", doc.Content.DocList[0].Content.String);
                Assert.Equal("2", doc.Content.DocList[1].Content.String);
                Assert.Null(doc.Content.DocList[2]);

                //---------------------
                // Clear

                doc.Content.DocList = new TestBinder[] { item1, item2 };

                ((INotifyCollectionChanged)doc.Content.DocList).CollectionChanged +=
                    (s, a) =>
                {
                    collectionChanged = true;
                };

                Assert.Equal(2, doc.Content.DocList.Count);

                prop              = null;
                changed           = false;
                collectionChanged = false;

                doc.Content.DocList.Clear();

                Assert.True(changed);
                Assert.True(collectionChanged);
                Assert.Null(prop);
                Assert.Equal(0, doc.Content.DocList.Count);
                Assert.True(Match(new string[0], doc.Content.DocList));

                //---------------------
                // Ensure that the array changes are persisted.

                prop              = null;
                changed           = false;
                collectionChanged = false;

                doc.Content.DocList.Clear();
                doc.Content.DocList.Add(item1);
                doc.Content.DocList.Add(null);
                doc.Content.DocList.Add(item3);

                Assert.True(changed);
                Assert.True(collectionChanged);
                Assert.Null(prop);
                Assert.Equal(3, doc.Content.DocList.Count);
                Assert.Equal("1", doc.Content.DocList[0].Content.String);
                Assert.Null(doc.Content.DocList[1]);
                Assert.Equal("3", doc.Content.DocList[2].Content.String);

                doc.Save();

                doc = db.GetBinderDocument <TestBinder>("0");

                Assert.NotNull(doc.Content.DocList);
                Assert.Equal(3, doc.Content.DocList.Count);
                Assert.Equal("1", doc.Content.DocList[0].Content.String);
                Assert.Null(doc.Content.DocList[1]);
                Assert.Equal("3", doc.Content.DocList[2].Content.String);

                //---------------------
                // Enumeration

                var list = new List <TestBinder>();

                foreach (var element in doc.Content.DocList)
                {
                    list.Add(element);
                }

                Assert.Equal(3, list.Count);
                Assert.Equal("1", list[0].Content.String);
                Assert.Null(list[1]);
                Assert.Equal("3", list[2].Content.String);
            }
        }