用於位元組陣列相等性的 Chai 斷言
chai-bytes 使用 equalBytes
函式擴展了 Chai,該函式可用於測試位元組陣列(即 Uint8Array
實例)的相等性。
基本用法
const { expect, assert } = require('chai')
.use(require('chai-bytes'));
const buffer = new Uint8Array([ 1, 2, 3, 4, 5 ]);
expect(buffer).to.equalBytes('0102030405');
// `assert` style works, too
assert.equalBytes(buffer, [1, 2, 3, 4, 5], 'error message');
傳遞給 equalBytes
的預期值可以是
- 十六進位字串,例如
'c0ffee'
- 陣列,例如
[1, 2, 3]
- 類陣列物件(即具有
length
屬性且可通過整數屬性訪問項目的物件)。這包括Uint8Array
實例等等
如果傳遞的值不屬於這些類別中的任何一個,則會擲出 TypeError
。
開發者注意事項
chai-bytes 使用來自受測試瀏覽器環境(Firefox 和 PhantomJS)的組合程式碼覆蓋率。這是因為 PhantomJS 是一個典型的舊環境,可能在 Uint8Array
上存在問題(例如,它遺漏了它們的大部分方法)。
許可證
(c) 2018 Alex Ostrovski
chai-bytes 在 Apache-2.0 許可證下可用。