Click here to Skip to main content
15,884,177 members
Articles / Desktop Programming / Windows Forms

C# and VB.NET Code Searcher - Using Roslyn

Rate me:
Please Sign up or sign in to vote.
4.84/5 (51 votes)
7 Mar 2013LGPL314 min read 199.9K   5.6K   130  
A fast C# and VB.NET code searcher using Roslyn.
using RoslynCodeSearcher;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using FastColoredTextBoxNS;
using KRBTabControl;
using System.Collections.Generic;

namespace RoslynCodesearcher.Unittest
{
   /// <summary>
   ///This is a test class for TabControllerTest and is intended
   ///to contain all TabControllerTest Unit Tests
   ///</summary>
   [TestClass()]
   public class TabControllerTest
   {
      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

      // Use ClassInitialize to run code before running the first test in the class
      [ClassInitialize()]
      public static void MyClassInitialize(TestContext testContext)
      {
         TabController.TabControl = null;
         TabController_Accessor._fastColoredTextBoxes = new List<FastColoredTextBox>();
      }

      //Use ClassCleanup to run code after all tests in a class have run
      [ClassCleanup()]
      public static void MyClassCleanup()
      {
         TabController.TabControl = null;
         TabController_Accessor._fastColoredTextBoxes = new List<FastColoredTextBox>();
      }

      // 
      //You can use the following additional attributes as you write your tests:
      //
      //
      //Use TestInitialize to run code before running each test
      //[TestInitialize()]
      //public void MyTestInitialize()
      //{
      //}
      //
      //Use TestCleanup to run code after each test has run
      //[TestCleanup()]
      //public void MyTestCleanup()
      //{
      //}
      //
      #endregion


      /// <summary>
      ///A test for RemoveTab
      ///</summary>
      [TestMethod()]
      public void RemoveTabTest()
      {
         //Clean up
         TabController_Accessor._fastColoredTextBoxes = new List<FastColoredTextBox>();

         Assert.IsTrue(TabController_Accessor._fastColoredTextBoxes.Count == 0);

         TabController.TabControl = new KRBTabControl.KRBTabControl();
         TabController.AddTab();
         TabPageEx tabPage = (TabPageEx)TabController.SelectedTab;
         Guid guid = tabPage.Guid;

         Assert.IsTrue(TabController_Accessor._fastColoredTextBoxes.Count == 1);

         int index = 0;
         TabController.RemoveTab(index);

         Assert.IsTrue(TabController_Accessor._fastColoredTextBoxes.Count == 0);
      }

      /// <summary>
      ///A test for InitImages
      ///</summary>
      [TestMethod()]
      public void InitImagesTest()
      {
         KRBTabControl.KRBTabControl tabControl = new KRBTabControl.KRBTabControl();
         TabController.TabControl = tabControl;
         TabController.TabControl.ImageList = new System.Windows.Forms.ImageList();

         Assert.IsTrue(TabController.TabControl.ImageList.Images.Count == 0);

         TabController.InitImages();

         Assert.IsTrue(TabController.TabControl.ImageList.Images.Count > 0);
      }

      /// <summary>
      ///A test for InitFirstTab
      ///</summary>
      [TestMethod()]
      public void InitFirstTabTest()
      {
         TabController_Accessor._fastColoredTextBoxes = new List<FastColoredTextBox>();

         Assert.IsTrue(TabController_Accessor._fastColoredTextBoxes.Count == 0);

         TabController.TabControl = new KRBTabControl.KRBTabControl();
         TabController.AddTab();
         TabController.InitFirstTab();
         TabPageEx tabPage = (TabPageEx)TabController.SelectedTab;
         Guid guid = tabPage.Guid;

         //AddTab and InitFirstTab both add a FastColoredTextBox to the tab
         Assert.IsTrue(TabController_Accessor._fastColoredTextBoxes.Count == 2);
      }

      /// <summary>
      ///A test for GetTabByGuid
      ///</summary>
      [TestMethod()]
      public void GetTabByGuidTest()
      {
         TabController.TabControl = new KRBTabControl.KRBTabControl();
         TabController.AddTab();
         TabPageEx tabPage = (TabPageEx)TabController.SelectedTab;
         Guid guid = tabPage.Guid;

         Guid expected = guid;

         TabPageEx actual = TabController.GetTabByGuid(guid);

         Assert.AreEqual(expected, actual.Guid);
      }

      /// <summary>
      ///A test for GetFastColoredTextBoxByGuid
      ///</summary>
      [TestMethod()]
      public void GetFastColoredTextBoxByGuidTest()
      {
         TabController.TabControl = new KRBTabControl.KRBTabControl();
         TabController.AddTab();
         TabPageEx tabPage = (TabPageEx)TabController.SelectedTab;
         Guid guid = tabPage.Guid;

         Guid expected = guid;
         FastColoredTextBox actual = TabController.GetFastColoredTextBoxByGuid(guid);

         Assert.AreEqual(expected, actual.Guid);
      }

