Repository: dotnet/roslyn
File: src/Compilers/CSharp/Test/Symbol/Compilation/GetSemanticInfoTests.cs (C#)

Code before:

            var compilation = CreateCompilation(sourceCode);
            var tree = compilation.SyntaxTrees[0];
            var model = (CSharpSemanticModel)compilation.GetSemanticModel(tree);

            var symbolInfo = symbolInfoAt("Z //bind region");
            Assert.Null(symbolInfo.Symbol);

            symbolInfo = symbolInfoAt("Z //bind endregion");
            Assert.Null(symbolInfo.Symbol);

            symbolInfo = symbolInfoAt("Z //bind pragma warning disable");
            Assert.Null(symbolInfo.Symbol);

            symbolInfo = symbolInfoAt("Z //bind warning");
            Assert.Null(symbolInfo.Symbol);

            symbolInfo = symbolInfoAt("Z //bind error");
            Assert.Null(symbolInfo.Symbol);

            PreprocessingSymbolInfo symbolInfoAt(string substringToMatch)
            {
                var position = sourceCode.IndexOf(substringToMatch, StringComparison.Ordinal);
                var nameSyntaxToBind = tree.GetRoot().FindToken(position, findInsideTrivia: true).Parent as IdentifierNameSyntax;

                if (nameSyntaxToBind is null)
                    return PreprocessingSymbolInfo.None;

                return model.GetPreprocessingSymbolInfo(nameSyntaxToBind);
            }
        }

        [Fact, WorkItem(720566, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/720566")]
        public void Bug720566_01()
        {
            string sourceCode = @"
#define Z

#if Z //bind
#endif
";
            PreprocessingSymbolInfo symbolInfo = GetPreprocessingSymbolInfoForTest(sourceCode, "Z //bind");
            Assert.Equal("Z", symbolInfo.Symbol.Name);
            Assert.True(symbolInfo.IsDefined, "must be defined");
        }

        [Fact, WorkItem(720566, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/720566")]
        public void Bug720566_02()
        {
            string sourceCode = @"
#if true

Review comment (suggestion):
>if (nameSyntaxToBind is null) [](http://example.com/codeflow?start=16&length=29)

This makes the test fragile. Someone can make a mistake on the previous statement and the test will silently stop testing the target scenario. Since the API takes `IdentifierNameSyntax`, we are not really interested in calling this method for other situations. #Closed

Code after:
        {
            string sourceCode = @"
#define Z

#if Z //bind
#endif
";
            PreprocessingSymbolInfo symbolInfo = GetPreprocessingSymbolInfoForTest(sourceCode, "Z //bind");
            Assert.Equal("Z", symbolInfo.Symbol.Name);
            Assert.True(symbolInfo.IsDefined, "must be defined");
        }

        [Fact, WorkItem(720566, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/720566")]
        public void Bug720566_02()
        {
            string sourceCode = @"
#if true
    #define Z

    #if Z //bind
    #endif
#endif
";
            PreprocessingSymbolInfo symbolInfo = GetPreprocessingSymbolInfoForTest(sourceCode, "Z //bind");
            Assert.Equal("Z", symbolInfo.Symbol.Name);
            Assert.True(symbolInfo.IsDefined, "must be defined");
        }

        [Fact, WorkItem(720566, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/720566")]
        public void Bug720566_03()
        {
            string sourceCode = @"
#if false
    #define Z

    #if Z //bind
    #endif
#endif
";
            PreprocessingSymbolInfo symbolInfo = GetPreprocessingSymbolInfoForTest(sourceCode, "Z //bind");
            Assert.Equal("Z", symbolInfo.Symbol.Name);
            Assert.True(symbolInfo.IsDefined, "must be defined");
        }

        [Fact, WorkItem(720566, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/720566")]
        public void Bug720566_04()
        {
            string sourceCode = @"
#if true
    #define Z
#endif