using RoslynCodeSearcher;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using Roslyn.Compilers.CSharp;
using Roslyn.Services;
using Roslyn.Compilers.VisualBasic;
using System.Linq;
using System.Linq.Expressions;
using System.IO;
using Roslyn.Compilers.Common;
using System.Diagnostics;
namespace RoslynCodesearcher.Unittest
{
/// <summary>
///</summary>
[TestClass()]
public class CodeSearcherTest
{
private TestContext testContextInstance;
/// <summary>
///Gets or sets the test context which provides
///information about and functionality for the current test run.
///</summary>
public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
#region Additional test attributes
//
//You can use the following additional attributes as you write your tests:
//
//Use ClassInitialize to run code before running the first test in the class
//[ClassInitialize()]
//public static void MyClassInitialize(TestContext testContext)
//{
//}
//
//Use ClassCleanup to run code after all tests in a class have run
//[ClassCleanup()]
//public static void MyClassCleanup()
//{
//}
//
//Use TestInitialize to run code before running each test
//[TestInitialize()]
//public void MyTestInitialize()
//{
//}
//
//Executed after each test has run
[TestCleanup()]
public void MyTestCleanup()
{
Cleanup();
}
#endregion
/// <summary>
///A test for CodeSearcher Constructor
///</summary>
[TestMethod()]
public void CodeSearcherConstructorTest()
{
CodeSearcher target = new CodeSearcher();
Assert.IsNotNull(target);
}
/// <summary>
///A test for GetExcludes
///</summary>
[TestMethod()]
public void GetExcludesTest()
{
string filterText = "first, second";
List<string> expected = new List<string>() { "FIRST", "SECOND" };
List<string> actual;
actual = CodeSearcher.GetExcludes(filterText);
Assert.IsTrue(expected.SequenceEqual(actual), "we expect a list of 2 strings");
}
/// <summary>
///A test for GetMethodOrPropertyTextCSharp
///</summary>
[TestMethod()]
[DeploymentItem("RoslynCodeSearcher.exe")]
public void GetMethodOrPropertyTextCSharpTest()
{
string actual = "";
string expected = TestConstants.SOMEMETHODTEXTCSHARP;
CodeSearcher_Accessor target = new CodeSearcher_Accessor();
foreach (IDocument document in TestHelper.GetTestDocumentsCSharp())
{
CommonSyntaxTree syntax = document.GetSyntaxTree();
var root = (Roslyn.Compilers.CSharp.CompilationUnitSyntax)syntax.GetRoot();
var methods = from method in root.DescendantNodes().Where(x => x is MethodDeclarationSyntax)
select method;
actual = target.GetMethodOrPropertyTextCSharp(methods.First(), document);
Assert.IsTrue(actual.Contains(expected), "We expect the method text");
}
}
/// <summary>
///A test for GetMethodOrPropertyTextVB
///</summary>
[TestMethod()]
[DeploymentItem("RoslynCodeSearcher.exe")]
public void GetMethodOrPropertyTextVBTest()
{
string actual = "";
string expected = TestConstants.SOMEMETHODTEXTVB;
CodeSearcher_Accessor target = new CodeSearcher_Accessor();
IDocument document = TestHelper.GetTestDocumentsVB().First();
CommonSyntaxTree syntax = document.GetSyntaxTree();
var root = (Roslyn.Compilers.VisualBasic.CompilationUnitSyntax)syntax.GetRoot();
var firstMethod = (from method in root.DescendantNodes().Where(x => x is Roslyn.Compilers.VisualBasic.MethodBlockSyntax)
select method).First();
actual = target.GetMethodOrPropertyTextVB(firstMethod, document);
Assert.IsTrue(actual.Contains(expected), "We expect the method text");
}
/// <summary>
///A test for GetSearchCallersCSharp
///</summary>
[TestMethod()]
[DeploymentItem("RoslynCodeSearcher.exe")]
public void GetSearchCallersCSharpTest()
{
CodeSearcher_Accessor target = new CodeSearcher_Accessor();
IDocument document = TestHelper.GetTestDocumentsCSharp().First();
string textToSearch = "SOMEMETHOD";
string expected = TestConstants.SOMEMETHODCALLERCSHARP;
string actual;
actual = target.GetSearchCallersCSharp(document, textToSearch, false);
Assert.IsTrue(actual.Contains(expected), "We expect the method text of the caller");
}
/// <summary>
///A test for GetSearchCallersVB
///</summary>
[TestMethod()]
[DeploymentItem("RoslynCodeSearcher.exe")]
public void GetSearchCallersVBTest()
{
CodeSearcher_Accessor target = new CodeSearcher_Accessor();
IDocument document = TestHelper.GetTestDocumentsVB().First();
string textToSearch = "SOMEMETHOD";
string expected = TestConstants.SOMEMETHODCALLERVB;
string actual;
actual = target.GetSearchCallersVB(document, textToSearch, false);
Assert.IsTrue(actual.Contains(expected), "We expect the method text of the caller");
}
/// <summary>
///A test for GetSearchMethodsResultCSharp
///</summary>
[TestMethod()]
[DeploymentItem("RoslynCodeSearcher.exe")]
public void GetSearchMethodsResultCSharpTest()
{
CodeSearcher_Accessor target = new CodeSearcher_Accessor();
IDocument document = TestHelper.GetTestDocumentsCSharp().First();
string textToSearch = "SOMEMETHOD";
string expected = TestConstants.SOMEMETHODTEXTCSHARP;
string actual;
actual = target.GetSearchMethodsResultCSharp(document, textToSearch, false);
Assert.IsTrue(actual.Contains(expected), "We expect the method text");
}
/// <summary>
///A test for GetSearchMethodsResultVb
///</summary>
[TestMethod()]
[DeploymentItem("RoslynCodeSearcher.exe")]
public void GetSearchMethodsResultVbTest()
{
CodeSearcher_Accessor target = new CodeSearcher_Accessor();
IDocument document = TestHelper.GetTestDocumentsVB().First();
string textToSearch = "SOMEMETHOD";
string expected = TestConstants.SOMEMETHODTEXTVB;
string actual;
actual = target.GetSearchMethodsResultVB(document, textToSearch, false);
Assert.IsTrue(actual.Contains(expected), "We expect the method text");
}
/// <summary>
///A test for GetSearchPropertiesCSharp
///</summary>
[TestMethod()]
[DeploymentItem("RoslynCodeSearcher.exe")]
public void GetSearchPropertiesCSharpTest()
{
CodeSearcher_Accessor target = new CodeSearcher_Accessor();
IDocument document = TestHelper.GetTestDocumentsCSharp().First();
string textToSearch = "SOMEPROPERTY";
string expected = TestConstants.SOMEPROPERTYTEXTCSHARP;
string actual;
actual = target.GetSearchPropertiesCSharp(document, textToSearch, false);
Assert.IsTrue(actual.Contains(expected), "We expect the method text");
}
/// <summary>
///A test for GetSearchPropertiesVB
///</summary>
[TestMethod()]
[DeploymentItem("RoslynCodeSearcher.exe")]
public void GetSearchPropertiesVBTest()
{
CodeSearcher_Accessor target = new CodeSearcher_Accessor();
IDocument document = TestHelper.GetTestDocumentsVB().First();
string textToSearch = "SOMEPROPERTY";
string expected = TestConstants.SOMEPROPERTYTEXTVB;
string actual = target.GetSearchPropertiesVB(document, textToSearch, false);
Assert.IsTrue(actual.Contains(expected), "We expect the method text");
}
/// <summary>
///A test for Search
///</summary>
[TestMethod()]
public void SearchTest()
{
string actual = "";
string expected = "";
string searchText = "";
CodeSearcher target = null;
SearchType searchType = new SearchType();
//*******************
//Search C# solutions
//*******************
Paths.BaseDirectorySolutionsTxtPath = TestConstants.BASEDIRECTORYSOLUTIONSTXTPATH;
File.WriteAllText(TestConstants.BASEDIRECTORYSOLUTIONSTXTPATH, TestConstants.SOLUTIONPATHCSHARP);
//Search methods
searchType = SearchType.SearchMethods;
searchText = "SOMEMETHOD";
expected = TestConstants.SOMEMETHODTEXTCSHARP;
target = new CodeSearcher(searchType, searchText, "", "", false);
actual = target.Search();
Assert.IsTrue(actual.Contains(expected));
//Search callers
searchType = SearchType.SearchCallers;
searchText = "SOMEMETHOD";
expected = TestConstants.SOMEMETHODCALLERCSHARP;
target = new CodeSearcher(searchType, searchText, "", "", false);
actual = target.Search();
Assert.IsTrue(actual.Contains(expected));
//Search text in method
searchType = SearchType.SearchTextInMethod;
searchText = "ANOTHER VAR";
expected = TestConstants.SOMEMETHODTEXTCSHARP;
target = new CodeSearcher(searchType, searchText, "", "", true);
actual = target.Search();
Assert.IsTrue(actual.Contains(expected));
//Search properties
searchType = SearchType.SearchProperties;
searchText = "SOMEPROPERTY";
expected = TestConstants.SOMEPROPERTYTEXTCSHARP;
target = new CodeSearcher(searchType, searchText, "", "", false);
actual = target.Search();
Assert.IsTrue(actual.Contains(expected));
Cleanup();
//***********************
//Search VB.NET solutions
//***********************
Paths.BaseDirectorySolutionsTxtPath = TestConstants.BASEDIRECTORYSOLUTIONSTXTPATH;
File.WriteAllText(TestConstants.BASEDIRECTORYSOLUTIONSTXTPATH, TestConstants.SOLUTIONPATHVB);
//Search methods
searchType = SearchType.SearchMethods;
searchText = "SOMEMETHOD";
expected = TestConstants.SOMEMETHODTEXTVB;
target = new CodeSearcher(searchType, searchText, "", "", false);
actual = target.Search();
Assert.IsTrue(actual.Contains(expected));
//Search callers
searchType = SearchType.SearchCallers;
searchText = "SOMEMETHOD";
expected = TestConstants.SOMEMETHODCALLERVB;
target = new CodeSearcher(searchType, searchText, "", "", false);
actual = target.Search();
Assert.IsTrue(actual.Contains(expected));
//Search text in method
searchType = SearchType.SearchTextInMethod;
searchText = "ANOTHER VAR";
expected = TestConstants.SOMEMETHODTEXTVB;
target = new CodeSearcher(searchType, searchText, "", "", true);
actual = target.Search();
Assert.IsTrue(actual.Contains(expected));
//Search properties
searchType = SearchType.SearchProperties;
searchText = "SOMEPROPERTY";
expected = TestConstants.SOMEPROPERTYTEXTVB;
target = new CodeSearcher(searchType, searchText, "", "", false);
actual = target.Search();
Assert.IsTrue(actual.Contains(expected));
}
private static void Cleanup()
{
if (File.Exists(TestConstants.BASEDIRECTORYSOLUTIONSTXTPATH))
{
File.Delete(TestConstants.BASEDIRECTORYSOLUTIONSTXTPATH);
}
CodeRepository.Solutions = new System.Collections.Generic.List<string>();
CodeRepository.Workspaces = new System.Collections.Generic.List<Roslyn.Services.IWorkspace>();
}
/// <summary>
///A test for SearchCallers
///</summary>
[TestMethod()]
public void SearchCallersTest()
{
string actual = "";
string expected = TestConstants.SOMEMETHODCALLERCSHARP;
string searchText = "SOMEMETHOD";
List<string> excludes = new List<string>();
List<string> includes = new List<string>();
//Search callers
SearchType searchType = new SearchType();
searchType = SearchType.SearchCallers;
//*********************************************
//Search C# solutions for callers of SomeMethod
//*********************************************
Paths.BaseDirectorySolutionsTxtPath = TestConstants.BASEDIRECTORYSOLUTIONSTXTPATH;
CodeSearcher target = new CodeSearcher(searchType, searchText, "", "", false);
List<IWorkspace> workspaces = new List<IWorkspace>();
workspaces.Add(TestHelper.GetTestWorkspaceCSharp());
actual = target.SearchCallersParallel(workspaces, searchText, excludes, includes, false);
Assert.IsTrue(actual.Contains(expected));
//*************************************************
//Search VB.NET solutions for callers of SomeMethod
//*************************************************
expected = TestConstants.SOMEMETHODCALLERVB;
Paths.BaseDirectorySolutionsTxtPath = TestConstants.SOLUTIONPATHVB;
workspaces = new List<IWorkspace>();
workspaces.Add(TestHelper.GetTestWorkspaceVB());
actual = target.SearchCallersParallel(workspaces, searchText, excludes, includes, false);
Assert.IsTrue(actual.Contains(expected));
}
/// <summary>
///A test for SearchMethods
///</summary>
[TestMethod()]
public void SearchMethodsTest()
{
string actual = "";
string expected = "";
string searchText = "SOMEMETHOD";
List<string> excludes = new List<string>();
List<string> includes = new List<string>();
//Search methods
SearchType searchType = new SearchType();
searchType = SearchType.SearchMethods;
//*********************************************
//Search C# solutions for method SomeMethod
//*********************************************
expected = TestConstants.SOMEMETHODTEXTCSHARP;
Paths.BaseDirectorySolutionsTxtPath = TestConstants.SOLUTIONPATHCSHARP;
CodeSearcher target = new CodeSearcher(searchType, searchText, "", "", false);
List<IWorkspace> workspaces = new List<IWorkspace>();
workspaces.Add(TestHelper.GetTestWorkspaceCSharp());
actual = target.SearchMethodsParallel(workspaces, searchText, excludes, includes, false);
Assert.IsTrue(actual.Contains(expected));
//*************************************************
//Search VB.NET solutions for method SomeMethod
//*************************************************
expected = TestConstants.SOMEMETHODTEXTVB;
Paths.BaseDirectorySolutionsTxtPath = TestConstants.SOLUTIONPATHVB;
workspaces = new List<IWorkspace>();
workspaces.Add(TestHelper.GetTestWorkspaceVB());
actual = target.SearchMethodsParallel(workspaces, searchText, excludes, includes, false);
Assert.IsTrue(actual.Contains(expected));
}
/// <summary>
///A test for SearchMethodsForText
///</summary>
[TestMethod()]
public void SearchMethodsForTextTest()
{
string actual = "";
string expected = "";
string searchText = "ANOTHER VAR";
List<string> excludes = new List<string>();
List<string> includes = new List<string>();
//Search methods
SearchType searchType = new SearchType();
searchType = SearchType.SearchTextInMethod;
//*************************************************
//Search C# solutions for text in method SomeMethod
//*************************************************
expected = TestConstants.SOMEMETHODTEXTCSHARP;
Paths.BaseDirectorySolutionsTxtPath = TestConstants.SOLUTIONPATHCSHARP;
CodeSearcher target = new CodeSearcher(searchType, searchText, "", "", false);
List<IWorkspace> workspaces = new List<IWorkspace>();
workspaces.Add(TestHelper.GetTestWorkspaceCSharp());
actual = target.SearchMethodsForTextParallel(workspaces, searchText, excludes, includes, true);
Assert.IsTrue(actual.Contains(expected));
//*****************************************************
//Search VB.NET solutions for text in method SomeMethod
//*****************************************************
expected = TestConstants.SOMEMETHODTEXTVB;
Paths.BaseDirectorySolutionsTxtPath = TestConstants.SOLUTIONPATHVB;
workspaces = new List<IWorkspace>();
workspaces.Add(TestHelper.GetTestWorkspaceVB());
actual = target.SearchMethodsForTextParallel(workspaces, searchText, excludes, includes, true);
Assert.IsTrue(actual.Contains(expected));
}
/// <summary>
///A test for SearchMethodsForTextCSharp
///</summary>
[TestMethod()]
[DeploymentItem("RoslynCodeSearcher.exe")]
public void SearchMethodsForTextCSharpTest()
{
CodeSearcher_Accessor target = new CodeSearcher_Accessor();
IDocument document = TestHelper.GetTestDocumentsCSharp().First();
string textToSearch = "SOMEMETHOD";
string expected = TestConstants.SOMEMETHODTEXTCSHARP;
string actual = target.SearchMethodsForTextCSharp(document, textToSearch, true);
Assert.IsTrue(actual.Contains(expected), "We expect the method text");
}
/// <summary>
///A test for SearchMethodsForTextVB
///</summary>
[TestMethod()]
[DeploymentItem("RoslynCodeSearcher.exe")]
public void SearchMethodsForTextVBTest()
{
CodeSearcher_Accessor target = new CodeSearcher_Accessor();
IDocument document = TestHelper.GetTestDocumentsVB().First();
string textToSearch = "SOMEMETHOD";
string expected = TestConstants.SOMEMETHODTEXTVB;
string actual = target.SearchMethodsForTextVB(document, textToSearch, true);
Assert.IsTrue(actual.Contains(expected), "We expect the method text");
}
/// <summary>
///A test for SearchProperties
///</summary>
[TestMethod()]
public void SearchPropertiesTest()
{
string actual = "";
string expected = "";
string searchText = "SOMEPROPERTY";
List<string> excludes = new List<string>();
List<string> includes = new List<string>();
//Search methods
SearchType searchType = new SearchType();
searchType = SearchType.SearchProperties;
//*************************************************
//Search C# solutions for text in method SomeMethod
//*************************************************
expected = TestConstants.SOMEPROPERTYTEXTCSHARP;
Paths.BaseDirectorySolutionsTxtPath = TestConstants.SOLUTIONPATHCSHARP;
CodeSearcher target = new CodeSearcher(searchType, searchText, "", "", false);
List<IWorkspace> workspaces = new List<IWorkspace>();
workspaces.Add(TestHelper.GetTestWorkspaceCSharp());
actual = target.SearchPropertiesParallel(workspaces, searchText, excludes, includes, false);
Assert.IsTrue(actual.Contains(expected));
//*****************************************************
//Search VB.NET solutions for text in method SomeMethod
//*****************************************************
expected = TestConstants.SOMEPROPERTYTEXTVB;
Paths.BaseDirectorySolutionsTxtPath = TestConstants.SOLUTIONPATHVB;
workspaces = new List<IWorkspace>();
workspaces.Add(TestHelper.GetTestWorkspaceVB());
actual = target.SearchPropertiesParallel(workspaces, searchText, excludes, includes, false);
Assert.IsTrue(actual.Contains(expected));
}
}
}