Ejemplo n.º 1
0
        public void CanRoundtripTestcase()
        {
            string testcase = GetTestcase();
            var    ast      = WebIDLParser.Parse(new StringReader(testcase));
            string actual   = WebIDLFormatter.Format(ast);

            //File.WriteAllText("Result.webidl", actual);

            var expectedLines = testcase.Replace("\r\n", "\n").Split('\n');
            var actualLines   = actual.Replace("\r\n", "\n").Split('\n').Select(Normalize).Where(s => s != "").ToArray();
            int actualIndex   = 0;

            for (int expectedIndex = 0; expectedIndex < expectedLines.Length; expectedIndex++)
            {
                string expectedLine = Normalize(expectedLines[expectedIndex].Trim());
                if (expectedLine == "")
                {
                    continue;
                }
                if (actualIndex == actualLines.Length)
                {
                    Assert.Fail("Testcase line #{0}:\nWas \"{1}\".\nNo more expected lines.", expectedIndex + 1, expectedLines[expectedIndex]);
                }
                if (expectedLine != actualLines[actualIndex])
                {
                    Assert.Fail("Testcase line #{0}:\nWas \"{1}\".\nexpected \"{2}\".", expectedIndex + 1, actualLines[actualIndex], expectedLines[expectedIndex]);
                }
                actualIndex++;
            }
        }
Ejemplo n.º 2
0
        private string Format(Dictionary <string, ResolvedDefinition> types)
        {
            var sb = new StringBuilder();

            foreach (var t in types.OrderBy(x => x.Key).Select(x => x.Value))
            {
                t.Decompose(
                    @interface => {
                    if (@interface.ExtendedAttributes.Count > 0)
                    {
                        sb.Append("[" + string.Join(", ", @interface.ExtendedAttributes.Select(WebIDLFormatter.Format)) + "] ");
                    }
                    sb.Append("interface " + @interface.Name + " ");
                    if (@interface.Base != null)
                    {
                        sb.Append(": " + @interface.Base + " ");
                    }
                    sb.AppendLine("{");
                    foreach (var imp in @interface.Implements.OrderBy(x => x))
                    {
                        sb.AppendLine("\timplements " + imp + ";");
                    }
                    sb.Append(string.Join("", @interface.Members.Select(m => "\t" + WebIDLFormatter.Format(m))));
                    sb.Append("}");
                },
                    callbackInterface => {
                    if (callbackInterface.ExtendedAttributes.Count > 0)
                    {
                        sb.Append("[" + string.Join(", ", callbackInterface.ExtendedAttributes.Select(WebIDLFormatter.Format)) + "] ");
                    }
                    sb.Append("callback interface " + callbackInterface.Name + " ");
                    if (callbackInterface.Base != null)
                    {
                        sb.Append(": " + callbackInterface.Base + " ");
                    }
                    sb.AppendLine("{");
                    sb.Append(string.Join("", callbackInterface.Members.Select(m => "\t" + WebIDLFormatter.Format(m))));
                    sb.Append("}");
                },
                    dictionary => {
                    if (dictionary.ExtendedAttributes.Count > 0)
                    {
                        sb.Append("[" + string.Join(", ", dictionary.ExtendedAttributes.Select(WebIDLFormatter.Format)) + "] ");
                    }
                    sb.Append("dictionary " + dictionary.Name + " ");
                    if (dictionary.Base != null)
                    {
                        sb.Append(": " + dictionary.Base + " ");
                    }
                    sb.AppendLine("{");
                    sb.Append(string.Join("", dictionary.Members.Select(m => "\t" + WebIDLFormatter.Format(m))));
                    sb.Append("}");
                },
                    callback => {
                    if (callback.ExtendedAttributes.Count > 0)
                    {
                        sb.Append("[" + string.Join(", ", callback.ExtendedAttributes.Select(WebIDLFormatter.Format)) + "] ");
                    }
                    sb.Append("callback " + callback.Name + " = " + WebIDLFormatter.Format(callback.ReturnType) + " ");
                    sb.Append("(" + string.Join(", ", callback.Arguments.Select(WebIDLFormatter.Format)) + ")");
                },
                    exception => {
                    if (exception.ExtendedAttributes.Count > 0)
                    {
                        sb.Append("[" + string.Join(", ", exception.ExtendedAttributes.Select(WebIDLFormatter.Format)) + "] ");
                    }
                    sb.Append("exception " + exception.Name + " ");
                    if (exception.Base != null)
                    {
                        sb.Append(": " + exception.Base + " ");
                    }
                    sb.AppendLine("{");
                    sb.Append(string.Join("", exception.Members.Select(m => "\t" + WebIDLFormatter.Format(m))));
                    sb.Append("}");
                },
                    @enum => {
                    if (@enum.ExtendedAttributes.Count > 0)
                    {
                        sb.Append("[" + string.Join(", ", @enum.ExtendedAttributes.Select(WebIDLFormatter.Format)) + "] ");
                    }
                    sb.Append("enum " + @enum.Name + " { " + string.Join(", ", @enum.Values.Select(v => "\"" + v + "\"")) + " }");
                },
                    declaredInterface => {
                    sb.Append("interface " + declaredInterface.Name);
                }
                    );
                sb.AppendLine(";");
            }

            return(sb.ToString());
        }