PageRenderTime 67ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/src/main/resources/assets/app/scripts/specs/parsers/iso8601.js

https://github.com/craigbeck/chronos
JavaScript | 1065 lines | 685 code | 148 blank | 232 comment | 21 complexity | 15a789e709fb59fec9640ce01cdc1fbd MD5 | raw file
Possible License(s): Apache-2.0, MIT
  1. define([
  2. 'underscore',
  3. 'jquery',
  4. 'mocha',
  5. 'chai',
  6. 'parsers/iso8601',
  7. 'components/date_node',
  8. 'json!specs/test_data.json'
  9. ],
  10. function(_,
  11. $,
  12. _mocha,
  13. Chai,
  14. Iso8601Parser,
  15. DateNode,
  16. TestData) {
  17. var expect = Chai.expect,
  18. Assertion = Chai.Assertion,
  19. parse = Iso8601Parser.parse,
  20. slice = Array.prototype.slice,
  21. Generators;
  22. function join() {
  23. return slice.call(arguments).join('');
  24. }
  25. function WriteTestData(name, values) {
  26. var $el = $('#test-data'),
  27. content = {};
  28. content[name] = values;
  29. if ($el.val().length > 0) {
  30. _.merge(content, JSON.parse($el.val()));
  31. }
  32. $el.val(JSON.stringify(content));
  33. }
  34. Chai.use(function(chai, Utils) {
  35. function IsNode(v) {
  36. return (v instanceof DateNode.__super__.constructor);
  37. }
  38. function IsParserResult(v) {
  39. return !!_.result(v, 'original') && !!_.result(v, 'parsed');
  40. }
  41. Assertion.addMethod('parseableAs', function (parseRule) {
  42. var obj = this._obj,
  43. error,
  44. parseResult;
  45. new Assertion(this._obj).to.be.a('string');
  46. try {
  47. parseResult = parse(obj, parseRule);
  48. } catch (e) {
  49. error = _.pick(e,
  50. 'name', 'expected', 'found', 'message', 'offset', 'line', 'column');
  51. }
  52. this.assert(
  53. !error,
  54. "expected #{this} to be parseable as #{exp} but got #{act}",
  55. "expected #{this} to not be parseable, but got #{act}",
  56. parseRule,
  57. !!error ? JSON.stringify(error) : parseResult
  58. );
  59. this._obj = {
  60. original: obj, parsed: parseResult
  61. };
  62. return this;
  63. });
  64. Assertion.overwriteMethod('respondTo', function(_super) {
  65. return function(methodName, arg) {
  66. var obj = Utils.flag(this, 'object');
  67. if (IsNode(obj)) {
  68. var _method = obj[methodName],
  69. fnAssertion, wAssertion;
  70. new chai.Assertion(_method).to.be.a('function');
  71. if (_.isObject(arg) && _.has(arg, 'with')) {
  72. new chai.Assertion(_method.call(obj)).to.be.eql(arg['with']);
  73. }
  74. } else if (IsParserResult(obj)) {
  75. var ass = new chai.Assertion(obj.parsed);
  76. ass.to.respondTo.apply(ass, arguments);
  77. } else {
  78. _super.apply(this, arguments);
  79. }
  80. };
  81. });
  82. });
  83. function T(msg, obj) {
  84. return ([
  85. msg, '- testing', _.size(obj), 'objects'
  86. ]).join(' ');
  87. }
  88. function PaddedNumRange(start, end, width, prefix) {
  89. width || (width = (end + '').length);
  90. prefix || (prefix = '');
  91. var steps = _.chain(1).range(width).map(function(padding) {
  92. var zeros = (new Array(padding + 1)).join('0');
  93. return {
  94. i: parseInt(1 + zeros, 10),
  95. z: zeros
  96. };
  97. }).sortBy(function(v) { return v.i; }).value();
  98. function format(val) {
  99. var result, zeros;
  100. result = _.chain(steps).filter(function(v) {
  101. return v.i > val;
  102. }).max(function(v) {
  103. return v.i;
  104. }).value();
  105. result = (Math.abs(result) === Infinity) ? null : result;
  106. if (!!result) {
  107. var len = result.i.toString().length - val.toString().length;
  108. return [
  109. prefix,
  110. _.first(result.z, len).join(''),
  111. val
  112. ].join('');
  113. } else {
  114. return [prefix, val].join('');
  115. }
  116. //find(steps, function(v) {
  117. //return (v.i > val) && ([v.z, val].join('').length <= width);
  118. //});
  119. return prefix + (!!result ? (result.z + val) : ('' + val));
  120. }
  121. return _.chain(start).range(end + 1).map(function(i) {
  122. return {
  123. str: format(i), int: i
  124. };
  125. }).value();
  126. }
  127. function MakeDates(lists, separator) {
  128. separator || (separator = '');
  129. function WithRest(a) {
  130. if (a.length === 1) { return a[0]; }
  131. else if (a.length > 1) {
  132. var rest = WithRest(a.slice(1));
  133. return rest.reduce(function(memo, item) {
  134. return memo.concat(_.first(a).map(function(w) {
  135. return {str: [w.str, item.str].join(separator)};
  136. }));
  137. }, []);
  138. }
  139. }
  140. return WithRest(lists);
  141. }
  142. Generators = (function () {
  143. var methods = {};
  144. methods.calendarYears = _.memoize(function() {
  145. var Y1 = {str: '1984', int: 1984},
  146. Y2 = {str: '0010', int: 10},
  147. Y3 = {str: '+00501', int: 501},
  148. Y4 = {str: '-00501', int: -501},
  149. Y5 = {str: '-10000', int: -10000};
  150. return {
  151. standard: [Y1, Y2],
  152. extended: [Y3, Y4, Y5],
  153. all: [Y1, Y2, Y3, Y4, Y5]
  154. };
  155. });
  156. methods.calendarMonths = _.memoize(function() {
  157. var months = PaddedNumRange(1, 12, 2);
  158. return {
  159. standard: months, extended: [], all: months
  160. };
  161. });
  162. methods.calendarDaysOfMonth = _.memoize(function() {
  163. var days = PaddedNumRange(1, 31);
  164. return {
  165. standard: days, extended: [], all: days
  166. };
  167. });
  168. methods.calendarWeeks = _.memoize(function() {
  169. var weeks = PaddedNumRange(1, 53, 2, 'W');
  170. return {
  171. standard: weeks, extended: [], all: weeks
  172. };
  173. });
  174. function makeResult(std, extended, all) {
  175. extended || (extended = []);
  176. all || (all = std);
  177. return {standard: std, extended: extended, all: all};
  178. }
  179. methods.calendarDaysOfWeek = _.memoize(function() {
  180. var days = PaddedNumRange(1, 7);
  181. return makeResult(days, [], days);
  182. });
  183. methods.calendarDaysOfYear = _.memoize(function() {
  184. var days = PaddedNumRange(1, 365, 3);
  185. return makeResult(days, [], days);
  186. });
  187. methods.time || (methods.time = {});
  188. methods.time.hours = _.memoize(function() {
  189. var hours = PaddedNumRange(0, 24);
  190. return makeResult(hours, [], hours);
  191. });
  192. methods.time.minutes = _.memoize(function() {
  193. var minutes = PaddedNumRange(0, 59);
  194. return makeResult(minutes, [], minutes);
  195. });
  196. methods.time.seconds = _.memoize(function() {
  197. var s = PaddedNumRange(0, 60);
  198. return makeResult(s, [], s);
  199. });
  200. methods.time.zones = _.memoize(function() {
  201. var tz = ['Z'];
  202. (['+', '-']).forEach(function(sign) {
  203. _.range(0, 10).forEach(function(i) {
  204. });
  205. });
  206. });
  207. return methods;
  208. }());
  209. describe('ISO-8601 parts', function() {
  210. var calendarYears, calendarMonths, calendarDaysOfMonth,
  211. calendarDaysOfWeek, calendarWeeks, calendarDaysOfYear;
  212. var timeHours, timeMinutes, timeSeconds, timeZones;
  213. before(function() {
  214. /**
  215. calendarYears = Generators.calendarYears();
  216. calendarMonths = Generators.calendarMonths();
  217. calendarDaysOfMonth = Generators.calendarDaysOfMonth();
  218. calendarDaysOfWeek = Generators.calendarDaysOfWeek();
  219. calendarWeeks = Generators.calendarWeeks();
  220. calendarDaysOfYear = Generators.calendarDaysOfYear();
  221. var obj = {
  222. parts: {
  223. years: calendarYears,
  224. months: calendarMonths,
  225. daysOfMonth: calendarDaysOfMonth,
  226. weeks: calendarWeeks,
  227. daysOfYear: calendarDaysOfYear
  228. }
  229. };
  230. WriteTestData('dates', obj);
  231. **/
  232. calendarYears = TestData.dates.parts.years;
  233. calendarMonths = TestData.dates.parts.months;
  234. calendarDaysOfMonth = TestData.dates.parts.daysOfMonth;
  235. calendarWeeks = TestData.dates.parts.weeks;
  236. calendarDaysOfYear = TestData.dates.parts.daysOfYear;
  237. });
  238. describe('Dates', function() {
  239. describe('Years part', function() {
  240. it('should properly parse standard YYYY', function() {
  241. var parseResult;
  242. calendarYears.standard.forEach(function(y, i) {
  243. expect(y.str).to.be.parseableAs('YYYY').
  244. and.to.respondTo('getValue', {'with': y.int});
  245. }, this);
  246. });
  247. it('should properly parse extended [+-]Y?YYYY', function() {
  248. var parseResult;
  249. calendarYears.extended.forEach(function(y, i) {
  250. expect(y.str).to.be.parseableAs('YYYY').
  251. and.to.respondTo('getValue', {'with': y.int});
  252. }, this);
  253. });
  254. });
  255. describe('Month of year part', function() {
  256. it('should properly parse MM', function() {
  257. var parseResult;
  258. calendarMonths.all.forEach(function(m, i) {
  259. var parseResult = expect(m.str).to.be.parseableAs('MM');
  260. });
  261. });
  262. });
  263. describe('Day of month part', function() {
  264. it('should properly parse DD', function() {
  265. var parseResult;
  266. calendarDaysOfMonth.all.forEach(function(d, i) {
  267. var parseResult = expect(d.str).to.be.parseableAs('DD');
  268. });
  269. });
  270. });
  271. describe('Calendar Dates', function() {
  272. var cDateFullDash, cDateFull, cDateDash, cDate;
  273. before(function() {
  274. /**
  275. cDateFullDash = MakeDates([
  276. calendarYears.all,
  277. calendarMonths.all,
  278. calendarDaysOfMonth.all
  279. ], '-');
  280. cDateFull = MakeDates([
  281. calendarYears.all,
  282. calendarMonths.all,
  283. calendarDaysOfMonth.all
  284. ]);
  285. cDateDash = MakeDates([
  286. calendarYears.all,
  287. calendarMonths.all
  288. ], '-');
  289. cDate = MakeDates([
  290. calendarYears.all,
  291. calendarMonths.all
  292. ]);
  293. var obj = {
  294. calendar: {
  295. dateFullDash: cDateFullDash,
  296. dateFull: cDateFull,
  297. dateDash: cDateDash,
  298. date: cDate
  299. }
  300. };
  301. WriteTestData('dates', obj);
  302. **/
  303. cDateFullDash = TestData.dates.calendar.dateFullDash;
  304. cDateFull = TestData.dates.calendar.dateFull;
  305. cDateDash = TestData.dates.calendar.dateDash;
  306. cDate = TestData.dates.calendar.date;
  307. });
  308. describe('with dashes', function() {
  309. it('should parse full dates', function() {
  310. cDateFullDash.forEach(function(date) {
  311. expect(date.str).to.be.parseableAs('Date');
  312. }, this);
  313. });
  314. it('should parse dates without [DD]', function() {
  315. cDateFull.forEach(function(date) {
  316. expect(date.str).to.be.parseableAs('Date');
  317. }, this);
  318. });
  319. });
  320. describe('without dashes', function() {
  321. it('should parse full dates', function() {
  322. cDateFull.forEach(function(date) {
  323. expect(date.str).to.be.parseableAs('Date');
  324. }, this);
  325. });
  326. it('should NOT parse dates without [DD]', function() {
  327. cDate.forEach(function(date) {
  328. expect(date.str).not.to.be.parseableAs('Date');
  329. }, this);
  330. });
  331. });
  332. });
  333. describe('Week Dates', function() {
  334. var wDateFullDash, wDateFull, wDateDash, wDate;
  335. before(function() {
  336. /**
  337. wDateFullDash = MakeDates([
  338. calendarYears.all,
  339. calendarWeeks.all,
  340. calendarDaysOfWeek.all
  341. ], '-');
  342. wDateFull = MakeDates([
  343. calendarYears.all,
  344. calendarWeeks.all,
  345. calendarDaysOfWeek.all
  346. ]);
  347. wDateDash = MakeDates([
  348. calendarYears.all,
  349. calendarWeeks.all
  350. ], '-');
  351. wDate = MakeDates([
  352. calendarYears.all,
  353. calendarWeeks.all
  354. ]);
  355. var obj = {
  356. week: {
  357. dateFullDash: wDateFullDash,
  358. dateFull: wDateFull,
  359. dateDash: wDateDash,
  360. date: wDate
  361. }
  362. };
  363. WriteTestData('dates', obj);
  364. **/
  365. wDateFullDash = TestData.dates.week.dateFullDash;
  366. wDateFull = TestData.dates.week.dateFull;
  367. wDateDash = TestData.dates.week.dateDash;
  368. wDate = TestData.dates.week.date;
  369. });
  370. describe('with dashes', function() {
  371. it('should parse full dates', function() {
  372. wDateFullDash.forEach(function(date) {
  373. expect(date.str).to.be.parseableAs('Date');
  374. }, this);
  375. });
  376. it('should parse dates without [D]', function() {
  377. wDateFull.forEach(function(date) {
  378. expect(date.str).to.be.parseableAs('Date');
  379. }, this);
  380. });
  381. });
  382. describe('without dashes', function() {
  383. it('should parse full dates', function() {
  384. wDateFull.forEach(function(date) {
  385. expect(date.str).to.be.parseableAs('Date');
  386. }, this);
  387. });
  388. it('should parse dates without [D]', function() {
  389. wDate.forEach(function(date) {
  390. expect(date.str).to.be.parseableAs('Date');
  391. }, this);
  392. });
  393. });
  394. });
  395. describe('Ordinal Dates', function() {
  396. var oDateDash, oDate;
  397. before(function() {
  398. /**
  399. oDateDash = MakeDates([
  400. calendarYears.all,
  401. calendarDaysOfYear.all
  402. ], '-');
  403. oDate = MakeDates([
  404. calendarYears.all,
  405. calendarDaysOfYear.all
  406. ]);
  407. var obj = {
  408. ordinal: {
  409. dateDash: oDateDash,
  410. date: oDate
  411. }
  412. };
  413. WriteTestData('dates', obj);
  414. **/
  415. oDateDash = TestData.dates.ordinal.dateDash;
  416. oDate = TestData.dates.ordinal.date;
  417. });
  418. describe('with dashes', function() {
  419. it('should parse full dates', function() {
  420. oDateDash.forEach(function(date) {
  421. expect(date.str).to.be.parseableAs('Date');
  422. }, this);
  423. });
  424. });
  425. describe('without dashes', function() {
  426. it('should parse full dates', function() {
  427. oDate.forEach(function(date) {
  428. expect(date.str).to.be.parseableAs('Date');
  429. }, this);
  430. });
  431. });
  432. });
  433. });
  434. before(function() {
  435. timeHours = Generators.time.hours();
  436. timeMinutes = Generators.time.minutes();
  437. timeSeconds = Generators.time.seconds();
  438. timeZones = Generators.time.zones();
  439. });
  440. describe('Times', function() {
  441. var hh_mm_ss, hh_mm, hhmmss, hhmm, hh,
  442. hh_mm_ss_tz, hh_mm_tz, hhmmss_tz, hhmm_tz, hh_tz;
  443. before(function() {
  444. /**
  445. hh_mm_ss = MakeDates([
  446. timeHours.all,
  447. timeMinutes.all,
  448. timeSeconds.all
  449. ], ':');
  450. hh_mm = MakeDates([
  451. timeHours.all,
  452. timeMinutes.all
  453. ], ':');
  454. hhmmss = MakeDates([
  455. timeHours.all,
  456. timeMinutes.all,
  457. timeSeconds.all
  458. ]);
  459. hhmm = MakeDates([
  460. timeHours.all,
  461. timeMinutes.all
  462. ]);
  463. hh = MakeDates([
  464. timeHours.all
  465. ]);
  466. var obj = {
  467. week: {
  468. hh_mm_ss: hh_mm_ss,
  469. hh_mm: hh_mm,
  470. hhmmss: hhmmss,
  471. hhmm: hhmm,
  472. hh: hh
  473. }
  474. };
  475. WriteTestData('times', obj);
  476. **/
  477. hh_mm_ss = TestData.times.week.hh_mm_ss;
  478. hh_mm = TestData.times.week.hh_mm;
  479. hhmmss = TestData.times.week.hhmmss;
  480. hhmm = TestData.times.week.hhmm;
  481. hh = TestData.times.week.hh;
  482. });
  483. describe('without timezones', function() {
  484. describe('with colons', function() {
  485. it(T('should parse hh:mm:ss', hh_mm_ss), function() {
  486. hh_mm_ss.forEach(function(time) {
  487. expect(time.str).to.be.parseableAs('Time');
  488. }, this);
  489. });
  490. it(T('should parse hh:mm', hh_mm), function() {
  491. hh_mm.forEach(function(time) {
  492. expect(time.str).to.be.parseableAs('Time');
  493. }, this);
  494. });
  495. });
  496. describe('without colons', function() {
  497. it(T('should parse hhmmss', hhmmss), function() {
  498. hhmmss.forEach(function(time) {
  499. expect(time.str).to.be.parseableAs('Time');
  500. }, this);
  501. });
  502. it(T('should parse hhmm', hhmm), function() {
  503. hhmm.forEach(function(time) {
  504. expect(time.str).to.be.parseableAs('Time');
  505. }, this);
  506. });
  507. it(T('should parse hh', hh), function() {
  508. hh.forEach(function(time) {
  509. expect(time.str).to.be.parseableAs('Time');
  510. }, this);
  511. });
  512. });
  513. });
  514. var datesWithTimezones;
  515. before(function() {
  516. /**
  517. var timezones = ['Z'];
  518. var tzFormats = {'-': [], '+': []};
  519. (['+', '-']).forEach(function(sign) {
  520. ([hh_mm, hhmm, hh]).forEach(function(list, j) {
  521. tzFormats[sign][j] = [];
  522. list.forEach(function(item, k) {
  523. tzFormats[sign][j].push(_.extend({}, item, {
  524. str: [sign, item.str].join('')
  525. }));
  526. });
  527. });
  528. });
  529. timezones = timezones.concat.apply(timezones,
  530. _.chain(tzFormats).values().flatten());
  531. function mergeTimezone(timeVal, tz) {
  532. return _.extend({}, timeVal, {
  533. str: [timeVal.str, tz.str].join(''),
  534. tz: tz.str
  535. });
  536. }
  537. function makeTzTime(times, tzs) {
  538. return times.reduce(function(memo, item) {
  539. tzs.forEach(function(tz) {
  540. memo.push(mergeTimezone(item, tz));
  541. });
  542. return memo;
  543. }, []);
  544. }
  545. hh_mm_ss_tz = makeTzTime(hh_mm_ss, timezones);
  546. hh_mm_tz = makeTzTime(hh_mm, timezones);
  547. hhmmss_tz = makeTzTime(hhmmss, timezones);
  548. hhmm_tz = makeTzTime(hhmm, timezones);
  549. hh_tz = makeTzTime(hh, timezones);
  550. var obj = {
  551. week: {
  552. hh_mm_ss_tz: hh_mm_ss_tz,
  553. hh_mm_tz: hh_mm_tz,
  554. hhmmss_tz: hhmmss_tz,
  555. hhmm_tz: hhmm_tz,
  556. hh_tz: hh_tz
  557. }
  558. };
  559. WriteTestData('time_zones', obj);
  560. **/
  561. hh_mm_ss_tz = TestData.time_zones.week.hh_mm_ss_tz;
  562. hh_mm_tz = TestData.time_zones.week.hh_mm_tz;
  563. hhmmss_tz = TestData.time_zones.week.hhmmss_tz;
  564. hhmm_tz = TestData.time_zones.week.hhmm_tz;
  565. hh_tz = TestData.time_zones.week.hh_tz;
  566. });
  567. describe('with timezones', function() {
  568. describe('just timzones', function() {
  569. it('should parse Z', function() {
  570. expect('Z').to.be.parseableAs('TimeZone');
  571. });
  572. it(T('should parse +hh:mm', hh_mm), function() {
  573. hh_mm.forEach(function(time) {
  574. expect(join('+', time.str)).to.be.parseableAs('TimeZone');
  575. }, this);
  576. });
  577. it(T('should parse -hh:mm', hh_mm), function() {
  578. hh_mm.forEach(function(time) {
  579. expect(join('-', time.str)).to.be.parseableAs('TimeZone');
  580. }, this);
  581. });
  582. it(T('should parse +hhmm', hh_mm), function() {
  583. hhmm.forEach(function(time) {
  584. expect(join('+', time.str)).to.be.parseableAs('TimeZone');
  585. }, this);
  586. });
  587. it(T('should parse -hhmm', hh_mm), function() {
  588. hhmm.forEach(function(time) {
  589. expect(join('-', time.str)).to.be.parseableAs('TimeZone');
  590. }, this);
  591. });
  592. it(T('should parse +hh', hh), function() {
  593. hh.forEach(function(time) {
  594. expect(join('+', time.str)).to.be.parseableAs('TimeZone');
  595. });
  596. });
  597. it(T('should parse -hh', hh), function() {
  598. hh.forEach(function(time) {
  599. expect(join('-', time.str)).to.be.parseableAs('TimeZone');
  600. });
  601. });
  602. });
  603. describe('combined timezones and times', function() {
  604. describe('with colons', function() {
  605. it(T('should parse hh:mm:ss + tz', hh_mm_ss_tz), function() {
  606. hh_mm_ss_tz.forEach(function(time) {
  607. expect(time.str).to.be.parseableAs('Time');
  608. }, this);
  609. });
  610. it(T('should parse hh:mm + tz', hh_mm_tz), function() {
  611. hh_mm_tz.forEach(function(time) {
  612. expect(time.str).to.be.parseableAs('Time');
  613. }, this);
  614. });
  615. });
  616. describe('without colons', function() {
  617. it(T('should parse hhmmss + tz', hhmmss_tz), function() {
  618. hhmmss_tz.forEach(function(time) {
  619. expect(time.str).to.be.parseableAs('Time');
  620. }, this);
  621. });
  622. it(T('should parse hhmm + tz', hhmm_tz), function() {
  623. hhmm_tz.forEach(function(time) {
  624. expect(time.str).to.be.parseableAs('Time');
  625. }, this);
  626. });
  627. it(T('should parse hh + tz', hh_tz), function() {
  628. hh_tz.forEach(function(time) {
  629. expect(time.str).to.be.parseableAs('Time');
  630. }, this);
  631. });
  632. });
  633. });
  634. });
  635. });
  636. describe('Date Times', function() {
  637. var oDates, calendarDates, weekDates, times, tzTimes;
  638. before(function() {
  639. //dashed: TestData.dates.ordinal.dateDash,
  640. //normal: TestData.dates.ordinal.date
  641. oDates = TestData.dates.ordinal;
  642. //cDateFullDash = TestData.dates.calendar.dateFullDash;
  643. //cDateFull = TestData.dates.calendar.dateFull;
  644. //cDateDash = TestData.dates.calendar.dateDash;
  645. //cDate = TestData.dates.calendar.date;
  646. calendarDates = TestData.dates.calendar;
  647. //wDateFullDash = TestData.dates.week.dateFullDash;
  648. //wDateFull = TestData.dates.week.dateFull;
  649. //wDateDash = TestData.dates.week.dateDash;
  650. //wDate = TestData.dates.week.date;
  651. weekDates = TestData.dates.week;
  652. //hh_mm_ss = TestData.times.week.hh_mm_ss;
  653. //hh_mm = TestData.times.week.hh_mm;
  654. //hhmmss = TestData.times.week.hhmmss;
  655. //hhmm = TestData.times.week.hhmm;
  656. //hh = TestData.times.week.hh;
  657. times = TestData.times.week;
  658. //hh_mm_ss_tz = TestData.time_zones.week.hh_mm_ss_tz;
  659. //hh_mm_tz = TestData.time_zones.week.hh_mm_tz;
  660. //hhmmss_tz = TestData.time_zones.week.hhmmss_tz;
  661. //hhmm_tz = TestData.time_zones.week.hhmm_tz;
  662. //hh_tz = TestData.time_zones.week.hh_tz;
  663. tzTimes = TestData.time_zones.week;
  664. });
  665. describe('with ordinal dates', function() {
  666. var dates = oDates;
  667. describe('with timezones', function() {
  668. it('should parse', function() {
  669. _.each(dates, function(dateList, k) {
  670. dateList.forEach(function(date) {
  671. _.each(tzTimes, function(tzList, tzKey) {
  672. tzList.forEach(function(tzTime) {
  673. expect(join(date, 'T', tzTime)).to.be.parseableAs('DateTime');
  674. }, this);
  675. }, this);
  676. }, this);
  677. }, this);
  678. });
  679. });
  680. describe('without timezones', function() {
  681. it('should parse', function() {
  682. _.each(dates, function(dateList, k) {
  683. dateList.forEach(function(date) {
  684. _.each(times, function(tzList, tzKey) {
  685. tzList.forEach(function(tzTime) {
  686. expect(join(date, 'T', tzTime)).to.be.parseableAs('DateTime');
  687. }, this);
  688. }, this);
  689. }, this);
  690. }, this);
  691. });
  692. });
  693. });
  694. describe('with calendar dates', function() {
  695. var dates = calendarDates;
  696. describe('with timezones', function() {
  697. it('should parse', function() {
  698. _.each(dates, function(dateList, k) {
  699. dateList.forEach(function(date) {
  700. _.each(tzTimes, function(tzList, tzKey) {
  701. tzList.forEach(function(tzTime) {
  702. expect(join(date, 'T', tzTime)).to.be.parseableAs('DateTime');
  703. }, this);
  704. }, this);
  705. }, this);
  706. }, this);
  707. });
  708. });
  709. describe('without timezones', function() {
  710. it('should parse', function() {
  711. _.each(dates, function(dateList, k) {
  712. dateList.forEach(function(date) {
  713. _.each(times, function(tzList, tzKey) {
  714. tzList.forEach(function(tzTime) {
  715. expect(join(date, 'T', tzTime)).to.be.parseableAs('DateTime');
  716. }, this);
  717. }, this);
  718. }, this);
  719. }, this);
  720. });
  721. });
  722. });
  723. describe('with week dates', function() {
  724. var dates = weekDates;
  725. describe('with timezones', function() {
  726. it('should parse', function() {
  727. _.each(dates, function(dateList, k) {
  728. dateList.forEach(function(date) {
  729. _.each(tzTimes, function(tzList, tzKey) {
  730. tzList.forEach(function(tzTime) {
  731. expect(join(date, 'T', tzTime)).to.be.parseableAs('DateTime');
  732. }, this);
  733. }, this);
  734. }, this);
  735. }, this);
  736. });
  737. });
  738. describe('without timezones', function() {
  739. it('should parse', function() {
  740. _.each(dates, function(dateList, k) {
  741. dateList.forEach(function(date) {
  742. _.each(times, function(tzList, tzKey) {
  743. tzList.forEach(function(tzTime) {
  744. expect(join(date, 'T', tzTime)).to.be.parseableAs('DateTime');
  745. }, this);
  746. }, this);
  747. }, this);
  748. }, this);
  749. });
  750. });
  751. });
  752. });
  753. describe('Durations', function() {
  754. var oDates, calendarDates, weekDates, times, tzTimes;
  755. before(function() {
  756. //dashed: TestData.dates.ordinal.dateDash,
  757. //normal: TestData.dates.ordinal.date
  758. oDates = TestData.dates.ordinal;
  759. //cDateFullDash = TestData.dates.calendar.dateFullDash;
  760. //cDateFull = TestData.dates.calendar.dateFull;
  761. //cDateDash = TestData.dates.calendar.dateDash;
  762. //cDate = TestData.dates.calendar.date;
  763. calendarDates = TestData.dates.calendar;
  764. //wDateFullDash = TestData.dates.week.dateFullDash;
  765. //wDateFull = TestData.dates.week.dateFull;
  766. //wDateDash = TestData.dates.week.dateDash;
  767. //wDate = TestData.dates.week.date;
  768. weekDates = TestData.dates.week;
  769. //hh_mm_ss = TestData.times.week.hh_mm_ss;
  770. //hh_mm = TestData.times.week.hh_mm;
  771. //hhmmss = TestData.times.week.hhmmss;
  772. //hhmm = TestData.times.week.hhmm;
  773. //hh = TestData.times.week.hh;
  774. times = TestData.times.week;
  775. //hh_mm_ss_tz = TestData.time_zones.week.hh_mm_ss_tz;
  776. //hh_mm_tz = TestData.time_zones.week.hh_mm_tz;
  777. //hhmmss_tz = TestData.time_zones.week.hhmmss_tz;
  778. //hhmm_tz = TestData.time_zones.week.hhmm_tz;
  779. //hh_tz = TestData.time_zones.week.hh_tz;
  780. tzTimes = TestData.time_zones.week;
  781. });
  782. describe('as PnYnMnDTnHnMnS', function() {
  783. var firstHalves, secondHalves, together;
  784. before(function() {
  785. var n = 10;
  786. var fHalves, sHalves;
  787. fHalves = _.chain(n).range().map(function(i) {
  788. return (['Y', 'M', 'D']).reduce(function(memo, sig) {
  789. var v = _.random(0, n - 1);
  790. if (v > 0) { memo.push(join(v, sig)) }
  791. return memo;
  792. }, []);
  793. });
  794. sHalves = _.chain(n).range().map(function(i) {
  795. return join.apply(null, (['H', 'M', 'S']).reduce(function(memo, sig) {
  796. var v = _.random(0, i);
  797. if (v > 0) { memo.push(join(v, sig)) }
  798. return memo;
  799. }, []));
  800. });
  801. firstHalves = fHalves.map(function(h) {
  802. return join.apply(null, (['P']).concat(h));
  803. });
  804. secondHalves = sHalves.map(function(h) {
  805. return join.apply(null, (['P', 'T']).concat(h));
  806. });
  807. together = fHalves.reduce(function(memo, fh) {
  808. sHalves.forEach(function(sh) {
  809. memo.push(join.apply(null, (['P']).concat(fh).concat('T', sh)));
  810. });
  811. return memo;
  812. }, []);
  813. });
  814. describe('full specifier', function() {
  815. it('should parse', function() {
  816. together.forEach(function(d) {
  817. expect(d).to.be.parseableAs('Duration');
  818. });
  819. });
  820. it('should parse just first half', function() {
  821. firstHalves.forEach(function(d) {
  822. expect(d).to.be.parseableAs('Duration');
  823. });
  824. });
  825. it('should parse just second half', function() {
  826. secondHalves.forEach(function(d) {
  827. expect(d).to.be.parseableAs('Duration');
  828. });
  829. });
  830. });
  831. });
  832. describe('as PnW', function() {
  833. _.range(1, 53).forEach(function(i) {
  834. expect(join('P', i, 'W')).to.be.parseableAs('Duration');
  835. }, this);
  836. });
  837. describe('as P<date>T<time>', function() {
  838. var n = 20;
  839. describe('with week dates', function() {
  840. it('should parse', function() {
  841. _.chain(weekDates).first(n).forEach(function(date) {
  842. _.each(tzTimes, function(tzList, tzKey) {
  843. tzList.forEach(function(tzTime) {
  844. expect(join('P', date.str, 'T', tzTime.str)).to.be.parseableAs('Duration');
  845. }, this);
  846. }, this);
  847. }, this);
  848. });
  849. });
  850. describe('with ordinal dates', function() {
  851. it('should parse', function() {
  852. _.chain(oDates).first(n).forEach(function(date) {
  853. _.each(tzTimes, function(tzList, tzKey) {
  854. tzList.forEach(function(tzTime) {
  855. expect(join('P', date.str, 'T', tzTime.str)).to.be.parseableAs('Duration');
  856. }, this);
  857. }, this);
  858. }, this);
  859. });
  860. });
  861. describe('with calendar dates', function() {
  862. it('should parse', function() {
  863. _.chain(calendarDates).first(n).forEach(function(date) {
  864. _.each(tzTimes, function(tzList, tzKey) {
  865. tzList.forEach(function(tzTime) {
  866. expect(join('P', date.str, 'T', tzTime.str)).to.be.parseableAs('Duration');
  867. }, this);
  868. }, this);
  869. }, this);
  870. });
  871. });
  872. });
  873. });
  874. describe('Time intervals', function() {
  875. var formats = {
  876. '<start>/<end>': '2007-03-01T13:00:00Z/2008-05-11T15:30:00Z',
  877. '<start>/<duration>': '2007-03-01T13:00:00Z/P1Y2M10DT2H30M',
  878. '<duration>/<end>': 'P1Y2M10DT2H30M/2008-05-11T15:30:00Z'
  879. };
  880. _.each(formats, function(v, k) {
  881. it(join('should parse', k), function() {
  882. expect(v).to.be.parseableAs('Interval');
  883. });
  884. }, this);
  885. });
  886. describe('Repeating intervals', function() {
  887. var formats = {
  888. 'Rnn/<interval>': 'R5/2008-03-01T13:00:00Z/P1Y2M10DT2H30M',
  889. 'R/<interval>': 'R/P1Y2M10DT2H30M/2008-05-11T15:30:00Z'
  890. };
  891. _.each(formats, function(v, k) {
  892. it(join('should parse', k), function() {
  893. expect(v).to.be.parseableAs('RepeatingInterval');
  894. });
  895. }, this);
  896. });
  897. return true;
  898. });
  899. });