Example #1
0
 public void BindingGrammarCanReadAnEntireString()
 {
     var grammar = new BindingGrammar();
     var pos = new Position(new SourceContext("hello world"));
     var result = (BindingLiteral)grammar.Literal(pos).Value;
     Assert.That(result.Text, Is.EqualTo("hello world"));
 }
        public IEnumerable <Binding> LoadStandardMarkup(TextReader reader)
        {
            IEnumerable <XElement> enumerable = XDocument.Load(reader).Elements("bindings").Elements <XElement>("element");
            BindingGrammar         grammar    = new BindingGrammar();

            return(from element in enumerable select ParseBinding(element, grammar));
        }
Example #3
0
        public void AttrNameCanHaveSomeSpecialCharacters()
        {
            var grammar = new BindingGrammar();
            var result = grammar.NameReference(Source("@extra:special.more-name stuff"));

            Assert.That(result.Value, Is.InstanceOfType(typeof(BindingNameReference)));
            var value = (BindingNameReference)result.Value;
            Assert.That(value.Name, Is.EqualTo("extra:special.more-name"));
        }
Example #4
0
        public IEnumerable<Binding> LoadStandardMarkup(TextReader reader)
        {
            var document = XDocument.Load(reader);
            var elements = document.Elements("bindings").Elements("element");

            var grammar = new BindingGrammar();
            var bindings = elements.Select(element => ParseBinding(element, grammar));

            return bindings;
        }
Example #5
0
        public IEnumerable <Binding> LoadStandardMarkup(TextReader reader)
        {
            var document = XDocument.Load(reader);
            var elements = document.Elements("bindings").Elements("element");

            var grammar  = new BindingGrammar();
            var bindings = elements.Select(element => ParseBinding(element, grammar));

            return(bindings);
        }
Example #6
0
        public void MixedResultsComeBackAtTheRightPlaces()
        {
            var grammar = new BindingGrammar();
            var result = grammar.Nodes(Source("Html.ActionLink(@caption, @action)"));

            Assert.That(result.Value.Count(), Is.EqualTo(5));

            Assert.That(((BindingLiteral)result.Value[0]).Text, Is.EqualTo("Html.ActionLink("));
            Assert.That(((BindingNameReference)result.Value[1]).Name, Is.EqualTo("caption"));
            Assert.That(((BindingLiteral)result.Value[2]).Text, Is.EqualTo(", "));
            Assert.That(((BindingNameReference)result.Value[3]).Name, Is.EqualTo("action"));
            Assert.That(((BindingLiteral)result.Value[4]).Text, Is.EqualTo(")"));
        }
Example #7
0
        public IEnumerable<Binding> LoadStandardMarkup(TextReader reader)
        {
            var document = XDocument.Load(reader);
            var elements = document.Elements("bindings").Elements("element");

            var grammar = new BindingGrammar();
            var bindings = elements.Select(
                element =>
                new Binding
                    {
                        ElementName = (string) element.Attribute("name"),
                        Nodes = grammar.Nodes(new Position(new SourceContext(element.Value))).Value
                    });

            return bindings;
        }
Example #8
0
        public void AttrsRecognizedStartingWithAtAndEndAfterName()
        {
            var grammar = new BindingGrammar();
            var result1 = grammar.NameReference(Source("@caption"));
            var result2 = grammar.NameReference(Source(" @caption"));

            Assert.That(result1.Value, Is.InstanceOfType(typeof(BindingNameReference)));
            Assert.That(result2, Is.Null);

            var value1 = (BindingNameReference)result1.Value;
            Assert.That(value1.Name, Is.EqualTo("caption"));

            var result3 = grammar.NameReference(Source("@extra stuff"));

            Assert.That(result3.Value, Is.InstanceOfType(typeof(BindingNameReference)));
            var value3 = (BindingNameReference)result3.Value;
            Assert.That(value3.Name, Is.EqualTo("extra"));
        }
Example #9
0
        public void BothBracesMustBePresentToMatch()
        {
            var grammar = new BindingGrammar();
            var result = grammar.Nodes(Source("a{{@*@hello*}}b"));

            Assert.That(result.Value.Count(), Is.EqualTo(4));
            var a = (BindingLiteral)result.Value[0];
            var match1 = (BindingPrefixReference)result.Value[1];
            var match2 = (BindingPrefixReference)result.Value[2];
            var b = (BindingLiteral)result.Value[3];

            Assert.That(a.Text, Is.EqualTo("a{{"));
            Assert.That(b.Text, Is.EqualTo("}}b"));
            Assert.That(match1.Prefix, Is.Null);
            Assert.That(match2.Prefix, Is.EqualTo("hello"));
            Assert.That(match1.AssumeDictionarySyntax, Is.False);
            Assert.That(match2.AssumeDictionarySyntax, Is.False);
        }
