public void String_ToByte()
 {
     using (ValueBuilder builder = new ValueBuilder())
     {
         object value;
         Assert.True(builder.Build(typeof(byte),
                                   new LiteralValue(SourceInfo.Empty, LiteralValueType.String, "255"),
                                   out value));
         Assert.Equal((byte)255, (byte)value);
         Assert.Empty(builder.Errors);
     }
 }
 public void String_Nop()
 {
     using (ValueBuilder builder = new ValueBuilder())
     {
         object value;
         Assert.True(builder.Build(typeof(string),
                                   new LiteralValue(SourceInfo.Empty, LiteralValueType.String, "test"),
                                   out value));
         Assert.Equal("test", (string)value);
         Assert.Empty(builder.Errors);
     }
 }
 public void String_ToArrayOfString()
 {
     using (ValueBuilder builder = new ValueBuilder())
     {
         object value;
         Assert.True(builder.Build(typeof(string[]),
                                   new LiteralValue(SourceInfo.Empty, LiteralValueType.String, "255"),
                                   out value));
         Assert.IsType<string[]>(value);
         Assert.Equal(new[]{"255"}, value);
         Assert.Empty(builder.Errors);
     }
 }
        public void AssocArray_To_ILookup(Type targetType)
        {
            using (ValueBuilder builder = new ValueBuilder())
            {
                object value;
                Assert.True(builder.Build(targetType,
                                          new AssociativeArray(
                                              SourceInfo.Empty,
                                              new[]
                                                  {
                                                      new KeyValuePair<AstNode, AstNode>(
                                                          new LiteralValue(SourceInfo.Empty,
                                                                           LiteralValueType.Numeric,
                                                                           "1"),
                                                          new LiteralValue(SourceInfo.Empty,
                                                                           LiteralValueType.String, 
                                                                           "2")),
                                                      new KeyValuePair<AstNode, AstNode>(
                                                          new LiteralValue(SourceInfo.Empty,
                                                                           LiteralValueType.Numeric,
                                                                           "1"),
                                                          new LiteralValue(SourceInfo.Empty,
                                                                           LiteralValueType.String, 
                                                                           "4"))
                                                  }),
                                          out value));

                Assert.IsAssignableFrom(targetType, value);

                Assert.IsAssignableFrom<IEnumerable>(value);

                Assert.Equal(1, ((IEnumerable)value).Cast<object>().Count());

                object group = ((IEnumerable) value).Cast<object>().Single();

                Assert.Equal("1", group.GetType().GetProperty("Key").GetValue(group, null).ToString());

                object[] values = ((IEnumerable) group).Cast<object>().ToArray();

                Assert.Equal(2, values.Length);

                Assert.Equal("2", values[0].ToString());

                Assert.Equal("4", values[1].ToString());

                Assert.Empty(builder.Errors);
            }
        }
        public void AssocArray(Type targetType, string keyName, string valueName)
        {
            using (ValueBuilder builder = new ValueBuilder())
            {
                object value;
                Assert.True(builder.Build(targetType,
                                          new AssociativeArray(
                                              SourceInfo.Empty,
                                              new[]
                                                  {
                                                      new KeyValuePair<AstNode, AstNode>(
                                                          new LiteralValue(SourceInfo.Empty,
                                                                           LiteralValueType.Numeric,
                                                                           "1"),
                                                          new LiteralValue(SourceInfo.Empty,
                                                                           LiteralValueType.String, 
                                                                           "2")),
                                                      new KeyValuePair<AstNode, AstNode>(
                                                          new LiteralValue(SourceInfo.Empty,
                                                                           LiteralValueType.Numeric,
                                                                           "3"),
                                                          new LiteralValue(SourceInfo.Empty,
                                                                           LiteralValueType.String, 
                                                                           "4"))
                                                  }),
                                          out value));

                Assert.IsAssignableFrom(targetType, value);

                Assert.IsAssignableFrom<IEnumerable>(value);

                Assert.Equal(2, ((IEnumerable)value).Cast<object>().Count());

                int i = 0;
                foreach (object kvp in ((IEnumerable)value))
                {
                    object keyValue = kvp.GetType().GetProperty(keyName).GetValue(kvp, null);
                    object valueValue = kvp.GetType().GetProperty(valueName).GetValue(kvp, null);

                    switch (i)
                    {
                        case 0:
                            Assert.Equal("1", keyValue.ToString());
                            Assert.Equal("2", valueValue.ToString());
                            break;
                        case 1:
                            Assert.Equal("3", keyValue.ToString());
                            Assert.Equal("4", valueValue.ToString());
                            break;
                    }

                    i++;
                }

                Assert.Empty(builder.Errors);
            }
        }
        public void Sequence_To_Array_of_Decimal()
        {
            using (ValueBuilder builder = new ValueBuilder())
            {
                object value;
                Assert.True(builder.Build(typeof(decimal[]),
                                          new Sequence(SourceInfo.Empty,
                                                       new[]
                                                           {
                                                               new LiteralValue(SourceInfo.Empty,
                                                                                LiteralValueType.Numeric, "1"),
                                                               new LiteralValue(SourceInfo.Empty,
                                                                                LiteralValueType.String, "2")
                                                           }),
                                          out value));

                Assert.Equal(new[] { 1m, 2m }, (decimal[])value);

                Assert.Empty(builder.Errors);
            }
        }
        public void Sequence_To_Array_of_Boolean()
        {
            using (ValueBuilder builder = new ValueBuilder())
            {
                object value;
                Assert.True(builder.Build(typeof(bool[]),
                                          new Sequence(SourceInfo.Empty,
                                                       new[]
                                                           {
                                                               new LiteralValue(SourceInfo.Empty,
                                                                                LiteralValueType.Boolean, "$true"),
                                                               new LiteralValue(SourceInfo.Empty,
                                                                                LiteralValueType.Boolean, "$false"),
                                                               new LiteralValue(SourceInfo.Empty,
                                                                                LiteralValueType.Numeric, "1"),
                                                               new LiteralValue(SourceInfo.Empty,
                                                                                LiteralValueType.Numeric, "0"),
                                                               new LiteralValue(SourceInfo.Empty,
                                                                                LiteralValueType.String, "Any"),
                                                               new LiteralValue(SourceInfo.Empty,
                                                                                LiteralValueType.String, "")
                                                           }),
                                          out value));

                Assert.True(new[] { true, false, true, false, true, false }.SequenceEqual((IEnumerable<bool>)value));

                Assert.Empty(builder.Errors);
            }
        }
 public void Sequence_Empty_To_IEnumerable_of_Object()
 {
     using (ValueBuilder builder = new ValueBuilder())
     {
         object value;
         Assert.True(builder.Build(typeof(IEnumerable<object>),
                                   new Sequence(SourceInfo.Empty, Enumerable.Empty<AstNode>()), out value));
         Assert.Empty((IEnumerable)value);
         Assert.Empty(builder.Errors);
     }
 }
