Exemplo n.º 1
0
        public void DefaultObjectFactorySupportsNonGenericInterfaces()
        {
            var sut    = new DefaultObjectFactory();
            var result = sut.Create(typeof(IList));

            Assert.IsAssignableFrom <IList>(result);
        }
Exemplo n.º 2
0
        public virtual void DefaultObjectFactory_CreateMultipleInstances()
        {
            // Arrange
            var parameter = 56789;
            var name = "another";
            var rootElement = CreateRootElement();
            AddXmlForDefaultConstructor(rootElement);
            var itemElement = AddXmlForParameterizedConstructor(rootElement, parameter.ToString(CultureInfo.InvariantCulture));
            AddAttribute(itemElement, DefaultObjectFactory.FactoryNameAttributeName, name);
            var factory = new DefaultObjectFactory(rootElement, null);

            // Action
            ITest instanceDefault;
            var successDefault = factory.TryGetInstance<ITest>(out instanceDefault);
            ITest instanceParameterized;
            var successParameterized = factory.TryGetInstance<ITest>(out instanceParameterized, name);

            // Assert
            Assert.IsTrue(successDefault);
            Assert.IsInstanceOfType(instanceDefault, typeof(TestClass));
            Assert.AreEqual(ActionToken.DefaultConstructor, instanceDefault.ActionWasToken);
            Assert.AreEqual(0, instanceDefault.Value);

            Assert.IsTrue(successParameterized);
            Assert.IsInstanceOfType(instanceParameterized, typeof(TestClass));
            Assert.AreEqual(ActionToken.WithParameter, instanceParameterized.ActionWasToken);
            Assert.AreEqual(parameter, instanceParameterized.Value);
        }
