Skip to content

Commit 22ed208

Browse files
committed
Improve tests: unit and functional (i.e., test the compiled thing)
1 parent 81f6f49 commit 22ed208

File tree

6 files changed

+83
-26
lines changed

6 files changed

+83
-26
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules
2+
test/functional/fixtures/basic-compiled-en-formatters.js

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313
"globalize-compiler": "./bin/globalize-compiler"
1414
},
1515
"scripts": {
16-
"test": "eslint --ignore-path .gitignore --ext .js index.js lib test bin && mocha"
16+
"test": "npm run lint && npm run test:unit && npm run test:functional",
17+
"lint": "eslint --ignore-path .gitignore --ext .js index.js lib test bin",
18+
"test:unit": "mocha test/unit",
19+
"test:build:functional": "mkdir test/functional/fixtures 2>/dev/null; ./bin/globalize-compiler -l en -m test/unit/fixtures/en-messages.json -o test/functional/fixtures/basic-compiled-en-formatters.js test/unit/fixtures/basic.js",
20+
"test:functional": "npm run test:build:functional && mocha test/functional"
1721
},
1822
"repository": {
1923
"type": "git",

test/functional/index.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
var Globalize = require("./fixtures/basic-compiled-en-formatters");
2+
var expect = require("chai").expect;
3+
Globalize.locale("en");
4+
5+
describe("The compiled `basic.js`", function() {
6+
it("should include formatDate", function() {
7+
var result = Globalize.formatDate(new Date(2017, 3, 15), {datetime: "medium"});
8+
// Note, the reason for the loose match below is due to ignore the local time zone differences.
9+
expect(result).to.have.string("Apr");
10+
expect(result).to.have.string("2017");
11+
});
12+
13+
it("should include formatDateToParts", function() {
14+
var result = Globalize.formatDateToParts(new Date(2017, 3, 15), {datetime: "medium"});
15+
expect(result).to.include({type: "month", value: "Apr"});
16+
expect(result).to.include({type: "year", value: "2017"});
17+
});
18+
19+
it("should include formatNumber", function() {
20+
var result = Globalize.formatNumber(Math.PI);
21+
expect(result).to.equal("3.142");
22+
});
23+
24+
it("should include formatCurrency", function() {
25+
var result = Globalize.formatCurrency(69900, "USD");
26+
expect(result).to.equal("$69,900.00");
27+
});
28+
29+
it("should include formatMessage", function() {
30+
var result = Globalize.formatMessage("like", 0);
31+
expect(result).to.equal("Be the first to like this");
32+
});
33+
34+
it("should include formatRelativeTime", function() {
35+
var result = Globalize.formatRelativeTime(1, "second");
36+
expect(result).to.equal("in 1 second");
37+
});
38+
39+
it("should include formatUnit", function() {
40+
var result = Globalize.formatUnit(60, "mile/hour", {form: "short"});
41+
expect(result).to.equal("60 mph");
42+
});
43+
44+
it("should include parseNumber", function() {
45+
var result = Globalize.parseNumber("1,234.56");
46+
expect(result).to.equal(1234.56);
47+
});
48+
49+
it("should include parseDate", function() {
50+
var result = Globalize.parseDate("1/2/1982");
51+
expect(result.getFullYear()).to.equal(1982);
52+
expect(result.getMonth()).to.equal(0);
53+
expect(result.getDate()).to.equal(2);
54+
});
55+
});
Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,12 @@
11
var like;
22
var Globalize = require( "globalize" );
33

4-
// Before we can use Globalize, we need to feed it on the appropriate I18n content (Unicode CLDR). Read Requirements on Getting Started on the root's README.md for more information.
5-
Globalize.load(
6-
require( "cldr-data/main/en/ca-gregorian" ),
7-
require( "cldr-data/main/en/currencies" ),
8-
require( "cldr-data/main/en/dateFields" ),
9-
require( "cldr-data/main/en/numbers" ),
10-
require( "cldr-data/supplemental/currencyData" ),
11-
require( "cldr-data/supplemental/likelySubtags" ),
12-
require( "cldr-data/supplemental/plurals" ),
13-
require( "cldr-data/supplemental/timeData" ),
14-
require( "cldr-data/supplemental/weekData" )
15-
);
16-
Globalize.loadMessages( require( "./messages/en" ) );
17-
18-
// Set "en" as our default locale.
19-
Globalize.locale( "en" );
20-
214
// Use Globalize to format dates.
225
console.log( Globalize.formatDate( new Date(), { datetime: "medium" } ) );
236

7+
// Use Globalize to format dates to parts.
8+
console.log( Globalize.formatDateToParts( new Date(), { datetime: "medium" } ) );
9+
2410
// Use Globalize to format numbers.
2511
console.log( Globalize.formatNumber( 12345.6789 ) );
2612

@@ -40,5 +26,11 @@ console.log( like( 3 ) );
4026
// Use Globalize to format relative time.
4127
console.log( Globalize.formatRelativeTime( -35, "second" ) );
4228

29+
// Use Globalize to format unit.
30+
console.log( Globalize.formatUnit( 60, "mile/hour", { form: "short" } ) );
31+
4332
// Use Globalize to parse a number.
4433
console.log( Globalize.parseNumber( "12345.6789" ) );
34+
35+
// Use Globalize to parse a date.
36+
console.log( Globalize.parseDate( "1/2/1982" ) );

test/unit/fixtures/en-messages.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"en": {
3+
"like": [
4+
"{0, plural, offset:1",
5+
" =0 {Be the first to like this}",
6+
" =1 {You liked this}",
7+
" one {You and someone else liked this}",
8+
" other {You and # others liked this}",
9+
"}"
10+
]
11+
}
12+
}

test/index.js renamed to test/unit/index.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var fs = require("fs");
22
var esprima = require("esprima");
33
var expect = require("chai").expect;
4-
var globalizeCompiler = require("../index");
4+
var globalizeCompiler = require("../../index");
55

66
var fixtures = {
77
//babel: esprima.parse(babel.transform(fs.readFileSync(__dirname + "/fixtures/es6.js")).code),
@@ -29,11 +29,4 @@ describe("Extract Formatters & Parsers", function() {
2929

3030
expect(compiledString).to.be.a("string");
3131
});
32-
33-
describe("compile.js", function() {
34-
it("should handle runtimeArgs regexps", function() {
35-
expect(compiledString).to.have.string("numberParserFn([{\".\":\".\",\",\":\",\",\"%\":\"%\",\"+\":\"+\",\"-\":\"-\",\"E\":\"E\",\"‰\":\"‰\"},,{\"infinity\":/^∞/,\"nan\":/^NaN/,\"negativePrefix\":/^-/,\"negativeSuffix\":/^/,\"number\":/^((\\d{1,3}(,\\d{3})+|\\d+))?(\\.\\d+)?/,\"prefix\":/^/,\"suffix\":/^/}]);");
36-
});
37-
});
38-
3932
});

0 commit comments

Comments
 (0)