<commit_before>﻿// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Globalization;
using FluentAssertions;
using Microsoft.CodeAnalysis.Sarif;
using Microsoft.Sarif.Viewer.Converters;
using Microsoft.Sarif.Viewer.Models;
using Xunit;

namespace Microsoft.Sarif.Viewer.VisualStudio.Converters.UnitTests
{
    public class CallTreeNodeToTextConverterTests
    {
        [Fact]
        public void CallTreeNodeToTextConverter_HandlesCall()
        {
            var callTreeNode = new CallTreeNode
            {
                Location = new AnnotatedCodeLocation
                {
                    Kind = AnnotatedCodeLocationKind.Call,
                    Callee = "my_function",
                    PhysicalLocation = new PhysicalLocation
                    {
                        Region = new Region
                        {
                            StartLine = 42
                        }
                    }
                }
            };

            VerifyConversion(callTreeNode, "42: my_function");
        }

        [Fact]
        public void CallTreeNodeToTextConverter_HandlesCallWithNoCallee()
        {
            var callTreeNode = new CallTreeNode
            {
                Location = new AnnotatedCodeLocation
                {
                    Kind = AnnotatedCodeLocationKind.Call,
                    PhysicalLocation = new PhysicalLocation
                    {
                        Region = new Region
                        {
                            StartLine = 42
                        }
                    }
                }
            };

            VerifyConversion(callTreeNode, "42: <unknown callee>");
        }

        [Fact]
        public void CallTreeNodeToTextConverter_HandlesReturn()
        {
            var callTreeNode = new CallTreeNode
            {
                Location = new AnnotatedCodeLocation
                {
                    Kind = AnnotatedCodeLocationKind.CallReturn,
                    PhysicalLocation = new PhysicalLocation
                    {
                        Region = new Region
                        {
                            StartLine = 42
                        }
                    }
                }
            };

            VerifyConversion(callTreeNode, "42: Return");
        }

        [Fact]
        public void CallTreeNodeToTextConverter_HandlesNonCallOrReturnNodes()
        {
            var callTreeNode = new CallTreeNode
            {
                Location = new AnnotatedCodeLocation
                {
                    Kind = AnnotatedCodeLocationKind.Continuation,
                    PhysicalLocation = new PhysicalLocation
                    {
                        Region = new Region
                        {
                            StartLine = 42
                        }
                    }
                }
            };

            VerifyConversion(callTreeNode, "42: Continuation");
        }

        [Fact]
        public void CallTreeNodeToTextConverter_HandlesMissingRegion()
        {
            var callTreeNode = new CallTreeNode
            {
                Location = new AnnotatedCodeLocation
                {
                    Kind = AnnotatedCodeLocationKind.CallReturn,
                    PhysicalLocation = new PhysicalLocation()
                }
            };

            VerifyConversion(callTreeNode, "Return");
        }
<commit_msg>Fix broken vsix tests Add tests cases for display names for CallTreeNodes with Message and Snippet<commit_after>﻿// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Globalization;
using FluentAssertions;
using Microsoft.CodeAnalysis.Sarif;
using Microsoft.Sarif.Viewer.Converters;
using Microsoft.Sarif.Viewer.Models;
using Xunit;

namespace Microsoft.Sarif.Viewer.VisualStudio.Converters.UnitTests
{
    public class CallTreeNodeToTextConverterTests
    {
        [Fact]
        public void CallTreeNodeToTextConverter_HandlesCall()
        {
            var callTreeNode = new CallTreeNode
            {
                Location = new AnnotatedCodeLocation
                {
                    Kind = AnnotatedCodeLocationKind.Call,
                    Callee = "my_function",
                    PhysicalLocation = new PhysicalLocation
                    {
                        Region = new Region
                        {
                            StartLine = 42
                        }
                    }
                }
            };

            VerifyConversion(callTreeNode, "my_function");
        }

        [Fact]
        public void CallTreeNodeToTextConverter_HandlesCallWithNoCallee()
        {
            var callTreeNode = new CallTreeNode
            {
                Location = new AnnotatedCodeLocation
                {
                    Kind = AnnotatedCodeLocationKind.Call,
                    PhysicalLocation = new PhysicalLocation
                    {
                        Region = new Region
                        {
                            StartLine = 42
                        }
                    }
                }
            };

            VerifyConversion(callTreeNode, "<unknown callee>");
        }

