Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 7 Next »

This page walks you through a simple TDD kata

git clone the code from git clone https://livingwater@bitbucket.org/living-water-js/tdd-walkthrough.git

Pre-requisite knowledge

  • How to install a the unit testing framework for the particular language

  • An understanding of why testing is important

  • How to write a unit test and run the test

  • The three A's (Arrange, Act, Assert)

  • Writing incremental tests and why this is important - one test, some production code, then next test and some production code

The problem statement

A user can send from a front end application a series of comma separated words to be logged as part of a wider application. You can assume that there already exists some code that sends a cleansed array of words to the business/database tier - these are referred to as tags. You must design an implementation class (the CUT) that splits that string into an array of strings. You will need to write the tests first, then the production code.

Overview

You are going to develop a small piece of code that accepts a comma delimited list of tags (words/tokens - characters, numbers, some symbols like $£%& but not a comma) and must return an array of tags. The preceding and proceeding commas should be removed, white spaces preceding the first tag must be removed, white spaces proceeding the last tag must be removed, contiguous series of words separated with spaces and ended with a comma should be treated as single tag

Examples of the rules are as follows

  • "" must return []

  • "java" must return ["java"]

  • " java,python" must return ["java", “python”]

  • "java byte code,python" must return must return ["java byte code", "python"]

Constructs your tests in the following order

Iteration 1

Test 1

var assert = require("chai").assert;
var expect = require("chai").expect;


// Put your CUT here

describe('Splitting a string', function() {
    it('Input an empty string, return an empty array []', function() {
        // arrange...
        var cut = new StringSplitter();
        var expectedResult = []
        var emptyString = "";

        // act...
        var actualResult = cut.splitString( emptyString );
        console.log( actualResult.length);

        // assert...
        assert.equal(actualResult.length, expectedResult.length );
        expect(actualResult).to.eql(expectedResult);
    });
});

CUT 1

class StringSplitter
{
    splitString( stringToSplit )
    {
        var result = [];

        if( stringToSplit.length < 1 )
            return result;

        return result;
    }

}

Iteration 2

Test 2

var assert = require("chai").assert;
var expect = require("chai").expect;

// put your CUT here

describe('Splitting a string', function() {
    it('Input an empty string, return an empty array []', function() {
        // arrange...
        var cut = new StringSplitter();
        var expectedResult = []
        var emptyString = "";

        // act...
        var actualResult = cut.splitString( emptyString );
        console.log( actualResult.length);

        // assert...
        assert.equal(actualResult.length, expectedResult.length );
        expect(actualResult).to.eql(expectedResult);
    });

    it('Input one string, return array with one item', function() {
        // arrange...
        var cut = new StringSplitter();
        var expectedResult = ["java"]
        var emptyString = "java";

        // act...
        var actualResult = cut.splitString( emptyString );
        console.log( actualResult.length);

        // assert...
        assert.equal(actualResult.length, expectedResult.length );
        expect(actualResult).to.eql(expectedResult);
    });

});

CUT 2

class StringSplitter
{
    splitString( stringToSplit )
    {
        var result = [];

        if( stringToSplit.length > 0 )
            result.push(stringToSplit);

        return result;
    }

}

Phase 3

Test 3

var assert = require("chai").assert;
var expect = require("chai").expect;

// put your CUT here

describe('Splitting a string', function() {
    it.skip('Input an empty string, return an empty array []', function() {
        // arrange...
        var cut = new StringSplitter();
        var expectedResult = []
        var emptyString = "";

        // act...
        var actualResult = cut.splitString( emptyString );
        console.log( actualResult.length);

        // assert...
        assert.equal(actualResult.length, expectedResult.length );
        expect(actualResult).to.eql(expectedResult);
    });

    it('Input one string, return array with one item', function() {
        // arrange...
        var cut = new StringSplitter();
        var expectedResult = ["java"]
        var emptyString = "java";

        // act...
        var actualResult = cut.splitString( emptyString );
        console.log( actualResult.length);

        // assert...
        assert.equal(actualResult.length, expectedResult.length );
        expect(actualResult).to.eql(expectedResult);
    });

    it('Input two comma seperated strings, return array with two items', function() {
        // arrange...
        var cut = new StringSplitter();
        var expectedResult = ["java", "python"]
        var inputString = "java,python";

        // act...
        var actualResult = cut.splitString( inputString );
        console.log( actualResult.length);

        // assert...
        assert.equal(actualResult.length, expectedResult.length );
        expect(actualResult).to.eql(expectedResult);
    });

});

CUT 3

class StringSplitter
{
    splitString( stringToSplit )
    {
        var result = [];
        var tempResult = stringToSplit.split( ',' );
        console.log("tempResult:");
        console.log( tempResult );
        console.log(tempResult.length );

        if( tempResult.length > 0 )
            result = tempResult;

        return result;

    }
}

  • No labels