Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests #240

Merged
merged 6 commits into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Microsoft.VisualStudio.Debugger.Interop.10.0" Version="17.0.32112.339" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="8.0.0" />
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using ast_visual_studio_extension.CxExtension.Enums;
using ast_visual_studio_extension.CxWrapper.Models;

using System.Windows.Controls;
using Xunit;

namespace ast_visual_studio_extension_tests.cx_unit_tests.cx_extansion_test
{
public class CxUtilsTests
{
[Theory]
[InlineData("CRITICAL", Severity.CRITICAL)]
[InlineData("HIGH", Severity.HIGH)]
[InlineData("MEDIUM", Severity.MEDIUM)]
[InlineData("LOW", Severity.LOW)]
[InlineData("INFO", Severity.INFO)]
public void GetSeverityFromString_ShouldReturnCorrectSeverity(string severity, Severity expectedSeverity)
{
var result = ast_visual_studio_extension.CxExtension.Utils.CxUtils.GetSeverityFromString(severity);

Assert.Equal(expectedSeverity, result);
}


[Theory]
[InlineData("Project2", ComboboxType.PROJECTS, new[] { "Project1", "Project2" }, 1)]
[InlineData("Scan2", ComboboxType.SCANS, new[] { "Scan1", "Scan2" }, 1)]
[InlineData("NonExistent", ComboboxType.PROJECTS, new[] { "Project1", "Project2" }, -1)]
public void GetItemIndexInCombo_ShouldReturnCorrectIndexForTag(string searchValue, ComboboxType comboType, string[] items, int expectedIndex)
{
StaThreadHelper.RunInStaThread(() =>
{
var comboBox = new ComboBox();

foreach (var item in items)
{
object tag = null;

if (comboType == ComboboxType.PROJECTS)
tag = new Project { Id = item };
else if (comboType == ComboboxType.SCANS)
tag = new Scan { ID = item };

var comboBoxItem = new ComboBoxItem { Tag = tag };
comboBox.Items.Add(comboBoxItem);
}

var result = ast_visual_studio_extension.CxExtension.Utils.CxUtils.GetItemIndexInCombo(searchValue, comboBox, comboType);

Assert.Equal(expectedIndex, result);
});
}


[Theory]
[InlineData("Branch2", ComboboxType.BRANCHES, new[] { "Branch1", "Branch2" }, 1)]
[InlineData("HIGH", ComboboxType.SEVERITY, new[] { "LOW", "HIGH" }, 1)]
[InlineData("CONFIRMED", ComboboxType.STATE, new[] { "TO_VERIFY", "CONFIRMED" }, 1)]
[InlineData("NonExistent", ComboboxType.BRANCHES, new[] { "Branch1", "Branch2" }, -1)]
public void GetItemIndexInCombo_ShouldReturnCorrectIndexForContent(string searchValue, ComboboxType comboType, string[] items, int expectedIndex)
{
StaThreadHelper.RunInStaThread(() =>
{
var comboBox = new ComboBox();
foreach (var item in items)
{
comboBox.Items.Add(new ComboBoxItem { Content = item });
}

var result = ast_visual_studio_extension.CxExtension.Utils.CxUtils.GetItemIndexInCombo(searchValue, comboBox, comboType);

Assert.Equal(expectedIndex, result);
});
}

[Theory]
[InlineData("ThisIsAVeryLongFileNameThatExceedsTheMaximumAllowedLength.txt", "...NameThatExceedsTheMaximumAllowedLength.txt")]
[InlineData("ShortFileName.txt", "ShortFileName.txt")]
public void CapToLen_ShouldHandleFileNameProperly(string fileName, string expected)
{
var result = ast_visual_studio_extension.CxExtension.Utils.CxUtils.CapToLen(fileName);

Assert.Equal(expected, result);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Threading;

namespace ast_visual_studio_extension_tests.cx_unit_tests.cx_extansion_test
{
public static class StaThreadHelper
{
public static void RunInStaThread(Action action)
{
var staThread = new Thread(() =>
{
action();
});
staThread.SetApartmentState(ApartmentState.STA);
staThread.Start();
staThread.Join();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using System.Collections.Generic;
using System.Windows.Controls;
using Xunit;
using ast_visual_studio_extension.CxExtension.Utils;
using ast_visual_studio_extension.CxExtension.Enums;

namespace ast_visual_studio_extension_tests.cx_unit_tests.cx_extansion_test
{
public class UIUtilsTests
{
[Fact]
public void CreateTreeViewItemHeader_ShouldReturnTextBlock()
{
StaThreadHelper.RunInStaThread(() =>
{
string severity = Severity.HIGH.ToString();
string displayName = "Test_Item";

TextBlock result = UIUtils.CreateTreeViewItemHeader(severity, displayName);

Assert.NotNull(result);
Assert.IsType<TextBlock>(result);
Assert.Contains("Test__Item", result.Tag.ToString());
});
}

[Fact]
public void CreateTreeViewItemWithItemsSource_ShouldReturnTreeViewItem()
{
StaThreadHelper.RunInStaThread(() =>
{
string headerText = "Header";
List<TreeViewItem> source = new List<TreeViewItem>
{
new TreeViewItem { Header = "Item1" },
new TreeViewItem { Header = "Item2" }
};

TreeViewItem result = UIUtils.CreateTreeViewItemWithItemsSource(headerText, source);

Assert.NotNull(result);
Assert.IsType<TreeViewItem>(result);
Assert.Equal(headerText, result.Tag);
Assert.Equal(source, result.ItemsSource);
});
}

[Fact]
public void CreateTextBlock_ShouldReturnTextBlock()
{
StaThreadHelper.RunInStaThread(() =>
{
string message = "Test Message";

TextBlock result = UIUtils.CreateTextBlock(message);

Assert.NotNull(result);
Assert.IsType<TextBlock>(result);
Assert.Equal(message, result.Text);
});
}

[Fact]
public void CreateSeverityLabelWithIcon_ShouldReturnStackPanel()
{
StaThreadHelper.RunInStaThread(() =>
{
string severity = Severity.HIGH.ToString();

StackPanel result = UIUtils.CreateSeverityLabelWithIcon(severity);

Assert.NotNull(result);
Assert.IsType<StackPanel>(result);
Assert.Equal(2, result.Children.Count);
Assert.IsType<Image>(result.Children[0]);
Assert.IsType<Label>(result.Children[1]);
});
}

[Fact]
public void CreateLabelWithImage_ShouldReturnStackPanel()
{
StaThreadHelper.RunInStaThread(() =>
{
string message = "Test Message";
string icon = CxConstants.ICON_FLAG;

StackPanel result = UIUtils.CreateLabelWithImage(message, icon);

Assert.NotNull(result);
Assert.IsType<StackPanel>(result);
Assert.Equal(2, result.Children.Count);
Assert.IsType<Image>(result.Children[0]);
Assert.IsType<Label>(result.Children[1]);
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System.Windows;
using System.Windows.Controls;
using ast_visual_studio_extension;
using Xunit;

namespace ast_visual_studio_extension_tests.cx_unit_tests.cx_extansion_test
{
public class VsThemeTests
{
[Fact]
public void SetUseVsTheme_ShouldApplyTheme_WhenValueIsTrue()
{
StaThreadHelper.RunInStaThread(() =>
{
var element = new ContentControl();
VsTheme.SetUseVsTheme(element, true);

var isUsingTheme = VsTheme.GetUseVsTheme(element);

Assert.True(isUsingTheme);
Assert.NotNull(element.Resources);
});
}

[Fact]
public void GetUseVsTheme_ShouldReturnFalse_WhenElementIsNotThemed()
{
StaThreadHelper.RunInStaThread(() =>
{
var element = new ContentControl();

var isUsingTheme = VsTheme.GetUseVsTheme(element);

Assert.False(isUsingTheme);
});
}

[Fact]
public void GetUseVsTheme_ShouldReturnTrue_WhenElementIsThemed()
{
StaThreadHelper.RunInStaThread(() =>
{
var element = new ContentControl();
VsTheme.SetUseVsTheme(element, true);

var isUsingTheme = VsTheme.GetUseVsTheme(element);

Assert.True(isUsingTheme);
});
}

[Fact]
public void UseVsThemePropertyChanged_ShouldApplyTheme_WhenValueIsTrue()
{
StaThreadHelper.RunInStaThread(() =>
{
var element = new ContentControl();
var dependencyObject = (DependencyObject)element;
var args = new DependencyPropertyChangedEventArgs(VsTheme.UseVsThemeProperty, false, true);

typeof(VsTheme).GetMethod("UseVsThemePropertyChanged", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static)
.Invoke(null, new object[] { dependencyObject, args });

var isUsingTheme = VsTheme.GetUseVsTheme(element);
Assert.True(isUsingTheme);
});
}

[Fact]
public void UseVsThemePropertyChanged_ShouldRemoveTheme_WhenValueIsFalse()
{
StaThreadHelper.RunInStaThread(() =>
{
var element = new ContentControl();
var dependencyObject = (DependencyObject)element;
var args = new DependencyPropertyChangedEventArgs(VsTheme.UseVsThemeProperty, true, false);

typeof(VsTheme).GetMethod("UseVsThemePropertyChanged", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static)
.Invoke(null, new object[] { dependencyObject, args });

var isUsingTheme = VsTheme.GetUseVsTheme(element);
Assert.False(isUsingTheme);
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Collections.Generic;
using Xunit;
using ast_visual_studio_extension.CxCLI;

namespace ast_visual_studio_extension.Tests
{
public class CxUtilsTests
{
[Theory]
[InlineData("", new string[] { })]
[InlineData("param1 param2", new string[] { "param1", "param2" })]
[InlineData("\"param with spaces\" 'another param'", new string[] { "\"param with spaces\"", "\"another param\"" })]
[InlineData("param1 \"param with spaces\" param2", new string[] { "param1", "\"param with spaces\"", "param2" })]
[InlineData("param1 'param with spaces' param2", new string[] { "param1", "\"param with spaces\"", "param2" })]
public void ParseAdditionalParameters_ValidInput_ReturnsExpectedList(string input, string[] expected)
{
List<string> result = CxUtils.ParseAdditionalParameters(input);

Assert.Equal(expected, result);
}

[Fact]
public void ParseAdditionalParameters_NullInput_ReturnsEmptyList()
{
List<string> result = CxUtils.ParseAdditionalParameters(null);

Assert.Empty(result);
}
}
}
Loading
Loading