      /// <summary>
      ///A test for AddTab
      ///</summary>
      [TestMethod()]
      public void AddTabTest()
      {
         TabController_Accessor._fastColoredTextBoxes = new List<FastColoredTextBox>();

         Assert.IsTrue(TabController_Accessor._fastColoredTextBoxes.Count == 0);

         TabController.TabControl = new KRBTabControl.KRBTabControl();
         TabController.AddTab();
         TabPageEx tabPage = (TabPageEx)TabController.SelectedTab;
         Guid guid = tabPage.Guid;

         Assert.IsTrue(TabController_Accessor._fastColoredTextBoxes.Count == 1);
      }

      /// <summary>
      ///A test for AddFastColoredTextBoxToTabPage
      ///</summary>
      [TestMethod()]
      public void AddFastColoredTextBoxToTabPageTest()
      {
         TabPageEx tabPage = new TabPageEx();
         FastColoredTextBox fastColoredTextBox = new FastColoredTextBox();

         Assert.IsTrue(tabPage.Controls.Count == 0);

         TabController.AddFastColoredTextBoxToTabPage(tabPage, fastColoredTextBox);

         Assert.IsTrue(tabPage.Controls.Count == 1);
      }

      /// <summary>
      ///A test for UpdateSearchTextOnTab
      ///</summary>
      [TestMethod()]
      public void UpdateSearchTextOnTabTest()
      {
         TabController.TabControl = new KRBTabControl.KRBTabControl();
         TabController.AddTab();
         TabPageEx tabPage = (TabPageEx)TabController.SelectedTab;
         Guid guid = tabPage.Guid;

         string text = "textontab";

         TabController.UpdateSearchTextOnTab(text);

         Assert.AreEqual(text, tabPage.Text);
      }

      /// <summary>
      ///A test for WriteResults
      ///</summary>
      [TestMethod()]
      public void WriteResultsTest()
      {
         TabController.TabControl = new KRBTabControl.KRBTabControl();
         TabController.AddTab();
         TabPageEx tabPage = (TabPageEx)TabController.SelectedTab;
         Guid guid = tabPage.Guid;

         RoslynCodeSearcher.Language language = new RoslynCodeSearcher.Language();
         string text = "WriteResults test";
         TabController.WriteResults(guid, language, text);
         FastColoredTextBox findTextbox = TabController.GetFastColoredTextBoxByGuid(guid);

         Assert.IsTrue(findTextbox.Text.Contains(text), "textbox should contain text");
      }

      /// <summary>
      ///A test for FastColoredTextBoxes
      ///</summary>
      [TestMethod()]
      public void FastColoredTextBoxesTest()
      {
         TabController.FastColoredTextBoxes = new List<FastColoredTextBox>();

         Assert.IsTrue(TabController.FastColoredTextBoxes.Count == 0);

         TabController.TabControl = new KRBTabControl.KRBTabControl();
         TabController.AddTab();
         TabPageEx tabPage = (TabPageEx)TabController.SelectedTab;
         Guid guid = tabPage.Guid;

         Assert.IsTrue(TabController.FastColoredTextBoxes.Count == 1);
      }

      /// <summary>
      ///A test for SelectedTab
      ///</summary>
      [TestMethod()]
      public void SelectedTabTest()
      {
         TabController.TabControl = new KRBTabControl.KRBTabControl();
         TabController.AddTab();
         TabPageEx tabPage = (TabPageEx)TabController.SelectedTab;

         Assert.IsNotNull(tabPage);
      }

      /// <summary>
      ///A test for TabControl
      ///</summary>
      [TestMethod()]
      public void TabControlTest()
      {
         KRBTabControl.KRBTabControl tabControl = new KRBTabControl.KRBTabControl();
         
         TabController.TabControl = tabControl;

         Assert.IsNotNull(TabController.TabControl);
      }
   }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Software Developer (Senior)
Netherlands Netherlands
Languages: C#, ASP.NET, HTML, VB, Javascript.
Tools: Visual Studio 2010, 2012.
Databases: MS SQL Server, Oracle, SQLite.
Skills: MCPD Web Developer 4.0, MCSD 2.0
Likes: Solving problems at projecteuler.net. at #66 now.
Technologies:C#, Azure, Xamarin.iOS, Web API, T/SQL, PL/SQL, MSBuild, WIX, XSLT, WPF, WCF, JavaScript

I have been a programmer since 1995. At first at school where we had a computer club with nice Aster CT-80 (TRS-80 clone) computers and 1 MSX computer where I learned to program in BASIC. Later I had my own 8086 XT computer.
My first experience with programming was way before that when I was about 8. A friend of mine had a Commodore 64. He typed over a BASIC game from a computing magazine. The program worked but it contained an error. The thing he did was go to the source line that caused the error, deleted the line, and restarted the program. It worked every time he got an error...

Comments and Discussions