In Pt.1 we introduced the concepts of Test-Driven Development, Unit Test, Roman number, and we listed the requirements to build our Roman Number library.
The second requirement: invoke library as a function (without new)
Let’s create the test for this requirement by adding this code to our testRomanNumber.js:
|
|
If we run this new test by typing:$ npm test
we’ll see this error:
In order to pass this test we may use the property new.target added in ECMAScript 2015 (ES6)
The new.target property lets you detect whether a function or constructor was called using the new operator. In constructors and functions instantiated with the new operator, new.target returns a reference to the constructor or function. In normal function calls, new.target is undefined.
Let’s add this piece of code inside RomanNumber constructor
Then we run the test$ npm test
And, as if by magic, the error disappears:
The third requirement: add toInt and toString() methods
Add two test cases for this requirement, inside the describe “Check exceptions”
These test cases are bases on the isFunction method of Chai assert interface
If we run the test with:$ npm test
we’ll see that only the toInt() method does not exist:
This is because in Javascript every object has a toString() method
So, in order to pass the test, we just have to add this code to RomanNumber.js:
Test the correct result by yourself, by typing:$ npm test
Requirement: value passed not null or empty
We have to create three test cases to check null value, empty value, and no value.
|
|
These test cases use Chai expect interface and the method throw.
In this way we can check that, given a particular value, the library constructor throws an Error with a particular message.
We already know that no exception will be thrown, so let’s step forward and add the code to our library constructor:
Let’s run our test$ npm test
and… All tests passed! Very good job indeed so far!
Requirement: invalid range value
The test cases for this requirement, will be integer values not in [1,3999]
Even these test cases use Chai expect interface and the method throw.
As for the previous requirement we’ll skip the test part here and we’ll go to add this piece of code to our library constructor:
As expected: all tests passed.
Invalid values
The next two tests will be about checking two types of invalid values:
- A String containing non Roman symbols: ‘error’
- A String containing Roman symbols and Hindu-Arabic symbols: ‘CD1X’
For the both of these cases, our roman number library will have to throw an Error exception with message: ‘Invalid value’
Here are the test cases to add to testRomanNumber.js
In order to pass these tests, we have to check that any string passed in input must contain only Roman symbols.
To add this little improvement a minor refactor must be performed on our constructor. Replace:
with:
If the input is a string, we make sure that any of its characters belong to our romanSymbols array.
Test the correct result by yourself, by typing:$ npm test
Allow Hindu-Arabic numbers as String
Because we want to allow anyone using our library to pass an integer as a string (i.e. ‘1473’ => 1473), we’ll add this quick test:
Let’s have a look at what happens if we run the test now:
The exception is thrown because so far we assumed that any string was supposed to be in Roman symbols only.
For this reason we have must handle also this case, when a string contains Hindu-Arabic symbols (integer).
We will transform the constructor in this way:
|
|
In this way we can treat an integer string like an integer.
Let’s run the test:$ npm test
All tests passed
Even more to come
In Pt.3 we will start converting numbers (from Hindu-Arabic to Roman numbers).
Check out the Roman Library Repository if you cannot wait for the last chapter of this blog