Angel \”Java\” Lopez on Blog

March 28, 2011

Writing an Application using TDD (Part 3) First Detail

First post
Next post
Previous post

Since the previous post, I applied the suggestions in comments from @MartinSalias and @theHumanFlag, thanks!. First, I renamed Genre class to Subject, using the refactoring features of Visual Studio:

public class Subject
{
    public string Name { get; set; }
}

Second, now the controller receives and process an IEnumerable<Subject>:

public class SubjectController : Controller
{
    private IEnumerable<Subject> subjects;
    public SubjectController()
    {
    }
    public SubjectController(IEnumerable<Subject> subjects)
    {
        this.subjects = subjects;
    }
    public ActionResult Index()
    {
        return View(subjects);
    }
}

I made the change with confidence, thanks to the test.

In this new step, I added an action: to get a Subject by Id. A new property Id in Subjec:

public class Subject
{
    public int Id { get; set; }
    public string Name { get; set; }
}

I wrote the test:

[TestMethod]
public void GetSubjectForDetail()
{
    IEnumerable<Subject> subjects = new List<Subject>()
    {
        new Subject() { Id = 1, Name = "Mathematics" },
        new Subject() { Id = 2, Name = "Physics" },
        new Subject() { Id = 3, Name = "Biology" },
        new Subject() { Id = 4, Name = "Literature" }
    };
    SubjectController controller = new SubjectController(subjects);
    ActionResult result = controller.Details(1);
    Assert.IsNotNull(result);
    Assert.IsInstanceOfType(result, typeof(ViewResult));
    ViewResult viewResult = (ViewResult)result;
    Assert.IsInstanceOfType(viewResult.ViewData.Model, typeof(Subject));
    Subject model = (Subject)viewResult.ViewData.Model;
    Assert.AreEqual(1, model.Id);
    Assert.AreEqual("Mathematics", model.Name);
}

The test didn’t compile. I created the action:

public ActionResult Details(int id)
{
    return View();
}

The test compile, but its result was red. Now, go for the green light:

public ActionResult Details(int id)
{
    var model = this.subjects.Where(s => s.Id == id).FirstOrDefault();
    return View(model);
}

I had two tests, that give a list of Subjects to the actions. I refactored the test to separate the creation of such list. I extracted the method:

private static IEnumerable<Subject> GetSubjects()
{
    IEnumerable<Subject> subjects = new List<Subject>()
    {
        new Subject() { Id = 1, Name = "Mathematics" },
        new Subject() { Id = 2, Name = "Physics" },
        new Subject() { Id = 3, Name = "Biology" },
        new Subject() { Id = 4, Name = "Literature" }
    };
    return subjects;
}

Now, the test code is:

[TestMethod]
public void GetSubjectInDetail()
{
    IEnumerable<Subject> subjects = GetSubjects();
    SubjectController controller = new SubjectController(subjects);
    ActionResult result = controller.Details(1);
    Assert.IsNotNull(result);
    Assert.IsInstanceOfType(result, typeof(ViewResult));
    ViewResult viewResult = (ViewResult)result;
    Assert.IsInstanceOfType(viewResult.ViewData.Model, typeof(Subject));
    Subject model = (Subject)viewResult.ViewData.Model;
    Assert.AreEqual(1, model.Id);
    Assert.AreEqual("Mathematics", model.Name);
}

All tests in green!

As usual, the code is in my AjCodeKatas Google Project, under trunk/AppTdd/Step02)

Next steps: more actions, integrate views.

Keep tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

March 22, 2011

Writing An Application Using TDD (Part 2) First List

Next post in this series
Previous post in this series

Thanks to a suggestion by @MartinSalias, I choose to develop a simple domain: my personal library, my books. In this first code iteration, I want to list the book genres: Physics, Mathematics, Biology, Literature… (similar to genres in MvcMusicStore by @jongalloway).

First, I created the ASP.NET MVC Solution (I’m using Visual Studio 2008) (the code for this post is in my AjCodeKatas Google Project, under trunk/AppTdd/Step01)

Then, I add a test project:

I added references to System.Web.Mvc and MyLibrary.Web, to be used in this test project.

I added a new test:

I cleaned the code of my first test class, removing unneeded methods. The result:

namespace MyLibrary.Web.Tests.Controllers
{
    using System;
    using System.Text;
    using System.Collections.Generic;
    using System.Linq;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    [TestClass]
    public class GenreControllerTests
    {
    }
}

