Raba - Defend your code RSS 2.0
# Saturday, August 01, 2009

Test methods must be written as production code.
While writing your tests you must act the same methods you write for your production code which means:

  1. human-readable (code standard, pattern etc.)
  2. code re-use (copy paste is never acceptable)
  3. etc...

This is a code you will probably find in your test projects

   1: [Test]
   2: public void GetCommentsByQuery_LookFormHmmmText_FindAtLeaseOne()
   3: {
   4:     SvnLogParser parser = new SvnLogParser();
   5:     string textToSearch = "Hmmm...";
   6:     var results = parser.GetCommentsByQuery(comment => comment.Contains(textToSearch));
   7:     CollectionAssertExtensions.IsNotEmpty(results);
   8: }
   9:  
  10: [Test]
  11: public void GetCommentsByQuery_LookFormBugText_FindAtLeastOne()
  12: {
  13:     SvnLogParser parser = new SvnLogParser();
  14:     string textToSearch = "bug";
  15:     var results = parser.GetCommentsByQuery(comment => comment.Contains(textToSearch));
  16:     CollectionAssertExtensions.IsNotEmpty(results);
  17: }

You can see that the two methods are pretty much the same, the only change is the textToSearch variable.
There are some excuses for such duplication:

  • "We want two different names... to easily understand the failiure reason"
  • "we want it to be easy to read, without context switches..." (method calls and un-needed inheritance complexity)

We, of course, can refactor this code, like this:

   1: [Test]
   2: public void GetCommentsByQuery_LookFormBugText_FindAtLeastOne()
   3: {
   4:     string textToSearch = "bug";
   5:     GetCommentsByQuery_AssertAllItemsStartsWith(textToSearch);
   6: }
   7:  
   8:  
   9: [Test]
  10: public void GetCommnentsByQuery_LookForHmmmText_VerifyInsideComment()
  11: {
  12:     string textToSearch = "Hmmm...";
  13:     GetCommentsByQuery_AssertAllItemsStartsWith(textToSearch);
  14: }
  15:  
  16: public void GetCommentsByQuery_AssertAllItemsStartsWith(string textToSearch)
  17: {
  18:     SvnLogParser parser = new SvnLogParser();
  19:     var results = parser.GetCommentsByQuery(comment => comment.StartsWith(textToSearch));
  20:  
  21:     CollectionAssertExtensions.AllItemsSatisfy(results, res => res.Comment.StartsWith(textToSearch));
  22: }

This is better when you think of clone detection: less identical rows and the logic was extracted to one method.
But people might say that they can't see the AAA (Arrange, Act, Assert) and they want it in one place,
Moreover, in such code sample, it is harder to find all the input parameters, cause they will be spread all over the TestFixture methods.

 

NUnit 2.5 added TestCaseAttribute, this can help us write tests that are:

  • more readable
  • shorter test-fixture
  • all-in-one-place tests
   1: [TestCase("Hm...")]
   2: [TestCase("bug")]
2: [TestCase("bug")]
   3: public void GetCommentsByQuery_LookForText_FindAtLeastOne(string textToSearch)
   4: {
   5:     SvnLogParser parser = new SvnLogParser();
   6:     var results = parser.GetCommentsByQuery(comment => comment.Contains(textToSearch));
   7:     CollectionAssertExtensions.IsNotEmpty(results);
   8: }

Here you can avoid the duplication and you won't need to refactor your Tests to more than one method, while you still see them running using the given parameter,
for example when the second parameter will fail you will see this output:

------ Test started: Assembly: App.Tests.dll ------

TestCase 'App.Tests.NewFolder1.SvnLogParserTests.GetCommentsByQuery_LookForText_FindAtLeastOne("Hm...")'
failed: 
  Expected: True
  But was:  False
    C:\ShaniData\ProjectsByTitle\Delver\App.Tests\NewFolder1\SvnLogParserTests.cs(75,0): at App.Tests.NewFolder1.CollectionAssertExtensions.IsNotEmpty[T]
    C:\ShaniData\ProjectsByTitle\Delver\App.Tests\NewFolder1\SvnLogParserTests.cs(33,0): at App.Tests.NewFolder1.SvnLogParserTests.GetCommentsByQuery_LookForText_Find...


