-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
228 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import * as sut from './ResponseDecoder' | ||
import { Buffer } from '@taichunmin/buffer' | ||
|
||
describe('is not buffer', () => { | ||
test.each([ | ||
{ name: 'SlotInfo.fromCmd1019', fn: sut.SlotInfo.fromCmd1019 }, | ||
{ name: 'SlotFreqIsEnable.fromCmd1023', fn: sut.SlotFreqIsEnable.fromCmd1023 }, | ||
{ name: 'BatteryInfo.fromCmd1025', fn: sut.BatteryInfo.fromCmd1025 }, | ||
{ name: 'DeviceSettings.fromCmd1034', fn: sut.DeviceSettings.fromCmd1034 }, | ||
{ name: 'Hf14aAntiColl.fromCmd2000', fn: sut.Hf14aAntiColl.fromCmd2000 }, | ||
{ name: 'Mf1AcquireStaticNestedRes.fromCmd2003', fn: sut.Mf1AcquireStaticNestedRes.fromCmd2003 }, | ||
{ name: 'Mf1DarksideRes.fromCmd2004', fn: sut.Mf1DarksideRes.fromCmd2004 }, | ||
{ name: 'Mf1NtDistanceRes.fromCmd2005', fn: sut.Mf1NtDistanceRes.fromCmd2005 }, | ||
{ name: 'Mf1NestedRes.fromCmd2006', fn: sut.Mf1NestedRes.fromCmd2006 }, | ||
{ name: 'Mf1CheckKeysOfSectorsRes.fromCmd2012', fn: sut.Mf1CheckKeysOfSectorsRes.fromCmd2012 }, | ||
{ name: 'Mf1DetectionLog.fromBuffer', fn: sut.Mf1DetectionLog.fromBuffer }, | ||
{ name: 'Mf1DetectionLog.fromCmd4006', fn: sut.Mf1DetectionLog.fromCmd4006 }, | ||
{ name: 'Mf1EmuSettings.fromCmd4009', fn: sut.Mf1EmuSettings.fromCmd4009 }, | ||
])('$name should throw error', ({ name, fn }) => { | ||
expect.hasAssertions() | ||
try { | ||
fn('not a buffer' as any) | ||
} catch (err) { | ||
expect(err).toBeInstanceOf(TypeError) | ||
expect(err.message).toMatch(/should be a Buffer/) | ||
} | ||
}) | ||
|
||
test.each([ | ||
{ name: 'SlotInfo.fromCmd1019', fn: sut.SlotInfo.fromCmd1019 }, | ||
{ name: 'SlotFreqIsEnable.fromCmd1023', fn: sut.SlotFreqIsEnable.fromCmd1023 }, | ||
{ name: 'BatteryInfo.fromCmd1025', fn: sut.BatteryInfo.fromCmd1025 }, | ||
{ name: 'DeviceSettings.fromCmd1034', fn: sut.DeviceSettings.fromCmd1034 }, | ||
{ name: 'Mf1DarksideRes.fromCmd2004', fn: sut.Mf1DarksideRes.fromCmd2004 }, | ||
{ name: 'Mf1NtDistanceRes.fromCmd2005', fn: sut.Mf1NtDistanceRes.fromCmd2005 }, | ||
{ name: 'Mf1CheckKeysOfSectorsRes.fromCmd2012', fn: sut.Mf1CheckKeysOfSectorsRes.fromCmd2012 }, | ||
{ name: 'Mf1DetectionLog.fromBuffer', fn: sut.Mf1DetectionLog.fromBuffer }, | ||
])('$name should throw error', ({ name, fn }) => { | ||
expect.hasAssertions() | ||
try { | ||
fn(new Buffer(0)) | ||
} catch (err) { | ||
expect(err).toBeInstanceOf(TypeError) | ||
expect(err.message).toMatch(/should be a Buffer/) | ||
} | ||
}) | ||
}) | ||
|
||
describe('Hf14aAntiColl', () => { | ||
test('fromBuffer should throw invalid length error', () => { | ||
expect.hasAssertions() | ||
try { | ||
sut.Hf14aAntiColl.fromBuffer(Buffer.from('01', 'hex')) | ||
} catch (err) { | ||
expect(err).toBeInstanceOf(Error) | ||
expect(err.message).toMatch(/invalid length of/) | ||
} | ||
}) | ||
|
||
test('fromBuffer should throw invalid length error', () => { | ||
expect.hasAssertions() | ||
try { | ||
sut.Hf14aAntiColl.fromBuffer(Buffer.from('040102030404000801', 'hex')) | ||
} catch (err) { | ||
expect(err).toBeInstanceOf(Error) | ||
expect(err.message).toMatch(/invalid length of/) | ||
} | ||
}) | ||
}) | ||
|
||
describe('Mf1DarksideRes', () => { | ||
test('fromBuffer should throw invalid length error', () => { | ||
const actual = sut.Mf1DarksideRes.fromCmd2004(Buffer.from('01', 'hex')) | ||
expect(actual).toMatchObject({ status: 1 }) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import _ from 'lodash' | ||
import * as sut from './Debug' | ||
|
||
test('errToJson', async () => { | ||
const err = _.merge(new Error('test'), { originalError: new Error('test') }) | ||
const actual = sut.errToJson(err) | ||
expect(actual).toMatchObject({ | ||
name: 'Error', | ||
message: 'test', | ||
originalError: { | ||
name: 'Error', | ||
message: 'test', | ||
}, | ||
}) | ||
}) | ||
|
||
test('jsonStringify', async () => { | ||
const circular = { b: 1 } | ||
const obj = { | ||
_censored: ['censored', 'censored1'], | ||
bigint: 1n, | ||
censored: 'test', | ||
circular1: circular, | ||
circular2: circular, | ||
date: new Date('2000-01-01T00:00:00Z'), | ||
error: new Error('test'), | ||
map: new Map([['a', 1]]), | ||
number: 1, | ||
set: new Set([1, 2, 3]), | ||
string: 'abc', | ||
} | ||
const actual = JSON.parse(sut.jsonStringify(obj)) | ||
expect(actual).toMatchObject({ | ||
bigint: '1', | ||
censored: '[Censored]', | ||
circular1: { b: 1 }, | ||
circular2: '[Circular]', | ||
date: '2000-01-01T00:00:00.000Z', | ||
error: { message: 'test', name: 'Error' }, | ||
map: { a: 1 }, | ||
number: 1, | ||
set: [1, 2, 3], | ||
string: 'abc', | ||
}) | ||
}) |
Oops, something went wrong.