Then, I added my first test:

        [TestMethod]
        public void GetGenres()
        {
            IList<Genre> genres = new List<Genre>()
            {
                new Genre() { Name = "Mathematics" },
                new Genre() { Name = "Physics" },
                new Genre() { Name = "Biology" },
                new Genre() { Name = "Literature" }
            };
            GenreController controller = new GenreController(genres);
            ActionResult result = controller.Index();
            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(ViewResult));
            ViewResult viewResult = (ViewResult)result;
            Assert.IsInstanceOfType(viewResult.ViewData.Model, typeof(IList<Genre>));
            Assert.AreSame(genres, viewResult.ViewData.Model);
        }
		

The idea: create the controller, feed it with a list of genres, then invoke the Index action, and test the returned action result. The code didn’t compile: the controller didn’t exist, there were no Genre class. So, I wrote the Genre class in Model folder at MyLibrary.Web:

    public class Genre
    {
        public string Name { get; set; }
    }

A simple GenreController (added with right click on Model folder):

The wizard created this code:

    public class GenreController : Controller
    {
        //
        // GET: /Genre/
        public ActionResult Index()
        {
            return View();
        }
    }

I modified it to have an appropriate constructor:

    public class GenreController : Controller
    {
        public GenreController()
        {
        }
        public GenreController(IList<Genre> genres)
        {
        }
        public ActionResult Index()
        {
            return View();
        }
    }
	

Note that my intention was to compile the test: the list of genres are not used by the controller, yet. With all these changes, I could compile the solution, and run the test:

The result was:

The problem:

As expected. This is part of the TDD cycle: compile, red, and now, go for green.

I changed the controller behavior (the interface was already completed):

    public class GenreController : Controller
    {
        private IList<Genre> genres;
        public GenreController()
        {
        }
        public GenreController(IList<Genre> genres)
        {
            this.genres = genres;
        }
        public ActionResult Index()
        {
            return View(genres);
        }
    }

Now, the test result:

Triumph of mind over matter! ;-)

Now, time for refactoring. Well, I have not much for this now. I could improve same naming. The main missing piece: a view. Note: the controller is not prepared, yet, for production. It has no genre provider object.

Next steps: improve controller to use a view, add CRUD actions with tests, and views.

You could ask: where is the repository of genres? where is the database? what about a service layer? domain separation? Ok, all valid questions. My idea is to show, with incremental steps, how all those artifacts arise from improving the controllers, actions, by the force of new tests and refactoring. As I wrote in my previous post, this is a kind of top-down approach. In this case, the domain is simple, so I can start from the top presentation layer. After this simple example, I will switch to a less simple domain. In this case, we can start testing the domain, before any presentation layer.

Keep tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

March 19, 2011

Dynamic Language Runtime (Part 1) Hello World!

Filed under: .NET, C Sharp, Code Generation, Dynamic Language Runtime — ajlopez @ 11:22 am

Next post

I want to compile my interpreters (AjSharp, AjLisp, upcoming AjScript), written in C#. I could use CodeDome, Reflection.Emit, Cecil. But there is a library dedicated to implement dynamic languages: Dynamic Language Runtime. I got 1.0 version from

http://dlr.codeplex.com

and I compiled its release version using VS2008. Notably, they removed a simple language implementation (I guess it is still at version 0.9, as ToyScript). So, I need help to understand the inner guts and features of this library. Documentation is technically good, but they introduce so many pieces with names like “Call Site” that for me (English is not my mother tongue) are ambiguous. What is a “Call Site”? Maybe a web site for phone calls ;-) ;-). So, I decided to write some code katas, short experiments, to investigate DLR. Hopefully, now I have the help of the book “Pro DLR in .NET 4” by Chaur Wu. I found this first example in that book.

First experiment: create a REPL (Read-Eval-Print Loop), for a simple “Hello, World!”.

(The code of this post series is at
http://code.google.com/p/ajcodekatas/
under trunk/Dlr)

I created a console project, using Visual Studio 2008 (my interpreters are simple, and they don’t need any .NET 4.x feature, yet), named DlrHelloWorld.

I added the reference to Microsoft.Scripting.dll, Microsoft.Scripting.Code.dll, Microsoft.Dynamic.dll (curiously, one of the class I need for this example, ConsoleHost, in this dll).

