protected void AssertAreEquivalent(MvxSerializableBindingDescription expected,
                                  MvxSerializableBindingDescription actual)
 {
     Assert.AreEqual(expected.Converter, actual.Converter);
     Assert.AreEqual(expected.ConverterParameter, actual.ConverterParameter);
     Assert.AreEqual(expected.FallbackValue, actual.FallbackValue);
     Assert.AreEqual(expected.Mode, actual.Mode);
     Assert.AreEqual(expected.Path, actual.Path);
 }
Esempio n. 2
0
 public bool TryParseBindingDescription(string text, out MvxSerializableBindingDescription requestedDescription)
 {
     try
     {
         Reset(text);
         requestedDescription = this.ParseBindingDescription();
         return true;
     }
     catch (Exception exception)
     {
         MvxBindingTrace.Trace(MvxTraceLevel.Error,
                               "Problem parsing Lang binding {0}", exception.ToLongString());
         requestedDescription = null;
         return false;
     }
 }
 protected void AssertAreEquivalent(MvxSerializableBindingDescription expected,
                                  MvxSerializableBindingDescription actual)
 {
     Assert.AreEqual(expected.Converter, actual.Converter);
     Assert.AreEqual(expected.ConverterParameter, actual.ConverterParameter);
     Assert.AreEqual(expected.FallbackValue, actual.FallbackValue);
     Assert.AreEqual(expected.Mode, actual.Mode);
     Assert.AreEqual(expected.Path, actual.Path);
     Assert.AreEqual(expected.Function, actual.Function);
     Assert.AreEqual(expected.Literal, actual.Literal);
     if (expected.Sources == null)
         Assert.IsNull(actual.Sources);
     else
     {
         Assert.AreEqual(expected.Sources.Count, actual.Sources.Count);
         for (var i = 0; i < expected.Sources.Count; i++)
             AssertAreEquivalent(expected.Sources[i], actual.Sources[i]);
     }
 }
        private void DoTest(KeyValuePair<string, MvxSerializableBindingSpecification> testPair)
        {
            var language = new MvxLanguageBindingParser();
            MvxSerializableBindingSpecification result;
            var parsed = language.TryParseBindingSpecification(testPair.Key, out result);
            Assert.IsTrue(parsed, "Failed to parse " + testPair.Key);
            Assert.AreEqual(1, result.Count);
            var keyAndDescription = testPair.Value.First();
            var resultKeyAndDescription = result.First();
            var expectedDescription = new MvxSerializableBindingDescription()
                {
                    Path = keyAndDescription.Value.Path ?? "TextSource",
                    Converter = keyAndDescription.Value.Converter ?? "Language",
                    ConverterParameter = keyAndDescription.Value.ConverterParameter,
                    FallbackValue = keyAndDescription.Value.FallbackValue,
                    Mode = MvxBindingMode.OneTime
                };

            Assert.AreEqual(keyAndDescription.Key, resultKeyAndDescription.Key);
            AssertAreEquivalent(expectedDescription, resultKeyAndDescription.Value);
        }
        private MvxSourceStepDescription SourceStepDescriptionFrom(MvxSerializableBindingDescription description)
        {
            if (description.Path != null)
            {
                return(new MvxPathSourceStepDescription()
                {
                    SourcePropertyPath = description.Path,
                    Converter = FindConverter(description.Converter),
                    ConverterParameter = description.ConverterParameter,
                    FallbackValue = description.FallbackValue
                });
            }

            if (description.Literal != null)
            {
                var literal = description.Literal;
                if (literal == MvxTibetBindingParser.LiteralNull)
                {
                    literal = null;
                }

                return(new MvxLiteralSourceStepDescription()
                {
                    Literal = literal,
                    Converter = FindConverter(description.Converter),
                    ConverterParameter = description.ConverterParameter,
                    FallbackValue = description.FallbackValue
                });
            }

            if (description.Function != null)
            {
                // first look for a combiner with the name
                var combiner = FindCombiner(description.Function);
                if (combiner != null)
                {
                    return(new MvxCombinerSourceStepDescription()
                    {
                        Combiner = combiner,
                        InnerSteps = description.Sources == null
                            ? new List <MvxSourceStepDescription>() :
                                     description.Sources.Select(s => SourceStepDescriptionFrom(s)).ToList(),
                        Converter = FindConverter(description.Converter),
                        ConverterParameter = description.ConverterParameter,
                        FallbackValue = description.FallbackValue
                    });
                }
                else
                {
                    // no combiner, then drop back to looking for a converter
                    var converter = FindConverter(description.Function);
                    if (converter == null)
                    {
                        MvxBindingTrace.Error("Failed to find combiner or converter for {0}", description.Function);
                    }

                    if (description.Sources == null || description.Sources.Count == 0)
                    {
                        MvxBindingTrace.Error("Value Converter {0} supplied with no source", description.Function);
                        return(new MvxLiteralSourceStepDescription()
                        {
                            Literal = null,
                        });
                    }
                    else if (description.Sources.Count > 2)
                    {
                        MvxBindingTrace.Error("Value Converter {0} supplied with too many parameters - {1}", description.Function, description.Sources.Count);
                        return(new MvxLiteralSourceStepDescription()
                        {
                            Literal = null,
                        });
                    }
                    else
                    {
                        return(new MvxCombinerSourceStepDescription()
                        {
                            Combiner = new MvxValueConverterValueCombiner(converter),
                            InnerSteps = description.Sources.Select(source => SourceStepDescriptionFrom(source)).ToList(),
                            Converter = FindConverter(description.Converter),
                            ConverterParameter = description.ConverterParameter,
                            FallbackValue = description.FallbackValue
                        });
                    }
                }
            }

            // this probably suggests that the path is the entire source object
            return(new MvxPathSourceStepDescription()
            {
                SourcePropertyPath = null,
                Converter = FindConverter(description.Converter),
                ConverterParameter = description.ConverterParameter,
                FallbackValue = description.FallbackValue
            });
        }
 public MvxBindingDescription SerializableBindingToBinding(string targetName,
                                                           MvxSerializableBindingDescription description)
 {
     return new MvxBindingDescription
         {
             TargetName = targetName,
             SourcePropertyPath = description.Path,
             Converter = FindConverter(description.Converter),
             ConverterParameter = description.ConverterParameter,
             Mode = description.Mode,
             FallbackValue = description.FallbackValue
         };
 }
        private MvxSourceStepDescription SourceStepDescriptionFrom(MvxSerializableBindingDescription description)
        {
            if (description.Path != null)
            {
                return new MvxPathSourceStepDescription()
                    {
                        SourcePropertyPath = description.Path,
                        Converter = FindConverter(description.Converter),
                        ConverterParameter = description.ConverterParameter,
                        FallbackValue = description.FallbackValue
                    };
            }

            if (description.Literal != null)
            {
                var literal = description.Literal;
                if (literal == MvxTibetBindingParser.LiteralNull)
                    literal = null;

                return new MvxLiteralSourceStepDescription()
                    {
                        Literal = literal,
                        Converter = FindConverter(description.Converter),
                        ConverterParameter = description.ConverterParameter,
                        FallbackValue = description.FallbackValue
                    };
            }

            if (description.Function != null)
            {
                // first look for a combiner with the name
                var combiner = FindCombiner(description.Function);
                if (combiner != null)
                {
                    return new MvxCombinerSourceStepDescription()
                    {
                        Combiner = combiner,
                        InnerSteps = description.Sources == null
                            ? new List<MvxSourceStepDescription>() :
                            description.Sources.Select(s => SourceStepDescriptionFrom(s)).ToList(),
                        Converter = FindConverter(description.Converter),
                        ConverterParameter = description.ConverterParameter,
                        FallbackValue = description.FallbackValue
                    };
                }
                else
                {
                    // no combiner, then drop back to looking for a converter
                    var converter = FindConverter(description.Function);
                    if (converter == null)
                    {
                        MvxBindingTrace.Error("Failed to find combiner or converter for {0}", description.Function);
                    }

                    if (description.Sources == null || description.Sources.Count == 0)
                    {
                        MvxBindingTrace.Error("Value Converter {0} supplied with no source", description.Function);
                        return new MvxLiteralSourceStepDescription()
                        {
                            Literal = null,
                        };
                    }
                    else if (description.Sources.Count > 2)
                    {
                        MvxBindingTrace.Error("Value Converter {0} supplied with too many parameters - {1}", description.Function, description.Sources.Count);
                        return new MvxLiteralSourceStepDescription()
                        {
                            Literal = null,
                        };
                    }
                    else
                    {
                        return new MvxCombinerSourceStepDescription()
                        {
                            Combiner = new MvxValueConverterValueCombiner(converter),
                            InnerSteps = description.Sources.Select(source => SourceStepDescriptionFrom(source)).ToList(),
                            Converter = FindConverter(description.Converter),
                            ConverterParameter = description.ConverterParameter,
                            FallbackValue = description.FallbackValue
                        };
                    }
                }
            }

            // this probably suggests that the path is the entire source object
            return new MvxPathSourceStepDescription()
            {
                SourcePropertyPath = null,
                Converter = FindConverter(description.Converter),
                ConverterParameter = description.ConverterParameter,
                FallbackValue = description.FallbackValue
            };
        }
 public MvxBindingDescription SerializableBindingToBinding(string targetName,
                                                           MvxSerializableBindingDescription description)
 {
     return new MvxBindingDescription
         {
             TargetName = targetName,
             Source = SourceStepDescriptionFrom(description),
             Mode = description.Mode,
         };
 }