Introduction This tutorial is an introduction to Test Driven Development (TDD) in Visual Studio 2010 (VS2010) with C# 4 and ASP.NET 4. This will focus on as using the tool and coding as much as possible and not a great deal of words so enjoy the ride. We are covering some material that was presented in my earlier tutorial http://domscode.com/2010/05/21/net-tutorial-c4-0-and-test-driven-development-cannonattack/ so if you haven't used VS2010 and TDD have a look. The WebSharpCompiler Requirements/Specs: The following is a combination of requirements and specifications that will give us an idea of what we are trying to build:
Iteration 1 We are going to setup the project and build our first test. Iteration 1 - Creating the Solution and our first test
Just remember you can use some cool VS2010 features to make these changes including:
Iteration 2
So that's almost the end of the 2nd iteration It is worthwhile adding some more tests at this time although I wont walk through the process anymore but I'll show you the tests we added: [TestMethod] public void TestCompilerFiveErrors() WebSharpCompiler compiler = new WebSharpCompiler(); string programText = @" using **** System; namesp8ce HelloWorld { clas HelloWorldClass { static void Main(string[] args) { Console.ReadLine(); } }"; List<string> compilerErrors = compiler.Compile(programText); Assert.AreEqual(compilerErrors.Count, 5); [TestMethod] public void TestCompilerSuccessfulCompilation() WebSharpCompiler compiler = new WebSharpCompiler(); string programText = @" using System; namespace HelloWorld { class HelloWorldClass { static void Main(string[] args) { Console.ReadLine(); } } }"; List<string> compilerErrors = compiler.Compile((programText)); Assert.AreEqual(compilerErrors.Count, 0); }
3rd Iteration The third iteration of work is to complete the web site and given that the UI is very simple this won't take very long.
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebSharpCompiler._Default" %><asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"></asp:Content><asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> <h2>Code</h2> <p> <asp:TextBox ID="txtCode" runat="server" Height="240px" Width="100%" TextMode="MultiLine">using System;namespace ConsoleApplication1{ class Program { static void Main(string[] args) { Console.WriteLine("Hello World"); } }}</asp:TextBox> </p> <p> <asp:Button ID="btnCompile" runat="server" onclick="btnCompile_Click" Text="Compile" /> <asp:Button ID="btnClear" runat="server" onclick="btnClear_Click" Text="Clear" /> </p> <h2>Compiler Output</h2> <p> <asp:ListBox ID="lstCompilerOutput" runat="server" Width="100%"></asp:ListBox> </p></asp:Content>
WebSharpCompilerBusiness.WebSharpCompiler(); List<string> compilerErrors = compiler.Compile(txtCode.Text); if (compilerErrors.Count == 0) { lstCompilerOutput.Items.Add("No Errors"); } foreach (string error in compilerErrors) { lstCompilerOutput.Items.Add(error); } } protected void btnClear_Click(object sender, EventArgs e) { txtCode.Text = string.Empty; } }}
Or In Conclusion So there we have our C# compiler. Source code link is below and feel free to drop me a line here if you have any thoughts/advice on this. All the best Dom. References http://msdn.microsoft.com/en-us/library/system.codedom.compiler.codedomprovider.aspx
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: