Skip to content

Commit faaa207

Browse files
Use tape in tests and run them on every Node.js version
1 parent 396ba12 commit faaa207

File tree

5 files changed

+151
-90
lines changed

5 files changed

+151
-90
lines changed

.travis.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
version: ~> 1.0
12
language: node_js
2-
node_js:
3-
- "0.10"
4-
script:
5-
"node tests/tests.js"
3+
os:
4+
- linux
5+
import:
6+
- ljharb/travis-ci:node/all.yml
7+
- ljharb/travis-ci:node/pretest.yml

package.json

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "A robust & optimized `String.prototype.codePointAt` polyfill, based on the ECMAScript 6 specification.",
55
"homepage": "https://mths.be/codepointat",
66
"main": "index.js",
7-
"exports": {
7+
"exports": {
88
".": "./index.js",
99
"./auto": "./auto.js",
1010
"./shim": "./shim.js",
@@ -32,15 +32,18 @@
3232
"scripts": {
3333
"pretest": "es-shim-api --bound",
3434
"test": "npm run tests-only",
35-
"tests-only": "node tests/tests.js",
36-
"cover": "istanbul cover --report html --verbose --dir coverage tests/tests.js"
35+
"tests-only": "tape 'tests/*.js'",
36+
"cover": "istanbul cover --report html --verbose --dir coverage tape 'tests/*.js'"
3737
},
3838
"dependencies": {
39-
"define-properties": "^1.1.3",
4039
"es-abstract": "^1.17.5"
4140
},
4241
"devDependencies": {
4342
"@es-shims/api": "^2.1.2",
44-
"istanbul": "^0.4.5"
43+
"define-properties": "^1.1.3",
44+
"function-bind": "^1.1.1",
45+
"functions-have-names": "^1.2.1",
46+
"istanbul": "^0.4.5",
47+
"tape": "^5.0.0"
4548
}
4649
}

tests/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
var codePointAt = require('../');
4+
var test = require('tape');
5+
6+
var runTests = require('./tests');
7+
8+
test('as a function', function (t) {
9+
runTests(codePointAt, t);
10+
11+
t.end();
12+
});

tests/shimmed.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
3+
var codePointAt = require('../');
4+
codePointAt.shim();
5+
6+
var test = require('tape');
7+
var defineProperties = require('define-properties');
8+
var bind = require('function-bind');
9+
var isEnumerable = Object.prototype.propertyIsEnumerable;
10+
var functionsHaveNames = require('functions-have-names')();
11+
12+
var runTests = require('./tests');
13+
14+
test('shimmed', function (t) {
15+
t.equal(String.prototype.codePointAt.length, 1, 'String#codePointAt has a length of 1');
16+
17+
t.test('Function name', { skip: !functionsHaveNames }, function (st) {
18+
st.equal(String.prototype.codePointAt.name, 'codePointAt', 'String#codePointAt has name "codePointAt"');
19+
st.end();
20+
});
21+
22+
t.test('enumerability', { skip: !defineProperties.supportsDescriptors }, function (et) {
23+
et.equal(false, isEnumerable.call(String.prototype, 'codePointAt'), 'String#codePointAt is not enumerable');
24+
et.end();
25+
});
26+
27+
runTests(bind.call(Function.call, String.prototype.codePointAt), t);
28+
29+
t.end();
30+
});

tests/tests.js

Lines changed: 95 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,104 @@
1-
var assert = require('assert');
2-
var assertEquals = assert.equal;
3-
var assertThrows = assert['throws'];
1+
'use strict';
42

5-
var codePointAt = require('../implementation');
3+
module.exports = function (codePointAt, t) {
4+
t.test('String that starts with a BMP symbol', function (st) {
5+
st.equal(codePointAt('abc\uD834\uDF06def', -1), undefined);
6+
st.equal(codePointAt('abc\uD834\uDF06def', -0), 0x61);
7+
st.equal(codePointAt('abc\uD834\uDF06def', 0), 0x61);
8+
st.equal(codePointAt('abc\uD834\uDF06def', 3), 0x1D306);
9+
st.equal(codePointAt('abc\uD834\uDF06def', 4), 0xDF06);
10+
st.equal(codePointAt('abc\uD834\uDF06def', 5), 0x64);
11+
st.equal(codePointAt('abc\uD834\uDF06def', 42), undefined);
12+
st.end();
13+
});
614

7-
assertEquals(codePointAt.length, 1);
15+
t.test('String that starts with a BMP symbol - cast position', function (st) {
16+
st.equal(codePointAt('abc\uD834\uDF06def', ''), 0x61);
17+
st.equal(codePointAt('abc\uD834\uDF06def', '_'), 0x61);
18+
st.equal(codePointAt('abc\uD834\uDF06def'), 0x61);
19+
st.equal(codePointAt('abc\uD834\uDF06def', -Infinity), undefined);
20+
st.equal(codePointAt('abc\uD834\uDF06def', Infinity), undefined);
21+
st.equal(codePointAt('abc\uD834\uDF06def', Infinity), undefined);
22+
st.equal(codePointAt('abc\uD834\uDF06def', NaN), 0x61);
23+
st.equal(codePointAt('abc\uD834\uDF06def', false), 0x61);
24+
st.equal(codePointAt('abc\uD834\uDF06def', null), 0x61);
25+
st.equal(codePointAt('abc\uD834\uDF06def', undefined), 0x61);
26+
st.end();
27+
});
828

9-
// String that starts with a BMP symbol
10-
assertEquals(codePointAt.call('abc\uD834\uDF06def', ''), 0x61);
11-
assertEquals(codePointAt.call('abc\uD834\uDF06def', '_'), 0x61);
12-
assertEquals(codePointAt.call('abc\uD834\uDF06def', ), 0x61);
13-
assertEquals(codePointAt.call('abc\uD834\uDF06def', -Infinity), undefined);
14-
assertEquals(codePointAt.call('abc\uD834\uDF06def', -1), undefined);
15-
assertEquals(codePointAt.call('abc\uD834\uDF06def', -0), 0x61);
16-
assertEquals(codePointAt.call('abc\uD834\uDF06def', 0), 0x61);
17-
assertEquals(codePointAt.call('abc\uD834\uDF06def', 3), 0x1D306);
18-
assertEquals(codePointAt.call('abc\uD834\uDF06def', 4), 0xDF06);
19-
assertEquals(codePointAt.call('abc\uD834\uDF06def', 5), 0x64);
20-
assertEquals(codePointAt.call('abc\uD834\uDF06def', 42), undefined);
21-
assertEquals(codePointAt.call('abc\uD834\uDF06def', Infinity), undefined);
22-
assertEquals(codePointAt.call('abc\uD834\uDF06def', Infinity), undefined);
23-
assertEquals(codePointAt.call('abc\uD834\uDF06def', NaN), 0x61);
24-
assertEquals(codePointAt.call('abc\uD834\uDF06def', false), 0x61);
25-
assertEquals(codePointAt.call('abc\uD834\uDF06def', null), 0x61);
26-
assertEquals(codePointAt.call('abc\uD834\uDF06def', undefined), 0x61);
29+
t.test('String that starts with an astral symbol', function (st) {
30+
st.equal(codePointAt('\uD834\uDF06def', -1), undefined);
31+
st.equal(codePointAt('\uD834\uDF06def', -0), 0x1D306);
32+
st.equal(codePointAt('\uD834\uDF06def', 0), 0x1D306);
33+
st.equal(codePointAt('\uD834\uDF06def', 1), 0xDF06);
34+
st.equal(codePointAt('\uD834\uDF06def', 42), undefined);
35+
st.end();
36+
});
2737

28-
// String that starts with an astral symbol
29-
assertEquals(codePointAt.call('\uD834\uDF06def', ''), 0x1D306);
30-
assertEquals(codePointAt.call('\uD834\uDF06def', '1'), 0xDF06);
31-
assertEquals(codePointAt.call('\uD834\uDF06def', '_'), 0x1D306);
32-
assertEquals(codePointAt.call('\uD834\uDF06def', ), 0x1D306);
33-
assertEquals(codePointAt.call('\uD834\uDF06def', -1), undefined);
34-
assertEquals(codePointAt.call('\uD834\uDF06def', -0), 0x1D306);
35-
assertEquals(codePointAt.call('\uD834\uDF06def', 0), 0x1D306);
36-
assertEquals(codePointAt.call('\uD834\uDF06def', 1), 0xDF06);
37-
assertEquals(codePointAt.call('\uD834\uDF06def', 42), undefined);
38-
assertEquals(codePointAt.call('\uD834\uDF06def', false), 0x1D306);
39-
assertEquals(codePointAt.call('\uD834\uDF06def', null), 0x1D306);
40-
assertEquals(codePointAt.call('\uD834\uDF06def', undefined), 0x1D306);
38+
t.test('String that starts with an astral symbol - cast position', function (st) {
39+
st.equal(codePointAt('\uD834\uDF06def', ''), 0x1D306);
40+
st.equal(codePointAt('\uD834\uDF06def', '1'), 0xDF06);
41+
st.equal(codePointAt('\uD834\uDF06def', '_'), 0x1D306);
42+
st.equal(codePointAt('\uD834\uDF06def'), 0x1D306);
43+
st.equal(codePointAt('\uD834\uDF06def', false), 0x1D306);
44+
st.equal(codePointAt('\uD834\uDF06def', null), 0x1D306);
45+
st.equal(codePointAt('\uD834\uDF06def', undefined), 0x1D306);
46+
st.end();
47+
});
4148

42-
// Lone high surrogates
43-
assertEquals(codePointAt.call('\uD834abc', ''), 0xD834);
44-
assertEquals(codePointAt.call('\uD834abc', '_'), 0xD834);
45-
assertEquals(codePointAt.call('\uD834abc', ), 0xD834);
46-
assertEquals(codePointAt.call('\uD834abc', -1), undefined);
47-
assertEquals(codePointAt.call('\uD834abc', -0), 0xD834);
48-
assertEquals(codePointAt.call('\uD834abc', 0), 0xD834);
49-
assertEquals(codePointAt.call('\uD834abc', false), 0xD834);
50-
assertEquals(codePointAt.call('\uD834abc', NaN), 0xD834);
51-
assertEquals(codePointAt.call('\uD834abc', null), 0xD834);
52-
assertEquals(codePointAt.call('\uD834abc', undefined), 0xD834);
49+
t.test('Lone high surrogates', function (st) {
50+
st.equal(codePointAt('\uD834abc', -1), undefined);
51+
st.equal(codePointAt('\uD834abc', -0), 0xD834);
52+
st.equal(codePointAt('\uD834abc', 0), 0xD834);
53+
st.end();
54+
});
5355

54-
// Lone low surrogates
55-
assertEquals(codePointAt.call('\uDF06abc', ''), 0xDF06);
56-
assertEquals(codePointAt.call('\uDF06abc', '_'), 0xDF06);
57-
assertEquals(codePointAt.call('\uDF06abc', ), 0xDF06);
58-
assertEquals(codePointAt.call('\uDF06abc', -1), undefined);
59-
assertEquals(codePointAt.call('\uDF06abc', -0), 0xDF06);
60-
assertEquals(codePointAt.call('\uDF06abc', 0), 0xDF06);
61-
assertEquals(codePointAt.call('\uDF06abc', false), 0xDF06);
62-
assertEquals(codePointAt.call('\uDF06abc', NaN), 0xDF06);
63-
assertEquals(codePointAt.call('\uDF06abc', null), 0xDF06);
64-
assertEquals(codePointAt.call('\uDF06abc', undefined), 0xDF06);
56+
t.test('Lone high surrogates - cast position', function (st) {
57+
st.equal(codePointAt('\uD834abc', ''), 0xD834);
58+
st.equal(codePointAt('\uD834abc', '_'), 0xD834);
59+
st.equal(codePointAt('\uD834abc'), 0xD834);
60+
st.equal(codePointAt('\uD834abc', false), 0xD834);
61+
st.equal(codePointAt('\uD834abc', NaN), 0xD834);
62+
st.equal(codePointAt('\uD834abc', null), 0xD834);
63+
st.equal(codePointAt('\uD834abc', undefined), 0xD834);
64+
st.end();
65+
});
6566

66-
assertThrows(function() { codePointAt.call(undefined); }, TypeError);
67-
assertThrows(function() { codePointAt.call(undefined, 4); }, TypeError);
68-
assertThrows(function() { codePointAt.call(null); }, TypeError);
69-
assertThrows(function() { codePointAt.call(null, 4); }, TypeError);
70-
assertEquals(codePointAt.call(42, 0), 0x34);
71-
assertEquals(codePointAt.call(42, 1), 0x32);
72-
assertEquals(codePointAt.call({ 'toString': function() { return 'abc'; } }, 2), 0x63);
73-
var tmp = 0;
74-
assertEquals(codePointAt.call({ 'toString': function() { ++tmp; return String(tmp); } }, 0), 0x31);
75-
assertEquals(tmp, 1);
67+
t.test('Lone low surrogates', function (st) {
68+
st.equal(codePointAt('\uDF06abc', -1), undefined);
69+
st.equal(codePointAt('\uDF06abc', -0), 0xDF06);
70+
st.equal(codePointAt('\uDF06abc', 0), 0xDF06);
71+
st.end();
72+
});
7673

77-
assertThrows(function() { codePointAt.apply(undefined); }, TypeError);
78-
assertThrows(function() { codePointAt.apply(undefined, [4]); }, TypeError);
79-
assertThrows(function() { codePointAt.apply(null); }, TypeError);
80-
assertThrows(function() { codePointAt.apply(null, [4]); }, TypeError);
81-
assertEquals(codePointAt.apply(42, [0]), 0x34);
82-
assertEquals(codePointAt.apply(42, [1]), 0x32);
83-
assertEquals(codePointAt.apply({ 'toString': function() { return 'abc'; } }, [2]), 0x63);
84-
tmp = 0;
85-
assertEquals(codePointAt.apply({ 'toString': function() { ++tmp; return String(tmp); } }, [0]), 0x31);
86-
assertEquals(tmp, 1);
74+
t.test('Lone low surrogates - cast position', function (st) {
75+
st.equal(codePointAt('\uDF06abc', ''), 0xDF06);
76+
st.equal(codePointAt('\uDF06abc', '_'), 0xDF06);
77+
st.equal(codePointAt('\uDF06abc'), 0xDF06);
78+
st.equal(codePointAt('\uDF06abc', false), 0xDF06);
79+
st.equal(codePointAt('\uDF06abc', NaN), 0xDF06);
80+
st.equal(codePointAt('\uDF06abc', null), 0xDF06);
81+
st.equal(codePointAt('\uDF06abc', undefined), 0xDF06);
82+
st.end();
83+
});
8784

88-
// NOTE: This test will test our code only in engines without native support
89-
require('../auto');
90-
assertEquals(String.prototype.propertyIsEnumerable('codePointAt'), false);
85+
var supportsStrictMode = (function () { return typeof this === 'undefined'; }());
86+
87+
t.test('bad string/this value', { skip: !supportsStrictMode }, function (st) {
88+
st['throws'](function () { return codePointAt(undefined, 'a'); }, TypeError, 'undefined is not an object');
89+
st['throws'](function () { return codePointAt(null, 'a'); }, TypeError, 'null is not an object');
90+
st.end();
91+
});
92+
93+
t.test('cast this value', function (st) {
94+
st.equal(codePointAt(42, 0), 0x34);
95+
st.equal(codePointAt(42, 1), 0x32);
96+
st.equal(codePointAt({ 'toString': function() { return 'abc'; } }, 2), 0x63);
97+
98+
var tmp = 0;
99+
st.equal(codePointAt({ 'toString': function() { ++tmp; return String(tmp); } }, 0), 0x31);
100+
st.equal(tmp, 1);
101+
102+
st.end();
103+
});
104+
};

0 commit comments

Comments
 (0)