Exemplo n.º 3
0
        public override void WriteYaml(ref ObjectContext objectContext)
        {
            var value = objectContext.Instance;

            // If value is null, then just output a plain null scalar
            if (value == null)
            {
                objectContext.Writer.Emit(new ScalarEventInfo(null, typeof(object))
                {
                    RenderedValue = "null", IsPlainImplicit = true, Style = ScalarStyle.Plain
                });
                return;
            }

            var typeOfValue = value.GetType();


            // If we have a nullable value, get its type directly and replace the descriptor
            if (objectContext.Descriptor is NullableDescriptor)
            {
                objectContext.Descriptor = objectContext.SerializerContext.FindTypeDescriptor(((NullableDescriptor)objectContext.Descriptor).UnderlyingType);
            }

            // Expected type
            var  expectedType = objectContext.Descriptor != null ? objectContext.Descriptor.Type : null;
            bool isAutoMapSeq = false;

            // Allow to serialize back to plain YAML !!map and !!seq if the expected type is an object
            // and the value is of the type Dictionary<object, object> or List<object>
            if (expectedType == typeof(object))
            {
                if (typeOfValue == typeof(Dictionary <object, object>) || typeOfValue == typeof(List <object>))
                {
                    isAutoMapSeq = true;
                }
            }

            // Auto !!map !!seq for collections/dictionaries
            var defaultImplementationType = DefaultObjectFactory.GetDefaultImplementation(expectedType);

            if (defaultImplementationType != null && defaultImplementationType == typeOfValue)
            {
                isAutoMapSeq = true;
            }

            // If this is an anonymous tag we will serialize only a default untyped YAML mapping
            var tag = typeOfValue.IsAnonymous() || typeOfValue == expectedType || isAutoMapSeq
                ? null
                : objectContext.SerializerContext.TagFromType(typeOfValue);

            // Set the tag
            objectContext.Tag = objectContext.Settings.EmitTags ? tag : null;

            // We will use the type of the value for the rest of the WriteYaml serialization
            objectContext.Descriptor = objectContext.SerializerContext.FindTypeDescriptor(typeOfValue);

            // Go next to the chain
            base.WriteYaml(ref objectContext);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Builds a deserializer that will provide YamlDotNet with the given object instead of creating a new one.
        /// This will make YamlDotNet update this object when deserializing.
        /// </summary>
        /// <param name="objectToBind">The object to set fields on</param>
        /// <returns>The newly-created deserializer</returns>
        public static IDeserializer DeserializerUsing(object objectToBind)
        {
            IObjectFactory defaultObjectFactory = new DefaultObjectFactory();
            Type           objectType           = objectToBind.GetType();

            return(new DeserializerBuilder()
                   .IgnoreUnmatchedProperties()
                   // provide the given object if type matches, fall back to default behavior otherwise.
                   .WithObjectFactory(type => type == objectType ? objectToBind : defaultObjectFactory.Create(type))
                   .Build());
        }
Exemplo n.º 5
0
        protected BusSpecificationBase()
        {
            bus = new CoreBus(messageChannel, messageChannel);

            var objectFactory = new DefaultObjectFactory();

            objectFactory.Initialize();

            host = objectFactory.GetInstance <IHost>();
            host.Start();

            LogManager.SetLogLevel(LogLevel.Verbose);
        }
Exemplo n.º 6
0
        public virtual void DefaultObjectFactory_CreateInstance_DefaultConstructor()
        {
            // Arrange
            var rootElement = CreateRootElement();
            AddXmlForDefaultConstructor(rootElement);
            var factory = new DefaultObjectFactory(rootElement, null);

            // Action
            ITest instance;
            var success = factory.TryGetInstance<ITest>(out instance);

            // Assert
            Assert.IsTrue(success);
            Assert.IsInstanceOfType(instance, typeof(TestClass));
            Assert.AreEqual(ActionToken.DefaultConstructor,  instance.ActionWasToken);
            Assert.AreEqual(0, instance.Value);
        }
Exemplo n.º 7
0
        public virtual void DefaultObjectFactory_CreateInstance_WithParameter()
        {
            // Arrange
            var parameter = 123456;
            var rootElement = CreateRootElement();
            AddXmlForParameterizedConstructor(rootElement, parameter.ToString(CultureInfo.InvariantCulture));
            var factory = new DefaultObjectFactory(rootElement, null);

            // Action
            ITest instance;
            var success = factory.TryGetInstance<ITest>(out instance);

            // Assert
            Assert.IsTrue(success);
            Assert.IsInstanceOfType(instance, typeof(TestClass));
            Assert.AreEqual(ActionToken.WithParameter, instance.ActionWasToken);
            Assert.AreEqual(parameter, instance.Value);
        }
Exemplo n.º 8
0
        public virtual void DefaultObjectFactory_Convert_Directly()
        {
            // Arrange
            var rootElement = CreateRootElement();
            var value = Guid.NewGuid().ToString();
            var itemElement = rootElement.AddTextElement(DefaultObjectFactory.FactoryElementName, value);
            AddAttribute(itemElement, DefaultObjectFactory.SourceTypeAttributeName, typeof(string).FullName);
            AddAttribute(itemElement, DefaultObjectFactory.TargetTypeAttributeName, typeof(string).FullName);
            AddAttribute(itemElement, DefaultObjectFactory.ConvertAttributeName, bool.TrueString);
            var factory = new DefaultObjectFactory(rootElement, null);

            // Action
            string actual;
            var success = factory.TryGetInstance<string>(out actual);

            // Assert
            Assert.IsTrue(success);
            Assert.AreEqual(value, actual);
        }
Exemplo n.º 9
0
        public virtual void DefaultObjectFactory_CreateInstance_WithParameter_FromConfiguration()
        {
            // Arrange
            var parameter = 123456;
            var configuration = new NameValueCollection();
            var key = Guid.NewGuid().ToString("N");
            configuration.Add(key, parameter.ToString(CultureInfo.InvariantCulture));
            var rootElement = CreateRootElement();
            var itemElement = AddXmlForParameterizedConstructor(rootElement, key, true);
            var factory = new DefaultObjectFactory(rootElement, configuration);

            // Action
            ITest instance;
            var success = factory.TryGetInstance<ITest>(out instance);

            // Assert
            Assert.IsTrue(success);
            Assert.IsInstanceOfType(instance, typeof(TestClass));
            Assert.AreEqual(ActionToken.WithParameter, instance.ActionWasToken);
            Assert.AreEqual(parameter, instance.Value);
        }
Exemplo n.º 10
0
 public LightInjectObjectFactoryTest()
 {
     defaultObjectFactory = new DefaultObjectFactory();
 }
Exemplo n.º 11
0
 public EnvironmentYamlInspector()
 {
     defaultObjectFactory            = new DefaultObjectFactory();
     defaultInterfaceImplementations = new Dictionary <Type, Type> {
     };
 }
 protected given_default_object_factory()
 {
     HandlerFactory = new DefaultObjectFactory();
 }