Ejemplo n.º 1
0
        public void SimpleAst()
        {
            var input = new Ast.Resource()
            {
                Span = Span(0, 9),
                Body = new Ast.Entry[]
                {
                    new Ast.Message()
                    {
                        Comment    = null,
                        Span       = Span(0, 9),
                        Attributes = new Ast.Attribute[0],
                        Id         = new Ast.Identifier()
                        {
                            Name = "foo",
                            Span = Span(0, 3)
                        },
                        Value = new Ast.Pattern()
                        {
                            Elements = new Ast.SyntaxNode[]
                            {
                                new Ast.TextElement("Foo")
                            },
                            Span = Span(6, 9)
                        }
                    }
                }
            };

            var serializer = new Serializer();

            using (var sw = new StringWriter())
            {
                serializer.Serialize(sw, input);
                string actual   = sw.ToString();
                string expected = "foo = Foo\n";
                actual.Should().Be(expected);
            }
        }
Ejemplo n.º 2
0
        public void VariantTest()
        {
            string ftl    = @"
                # Comment
                ## Group comment
                ### Resource comment
                foo = { 1 ->
                    *[one] One
                     [two] Two
                    }
            ";
            var    output = new Ast.Resource()
            {
                Span = new Ast.Span(
                    new Position(0, 1, 1),
                    new Position(97, 8, 1)),
                Body = new List <Ast.Entry>()
                {
                    new Ast.Comment()
                    {
                        Span = new Ast.Span(
                            new Position(0, 1, 1),
                            new Position(9, 1, 10)),
                        Content = "Comment"
                    },
                    new Ast.GroupComment()
                    {
                        Span = new Ast.Span(
                            new Position(10, 2, 1),
                            new Position(26, 2, 17)),
                        Content = "Group comment"
                    },
                    new Ast.ResourceComment()
                    {
                        Span = new Ast.Span(
                            new Position(27, 3, 1),
                            new Position(47, 3, 21)),
                        Content = "Resource comment"
                    },
                    new Ast.Message()
                    {
                        Id = new Ast.Identifier()
                        {
                            Span = new Ast.Span(
                                new Position(48, 4, 1),
                                new Position(51, 4, 4)),
                            Name = "foo"
                        },
                        Span = new Ast.Span(
                            new Position(48, 4, 1),
                            new Position(96, 7, 6)),
                        Value = new Ast.Pattern()
                        {
                            Span = new Ast.Span(
                                new Position(54, 4, 7),
                                new Position(96, 7, 6)),
                            Elements = new List <Ast.SyntaxNode>()
                            {
                                new Ast.Placeable()
                                {
                                    Span = new Ast.Span(
                                        new Position(54, 4, 7),
                                        new Position(96, 7, 6)),
                                    Expression = new Ast.SelectExpression()
                                    {
                                        Span = new Ast.Span(
                                            new Position(55, 4, 8),
                                            new Position(95, 7, 5)),
                                        Selector = new Ast.NumberLiteral()
                                        {
                                            Span = new Ast.Span(
                                                new Position(56, 4, 9),
                                                new Position(57, 4, 10)),
                                            Value = "1"
                                        },
                                        Variants = new List <Ast.Variant>()
                                        {
                                            new Ast.Variant()
                                            {
                                                Span = new Ast.Span(
                                                    new Position(65, 5, 5),
                                                    new Position(75, 5, 15)),
                                                IsDefault = true,
                                                Key       = new Ast.VariantName()
                                                {
                                                    Span = new Ast.Span(
                                                        new Position(67, 5, 7),
                                                        new Position(70, 5, 10)),
                                                    Name = "one"
                                                },
                                                Value = new Ast.Pattern()
                                                {
                                                    Span = new Ast.Span(
                                                        new Position(72, 5, 12),
                                                        new Position(75, 5, 15)),
                                                    Elements = new List <Ast.SyntaxNode>()
                                                    {
                                                        new Ast.TextElement()
                                                        {
                                                            Span = new Ast.Span(
                                                                new Position(72, 5, 12),
                                                                new Position(75, 5, 15)),
                                                            Value = "One",
                                                        }
                                                    }
                                                }
                                            },
                                            new Ast.Variant()
                                            {
                                                Span = new Ast.Span(
                                                    new Position(81, 6, 6),
                                                    new Position(90, 6, 15)),
                                                IsDefault = false,
                                                Key       = new Ast.VariantName()
                                                {
                                                    Span = new Ast.Span(
                                                        new Position(82, 6, 7),
                                                        new Position(85, 6, 10)),
                                                    Name = "two"
                                                },
                                                Value = new Ast.Pattern()
                                                {
                                                    Span = new Ast.Span(
                                                        new Position(87, 6, 12),
                                                        new Position(90, 6, 15)),
                                                    Elements = new List <Ast.SyntaxNode>()
                                                    {
                                                        new Ast.TextElement()
                                                        {
                                                            Span = new Ast.Span(
                                                                new Position(87, 6, 12),
                                                                new Position(90, 6, 15)),
                                                            Value = "Two",
                                                        }
                                                    }
                                                }
                                            },
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            };

            using (var sr = new StringReader(Ftl(ftl)))
            {
                var ps      = new Parser(true);
                var message = ps.Parse(sr);
                // Console.WriteLine(AstToJson.ToJson(message));
                message.Should().BeEquivalentTo(output, options =>
                                                options.RespectingRuntimeTypes());
            }
        }