Skip to content

Commit

Permalink
feat: port webidl tests to node test runner (#2554)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilteoood committed Dec 28, 2023
1 parent 2646bc7 commit d432961
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 110 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"test": "node scripts/generate-pem && npm run test:tap && npm run test:node-fetch && npm run test:fetch && npm run test:cookies && npm run test:wpt && npm run test:websocket && npm run test:jest && npm run test:typescript",
"test:cookies": "borp --coverage -p \"test/cookie/*.js\"",
"test:node-fetch": "mocha --exit test/node-fetch",
"test:fetch": "npm run build:node && tap --expose-gc test/fetch/*.js && tap test/webidl/*.js",
"test:fetch": "npm run build:node && tap --expose-gc test/fetch/*.js && borp --coverage -p \"test/webidl/*.js\"",
"test:jest": "jest",
"test:tap": "tap test/*.js test/diagnostics-channel/*.js",
"test:tdd": "tap test/*.js test/diagnostics-channel/*.js -w",
Expand Down
93 changes: 38 additions & 55 deletions test/webidl/converters.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,39 @@
'use strict'

const { test } = require('tap')
const { describe, test } = require('node:test')
const assert = require('node:assert')
const { webidl } = require('../../lib/fetch/webidl')

test('sequence', (t) => {
test('sequence', () => {
const converter = webidl.sequenceConverter(
webidl.converters.DOMString
)

t.same(converter([1, 2, 3]), ['1', '2', '3'])
assert.deepStrictEqual(converter([1, 2, 3]), ['1', '2', '3'])

t.throws(() => {
assert.throws(() => {
converter(3)
}, TypeError, 'disallows non-objects')

t.throws(() => {
assert.throws(() => {
converter(null)
}, TypeError)

t.throws(() => {
assert.throws(() => {
converter(undefined)
}, TypeError)

t.throws(() => {
assert.throws(() => {
converter({})
}, TypeError, 'no Symbol.iterator')

t.throws(() => {
assert.throws(() => {
converter({
[Symbol.iterator]: 42
})
}, TypeError, 'invalid Symbol.iterator')

t.throws(() => {
assert.throws(() => {
converter(webidl.converters.sequence({
[Symbol.iterator] () {
return {
Expand All @@ -41,28 +42,24 @@ test('sequence', (t) => {
}
}))
}, TypeError, 'invalid generator')

t.end()
})

test('webidl.dictionaryConverter', (t) => {
t.test('arguments', (t) => {
describe('webidl.dictionaryConverter', () => {
test('arguments', () => {
const converter = webidl.dictionaryConverter([])

t.throws(() => {
assert.throws(() => {
converter(true)
}, TypeError)

for (const value of [{}, undefined, null]) {
t.doesNotThrow(() => {
assert.doesNotThrow(() => {
converter(value)
})
}

t.end()
})

t.test('required key', (t) => {
test('required key', () => {
const converter = webidl.dictionaryConverter([
{
converter: () => true,
Expand All @@ -71,57 +68,51 @@ test('webidl.dictionaryConverter', (t) => {
}
])

t.throws(() => {
assert.throws(() => {
converter({ wrongKey: 'key' })
}, TypeError)

t.doesNotThrow(() => {
assert.doesNotThrow(() => {
converter({ Key: 'this key was required!' })
})

t.end()
})

t.end()
})

test('ArrayBuffer', (t) => {
t.throws(() => {
test('ArrayBuffer', () => {
assert.throws(() => {
webidl.converters.ArrayBuffer(true)
}, TypeError)

t.throws(() => {
assert.throws(() => {
webidl.converters.ArrayBuffer({})
}, TypeError)

t.throws(() => {
assert.throws(() => {
const sab = new SharedArrayBuffer(1024)
webidl.converters.ArrayBuffer(sab, { allowShared: false })
}, TypeError)

t.doesNotThrow(() => {
assert.doesNotThrow(() => {
const sab = new SharedArrayBuffer(1024)
webidl.converters.ArrayBuffer(sab)
})

t.doesNotThrow(() => {
assert.doesNotThrow(() => {
const ab = new ArrayBuffer(8)
webidl.converters.ArrayBuffer(ab)
})

t.end()
})

test('TypedArray', (t) => {
t.throws(() => {
test('TypedArray', () => {
assert.throws(() => {
webidl.converters.TypedArray(3)
}, TypeError)

t.throws(() => {
assert.throws(() => {
webidl.converters.TypedArray({})
}, TypeError)

t.throws(() => {
assert.throws(() => {
const uint8 = new Uint8Array([1, 2, 3])
Object.defineProperty(uint8, 'buffer', {
get () {
Expand All @@ -133,20 +124,18 @@ test('TypedArray', (t) => {
allowShared: false
})
}, TypeError)

t.end()
})

test('DataView', (t) => {
t.throws(() => {
test('DataView', () => {
assert.throws(() => {
webidl.converters.DataView(3)
}, TypeError)

t.throws(() => {
assert.throws(() => {
webidl.converters.DataView({})
}, TypeError)

t.throws(() => {
assert.throws(() => {
const buffer = new ArrayBuffer(16)
const view = new DataView(buffer, 0)

Expand All @@ -164,39 +153,33 @@ test('DataView', (t) => {
const buffer = new ArrayBuffer(16)
const view = new DataView(buffer, 0)

t.equal(webidl.converters.DataView(view), view)

t.end()
assert.equal(webidl.converters.DataView(view), view)
})

test('BufferSource', (t) => {
t.doesNotThrow(() => {
test('BufferSource', () => {
assert.doesNotThrow(() => {
const buffer = new ArrayBuffer(16)
const view = new DataView(buffer, 0)

webidl.converters.BufferSource(view)
})

t.throws(() => {
assert.throws(() => {
webidl.converters.BufferSource(3)
}, TypeError)

t.end()
})

test('ByteString', (t) => {
t.doesNotThrow(() => {
test('ByteString', () => {
assert.doesNotThrow(() => {
webidl.converters.ByteString('')
})

// https://github.com/nodejs/undici/issues/1590
t.throws(() => {
assert.throws(() => {
const char = String.fromCharCode(256)
webidl.converters.ByteString(`invalid${char}char`)
}, {
message: 'Cannot convert argument to a ByteString because the character at ' +
'index 7 has a value of 256 which is greater than 255.'
})

t.end()
})
31 changes: 12 additions & 19 deletions test/webidl/helpers.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
'use strict'

const { test } = require('tap')
const { describe, test } = require('node:test')
const assert = require('node:assert')
const { webidl } = require('../../lib/fetch/webidl')

test('webidl.interfaceConverter', (t) => {
test('webidl.interfaceConverter', () => {
class A {}
class B {}

const converter = webidl.interfaceConverter(A)

t.throws(() => {
assert.throws(() => {
converter(new B())
}, TypeError)

t.doesNotThrow(() => {
assert.doesNotThrow(() => {
converter(new A())
})

t.end()
})

test('webidl.dictionaryConverter', (t) => {
t.test('extraneous keys are provided', (t) => {
describe('webidl.dictionaryConverter', () => {
test('extraneous keys are provided', () => {
const converter = webidl.dictionaryConverter([
{
key: 'key',
Expand All @@ -31,7 +30,7 @@ test('webidl.dictionaryConverter', (t) => {
}
])

t.same(
assert.deepStrictEqual(
converter({
a: 'b',
key: 'string',
Expand All @@ -42,11 +41,9 @@ test('webidl.dictionaryConverter', (t) => {
}),
{ key: 'string' }
)

t.end()
})

t.test('defaultValue with key = null', (t) => {
test('defaultValue with key = null', () => {
const converter = webidl.dictionaryConverter([
{
key: 'key',
Expand All @@ -55,21 +52,17 @@ test('webidl.dictionaryConverter', (t) => {
}
])

t.same(converter({ key: null }), { key: 0 })
t.end()
assert.deepStrictEqual(converter({ key: null }), { key: 0 })
})

t.test('no defaultValue and optional', (t) => {
test('no defaultValue and optional', () => {
const converter = webidl.dictionaryConverter([
{
key: 'key',
converter: webidl.converters.ByteString
}
])

t.same(converter({ a: 'b', c: 'd' }), {})
t.end()
assert.deepStrictEqual(converter({ a: 'b', c: 'd' }), {})
})

t.end()
})

0 comments on commit d432961

Please sign in to comment.