4 passed, 1 failed, 0 skipped, took 0.78 seconds (NUnit 2.5).


The output will point the given method and the parameter used - which will ease finding the error.
You can extend reading about the usage here

Saturday, August 01, 2009 5:20:12 AM (GMT Daylight Time, UTC+01:00)  #    Comments [2] - Trackback
.Net 3.5 | C#3.0 | TDD | NUnit

# Tuesday, July 28, 2009

Yoav sent me this web site.

I like the idea of sharing knowledge,
I also like to read and learn from open sources.
This is even better: daily -public- code review...


Tuesday, July 28, 2009 7:05:03 AM (GMT Daylight Time, UTC+01:00)  #    Comments [0] - Trackback
Life | My Site | Software Development

# Wednesday, July 08, 2009

For more background about when we use this API you can read my last post about Rendering Objects to Html

Simple Rendering of query:

   1: var query = new StringTemplate("SELECT $column$ FROM $table$;");
   2: query.SetAttribute("column", "name");
   3: query.SetAttribute("table", "User");
   4: var data = query.ToString();
We will refactor this sample to handle the queries from an outside source:
   1: var group = new StringTemplateGroup("SimpleTemplates",@"C:\Templates\TemplateEngineTestCase");
   2: var query = group.GetInstanceOf("1_simple");
   3: query.SetAttribute("column", "name");
   4: query.SetAttribute("table", "User");
   5: var data = query.ToString();
Rows 1,2 - loading the template data from a source file. 
Here is the Template (1_simple.st):
//1_simple.st
SELECT $column$ FROM $table$;
 

Template Anonymous Type:

In the last template usage (1_simple.st) We bind the parameters using strings only. but we also have a better options: let's assume we wrote down such template:

//2_simpleObjects.st
Full Name: $Person.FirstName$ - $Person.LastName$

But let's assume that we don't have the Person class\instance, we can simply write it using the anonymous types:

   1: var user = new {FirstName = "Shani", LastName = "Raba"};
   2: var group = new StringTemplateGroup("OOTempaltes", @"C:\Templates\TemplateEngineTestCase");
   3: var query = group.GetInstanceOf("2_simpleObjects");
   4:  
   5: query.SetAttribute("Person", user);
You can also use this kind of bindings for setting template-attributes from each one of the Person sub-classes.
 
 
Template list:

Till now it is all pretty simple to implement by yourself, but the real issue here is the List binding feature.
You can write such template:

//3_simpleLoop.st
$items: {num1|
    <li>$num1$</li>
    }$
$end$

and bind the list using this code:

   1: var templateFolder = new StringTemplateGroup("SimpleLoopTemplates", @"C:\Tempaltes\TemplateEngineTestCase");
   2: var template = templateFolder.GetInstanceOf("3_simpleLoop");
   3:  
   4: template.SetAttribute("items", new List<string> {"Shani", "Doron", "Nati", "Yossi"});

 

Conclusion:

  • The code is pretty simple, the templates engine implements good functionality and is well documented.
  • The StringTemplate syntax is easy to learn and well documented.
  • I still don't like it when I need to learn a new language\syntax - it will sharpen the newbie's curve.
  • StringTemplate is missing good Object2Template Designer.
Wednesday, July 08, 2009 8:13:57 AM (GMT Daylight Time, UTC+01:00)  #    Comments [1] - Trackback
.Net 3.5 | Scripting Technology

# Sunday, June 14, 2009

Dr. Alistair Cockburn will be in Israel.
I recommend you this user-group: Scrum Event. come and learn from the best.

See you there.

Sunday, June 14, 2009 4:42:51 AM (GMT Daylight Time, UTC+01:00)  #    Comments [0] - Trackback
Agile | Scrum

