Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Welcome to DSpec

DSpec is a simple Behavior Driven Development Framework for D2 and is based on Templates and Delegates. The current Phobos Standard Library for D2.0 is necessary and it's inspired by DUnit, Scalatest and RSpec.

DSpec offers an alternative way for testing Software automatically written in D and uses the native unittest environemnt offered by DMD Compiler.

Currently, DSpec is still in Development. But you can already browse the current source (http://git.dsource.org/projects/dspec/).

Project Status

Development appears to have stalled with commit c4c7e63 on 2010-04-11.

Help & Feedback

If someone is interested to work on DSpec, help is always welcome. The source is stored in a git repository and can be checked out via:

git clone http://git.dsource.org/projects/dspec/

and write an email to ruunhb [at] googlemail.com

Introduction

Though DSpec is not finished and ready to use, a small introduction about the framework should be given here.

You can define a specification by using the unittest environment and several Spec-Templates.

import dspec.Spec;
import std.conv;

unittest 
{
  describe! "A specification provided by DSpec" = {
    it! "should provided generic ShouldMatchers" = {
      int a = 10;
      auto ex1 = new Exception("Hi");
      auto ex2 = new Exception("Hi");        
      Exception ex3 = null;
      
      bool exp = (10 == a);
        
      Value!(a).should_equal(10);
      Value!(a).shouldnt_equal(12);
      Value!(exp).should_be_true;
      
      Value!(ex3).should_be(null);
      Value!(ex1).should_be(ex2);
      Value!(ex1).shouldnt_be(null);
    };

    ignore! "should be able to ignore an example" = {
      Value!(10 == 20).should_be_true;
    };
    
    it! "should be able to pending an example";
    
    it! "should show error messages if a ShouldMatcher fails" = {
      int integer = 10;
      Value!(integer).shouldnt_equal(integer);
    };
    
    it! "should show a second error in a describe block" = {
      Value!(15).should_equal(20);
    };
    
    it! "should provided regular expression matcher for strings" = {
      string text = "I love Bremen, Germany";
        
      Value!(text).should_match(r"^I.love.Bremen");
      Value!(text).should_match(r"Bre.en");          
      Value!(text).should_match(r"I.love");
      Value!("Hi").should_match(r"H.");
    };
    
    it! "should inspect throwing exceptions" = {
      Inspect!(Exception).should_be_thrown = {
        throw new Exception("Test");            
      };
      
      Inspect!(Exception).shouldnt_be_thrown = {
        // Do Nothing
      };
    };
      
    it! "should be able to check arrays" = {
      int[] array = [1, 4, 6];
      
      Value!(array).should_equal([1, 4, 6]);
      Value!(array).should_contain(4);
      Value!(array).should_have_length(4);
    };
      
    it! "should be able to check maps" = {
      int[string] map = ["marani": 10, "rooitea": 4];
      
      Value!(map).should_map("marani", 10);
      Value!(map).should_map(["marani": 10, "rooitea": 4]);
      Value!(map).should_contain_key("marani");
      Value!(map).should_contain_keys(["marani", "rooitea"]);
      Value!(map).should_contain_val(10);
      Value!(map).should_have_length(2);
    };
  };
}

If you only want to run your specifications code in your linked executable, you need to add the following lines to your main function:

import spec = dspec.Runner;

import std.c.stdlib;
import std.stdio;
import std.string;

int main(string[] args) {
  version(unittest)
    return spec.resultSpec();

  // YOUR CODE
  
  return EXIT_SUCCESS;
}

This lines will output a small summary after running your specs, exit immediately the application and return an exit code if a spec failed.

After compiling your source with -unittest option offered by dmd, you can run the spec executable and see the following output.

A specification provided by DSpec
- should provided generic ShouldMatchers                       [FAILED]
  !! dspec.d(23): Value <ex1> is not the same as object.Exception: Hi
- should show error messages if a ShouldMatcher fails          [FAILED]
  !! dspec.d(35): Value <integer> is equal (10 == 10)
- should show a second error in a describe block               [FAILED]
  !! dspec.d(39): Value <15> is not equal (15 != 20)
- should be able to check arrays                               [FAILED]
  !! dspec.d(66): Value <array> doesn't have length 4, [1, 4, 6]

Finished in 0.010 seconds

7 examples, 4 failures