forked from d3/d3-format
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformatSpecifier-test.js
77 lines (68 loc) · 2.59 KB
/
formatSpecifier-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
var tape = require("tape"),
format = require("../");
tape("formatSpecifier(specifier) throws an error for invalid formats", function(test) {
test.throws(function() { format.formatSpecifier("foo"); }, /invalid format: foo/);
test.throws(function() { format.formatSpecifier(".-2s"); }, /invalid format: \.-2s/);
test.throws(function() { format.formatSpecifier(".f"); }, /invalid format: \.f/);
test.end();
});
tape("formatSpecifier(specifier) returns an instanceof formatSpecifier", function(test) {
var s = format.formatSpecifier("");
test.equal(s instanceof format.formatSpecifier, true);
test.end();
});
tape("formatSpecifier(\"\") has the expected defaults", function(test) {
var s = format.formatSpecifier("");
test.equal(s.fill, " ");
test.equal(s.align, ">");
test.equal(s.sign, "-");
test.equal(s.symbol, "");
test.equal(s.zero, false);
test.equal(s.width, undefined);
test.equal(s.comma, false);
test.equal(s.precision, undefined);
test.equal(s.type, "");
test.end();
});
tape("formatSpecifier(specifier) uses the none type for unknown types", function(test) {
test.equal(format.formatSpecifier("q").type, "");
test.equal(format.formatSpecifier("S").type, "");
test.end();
});
tape("formatSpecifier(\"n\") is an alias for \",g\"", function(test) {
var s = format.formatSpecifier("n")
test.equal(s.comma, true);
test.equal(s.type, "g");
test.end();
});
tape("formatSpecifier(\"0\") is an alias for \"0=\"", function(test) {
var s = format.formatSpecifier("0")
test.equal(s.zero, true);
test.equal(s.fill, "0");
test.equal(s.align, "=");
test.end();
});
tape("formatSpecifier(specifier).toString() reflects current field values", function(test) {
var s = format.formatSpecifier("");
test.equal((s.fill = "_", s) + "", "_>-");
test.equal((s.align = "^", s) + "", "_^-");
test.equal((s.sign = "+", s) + "", "_^+");
test.equal((s.symbol = "$", s) + "", "_^+$");
test.equal((s.zero = true, s) + "", "_^+$0");
test.equal((s.width = 12, s) + "", "_^+$012");
test.equal((s.comma = true, s) + "", "_^+$012,");
test.equal((s.precision = 2, s) + "", "_^+$012,.2");
test.equal((s.type = "f", s) + "", "_^+$012,.2f");
test.equal(format.format(s)(42), "+$0,000,042.00");
test.end();
});
tape("formatSpecifier(specifier).toString() clamps precision to zero", function(test) {
var s = format.formatSpecifier("");
test.equal((s.precision = -1, s) + "", " >-.0");
test.end();
});
tape("formatSpecifier(specifier).toString() clamps width to one", function(test) {
var s = format.formatSpecifier("");
test.equal((s.width = -1, s) + "", " >-1");
test.end();
});