The first class:

    public class HelloScriptCode : ScriptCode
    {
        public HelloScriptCode(SourceUnit sourceUnit)
            : base(sourceUnit)
        {
        }
        public override object Run(Scope scope)
        {
            Console.WriteLine("Hello, World");
            return null;
        }
    }

Objects of this class will run a simple “Hello, World”. Note that it receives a SourceUnit, something that has the source code to compile. But it will be ignored in this simple example.

Then, the class that manage the HelloScriptCode:

    public class HelloContext : LanguageContext
    {
        public HelloContext(ScriptDomainManager domainManager,
            IDictionary<string, object> options)
            : base(domainManager)
        {
        }
        public override ScriptCode CompileSourceCode(SourceUnit sourceUnit, 
            CompilerOptions options, ErrorSink errorSink)
        {
            return new HelloScriptCode(sourceUnit);
        }
    }

Its main responsability: given a SourceUnit, produce an ScriptCode object.

DLR can host a language, providing a REPL for it. I wrote a class inheriting ConsoleHost:

    public class HelloConsole : ConsoleHost
    {
        protected override Type Provider
        {
            get
            {
                return typeof(HelloContext);
            }
        }
    }

The simple main is:

    class Program
    {
        static void Main(string[] args)
        {
            (new HelloConsole()).Run(args);
        }
    }

 

The output could be boring ;-) :

I should study and understand the involved pieces in this example (host options, source unit, language context, script code). Next steps: research on compiling simple expressions and commands.

Keep tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

March 14, 2011

More Language Workbench Competition 2011 with AjGenesis

Filed under: AjGenesis, Code Generation, Lwc2011 — ajlopez @ 9:03 am

In my previous post I described my initial work for Language Workbench Competition 2011, using AjGenesis. Now, it’s time to write about some new tasks I wrote. You can read the tasks of the competition at:

http://www.languageworkbenches.net/LWCTask-1.0.pdf

First, I skipped Phase 0.3 (constraint checks such as name-uniqueness). I plan to complete it, but not now. I wrote Phase 0.4:

0.4 Show how to break down a (large) model into several parts, while still cross-referencing between the parts. For example, put the Car and Person entities into different files, while still having the Person -> Car reference work.

The code was committed to AjGenesis repository:

http://ajgenesis.codeplex.com/SourceControl/list/changesets

under examples/Lcw2011

This step was an easy one. The Project.txt is:

Project Lwc2011
	Entities
		Entity Person Source="Entities\Person.txt"	
		Entity Car Source="Entities\Car.txt"
	End Entities
End Project

 

The Source attribute is special in AjGenesis model loading: it indicates a separate file to load in order to complete the object. I added this feature in the early times of AjGenesis: I want to have model files that are manageable, that don’t hurt the eyes ;-)

This is Person.txt:

Entity Person
	Properties
		Property Name Type="Text"
		Property FirstName Type="Text"
		Property BirthDate Type="Date"
		Property OwnedCar Type="Car"
	End Properties
End Entity

I began to write Phase 1.0 first step:

1.1 Show the integration of several languages. Define a second language to define instances of the entities, including assignment of values to the properties. This should ideally really be a second language that integrates with the first one, not just "more syntax" in the same

grammar. We want to showcase language modularity and composition here.

This is the new model:

Project Lwc2011
	Description = "Lwc"
	Entities
		Entity Person Source="Entities\Person.txt"	
		Entity Car Source="Entities\Car.txt"
	End Entities
	
	Instances
		Instance p 
			Type = "Person"
			Properties
				Property Name Value = "Voelter"
				Property FirstName Value = "Markus"
				Property BirthDay Value = "14/02/1927"
				Property OwnedCar Value = "c"
			End Properties
		End Instance
		
		Instance c 
			Type = "Car"
			Properties
				Property Make Value = "VW"
				Property Model Value = "Touran"
			End Properties
		End Instance
	End Instances
End Project

But it's still work in progress. I could put the instances in another independent model file (Instances.txt) or I could put the each instance in a separate file, referenced by the above one using Source attribute. I should complete the tasks checking the existence of each property in the entity type that correspond to each instance. AjGenesis doesn't support Domain-Specific Language definition: the above example is still a free model. You can add any property to any instance, and the current implementation doesn't check the proper use of the model. So, this kind of check is pending work: I will add the checking logic in the tasks to execute.