# Sunday, June 07, 2009

Background:

We are writing a new task for the ArcGis Explorer, this task needs to place images on the globe using its X,Y coordinates.
While for each instance on the globe we need to render its details as html.

Mission:

Rendering the objects to html, we are still didn't find the right place (on the server or on the client), but let assume that they both need to do the same job.

Ideas:

  1. Render in code:
    This is the naive way of thinking. while you are first thinking about this  problem you might think to add the object an ToHtml() method.
    Always think about the: Single Responsibility Principle (SRP).
    You can still think about Writing an HtmlWriter or HtmlRenderer class that gets object and render the object using reflection or something else, but than you will find yourself indenting you JavaScript\html\CSS in C#, for example you can watch this code:

       1: var items = new List<string> {"Shani", "Doron", "Nati", "Yossi"};
       2: var sb = new StringBuilder();
       3: items.ForEach(currentItem => 
       4:         sb.AppendFormat("<li>{0}</li>", currentItem)
       5:         );
       6: string data = sb.ToString();
    Let's think about the time we will need to change the UI from <li> to one with style or maybe even change the list to be styled as table...
     
  2. XSL - is pretty more dynamic than code, isn't it?
    So we are now looking for something more dynamic, something that we can read from a file so we can give it to our designer to play with the UI.
    This lead us to the second option: at my past all our reports were rendered using XSL files, personally I don't like this option.
    But the main reason is that we will need to serialize our objects to XML, while in most cases all you need is to render only few fields.
  3. Let's found another Template Engine:
    The first one I can think of is the MVC Engine or Brail. after some searching reading and Testing I found StringTemplate.
    Here is a simple code for using the API:

       1: var templateFolder = new StringTemplateGroup("SimpleLoopTemplate", @"\TemplateEngineTestCase\WinApp");
       2: var template = templateFolder.GetInstanceOf("3_simpleLoop");
       3: //here we are binding our list to the template
       4: template.SetAttribute("items", new List<string> {"Shani", "Doron", "Nati", "Yossi"});
       5: var data = template.ToString();

    and here is the template (file name: 3_simpleLoop.st):
       1: $items: {num1| 
       2:   <li>$num1$</li>
       3:   }$
       4: $end$
     

Conclusion:

The StringTemplate give us an API to solve our problem - and using such code and template we can render our Models to different Views.
But is this API is easy or even stable?

In later posts we will see more samples using the StringTemplate.
Hopefully samples for using Brail too.
Sunday, June 07, 2009 3:48:07 AM (GMT Daylight Time, UTC+01:00)  #    Comments [0] - Trackback
.Net 3.5 | Software Development | Design Patterns

# Saturday, April 11, 2009

I've been looking around for samples and some good advices for writing resumes and found this site: VisualCV

Mostly, I like this Engineer profile: http://www.visualcv.com/alex1
The really good part is the "code sample" section on the top-right corner...

Have fun...

Saturday, April 11, 2009 5:56:02 AM (GMT Daylight Time, UTC+01:00)  #    Comments [0] - Trackback
Life | Software Development

# Friday, April 10, 2009

Last month I've presented a simple newbie's tutorial about NTS - Net Topology Suite.
Here you can download the code sample & presentation.

What is it NTS (Net Topology Suite)?

  • implements geometry model defined in OpenGis Consortium (OGC)
  • implements  simple features inside the OpenGis Consortium (OGC) as Geographic Data access
  • implements Some great OGC writers as: GmlWriter (Geographic markup language), WktWriter (Well Known Text)

How do I know whether I need NTS:

  1. You are coding a GIS (Geographic Information Systems) applications
  2. You want to use a known Convention for Point, Polygon, Polyline etc.
  3. You want simple implementation for OGC

3 times No - you don't need this
3 times Yes - you must read this article and probably use it.

You can read more about Kml & C#3.0 - at my last post.
I will keep posting more samples in the near future.

