tests.lua

Mon, 19 Mar 2012 22:26:07 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Mon, 19 Mar 2012 22:26:07 +0000
changeset 28
3ddc836b7845
parent 27
1422a41b53ac
permissions
-rw-r--r--

Add MIT/X11 licence

0
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local when = require "ndp".when;
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 if not when then
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 print("No 'when' function ?!");
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 end
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local all_ok = true;
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8
18
d6f0c12c152a tests: Add support for checking the consumed part of the input
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
9 local function check(input, output, e_start, e_finish)
d6f0c12c152a tests: Add support for checking the consumed part of the input
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
10 local ret, start, finish = when(input, 0);
0
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 if ret ~= output then
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 print("FAIL: "..input.." [produces "..ret.."/"..os.date("!%c", ret).."]");
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 all_ok = false;
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 return false;
18
d6f0c12c152a tests: Add support for checking the consumed part of the input
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
15 elseif (e_start and e_finish) and (start ~= e_start or finish ~= e_finish) then
d6f0c12c152a tests: Add support for checking the consumed part of the input
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
16 print("FAIL: "..input.." [uses "..start..","..finish.." instead of "..e_start..","..e_finish.."]");
d6f0c12c152a tests: Add support for checking the consumed part of the input
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
17 all_ok = false;
d6f0c12c152a tests: Add support for checking the consumed part of the input
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
18 return false;
0
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 end
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 end
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 check("today", 0);
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 check("tomorrow", 86400);
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 check("tomorrow morning", 117000);
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 check("tomorrow noon", 126000);
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 check("tomorrow afternoon", 131400);
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 check("tomorrow evening", 145800);
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 check("tomorrow midnight", 82800); -- Correct, but expected? :)
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29
27
1422a41b53ac Add 'the day after' to tests
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
30 check("the day after tomorrow", 172800, 0, 23);
1422a41b53ac Add 'the day after' to tests
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
31
0
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 check("in 1 hour", 3600);
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 check("in an hour", 3600);
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 check("in 2 hours", 7200);
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 check("in a couple of hours", 7200);
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 check("in 3 hours", 10800);
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 check("in a few hours", 10800);
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 check("in a day", 86400);
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 check("in 2 days", 172800);
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 check("in a couple of days", 172800);
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 check("in 3 days", 259200);
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 check("in a few days", 259200);
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 check("next week", 604800);
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 check("next year", 31536000);
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47
2
bb0892bc9687 Add tests for days of the week
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
48 check("monday", 345600);
bb0892bc9687 Add tests for days of the week
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
49 check("Monday", 345600);
bb0892bc9687 Add tests for days of the week
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
50 check("tuesday", 432000);
bb0892bc9687 Add tests for days of the week
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
51 check("wednesday", 518400);
bb0892bc9687 Add tests for days of the week
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
52 check("thursday", 604800);
bb0892bc9687 Add tests for days of the week
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
53 check("friday", 86400);
bb0892bc9687 Add tests for days of the week
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
54 check("the friday after tomorrow", 691200);
bb0892bc9687 Add tests for days of the week
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
55 check("the friday morning after tomorrow", 721800)
bb0892bc9687 Add tests for days of the week
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
56 check("the friday afternoon after tomorrow", 736200)
bb0892bc9687 Add tests for days of the week
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
57
8
8c6e74396e22 Add tests including years
Matthew Wild <mwild1@gmail.com>
parents: 2
diff changeset
58 check("2010", 1262307600);
8c6e74396e22 Add tests including years
Matthew Wild <mwild1@gmail.com>
parents: 2
diff changeset
59 check("Tuesday in July 2030", 1909184400);
8c6e74396e22 Add tests including years
Matthew Wild <mwild1@gmail.com>
parents: 2
diff changeset
60
20
1db3ee8b9bc3 Add test for 'feed the cat in 3 days'
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
61 check("feed the cat in 3 days", 259200, 13, 23);
1db3ee8b9bc3 Add test for 'feed the cat in 3 days'
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
62
0
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 if all_ok then
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 print("OK");
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 end

mercurial