1

I have written my own implementation of Date() in Javascript, BardyDate, which in addition to all the properties/methods of Date, does a bunch of other stuff. Why I have done this is a very good question indeed but it is such a long story I will save it for a rainy day.

But what I was thinking would be lovely would be to be able to verify that it still behaves correctly as a Date. I know nothing about test suites etc but wondered how I might apply any existing tests for the Javascript Date object to my BardyDate to show correctness?

Any advice very welcome.

Bardy
  • 2,100
  • 1
  • 13
  • 11
  • 1
    iterate the methods of date, and call the same methods with your date, seeing that they produce the same output. you will need to compare the string version of the dates since 2 objects are never equal. – dandavis Dec 06 '14 at 23:38

1 Answers1

0

I waited to respond because I am not certain that there are no Date test suites already out there for javascript. I wanted to give the benefit of the doubt to anyone who might know more.

However, as far as I know this sort of testing would all be done in browser building/validation. It certainly is possible to use some of the existing test suites from that realm of development, but I don't think it would be an easy task to set-up. Many of the browser's have a build process that you could fork (specifically you could isolate their date test cases). Within their test process you would have to find the given segment for Javascript Date objects, that would test to ensure w3 spec compatibility.

At that, Selenium is a very common and practical means of creating unit tests and if designed well integration tests for web-apps (hooking into the javascript as well), and is capable of producing nice reports on test results.

Lastly, a cummulative post on Javascript Testing libraries can be found on this post about Javascript TDD (Test Driven Design)

Or you could do something like the following (meant to be a guideline not a complete solution -- inspired by the comment from dandavis):

var epochTS = 0;
var bd = new BardyDate(epochTS);
var d = new Date(epochTS);

Object.getOwnPropertyNames(Date.prototype).forEach(function(dateFunction){
    //in this if statement you are testing the functions like getTime() and getYear()
    if(dateFunction.indexOf("get") == 0){
       console.log("dateFunction " + dateFunction + 
           "() pass: " + (bd[dateFunction]() === d[dateFunction]()))
    }
    //in this if statement you are testing the functions like toString() and toJSON()
    if(dateFunction.indexOf("to") == 0){
       console.log("dateFunction " + dateFunction + 
           "() pass: " + (bd[dateFunction]() === d[dateFunction]()))
    }
    //then there are the 16 set methods, those you probably would want to hardcode.
    //unless you are content with passing a hard coded value in like "10" -- the 
    //issue would be bounds testing, which you would likely want to hardcode.
    //beyond the scope of this for-each loop.
})

A little explanation on the above snippet

With Object.getOwnPropertNames(Date.prototype) you can obtain all the methods of Date despite the fact that Date has the property DontEnum (see this post for more info).

Also you are able to treat each function string as a key within the javascript object hence the d[dateFunction]() which if dateFunction === "toString" would be interpreted/compiled down to d[toString]() which is equivalent to d.toString()

Community
  • 1
  • 1
Arthur Weborg
  • 8,280
  • 5
  • 30
  • 67