Exemple #1
0
        public void Respect_generic_settings()
        {
            const string behaviorName = "behaviorWithSettings";

            BehaviorExtender.Register <BehaviorWithSettings>(behaviorName);
            try
            {
                var sut = new NZazuFieldBehaviorFactory();
                var behaviorDefinition = new BehaviorDefinition
                {
                    Name     = behaviorName,
                    Settings = new Dictionary <string, string>
                    {
                        { "AnInt", "42" },
                        { "AString", "AString" },
                        { "ADouble", "42.42" },
                        { "ABool", "True" }
                    }
                };
                var behavior = (BehaviorWithSettings)sut.CreateFieldBehavior(behaviorDefinition);

                behavior.AnInt.Should().Be(42);
                behavior.AString.Should().Be("AString");
                behavior.ADouble.Should().Be(42.42);
                behavior.ABool.Should().BeTrue();
            }
            finally
            {
                BehaviorExtender.Unregister(behaviorName);
            }
        }
Exemple #2
0
        public INZazuWpfFieldBehavior CreateFieldBehavior(BehaviorDefinition behaviorDefinition)
        {
            if (behaviorDefinition == null)
            {
                throw new ArgumentNullException(nameof(behaviorDefinition));
            }
            if (string.IsNullOrWhiteSpace(behaviorDefinition.Name))
            {
                throw new ArgumentException("BehaviorDefinition.Name should be set");
            }

            var behaviorTypes = BehaviorExtender.Instance.Behaviors.ToArray();
            var behaviorType  =
                behaviorTypes.FirstOrDefault(
                    kvp => string.Compare(kvp.Key, behaviorDefinition.Name, StringComparison.Ordinal) == 0).Value;

            if (behaviorType == null)
            {
                return(null);
            }

            var behavior = (INZazuWpfFieldBehavior)Activator.CreateInstance(behaviorType);

            return(Decorate(behavior, behaviorDefinition));
        }
Exemple #3
0
        private static INZazuWpfFieldBehavior Decorate(INZazuWpfFieldBehavior behavior,
                                                       BehaviorDefinition behaviorDefinition)
        {
            if (behavior == null)
            {
                throw new ArgumentNullException(nameof(behavior));
            }
            if (behaviorDefinition == null)
            {
                throw new ArgumentNullException(nameof(behaviorDefinition));
            }

            var settings = behaviorDefinition.Settings;

            if (settings == null)
            {
                return(behavior);
            }

            var behaviorSettings = settings.Where(setting => behavior.CanSetProperty(setting.Key));

            foreach (var setting in behaviorSettings)
            {
                behavior.SetProperty(setting.Key, setting.Value);
            }

            return(behavior);
        }
Exemple #4
0
        public void Define_Multiple_NullReference_Behaviours()
        {
            var fieldDefinition = new FieldDefinition {
                Key = "test"
            };
            BehaviorDefinition behavior1 = null;
            BehaviorDefinition behavior2 = null;
            BehaviorDefinition behavior3 = null;

            fieldDefinition.Behaviors = new List <BehaviorDefinition> {
                behavior1, behavior2, behavior3
            };
            var sut = new GenericDummyField(fieldDefinition, ServiceLocator);

            Assert.DoesNotThrow(() => sut.Dispose());
        }
        public void Attach_And_Detach_Behavior_To_Fields()
        {
            // lets mock the behavior and all the other stuff
            var behavior        = Substitute.For <INZazuWpfFieldBehavior>();
            var behaviorFactory = Substitute.For <INZazuWpfFieldBehaviorFactory>();
            var checkFactory    = Substitute.For <ICheckFactory>();
            var serializer      = Substitute.For <INZazuTableDataSerializer>();

            var behaviorDefinition = new BehaviorDefinition {
                Name = "Empty"
            };
            var fieldDefinition = new FieldDefinition
            {
                Key = "a", Type = "string", Behaviors = new[] { behaviorDefinition }
            };

            behaviorFactory.CreateFieldBehavior(behaviorDefinition).Returns(behavior);


            // make sure an attach happens
            var sut = new NZazuFieldFactory();

            sut.Use(behaviorFactory);
            sut.Use(checkFactory);
            sut.Use(serializer);

            var fld1 = sut.CreateField(fieldDefinition);

            behaviorFactory.Received(1).CreateFieldBehavior(behaviorDefinition);
            behavior.Received(1).AttachTo(Arg.Any <INZazuWpfField>(), Arg.Any <INZazuWpfView>());
            behavior.ClearReceivedCalls();

            fld1.Dispose();

            // now lets create a ner form and detach the existing behavior
            behavior.ReceivedWithAnyArgs(1).Detach();
        }