Phillip Trelford's Array

POKE 36879,255

BDD with ``TickSpec`` talk

The podcast for “BDD with TickSpec and Concurrency with Agents” from the F#unctional Londoners Meetup is now available, many thanks to Skills Matter: http://skillsmatter.com/podcast/scala/tickspec-bdd-for-c-sharp-f-sharp

Podcast content:

  • a quick look at F# in MonoDevelop
  • BDD with TickSpec with examples in Visual Studio using NUnit, MbUnit & Silverlight
  • Tomas Petricek on Concurrency with Agents including Chat and Twitter samples

This talk coincided with the November 2010 stable release of TickSpec, which supports both .Net 2.0+ and Silverlight 4. Plus examples for all major .Net testing frameworks.

All TickSpec examples presented are on CodePlex: http://tickspec.codeplex.com

Thanks to everyone who made it a really fun evening!

FSharpMeet

Picture taken at the Slaughtered Lamb pub courtesy of Ankur Gurha

Tic-tac-toe

Thanks to a recent article by Cameron Taggart it is now possible to Create F# Silverlight Apps from Visual Studio 2010 Shell. This means that you now don’t need to use a C# project to build the XAP file; which previously outside of VS2010 Pro meant resorting to building the XAP with Visual Web Developer 2010 Express and F# libraries in the VS2010 shell. The only issue I’ve found with this solution is that it is not possible to directly add new files to the F# application project, but you can easily add existing files.

From his example I've created a small reference Silverlight solution with a Tic-tac-toe theme:

  • TicTacToe: the Silverlight application
  • Game: a Silverlight library with some game logic
  • TicTacToe.Test: a Silverlight Unit Test project for testing game logic

If you have Silverlight 4 installed you should see a Tic-tac-toe board that you can mark:

The Silverlight Unit Test project contains a small Behavioural Driven Development (BDD) example to test winning positions using TickSpec:

Feature: Winning positions

Scenario: Winning positions
    Given a board layout:
        | 1   | 2   | 3   |
        | <O> | <O> | <X> |
        | <O> |     |     |
        | <X> |     | <X> |
    When a player marks <X> at <row> <col>
    Then <X> wins
    
Examples:
    | row    | col    | 
    | middle | right  |
    | middle | middle |
    | bottom | middle |

Examples:
    | X | O |
    | X | O |
    | O | X |

 

The example above shows that TickSpec will execute all combinations for multiple examples blocks. This makes it possible to also run the tests with X’s swapped for O’s:

image

The steps in the scenario are mapped to attributed F# tick methods:

let [<Given>] ``a board layout:`` (table:Table) =
    table.Rows |> Seq.iteri (fun y row -> 
        row |> Seq.iteri (fun x value -> board.[x,y] <- parseMark value)
    )
   
let [<When>] ``a player marks (X|O) at (top|middle|bottom) (left|middle|right)``
        (mark:string,Row row,Col col) =
    board.[col,row] <- parseMark mark

let [<Then>] ``(X|O) wins`` (mark:string) =
    Game.mark <- parseMark mark |> Option.get
    let line = winningLine()
    Assert.IsTrue(line.IsSome)

With the column and row positions parsed with F# Active Patterns:

let (|Col|) = function 
    | "left" -> 0 | "middle" -> 1 | "right" -> 2
    | s -> invalidCast s

let (|Row|) = function 
    | "top" -> 0 | "middle" -> 1 | "bottom" -> 2 
    | s -> invalidCast s

 

If you are interested in learning more about BDD & TickSpec there is a free F#unctional Londoners Meetup Group evening event on Wed 24th Nov 2010 at Skills Matter:

Teaser for Tomas Petricek’s talk on his Agent’s:

References:

Source code: TicTacToe.zip (334.65 kb)

Metadata: data about data

Compilers for statically typed languages like C# and F# maintain data about types from programs. This type data is used at design time to provide IntelliSense and during compilation to generate new types and bind to existing types. We can think about this type data in the abstract as data about data, or Metadata. In statically typed languages the typed Metadata is typically imported and generated from typed programs.


Question:

What if a compiler could be augmented with metadata from other sources, say a database or an XML DTD or a DSL?

Answer:

Then we could map to data easily inside our programs without resorting to either late binding or a code generation step!!!


Examples of accessing attributes of an XML element

1: Late-binding with string literals (F#)

let person = XElement.Parse("<Person Name=\"Bob\"/>")
let name = person.Attribute(XName.Get "Name").Value

 

2: Late-binding with dynamic lookup (F#)

// Define dynamic lookup operator
let (?) (el:XElement) (name:string) = el.Attribute(XName.Get name).Value
// Lookup Name attribute
let name = person?Name

 

3: Code generation using XSD.exe (C#)

string xml = "<Person Name=\"Bob\"/>";
var s = new XmlSerializer(typeof(Person));
var person = s.Deserialize(new StringReader(xml)) as Person;
var name = person.Name;

 

4: Hypothetical code binding (F#)

let person = XmlProvider.LoadXml("<Person Name=\"Bob\"/>")
let name = person.Name

 

The first 1st and 2nd methods using late-binding in effect use string-literals so lack type safety. The 3rd method required a code generation step that generated a whopping 62 lines of C# code to map 1 XML element with a single attribute.

The 4th (hypothetical) method provides all the immediacy benefits of a dynamic language like Python with the compile time safety and ease of use of a statically typed language.

PDC 2010: The Future of F#: Data & Services at Your Finger Tips – Don Syme:

Programming today exhibits a voracious appetite for information, and one of the most important trends in languages today is to make access to data and services fluent and seamless. Come and see the latest from the F# team, and learn how we are extending F# to embed the analytical programmer instantly in a world of typed data and services, whether they be web, enterprise, client or local.

From the slides that are now up on the PDC site, it looks like F# 3.0 is about to deliver!