Example #10
0
        public void OptionalQuotesMarkAssumeStringValueAsTrue()
        {
            var grammar = new BindingGrammar();
            var result = grammar.Nodes(Source("@one'@two'\"@three\"@four.*'@five.*'\"@six.*\""));
            Assert.That(result.Value.Count(), Is.EqualTo(6));
            var one = (BindingNameReference)result.Value[0];
            var two = (BindingNameReference)result.Value[1];
            var three = (BindingNameReference)result.Value[2];
            var four = (BindingPrefixReference)result.Value[3];
            var five = (BindingPrefixReference)result.Value[4];
            var six = (BindingPrefixReference)result.Value[5];

            Assert.That(one.AssumeStringValue, Is.False);
            Assert.That(two.AssumeStringValue, Is.True);
            Assert.That(three.AssumeStringValue, Is.True);
            Assert.That(four.AssumeStringValue, Is.False);
            Assert.That(five.AssumeStringValue, Is.True);
            Assert.That(six.AssumeStringValue, Is.True);
        }
Example #11
0
        public void BracesMayAppearAroundStrings()
        {
            var grammar = new BindingGrammar();
            var result = grammar.Nodes(Source("a{{'@*'}}{{\"@hello*\"}}b"));

            Assert.That(result.Value.Count(), Is.EqualTo(4));
            var a = (BindingLiteral)result.Value[0];
            var match1 = (BindingPrefixReference)result.Value[1];
            var match2 = (BindingPrefixReference)result.Value[2];
            var b = (BindingLiteral)result.Value[3];

            Assert.That(a.Text, Is.EqualTo("a"));
            Assert.That(b.Text, Is.EqualTo("b"));
            Assert.That(match1.Prefix, Is.Null);
            Assert.That(match2.Prefix, Is.EqualTo("hello"));
            Assert.That(match1.AssumeDictionarySyntax, Is.True);
            Assert.That(match2.AssumeDictionarySyntax, Is.True);
            Assert.That(match1.AssumeStringValue, Is.True);
            Assert.That(match2.AssumeStringValue, Is.True);
        }
Example #12
0
        private static Binding ParseBinding(XElement element, BindingGrammar grammar)
        {
            var binding = new Binding
                          {
                              ElementName = (string)element.Attribute("name")
                          };

            var start = element.Element("start");
            var end = element.Element("end");

            if (start != null && end != null)
            {
                binding.Phrases = new[]
                                  {
                                      ParsePhrase(start, grammar),
                                      ParsePhrase(end, grammar)
                                  };
            }
            else
            {
                binding.Phrases = new[]
                                  {
                                      ParsePhrase(element, grammar)
                                  };
            }

            binding.HasChildReference = binding.Phrases
                .SelectMany(phrase => phrase.Nodes)
                .OfType<BindingChildReference>()
                .Any();

            if (binding.Phrases.Count() > 1 && binding.HasChildReference)
            {
                throw new CompilerException("Binding element '" + element.Attribute("name") +
                                            "' can not have child::* in start or end phrases.");
            }

            return binding;
        }
Example #13
0
        private static Binding ParseBinding(XElement element, BindingGrammar grammar)
        {
            var binding = new Binding
            {
                ElementName = (string)element.Attribute("name")
            };

            var start = element.Element("start");
            var end   = element.Element("end");

            if (start != null && end != null)
            {
                binding.Phrases = new[]
                {
                    ParsePhrase(start, grammar),
                    ParsePhrase(end, grammar)
                };
            }
            else
            {
                binding.Phrases = new[]
                {
                    ParsePhrase(element, grammar)
                };
            }

            binding.HasChildReference = binding.Phrases
                                        .SelectMany(phrase => phrase.Nodes)
                                        .OfType <BindingChildReference>()
                                        .Any();

            if (binding.Phrases.Count() > 1 && binding.HasChildReference)
            {
                throw new CompilerException("Binding element '" + element.Attribute("name") +
                                            "' can not have child::* in start or end phrases.");
            }

            return(binding);
        }
        private static Binding ParseBinding(XElement element, BindingGrammar grammar)
        {
            Binding binding = new Binding {
                ElementName = (string)element.Attribute("name")
            };
            XElement element2 = element.Element("start");
            XElement element3 = element.Element("end");

            if ((element2 != null) && (element3 != null))
            {
                binding.Phrases = new BindingPhrase[] { ParsePhrase(element2, grammar), ParsePhrase(element3, grammar) };
            }
            else
            {
                binding.Phrases = new BindingPhrase[] { ParsePhrase(element, grammar) };
            }
            binding.HasChildReference = (from phrase in binding.Phrases select phrase.Nodes).OfType <BindingChildReference>().Any <BindingChildReference>();
            if ((binding.Phrases.Count <BindingPhrase>() > 1) && binding.HasChildReference)
            {
                throw new CompilerException("Binding element '" + element.Attribute("name") + "' can not have child::* in start or end phrases.");
            }
            return(binding);
        }
