1 |
|
2 | e = require 'expect.js'
|
3 |
|
4 | describe 'Timeout calculate', ->
|
5 | timeout = require '../lib/timeout'
|
6 | args = now = null
|
7 | beforeEach ()->
|
8 | now = new Date
|
9 | args = [
|
10 | now.getFullYear(), now.getMonth(), now.getDate(),
|
11 | now.getHours(),
|
12 | now.getMinutes(), now.getSeconds()
|
13 | ]
|
14 | test = (pattern, argsNum, type) ->
|
15 | [ms, tp] = timeout pattern
|
16 | ++args[argsNum - 1]
|
17 | args = args[0...argsNum]
|
18 | args.push 0 for i in [args.length...3]
|
19 | args[3] = (args[3] ? 0) + (if argsNum < 3 then 24 else 0) + now.getTimezoneOffset() / 60
|
20 | next = new Date Date.UTC.apply {}, args
|
21 | myMs = next.valueOf() - now.valueOf()
|
22 | e(tp).to.be type
|
23 | e(myMs).to.be.below ms + 100
|
24 | e(myMs).to.be.above ms - 100
|
25 |
|
26 |
|
27 | it 'use year pattern', ()->
|
28 | test '[some YYMMddhhmmss]YY[some others]', 1, 'year'
|
29 | it 'use month pattern', ()->
|
30 | test '[some YYMMddhhmmss]MM[some others]', 2, 'month'
|
31 | it 'use day pattern', ()->
|
32 | test '[some YYMMddhhmmss]DD[some others]', 3, 'day'
|
33 | it 'use hour pattern', ()->
|
34 | test '[some YYMMddhhmmss]HH[some others]', 4, 'hour'
|
35 | it 'use minute pattern', ()->
|
36 | test '[some YYMMddhhmmss]mm[some others]', 5, 'minute'
|
37 | it 'use second pattern', ()->
|
38 | test '[some YYMMddhhmmss]ss[some others]', 6, 'second'
|
39 | it 'use mixed pattern', ()->
|
40 | test '[some YYMMddhhmmss]YYMMddhhmmss[some others]', 6, 'second'
|
41 | it 'use non-time pattern', ()->
|
42 | e(timeout '[some YYMMddhhmmss]b[some others]').to.eql [null, undefined]
|