public void Tokenization()
        {
            string source = "void f(int x); void g(int x) { f(x); }";

            using (var empty = disposables.WriteToEmpty(source))
            {
                var file  = empty.GetFile(TestFiles.Empty);
                var range = SourceRange.Create(
                    file.GetLocationFromOffset((uint)source.IndexOf("f(x)")),
                    file.GetLocationFromOffset((uint)(source.IndexOf(" }"))));
                var tokens = empty.Tokenize(range);
                CollectionAssert.AreEqual(
                    new[] { "f", "(", "x", ")", ";" },
                    tokens.ToArray(token => token.GetSpelling()));

                Assert.AreEqual(TokenKind.Identifier, tokens[0].GetKind());
                Assert.AreEqual("f", tokens[0].GetSpelling());
                Assert.AreEqual(
                    file.GetLocationFromOffset((uint)source.IndexOf("f(x)")),
                    tokens[0].GetLocation());
                Assert.AreEqual(
                    (uint)source.IndexOf("f(x)"),
                    tokens[0].GetExtent().GetStart().Offset);
                Assert.AreEqual(
                    (uint)source.IndexOf("f(x)") + 1,
                    tokens[0].GetExtent().GetEnd().Offset);

                Assert.AreEqual(TokenKind.Punctuation, tokens[1].GetKind());
                Assert.AreEqual("(", tokens[1].GetSpelling());
                Assert.AreEqual(TokenKind.Identifier, tokens[2].GetKind());
                Assert.AreEqual("x", tokens[2].GetSpelling());
                Assert.AreEqual(TokenKind.Punctuation, tokens[3].GetKind());
                Assert.AreEqual(")", tokens[3].GetSpelling());
            }
        }
        public void VisitFunctionDeclaration()
        {
            string source = "void foo();";

            string spelling = null;
            var    visitor  = new DelegatingCursorVisitor((cursor, parent) =>
            {
                if (cursor.Kind == CursorKind.FunctionDecl)
                {
                    spelling = cursor.GetSpelling();
                }

                return(ChildVisitResult.Recurse);
            });

            using (var empty = disposables.WriteToEmpty(source))
            {
                Assert.AreEqual(false, visitor.VisitChildren(empty.GetCursor()));
                Assert.AreEqual("foo", spelling);
            }
        }
Example #3
0
 public void TestMethods(
     string source,
     int offset,
     string methodName,
     object[] parameters,
     object expected,
     string resultAdapter)
 {
     using (var empty = disposables.WriteToEmpty(source))
     {
         var    file     = empty.GetFile(TestFiles.Empty);
         var    location = file.GetLocationFromOffset((uint)offset);
         var    typeInfo = empty.GetCursor(location).GetTypeInfo();
         var    method   = typeof(TypeInfo).GetTypeInfo().GetMethod(methodName);
         object result   = method.Invoke(typeInfo, parameters);
         if (resultAdapter != null)
         {
             method = result.GetType().GetTypeInfo().GetMethod(resultAdapter);
             result = method.Invoke(result, null);
         }
         Assert.AreEqual(expected, result);
     }
 }
        public void LinkageKinds()
        {
            string source = "void a() { int b; } static int d; namespace { int c; }";

            using (var empty = disposables.WriteToEmpty(source))
            {
                var file = empty.GetFile(TestFiles.Empty);
                Assert.AreEqual(
                    LinkageKind.External,
                    empty.GetCursor(file.GetLocation(1, 6)).GetLinkage());
                Assert.AreEqual(
                    LinkageKind.NoLinkage,
                    empty.GetCursor(file.GetLocation(1, 16)).GetLinkage());
                Assert.AreEqual(
                    LinkageKind.Internal,
                    empty.GetCursor(file.GetLocation(1, 32)).GetLinkage());
                Assert.AreEqual(
                    LinkageKind.UniqueExternal,
                    empty.GetCursor(file.GetLocation(1, 51)).GetLinkage());
            }
        }