        [Fact]
        public void CallTreeNodeToTextConverter_HandlesReturn()
        {
            var callTreeNode = new CallTreeNode
            {
                Location = new AnnotatedCodeLocation
                {
                    Kind = AnnotatedCodeLocationKind.CallReturn,
                    PhysicalLocation = new PhysicalLocation
                    {
                        Region = new Region
                        {
                            StartLine = 42
                        }
                    }
                }
            };

            VerifyConversion(callTreeNode, "Return");
        }

        [Fact]
        public void CallTreeNodeToTextConverter_HandlesNonCallOrReturnNodes()
        {
            var callTreeNode = new CallTreeNode
            {
                Location = new AnnotatedCodeLocation
                {
                    Kind = AnnotatedCodeLocationKind.Continuation,
                    PhysicalLocation = new PhysicalLocation
                    {
                        Region = new Region
                        {
                            StartLine = 42
                        }
                    }
                }
            };

            VerifyConversion(callTreeNode, "Continuation");
        }

        [Fact]
        public void CallTreeNodeToTextConverter_HandlesMissingRegion()
        {
            var callTreeNode = new CallTreeNode
            {
                Location = new AnnotatedCodeLocation
                {
                    Kind = AnnotatedCodeLocationKind.CallReturn,
                    PhysicalLocation = new PhysicalLocation()
                }
            };

            VerifyConversion(callTreeNode, "Return");
        }

        [Fact]
        public void CallTreeNodeToTextConverter_HandlesMessage()
        {
            string snippet = "    contentStores[0] = contentStores[index];";
            string message = "The error happened here.";
            string sourceFile = @"file:///c:/dir1/dir%202\source%20file.cpp";

            var callTreeNode = new CallTreeNode
            {
                Location = new AnnotatedCodeLocation
                {
                    Kind = AnnotatedCodeLocationKind.Call,
                    Snippet = snippet,
                    Message = message,
                    Callee = "my_function",
                    PhysicalLocation = new PhysicalLocation
                    {
                        Uri = new System.Uri(sourceFile),
                        Region = new Region
                        {
                            StartLine = 42
                        }
                    }
                }
            };

            VerifyConversion(callTreeNode, message);
        }

        [Fact]
        public void CallTreeNodeToTextConverter_HandlesNullMessage()
        {
            string snippet = "    contentStores[0] = contentStores[index];";
            string message = null;
            string sourceFile = @"file:///c:/dir1/dir%202\source%20file.cpp";

            var callTreeNode = new CallTreeNode
            {
                Location = new AnnotatedCodeLocation
                {
                    Kind = AnnotatedCodeLocationKind.Call,
                    Snippet = snippet,
                    Message = message,
                    Callee = "my_function",
                    PhysicalLocation = new PhysicalLocation
                    {
                        Uri = new System.Uri(sourceFile),
                        Region = new Region
                        {
                            StartLine = 42
                        }
                    }
                }
            };

            VerifyConversion(callTreeNode, snippet.Trim());
        }

        [Fact]
        public void CallTreeNodeToTextConverter_HandlesSnippet()
        {
            string snippet = "    contentStores[0] = contentStores[index];";
            string sourceFile = @"file:///c:/dir1/dir%202\source%20file.cpp";

            var callTreeNode = new CallTreeNode
            {
                Location = new AnnotatedCodeLocation
                {
                    Kind = AnnotatedCodeLocationKind.Call,
                    Snippet = snippet,
                    Callee = "my_function",
                    PhysicalLocation = new PhysicalLocation
                    {
                        Uri = new System.Uri(sourceFile),
                        Region = new Region
                        {
                            StartLine = 42
                        }
                    }
                }
            };

            VerifyConversion(callTreeNode, snippet.Trim());
        }

        [Fact]
        public void CallTreeNodeToTextConverter_HandlesNullSnippet()
        {
            string snippet = null;
            string sourceFile = @"file:///c:/dir1/dir%202\source%20file.cpp";

            var callTreeNode = new CallTreeNode
            {
                Location = new AnnotatedCodeLocation
                {
                    Kind = AnnotatedCodeLocationKind.Call,
                    Snippet = snippet,
                    Callee = "my_function",
                    PhysicalLocation = new PhysicalLocation
                    {
                        Uri = new System.Uri(sourceFile),
                        Region = new Region
                        {
                            StartLine = 42
                        }
                    }
                }
            };

            VerifyConversion(callTreeNode, "my_function");
        }

{'fraction_non_alphanumeric': 0.057903826924825014, 'fraction_numerical': 0.005363148804654123, 'mean_word_length': 1.0465029761904763, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5111055', 'n_tokens_mistral': 2540, 'n_tokens_neox': 2295, 'n_words': 485}