Пример #9
0
        private static void SetPropertyValue(BuildContext ctx, Parameter parameter, AstNode astNode)
        {
            using (ValueBuilder builder = new ValueBuilder())
            {
                object value;
                if (builder.Build(parameter.Property.PropertyType, astNode, out value))
                {
                    parameter.Property.SetValue(ctx.Instance, value, null);
                }
                else
                {
                    foreach (ValueError valueError in builder.Errors)
                    {
                        TypeError typeError = valueError as TypeError;
                        if (typeError != null)
                        {
                            string message = string.Format(ErrorMessages.IncompatibleType,
                                                           ctx.Sequence.GetInputString(typeError.AstNode.SourceInfo),
                                                           parameter.Name);

                            ctx.Errors.Add(new BindError(ErrorType.IncompatibleType,
                                                         new[] { parameter },
                                                         new[] { astNode },
                                                         message));
                        }

                        AddError addError = valueError as AddError;

                        if (addError != null)
                        {
                            string message = string.Format(ErrorMessages.MethodInvocationFailed,
                                                           ctx.Sequence.GetInputString(addError.AstNodes.Select(ast => ast.SourceInfo)),
                                                           parameter.Name,
                                                           addError.Method.ReflectedType.Name,
                                                           addError.Method.Name,
                                                           addError.Exception.GetType().Name,
                                                           addError.Exception.Message);

                            ctx.Errors.Add(new BindError(ErrorType.IncompatibleType,
                                                         new[] { parameter },
                                                         new[] { astNode },
                                                         message));
                        }
                        else
                        {
                            ActivationError activationError = valueError as ActivationError;

                            if (activationError != null)
                            {
                                string message = string.Format(ErrorMessages.ObjectInitializationFailed,
                                                               ctx.Sequence.GetInputString(activationError.AstNodes.Select(ast => ast.SourceInfo)),
                                                               parameter.Name,
                                                               activationError.Constructor.ReflectedType.Name,
                                                               activationError.Exception.GetType().Name,
                                                               activationError.Exception.Message);

                                ctx.Errors.Add(new BindError(ErrorType.IncompatibleType,
                                                             new[] { parameter },
                                                             new[] { astNode },
                                                             message));
                            }
                        }
                    }
                }
            }
        }
 public void String_Empty_ToBoolean()
 {
     using (ValueBuilder builder = new ValueBuilder())
     {
         object value;
         Assert.True(builder.Build(typeof(bool), new LiteralValue(SourceInfo.Empty, LiteralValueType.String, ""),
                                   out value));
         Assert.False((bool)value);
         Assert.Empty(builder.Errors);
     }
 }
 public void Number_ToDecimal()
 {
     using (ValueBuilder builder = new ValueBuilder())
     {
         object value;
         Assert.True(builder.Build(typeof(decimal),
                                   new LiteralValue(SourceInfo.Empty, LiteralValueType.Numeric, "2.55"),
                                   out value));
         Assert.Equal((decimal)2.55, (decimal)value);
         Assert.Empty(builder.Errors);
     }
 }
 public void Number_ToSingle()
 {
     using (ValueBuilder builder = new ValueBuilder())
     {
         object value;
         Assert.True(builder.Build(typeof(Single),
                                   new LiteralValue(SourceInfo.Empty, LiteralValueType.Numeric, "2"),
                                   out value));
         Assert.Equal(2f, (Single)value);
         Assert.Empty(builder.Errors);
     }
 }
 public void String_ToDateTime()
 {
     using (ValueBuilder builder = new ValueBuilder())
     {
         object value;
         Assert.True(builder.Build(typeof(DateTime),
                                   new LiteralValue(SourceInfo.Empty, LiteralValueType.String,
                                                    "2001-02-03 04:05:06"),
                                   out value));
         Assert.Equal(new DateTime(2001, 02, 03, 04, 05, 06), (DateTime)value);
         Assert.Empty(builder.Errors);
     }
 }
 public void StringAlpha_ToByte()
 {
     using (ValueBuilder builder = new ValueBuilder())
     {
         object value;
         Assert.False(builder.Build(typeof(byte),
                                    new LiteralValue(SourceInfo.Empty, LiteralValueType.String, "abc"),
                                    out value));
         Assert.NotEmpty(builder.Errors);
     }
 }
 public void String_To_PathInfo()
 {
     using (ValueBuilder builder = new ValueBuilder())
     {
         object value;
         Assert.True(builder.Build(typeof(System.IO.DirectoryInfo),
                                   new LiteralValue(SourceInfo.Empty, LiteralValueType.String, "c:\\doesnt exist\\"),
                                   out value));
         Assert.IsType<System.IO.DirectoryInfo>(value);
         Assert.Empty(builder.Errors);
     }
 }
 public void String_To_Enum()
 {
     using (ValueBuilder builder = new ValueBuilder())
     {
         object value;
         Assert.True(builder.Build(typeof(ConsoleColor),
                                   new LiteralValue(SourceInfo.Empty, LiteralValueType.String, "Red"),
                                   out value));
         Assert.Equal(ConsoleColor.Red, (ConsoleColor)value);
         Assert.Empty(builder.Errors);
     }
 }
 public void Booean_False_ToInt()
 {
     using (ValueBuilder builder = new ValueBuilder())
     {
         object value;
         Assert.True(builder.Build(typeof(int),
                                   new LiteralValue(SourceInfo.Empty, LiteralValueType.Boolean, "$false"),
                                   out value));
         Assert.Equal(0, (int)value);
         Assert.Empty(builder.Errors);
     }
 }
