Skip to content

Commit

Permalink
Improve tests. Add .CATEGORY to StemmableNote.
Browse files Browse the repository at this point in the history
  • Loading branch information
ronyeh committed Aug 18, 2021
1 parent a783a9a commit 45b568a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
8 changes: 8 additions & 0 deletions src/stemmablenote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import { Note, NoteStruct } from './note';
import { GlyphProps } from './glyph';

export abstract class StemmableNote extends Note {
static get CATEGORY(): string {
return 'stemmablenote';
}

stem_direction?: number;
stem?: Stem;

Expand Down Expand Up @@ -253,4 +257,8 @@ export abstract class StemmableNote extends Note {
this.setStem(new Stem(stemOptions));
this.stem?.setContext(this.getContext()).draw();
}

getCategory(): string {
return StemmableNote.CATEGORY;
}
}
3 changes: 2 additions & 1 deletion src/typeguard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const isStaveNote = (obj: unknown): obj is StaveNote => isCategory(obj, S
export const isTabNote = (obj: unknown): obj is TabNote => isCategory(obj, TabNote);

/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
/* eslint-disable @typescript-eslint/ban-types */

/**
Expand All @@ -21,7 +22,7 @@ export const isTabNote = (obj: unknown): obj is TabNote => isCategory(obj, TabNo
* If `false`, we do not check the superclass or other ancestors.
* @returns true if `obj` is an instance of `ClassName`, or has a `.getCategory()` that matches `ClassName.CATEGORY`.
*/
function isCategory<T>(
export function isCategory<T>(
obj: any,
cls: Function & { prototype: T; CATEGORY?: string },
checkAncestors: boolean = true
Expand Down
22 changes: 12 additions & 10 deletions tests/typeguard_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// TypeGuard Tests

import { VexFlowTests } from './vexflow_test_helpers';
import { QUnit, ok, notOk } from './declarations';
import { QUnit, ok, notOk, test } from './declarations';
import { isCategory, isNote, isStaveNote, isStemmableNote, isTabNote } from 'typeguard';
import { StaveNote } from 'stavenote';
import { TabNote } from 'tabnote';
Expand All @@ -13,13 +13,13 @@ import { StemmableNote } from 'stemmablenote';
const TypeGuardTests = {
Start(): void {
QUnit.module('TypeGuard');
VexFlowTests.runTests('Type Checking', this.typeChecking);
VexFlowTests.runTests('Edge Case', this.edgeCase);
test('Type Checking', this.typeChecking);
test('Edge Case', this.edgeCase);
},

typeChecking(): void {
const s = new StaveNote({ keys: ['c/4'], duration: 'w' });
ok(isCategory(s, StaveNote));
ok(isStaveNote(s));

const fakeStaveNote = { getCategory: () => 'stavenotes' };
ok(isStaveNote(fakeStaveNote));
Expand All @@ -33,18 +33,20 @@ const TypeGuardTests = {
return StemmableNote.CATEGORY;
},
};
ok(isStemmableNote(fakeStemmableNote));
ok(isStemmableNote(fakeStemmableNote), 'A JS object that returns the correct category string.');
notOk(isNote(fakeStemmableNote), 'The fake stemmable note does not have any ancestors with the correct category.');

ok(isNote(s));
ok(isNote(t));
ok(isNote(fakeStaveNote));
ok(isNote(s), 'StaveNote extends StemmableNote which extends Note, so s is a Note');
ok(isNote(t), 'TabNote extends StemmableNote which extends Note, so t is a Note');
ok(isStemmableNote(t), 'TabNote extends StemmableNote directly');
notOk(isNote(fakeStaveNote), 'Fake StaveNote is not a Note, because categories do not match.');
},

edgeCase(): void {
class Z extends Object {}
const zInstance = new Z();
ok(isCategory(zInstance, Z));
ok(zInstance instanceof Z);
notOk(isCategory(zInstance, Z), 'z is not the same category as Z !?');
notOk(zInstance instanceof Z, 'z is not instanceof Z !?');
},
};

Expand Down

0 comments on commit 45b568a

Please sign in to comment.