Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test builder class for easier testing #43

Merged
merged 1 commit into from
Sep 15, 2016
Merged

Conversation

likerRr
Copy link
Collaborator

@likerRr likerRr commented Sep 14, 2016

Since we start cover locales with tests it would be great to have easier tool to test the time, because sometimes it's sad to calculate, e.g. what is the result of adding 15 days to 18 of December, 2016. Moreover it would be great to have ability to "read" test written for other languages.

This PR introduces a class for easier testing. It wraps the timeago and provides human-friendly methods for manipulations with internal timeago().format method. Thanks to these interface it becomes intuitively clear about which piece of time you test regardless of the language.

How to use

tests/test-builder.js exports factory method that accepts the same arguments as the timeago function and returns an instance of the builder with familiar API.

const tb = require('../test-builder')(Date.now(), 'en');
// you don't need to worry about initial date value, in fact it doesn't matter at all

Instance API

register(code, fn, use)

Falls back to timeago().register method. 3rd argument tells to use registered locale.

useLocale(code)

Same as timeago().setLocale

getFormat(format)

Same as timeago().format

addSeconds(n)
addMinutes(n)
addHours(n)
addDays(n)
addWeeks(n)
addMonths(n)
addYears(n)

According to the name of method adds n amount of time to initial time and returns timeago statement. E.g:

var tb = require('../test-builder')(Date.now(), 'en');
console.log(tb.addSeconds(1) === 'a while'); // true

subSeconds(n)
subMinutes(n)
subHours(n)
subDays(n)
subWeeks(n)
subMonths(n)
subYears(n)

Same as add${Time} but subs provided amount of time. E.g.:

var tb = require('../test-builder')(Date.now(), 'en');
console.log(tb.subSeconds(1) === 'just now'); // true

add(obj)
sub(obj)
obj: {seconds?: number, minutes?: number, hours?: number, days?: number, weeks?: number, months?: number, years?: number}

Methods allow to combine several time statements, e.g.:

var tb = require('../test-builder')(Date.now(), 'en');
console.log(tb.add({years: 1, months: 12}) === 'in 2 years'); // true
console.log(tb.sub({years: 1, months: 12}) === '2 years ago'); // true

IMPORTANT

add/sub methods don't actually add or sub amount of time to initial date, instead they just make call to internal timeago().format method providing corresponding argument to it.

Take a look onto attached test file for the ru locale, hope you like it.

@hustcc why did you change your profile photo? It was cute 😜

@hustcc
Copy link
Owner

hustcc commented Sep 15, 2016

Happy mid autumn day!

Thanks for your nice work, I will process later.

@hustcc
Copy link
Owner

hustcc commented Sep 15, 2016

Very nice. So, I can move

var tb = require('../test-builder')(Date.now()).register('ru', require('../../locales/ru'), true);

to test/locale_test.js, and the tb instance as one parameter of locale test module

module.exports = function(t, tb) {
  // test seconds
  t.equal(tb.subSeconds(1), 'только что');
  t.equal(tb.subSeconds(10), '10 секунд назад');
  t.equal(tb.subSeconds(21), '21 секунду назад');
  // ...
};

@hustcc hustcc merged commit 2290a0c into hustcc:master Sep 15, 2016
hustcc pushed a commit that referenced this pull request Sep 15, 2016
@hustcc
Copy link
Owner

hustcc commented Sep 15, 2016

I have change en, zh_CN, en_short 's testcases with TestBuilder, but other locales unchanged.

@likerRr
Copy link
Collaborator Author

likerRr commented Sep 15, 2016

I like it

module.exports = function(t, tb) {

Will help you with other tests.

@hustcc
Copy link
Owner

hustcc commented Sep 15, 2016

^_^, What is your time? why you are not sleeping ?

@likerRr
Copy link
Collaborator Author

likerRr commented Sep 15, 2016

@hustcc it would be great to have 1-2 extra sleeping hours 😴 But I'm awake, it's 9am already. What about you?

@hustcc
Copy link
Owner

hustcc commented Sep 15, 2016

14:41…

@likerRr
Copy link
Collaborator Author

likerRr commented Sep 15, 2016

@hustcc wish you happy remaining of the day 😄 there is 5 hours time difference between us, will take a note

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants