Introduction
CodeLens is a new feature of Visual Studio that is provided with Visual Studio 2013 Ultimate. Basically CodeLens provides the information about our project's code just before the methods/properties/class into editor, like reference counts of methods/properties and classes, tests (Unit Tests) associated with a method and how many times the line of code has been changed. In other words we can say that CodeLens describes the following three important things:
- References
- Impact
- Test
- Changes
Let's see a brief expiation of these three features of CodeLens as listed above.
References
References is on of the features of CodeLens that shows how many times a specific method/property/class is being used in the code files. Suppose we have a method named GetEmployeeDetails() that returns the information of an employee, like name, designation, organization, salary and so on. If we call this method inside the project code, the reference count will be increased from zero to the number of times we used this method. Let's see it through an example code.
Example: In the following example the GetEmployeeDetails() method is showing 1 reference because it is called/use only one time inside the Main method.
Program Code: (References)
- class EmployeeInfo
- {
-
- public string FullName { get; set; }
-
- public string Designation { get; set; }
-
- public string Organization { get; set; }
-
- public double Salary { get; set; }
-
- public string Email { get; set; }
-
-
- static void Main(string[] args)
- {
-
- EmployeeInfo employee = new EmployeeInfo();
-
- employee.GetEmployeeDetails("Ranjan Kumar","Software Developer","MCN Solutions Pvt. Ltd.","[email protected]",52000.00);
-
- }
-
-
- public void GetEmployeeDetails(string _name,string _designation,string _organization,string _email,double _salary)
- {
- FullName = _name;
- Designation = _designation;
- Organization = _organization;
- Email = _email;
- Salary = _salary;
-
- string data = "Name\t:" + FullName + "\nDesignation\t: " + Designation + "\nEmail\t: " + Email + "\nSalary\t: " + Salary;
- Console.WriteLine("Employee Detrails:\n\n" + data);
- Console.ReadLine();
- }
- }
To see the referencing code double-click on References (2 References), an indicator popup will be shown just above CodeLens. It will show where this method is being used.
If we want to see the graphical representation of references, it is possible. To do that double-click on references of CodeLens then a popup box will be shown. In the bottom of the popup box we will see a link named Show on Code Map. Now click on this link and a graphical view of CodeLens will be shown in the CodeMap1.dgml file.
Impact
Impact is nothing, it's the view of references that indicates the changes to a method that is referenced one or more times. If any changes occur inside the method, the CodeLens starts to blink which indicates that if we change anything inside this method, it will impact these places.
Test: Test is the important part of CodeLens that indicates whether this method was unit tested or not. For example if a method having 1 is passed in CodeLens then this method is tested by Unit Testing.
To do unit testing with a project, we have attached a UnitTest project with this article, download this file to see it practically. For the moment see the following example code of Unit Testing.
Step 1
Create a Console Application using Git Version Control and do the following:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
-
- namespace CodeLensExampleProject
- {
- class EmployeeInfo
- {
-
- public string FullName { get; set; }
-
- public string Designation { get; set; }
-
- public string Organization { get; set; }
-
- public double Salary { get; set; }
-
- public string Email { get; set; }
-
-
- static void Main(string[] args)
- {
-
- EmployeeInfo employee = new EmployeeInfo();
-
- employee.GetEmployeeDetails("Ranjan Kumar","Software Developer","MCN Solutions Pvt. Ltd.","[email protected]",52000.00);
- ErrorHandler erh = new ErrorHandler();
-
- }
-
- public void SetAndGetEmployeeDetails()
- {
- EmployeeInfo employee1 = new EmployeeInfo();
- employee1.GetEmployeeDetails("Suraj Kumar", "Web Designer", "MCN Solutions Pvt. Ltd.", "[email protected]", 38000.00);
- }
-
-
- public void GetEmployeeDetails(string _name,string _designation,string _organization,string _email,double _salary)
- {
- FullName = _name;
- Designation = _designation;
- Organization = _organization;
- Email = _email;
- Salary = _salary;
-
- string empDetails = "Name\t:" + FullName + "\nDesignation\t: " + Designation + "\nEmail\t: " + Email + "\nSalary\t: " + Salary;
- Console.WriteLine("Employee Detrails:\n\n" + empDetails);
- Console.ReadLine();
-
- }
-
- ErrorHandler obj1 = new ErrorHandler();
- }
-
-
- public class ErrorHandler
- {
- public void HanldeError()
- {
-
- }
- }
- }
Step 2
Now add a UnitTest Project to this Console Application and do the following code.
- using System;
- using Microsoft.VisualStudio.TestTools.UnitTesting;
-
- using CodeLensExampleProject;
- namespace UnitTest
- {
- [TestClass]
- public class UnitTest1
- {
- [TestMethod]
- public void TestMethod1()
- {
- ErrorHandler erh = new ErrorHandler();
- erh.HanldeError();
- }
- }
- }
Step 3
Before creating the object of the "ErrorHandler" class add the reference of the console application to this UnitTest Project.
By: Add Reference
Changes: Changes are also a part of CodeLens that shows how many times changes occurs to a specific method, if that method is tested by a UnitTest Project.
Summary
In this article we saw what is CodeLens does and how to use it with Git or Team Foundation Version Control (TFVC) version control as well as how to test it using unit testing.