commit
message
[NOW] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-04-26 02:09:28
stats
6 file(s) changed,
79 insertions(+),
25 deletions(-)
files
README.md
src/RawFormulas/Date.ts
src/RawFormulas/RawFormulas.ts
tests/DateFormulasTest.ts
tests/DateFormulasTestTimeOverride.ts
tests/utils/Asserts.ts
1diff --git a/README.md b/README.md
2index 4164f58..70516dc 100644
3--- a/README.md
4+++ b/README.md
5@@ -48,6 +48,7 @@ Right now we're just using the number of days since 1900, but we should check th
6
7 ### Numbers with commas in them should still parse to numbers.
8
9+### ArgsChecker.checkLength should be typed
10
11 ## Testing Guidelines
12
13diff --git a/src/RawFormulas/Date.ts b/src/RawFormulas/Date.ts
14index a9cab76..52f836e 100644
15--- a/src/RawFormulas/Date.ts
16+++ b/src/RawFormulas/Date.ts
17@@ -24,9 +24,9 @@ import {
18 */
19 var DATE = function (...values) : ExcelDate {
20 const FIRST_YEAR = 1900;
21- ArgsChecker.checkLength(values, 3);
22 var year = Math.abs(Math.floor(TypeCaster.firstValueAsNumber(values[0]))); // No negative values for year
23 var month = Math.floor(TypeCaster.firstValueAsNumber(values[1])) - 1; // Months are between 0 and 11.
24+ ArgsChecker.checkLength(values, 3);
25 var day = Math.floor(TypeCaster.firstValueAsNumber(values[2])) - 1; // Days are also zero-indexed.
26 var m = moment.utc(ORIGIN_MOMENT)
27 .add(2, "days")
28@@ -767,8 +767,14 @@ var NETWORKDAYS$INTL = function (...values) : number {
29 return networkDays;
30 };
31
32-var NOW = function () {
33- ArgsChecker.checkLength(arguments.length, 0);
34+/**
35+ * Returns the current date and time as a date value.
36+ * @returns {ExcelDate} representing the current date and time.
37+ * @constructor
38+ */
39+var NOW = function (...values) : ExcelDate {
40+ ArgsChecker.checkLength(values, 0);
41+ return new ExcelDate(moment());
42 };
43
44
45@@ -797,5 +803,6 @@ export {
46 MINUTE,
47 SECOND,
48 NETWORKDAYS,
49- NETWORKDAYS$INTL
50+ NETWORKDAYS$INTL,
51+ NOW
52 }
53\ No newline at end of file
54diff --git a/src/RawFormulas/RawFormulas.ts b/src/RawFormulas/RawFormulas.ts
55index 71e151e..0da8087 100644
56--- a/src/RawFormulas/RawFormulas.ts
57+++ b/src/RawFormulas/RawFormulas.ts
58@@ -127,7 +127,8 @@ import {
59 MINUTE,
60 SECOND,
61 NETWORKDAYS,
62- NETWORKDAYS$INTL
63+ NETWORKDAYS$INTL,
64+ NOW
65 } from "./Date"
66
67 var ACCRINT = Formula["ACCRINT"];
68@@ -258,5 +259,6 @@ export {
69 MINUTE,
70 SECOND,
71 NETWORKDAYS,
72- NETWORKDAYS$INTL
73+ NETWORKDAYS$INTL,
74+ NOW
75 }
76\ No newline at end of file
77diff --git a/tests/DateFormulasTest.ts b/tests/DateFormulasTest.ts
78index bcc8ad7..29116ef 100644
79--- a/tests/DateFormulasTest.ts
80+++ b/tests/DateFormulasTest.ts
81@@ -21,25 +21,12 @@ import {
82 NETWORKDAYS$INTL
83 } from "../src/RawFormulas/RawFormulas"
84 import * as ERRORS from "../src/Errors"
85-import {assertEquals} from "./utils/Asserts"
86+import {
87+ assertEquals,
88+ catchAndAssertEquals
89+} from "./utils/Asserts"
90 import moment = require("moment");
91
92-function catchAndAssertEquals(toExecute, expected) {
93- var toThrow = null;
94- try {
95- toExecute();
96- toThrow = true;
97- } catch (actualError) {
98- if (actualError.name !== expected) {
99- console.log("expected:", expected, " actual:", actualError.name);
100- console.trace();
101- }
102- }
103- if (toThrow) {
104- console.log("expected error: " + expected);
105- console.trace();
106- }
107-}
108
109 // Test NETWORKDAYS$INTL
110 assertEquals(NETWORKDAYS$INTL("1992-1-1", "1992-1-30"), 22);
111diff --git a/tests/DateFormulasTestTimeOverride.ts b/tests/DateFormulasTestTimeOverride.ts
112new file mode 100644
113index 0000000..960f28a
114--- /dev/null
115+++ b/tests/DateFormulasTestTimeOverride.ts
116@@ -0,0 +1,35 @@
117+
118+import {
119+ NOW,
120+ DATEVALUE
121+} from "../src/RawFormulas/RawFormulas"
122+import * as ERRORS from "../src/Errors"
123+import {
124+ assertEquals,
125+ catchAndAssertEquals
126+} from "./utils/Asserts"
127+
128+
129+// WARNING: Locking in Date by overriding prototypes.
130+function lockDate(year, month, day, hour, minute, second) {
131+ // WARNING: Locking in Date by overriding prototypes.
132+ var d = new Date(year, month, day, hour, minute, second); // Always "Dec 10 2012 04:55:04"
133+ Date.prototype.constructor = function () {
134+ return d;
135+ };
136+ Date.now = function () {
137+ return +(d);
138+ };
139+}
140+
141+lockDate(2012, 11, 10, 4, 55, 4);
142+assertEquals(NOW().toNumber(), DATEVALUE("Dec 10 2012"));
143+lockDate(1999, 11, 10, 4, 55, 4);
144+assertEquals(NOW().toNumber(), DATEVALUE("Dec 10 1999"));
145+lockDate(1999, 9, 22, 4, 55, 4);
146+assertEquals(NOW().toNumber(), DATEVALUE("Oct 22 1999"));
147+lockDate(1944, 1, 2, 1, 11, 55);
148+assertEquals(NOW().toNumber(), DATEVALUE("Feb 2 1944"));
149+catchAndAssertEquals(function() {
150+ NOW(12);
151+}, ERRORS.NA_ERROR);
152\ No newline at end of file
153diff --git a/tests/utils/Asserts.ts b/tests/utils/Asserts.ts
154index 49560d5..2851607 100644
155--- a/tests/utils/Asserts.ts
156+++ b/tests/utils/Asserts.ts
157@@ -38,7 +38,30 @@ function assertArrayEquals(actual: Array<any>, expected: Array<any>, ) {
158 }
159 }
160
161+/**
162+ * Catch exceptions, check their name for an expected value
163+ * @param toExecute function to execute
164+ * @param expected error message
165+ */
166+function catchAndAssertEquals(toExecute : Function, expected) {
167+ var toThrow = null;
168+ try {
169+ toExecute();
170+ toThrow = true;
171+ } catch (actualError) {
172+ if (actualError.name !== expected) {
173+ console.log("expected:", expected, " actual:", actualError.name);
174+ console.trace();
175+ }
176+ }
177+ if (toThrow) {
178+ console.log("expected error: " + expected);
179+ console.trace();
180+ }
181+}
182+
183 export {
184 assertEquals,
185- assertArrayEquals
186+ assertArrayEquals,
187+ catchAndAssertEquals
188 }
189\ No newline at end of file