mirror of
https://github.com/pybricks/pybricks-code.git
synced 2026-09-12 17:45:22 +00:00
refactor common test helpers into test/
This way we can share them with multiple tests
This commit is contained in:
committed by
David Lechner
parent
8ba4143fa7
commit
31c3e2b52e
@@ -1,7 +1,7 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// Copyright (c) 2020 The Pybricks Authors
|
||||
|
||||
import { assert, hex, lookup } from '.';
|
||||
import { assert, hex } from '.';
|
||||
|
||||
test('assert', () => {
|
||||
const assertTrue = jest.fn(() => assert(true, 'should not throw'));
|
||||
@@ -16,9 +16,3 @@ test('hex', () => {
|
||||
expect(hex(1, 4)).toBe('0x0001');
|
||||
expect(hex(2, 8)).toBe('0x00000002');
|
||||
});
|
||||
|
||||
test('lookup', () => {
|
||||
const obj = { a: { b: { c: 'd' } } };
|
||||
expect(lookup(obj, 'a.b.c')).toBe('d');
|
||||
expect(lookup(obj, 'a.x.y')).toBeUndefined();
|
||||
});
|
||||
|
||||
@@ -21,18 +21,3 @@ export function assert(condition: boolean, message: string): void {
|
||||
export function hex(n: number, pad: number): string {
|
||||
return `0x${n.toString(16).padStart(pad, '0')}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Looks up a nested property in an object.
|
||||
* @param obj The object
|
||||
* @param id The property path
|
||||
*/
|
||||
export function lookup(obj: object, id: string): string | undefined {
|
||||
const value = id
|
||||
.split('.')
|
||||
.reduce((pv, cv) => pv && (pv as Record<string, object>)[cv], obj);
|
||||
if (typeof value === 'string') {
|
||||
return value;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user