Пример #18
0
        private static void SetDefaultValues(ITypeConverter typeConverter, CultureInfo cultureInfo, BuildContext ctx)
        {
            // set default values
            foreach (Parameter parameter in ctx.ParameterSet)
            {
                var attr = parameter.GetAttribute<DefaultValueAttribute>();

                if (attr == null)
                    continue;

                string strVal = attr.Value as string;
                if (strVal != null)
                {
                    if (parameter.Property.PropertyType != typeof(string))
                    {
                        Parser parser = new Parser();
                        NodeSequence seq = parser.Parse(strVal);
                        using (ValueBuilder valueBuilder = new ValueBuilder())
                        {
                            object value;
                            if (valueBuilder.Build(parameter.Property.PropertyType, seq[0], out value))
                            {
                                parameter.Property.SetValue(ctx.Instance, value, null);
                            }
                            else
                            {
                                string message = string.Format(Exceptions.FailedToParseDefaultValue,
                                                               strVal,
                                                               parameter.Property.PropertyType.FullName);

                                throw new ParameterDefinitionException(parameter.Property, message);
                            }
                        }
                    }
                    else
                    {
                        parameter.Property.SetValue(ctx.Instance, strVal, null);
                    }
                }
                else
                {
                    object value = attr.Value;
                    if (value == null)
                    {
                        if (!parameter.Property.PropertyType.IsValueType)
                        {
                            parameter.Property.SetValue(ctx.Instance, value, null);
                        }
                        else
                        {
                            string message = string.Format(Exceptions.FailedToConvertDefaultValue,
                                                           "NULL",
                                                           "",
                                                           parameter.Property.PropertyType.FullName);
                            throw new ParameterDefinitionException(parameter.Property, message);
                        }
                    }
                    else
                    {
                        IEnumerable<Exception> exceptions;
                        if (typeConverter.TryConvertType(cultureInfo, parameter.Property.PropertyType, ref value, out exceptions))
                        {
                            parameter.Property.SetValue(ctx.Instance, value, null);
                        }
                        else
                        {
                            string message = string.Format(Exceptions.FailedToConvertDefaultValue,
                                                           value,
                                                           value.GetType().FullName,
                                                           parameter.Property.PropertyType.FullName);

                            throw new ParameterDefinitionException(parameter.Property, message);
                        }
                    }
                }
            }
        }