Apparently, this task doesn't involve code generation. In the next task, I should check the values of the properties, too.

Keep tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

March 8, 2011

AjGenesis and Language Workbench Competition 2011

Filed under: AjGenesis, Code Generation, Lwc2011 — ajlopez @ 5:33 pm

Last week, thanks to a @pmolinam, I met

Language Workbench Competition 2011

Molina had sent me the info last year, but I forget the challenge. It is mentioned in InfoQ:

Language Workbench Competition 2011 Submissions

You can read the task list at:

http://www.languageworkbenches.net/LWCTask-1.0.pdf

I don’t sure if AjGenesis could qualify as a Language Workbench: it’s more oriented to be a free-model code generation tool. It has no IDE integration, but it is flexible and language-independent. It doesn’t produce any DSL: instead, it has a free model that can be use in many ways, to describe the concepts you have to model, without attached domain specific language. @pmolinam asked if I could complete the tasks using AjGenesis, my open source codegeneration tool. My answer: Yes!

Then, some messages appeared at twitter:

Today, I started to write Phase 0, points 0.1-0.2. The example is at AjGenesis trunk, at:

http://ajgenesis.codeplex.com/SourceControl/list/changesets

under examples/Lcw2011

The first point 0.1 is:

0.1 Simple (structural) DSL without any fancy expression language or such. Build a simple data definition language to define entities with properties. Properties have a name and a type. It should be possible to use primitive types for properties, as well as other Entities.

entity Person { 
    string name
    string firstname
    date bithdate
    Car ownedCar
}


entity Car { 
    string make
    string model
}

I wrote a simple Project.txt (note that I’m using the textual model instead the XML one, see Models for AjGenesis and Textual model for Code Generation in AjGenesis ).

Project Lwc2011
    Entities
	Entity Person
	    Properties
		Property Name Type="Text"
		Property FirstName Type="Text"
		Property BirthDate Type="Date"
		Property OwnedCar Type="Car"
	    End Properties
	End Entity
	
	Entity Car
	    Properties
		Property Make Type="Text"
		Property Model Type="Text"
	    End Properties
	End Entity
    End Entities
End Project

The Phase 0-0.2:

0.2 Code generation to GPL such as Java, C#, C++ or XML Generate Java Beans (or some equivalent data structure in C#, Scala, etc.) with setters, getters and fields for the properties.

Using assets from my post Building An Application using AjGenesis (Part 4), I wrote the commands:

GenerateCSharp.cmd

GenerateJava.cmd

GenerateVbNet.cmd

I’m using additional model files for describing technology. This is my VbNet technology model:

<Technology>
	<Name>VbNet</Name>
</Technology>

Simple! This time, I'm using the XML model (in memory, textual and XML have the same features; it's only a serialization option).

Look at GenerateCSharp.cmd:

AjGenesis.Console Project\Project.txt Project\Technologies\CSharp.xml Tasks\Complete.ajg Tasks\Generate.ajg

It loads the Project.txt, the corresponding technology model, loads and executes the Complete.ajg task, and then loads and executes the Generate.ajg task. The Complete.task (see the mentioned post Building An Application using AjGenesis (Part 4)) it's used for complete the model: one thing it dows is to translate abstract types like “Text”, to the corresponding final language type, like “string” in C#, or “String” in Java.

The Generate.ajg takes the enriched model and generate the code for each entity, using templates. This is the template for C#:

// Entity Class, generated with AjGenesis (http://ajgenesis.codeplex.com)
namespace ${Project.CSharp.Namespace} {
	public class ${Entity.Name}
	{
<#
	for each Property in Entity.Properties
#>
		public ${Property.CSharp.Type} ${Property.Name} { get; set; }
<#
	end for
#>
	}
}

The generated code for Person entity:

// Entity Class, generated with AjGenesis (http://ajgenesis.codeplex.com)
namespace Lwc2011 {
	public class Person
	{
		public string Name { get; set; }
		public string FirstName { get; set; }
		public DateTime BirthDate { get; set; }
		public Car OwnedCar { get; set; }
	}
}

Next steps: complete 0.3, 0.4 tasks, and then, solve Phase 1. I could complete the assignment in two weeks. I will publish the advance in upcoming posts.

Keep tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

The Shocking Blue Green Theme. Create a free website or blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 66 other followers