Friday, April 10, 2009 10:15:45 PM (GMT Daylight Time, UTC+01:00)  #    Comments [0] - Trackback
.Net 3.5 | C#3.0 | GIS

This post inspired by Gal Ochana.

Gal is a new gifted programmer at my team, she presented a great presentation about Linq to XML - you can download code here.
The great bonus at her presentation was the Linq to Kml Sample, I would like to share this with you.

The kml itself is an XML file at our sample it looks like this:

   1: <?xml version="1.0" encoding="UTF-8"?>
   2: <kml xmlns="http://www.opengis.net/kml/2.2">
   3:     <Placemark>    
   4:         <name continent="America">America</name>    
   5:         <description>Attached to the ground. Intelligently places itself at the height of the underlying terrain.</description>    
   6:         <Point>      
   7:             <coordinates>-100.0822035425683,37.42228990140251,0</coordinates>    
   8:         </Point>  
   9:     </Placemark>
  10:     <Placemark>    
  11:         <name continent="Africa">Africa</name>    
  12:         <description>Attached to the ground. Intelligently places itself at the height of the underlying terrain.</description>    
  13:         <Point>      
  14:             <coordinates>20.0822035425683,20.42228990140251,0</coordinates>    
  15:         </Point>  
  16:     </Placemark>
  17:     <Placemark>    
  18:         <name continent="Asia">Asia</name>    
  19:         <description>Attached to the ground. Intelligently places itself at the height of the underlying terrain.</description>    
  20:         <Point>      
  21:             <coordinates>42.0822035425683,33.42228990140251,0</coordinates>    
  22:         </Point>  
  23:     </Placemark>
  24:     <Placemark>    
  25:         <name continent="Europe">Europe</name>    
  26:         <description>Europe</description>    
  27:         <Point>      
  28:             <coordinates>100.0822035425683,62.42228990140251,0</coordinates>    
  29:         </Point>  
  30:     </Placemark>
  31: </kml>


You can read more about Kml here.


Screen-shot of the above kml in the ArcGis-Explorer:

You can see in the above Kml file sample the continents represented as a simple points, of course you can extend it to include polygons, poly-lines or anything else that valid within KML standard.
The only things that differs Kml from the known Xml file is the namespace that we will need to handle in our code, let's see the query itself:

   1: XNamespace ns = "http://www.opengis.net/kml/2.2"; 
   2:  
   3: XDocument kmlDoc = XDocument.Load(InputKml);
   4:  
   5: var result = from placemark in kmlDoc.Descendants(ns + "Placemark")
   6:              where (placemark.Element(ns + "name")
   7:                              .Attribute("continent").Value == 
   8:                              Continents.SelectedItem.ToString())
   9:              select placemark;

 

line 1: declare the full namespace that will be used for the attributes and Tags
line 3: loading the kml input file
line 5-9: run a simple query to select a single continent, pay attention to the namespace that we concatenate for selecting the right node.

Here you can see a screen-shot with the result of the above query using the "Asia" continent as the selected item.

Enjoy.

Friday, April 10, 2009 10:03:14 PM (GMT Daylight Time, UTC+01:00)  #    Comments [0] - Trackback
.Net 3.5 | C#3.0 | LINQ | GIS | AGX

# Thursday, March 26, 2009

Next Friday (3.4.09) will be the second alt.net conference
You can read more here about alt.net 

Registration to the event at facebook

I'll be there.

Thursday, March 26, 2009 11:50:24 AM (GMT Standard Time, UTC+00:00)  #    Comments [2] - Trackback
alt-net-israel

Archive
<August 2009>
SunMonTueWedThuFriSat
2627282930311
2345678
9101112131415
16171819202122
23242526272829
303112345
Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2013
Shani Raba
Sign In
Statistics
Total Posts: 146
This Year: 0
This Month: 0
This Week: 0
Comments: 97
Cool Stuff
Add to Technorati Favorites
Themes
Pick a theme:
All Content © 2013, Shani Raba
DasBlog theme 'Business' created by Christoph De Baene (delarou)