Example #15
0
 private static BindingPhrase ParsePhrase(XElement element, BindingGrammar grammar)
 {
     return grammar.Phrase(new Position(new SourceContext(element.Value))).Value;
 }
Example #16
0
 private static BindingPhrase ParsePhrase(XElement element, BindingGrammar grammar)
 {
     return(grammar.Phrase(new Position(new SourceContext(element.Value))).Value);
 }
Example #17
0
        public void PrefixRequiresTrailingAsterisk()
        {
            var grammar = new BindingGrammar();

            var result1 = grammar.PrefixReference(Source("@caption.*x"));
            var value1 = (BindingPrefixReference)result1.Value;
            Assert.That(value1.Prefix, Is.EqualTo("caption."));
            Assert.That(result1.Rest.Peek(), Is.EqualTo('x'));

            var result2 = grammar.PrefixReference(Source("@*y"));
            var value2 = (BindingPrefixReference)result2.Value;
            Assert.That(value2.Prefix ?? "", Is.EqualTo(""));
            Assert.That(result2.Rest.Peek(), Is.EqualTo('y'));
        }
Example #18
0
        public void PrefixLetsYouWildcardAsTheLastCharacter()
        {
            var grammar = new BindingGrammar();
            var result = grammar.Nodes(Source("Html.ActionLink(@caption.**, @**)"));

            Assert.That(result.Value.Count(), Is.EqualTo(5));

            Assert.That(((BindingLiteral)result.Value[0]).Text, Is.EqualTo("Html.ActionLink("));
            Assert.That(((BindingPrefixReference)result.Value[1]).Prefix, Is.EqualTo("caption."));
            Assert.That(((BindingLiteral)result.Value[2]).Text, Is.EqualTo("*, "));
            Assert.That(((BindingPrefixReference)result.Value[3]).Prefix ?? "", Is.EqualTo(""));
            Assert.That(((BindingLiteral)result.Value[4]).Text, Is.EqualTo("*)"));
        }
Example #19
0
        public void DoubleSquareBracketIsAliasForAngleBracket()
        {
            var grammar = new BindingGrammar();
            var result = grammar.Nodes(Source("this[[that]]"));

            Assert.That(result.Value.Count(), Is.EqualTo(1));
            var node = (BindingLiteral)result.Value[0];

            Assert.That(node.Text, Is.EqualTo("this<that>"));
        }
Example #20
0
        public void CurleyBracesAroundWildcardIndicateDictionaryInitializingSyntax()
        {
            var grammar = new BindingGrammar();
            var result = grammar.Nodes(Source("a{{@*}}{{@hello*}}b"));

            Assert.That(result.Value.Count(), Is.EqualTo(4));
            var a = (BindingLiteral)result.Value[0];
            var match1 = (BindingPrefixReference)result.Value[1];
            var match2 = (BindingPrefixReference)result.Value[2];
            var b = (BindingLiteral)result.Value[3];

            Assert.That(a.Text, Is.EqualTo("a"));
            Assert.That(b.Text, Is.EqualTo("b"));
            Assert.That(match1.Prefix, Is.Null);
            Assert.That(match2.Prefix, Is.EqualTo("hello"));
            Assert.That(match1.AssumeDictionarySyntax, Is.True);
            Assert.That(match2.AssumeDictionarySyntax, Is.True);
            Assert.That(match1.AssumeStringValue, Is.False);
            Assert.That(match2.AssumeStringValue, Is.False);
        }
Example #21
0
        public void ChildContentCanAppearWithOrWithoutQuotes()
        {
            var grammar = new BindingGrammar();
            var result = grammar.Nodes(Source("achild::*b'child::*'c\"child::*\"d"));
            Assert.That(result.Value.Count(), Is.EqualTo(7));
            var a = (BindingLiteral)result.Value[0];
            var child1 = (BindingChildReference)result.Value[1];
            var b = (BindingLiteral)result.Value[2];
            var child2 = (BindingChildReference)result.Value[3];
            var c = (BindingLiteral)result.Value[4];
            var child3 = (BindingChildReference)result.Value[5];
            var d = (BindingLiteral)result.Value[6];

            Assert.That(a.Text, Is.EqualTo("a"));
            Assert.That(b.Text, Is.EqualTo("b"));
            Assert.That(c.Text, Is.EqualTo("c"));
            Assert.That(d.Text, Is.EqualTo("d"));
            Assert.That(child1, Is.Not.Null);
            Assert.That(child2, Is.Not.Null);
            Assert.That(child3, Is.Not.Null);
        }