Skip to content

Commit

Permalink
Tests for AutoBeamFormatting, Barline, Beam, BoundingBox, BoundingBox…
Browse files Browse the repository at this point in the history
…Computation,

Clef, Curve, Dot, GlyphNote, GraceNote, NoteSubGroup, Ornament, Rests,
Rhythm, StaveNote, TabNote, TextNote, ThreeVoice, TickContext, Tuning,
Tuplet...
  • Loading branch information
ronyeh committed Aug 30, 2021
1 parent 8ad86c2 commit f1723fc
Show file tree
Hide file tree
Showing 21 changed files with 1,038 additions and 1,110 deletions.
205 changes: 67 additions & 138 deletions tests/auto_beam_formatting_tests.ts

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions tests/barline_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
/* eslint-disable */
// @ts-nocheck

// TODO: Factory.BarNote()'s type parameter expects BarlineType, but we pass in string. Did the previous API allow string?

import { VexFlowTests, TestOptions } from './vexflow_test_helpers';
import { Barline, BarlineType } from 'stavebarline';

Expand All @@ -18,7 +20,7 @@ const BarlineTests = {
});

const run = VexFlowTests.runTests;
run('Simple BarNotes', function (options: TestOptions) {
run('Simple BarNotes', (options: TestOptions) => {
const f = VexFlowTests.makeFactory(options, 380, 160);
const stave = f.Stave();

Expand All @@ -40,7 +42,7 @@ const BarlineTests = {
ok(true, 'Simple Test');
});

run('Style BarNotes', function (options: TestOptions) {
run('Style BarNotes', (options: TestOptions) => {
const f = VexFlowTests.makeFactory(options, 380, 160);
const stave = f.Stave();

Expand Down
87 changes: 43 additions & 44 deletions tests/beam_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@
/* eslint-disable */
// @ts-nocheck

// TODO: Beam has a "private readonly stem_direction" without an accessor.
// TODO: Factory.Beam()'s 'notes' param is a StemmableNote[], but we only have access to Tickable[].

import { VexFlowTests, TestOptions, concat } from './vexflow_test_helpers';
import { Beam } from 'beam';
import { Stem } from 'stem';
import { Voice } from 'voice';
import { StemmableNote } from 'stemmablenote';
import { StaveNoteStruct } from 'stavenote';
import { TabNoteStruct } from 'tabnote';

const BeamTests = {
Start(): void {
Expand Down Expand Up @@ -185,7 +191,7 @@ const BeamTests = {
time: '6/4',
});

const notes = voice.getTickables();
const notes = voice.getTickables() as StemmableNote[];

const beams = [
f.Beam({ notes: notes.slice(0, 2), options: { autoStem: true } }),
Expand Down Expand Up @@ -230,17 +236,13 @@ const BeamTests = {
[0, 4],
[4, 8],
[8, 12],
].forEach(function (range) {
f.Beam({ notes: voice1.getTickables().slice(range[0], range[1]) });
});
].forEach((range) => f.Beam({ notes: voice1.getTickables().slice(range[0], range[1]) }));

[
[0, 4],
[4, 8],
[8, 12],
].forEach(function (range) {
f.Beam({ notes: voice2.getTickables().slice(range[0], range[1]) });
});
].forEach((range) => f.Beam({ notes: voice2.getTickables().slice(range[0], range[1]) }));

f.Formatter().joinVoices([voice1, voice2]).formatToStave([voice1, voice2], stave);

Expand Down Expand Up @@ -288,7 +290,7 @@ const BeamTests = {
{ time: '6/4' }
);

const notes = voice.getTickables();
const notes = voice.getTickables() as StemmableNote[];
f.Beam({ notes: notes.slice(0, 4) });
f.Beam({ notes: notes.slice(4, 8) });
f.Beam({ notes: notes.slice(8, 12) });
Expand Down Expand Up @@ -406,7 +408,7 @@ const BeamTests = {
const f = VexFlowTests.makeFactory(options, 600, 200);
const stave = f.TabStave({ y: 20 });

const specs = [
const specs: TabNoteStruct[] = [
{
positions: [
{ str: 3, fret: 6 },
Expand Down Expand Up @@ -462,8 +464,8 @@ const BeamTests = {
{ positions: [{ str: 3, fret: 6 }], duration: '8' },
];

const notes = specs.map(function (noteSpec) {
const tabNote = f.TabNote(noteSpec);
const notes = specs.map((struct) => {
const tabNote = f.TabNote(struct);
tabNote.render_options.draw_stem = true;
return tabNote;
});
Expand All @@ -485,7 +487,7 @@ const BeamTests = {
const f = VexFlowTests.makeFactory(options, 600, 250);
const stave = f.TabStave({ options: { num_lines: 10 } });

const specs = [
const specs: TabNoteStruct[] = [
{
stem_direction: -1,
positions: [
Expand Down Expand Up @@ -551,8 +553,8 @@ const BeamTests = {
{ stem_direction: -1, positions: [{ str: 10, fret: 6 }], duration: '8' },
];

const notes = specs.map(function (noteSpec) {
const tabNote = f.TabNote(noteSpec);
const notes = specs.map((struct) => {
const tabNote = f.TabNote(struct);
tabNote.render_options.draw_stem = true;
tabNote.render_options.draw_dots = true;
return tabNote;
Expand Down Expand Up @@ -580,7 +582,7 @@ const BeamTests = {
const f = VexFlowTests.makeFactory(options, 600, 200);
const stave = f.TabStave();

const specs = [
const specs: TabNoteStruct[] = [
{
positions: [
{ str: 1, fret: 6 },
Expand Down Expand Up @@ -619,8 +621,8 @@ const BeamTests = {
{ positions: [{ str: 6, fret: 6 }], duration: '16' },
];

const notes = specs.map(function (noteSpec) {
const tabNote = f.TabNote(noteSpec);
const notes = specs.map((struct) => {
const tabNote = f.TabNote(struct);
tabNote.render_options.draw_stem = true;
tabNote.render_options.draw_dots = true;
return tabNote;
Expand All @@ -633,9 +635,7 @@ const BeamTests = {

f.draw();

beams.forEach(function (beam) {
beam.setContext(f.getContext()).draw();
});
beams.forEach((beam) => beam.setContext(f.getContext()).draw());

ok(true, 'All objects have been drawn');
},
Expand All @@ -646,7 +646,7 @@ const BeamTests = {
const f = VexFlowTests.makeFactory(options, 600, 300);
const stave = f.TabStave();

const specs = [
const specs: TabNoteStruct[] = [
{
positions: [
{ str: 1, fret: 6 },
Expand Down Expand Up @@ -689,8 +689,8 @@ const BeamTests = {
{ positions: [{ str: 6, fret: 6 }], duration: '16', stem_direction: -1 },
];

const notes = specs.map(function (noteSpec) {
const tabNote = f.TabNote(noteSpec);
const notes = specs.map((struct) => {
const tabNote = f.TabNote(struct);
tabNote.render_options.draw_stem = true;
tabNote.render_options.draw_dots = true;
return tabNote;
Expand All @@ -714,7 +714,7 @@ const BeamTests = {
const f = VexFlowTests.makeFactory(options, 500, 200);
const stave = f.Stave({ y: 40 });

let notes = [
const s1: StaveNoteStruct[] = [
{ keys: ['e/4'], duration: '128', stem_direction: 1 },
{ keys: ['d/4'], duration: '16', stem_direction: 1 },
{ keys: ['e/4'], duration: '8', stem_direction: 1 },
Expand All @@ -724,7 +724,7 @@ const BeamTests = {
{ keys: ['c/4'], duration: '32', stem_direction: 1 },
];

let notes2 = [
const s2: StaveNoteStruct[] = [
{ keys: ['e/5'], duration: '128', stem_direction: -1 },
{ keys: ['d/5'], duration: '16', stem_direction: -1 },
{ keys: ['e/5'], duration: '8', stem_direction: -1 },
Expand All @@ -734,18 +734,18 @@ const BeamTests = {
{ keys: ['c/5'], duration: '32', stem_direction: -1 },
];

notes = notes.map(function (note) {
return f.StaveNote(note).addModifier(f.Annotation({ text: '1', vJustify: 'above' }), 0);
});
const notes1 = s1.map((struct) =>
f.StaveNote(struct).addModifier(f.Annotation({ text: '1', vJustify: 'above' }), 0)
);

notes2 = notes2.map(function (note) {
return f.StaveNote(note).addModifier(f.Annotation({ text: '3', vJustify: 'below' }), 0);
});
const notes2 = s2.map((struct) =>
f.StaveNote(struct).addModifier(f.Annotation({ text: '3', vJustify: 'below' }), 0)
);

f.Beam({ notes: notes });
f.Beam({ notes: notes1 });
f.Beam({ notes: notes2 });

const voice = f.Voice().setMode(Voice.Mode.SOFT).addTickables(notes).addTickables(notes2);
const voice = f.Voice().setMode(Voice.Mode.SOFT).addTickables(notes1).addTickables(notes2);

f.Formatter().joinVoices([voice]).formatToStave([voice], stave, { stave: stave });

Expand All @@ -758,7 +758,7 @@ const BeamTests = {
const f = VexFlowTests.makeFactory(options, 500, 200);
const stave = f.Stave({ y: 40 });

let notes = [
const s1: StaveNoteStruct[] = [
{ keys: ['e/4'], duration: '128', stem_direction: 1 },
{ keys: ['d/4'], duration: '16', stem_direction: 1 },
{ keys: ['e/4'], duration: '8', stem_direction: 1 },
Expand All @@ -768,7 +768,7 @@ const BeamTests = {
{ keys: ['c/4'], duration: '32', stem_direction: 1 },
];

let notes2 = [
const s2: StaveNoteStruct[] = [
{ keys: ['e/5'], duration: '128', stem_direction: -1 },
{ keys: ['d/5'], duration: '16', stem_direction: -1 },
{ keys: ['e/5'], duration: '8', stem_direction: -1 },
Expand All @@ -778,18 +778,17 @@ const BeamTests = {
{ keys: ['c/5'], duration: '32', stem_direction: -1 },
];

notes = notes.map(function (note) {
return f.StaveNote(note).addModifier(f.Articulation({ type: 'am', position: 'above' }), 0);
});

notes2 = notes2.map(function (note) {
return f.StaveNote(note).addModifier(f.Articulation({ type: 'a>', position: 'below' }), 0);
});
const notes1 = s1.map((struct) =>
f.StaveNote(struct).addModifier(f.Articulation({ type: 'am', position: 'above' }), 0)
);
const notes2 = s2.map((struct) =>
f.StaveNote(struct).addModifier(f.Articulation({ type: 'a>', position: 'below' }), 0)
);

f.Beam({ notes: notes });
f.Beam({ notes: notes1 });
f.Beam({ notes: notes2 });

const voice = f.Voice().setMode(Voice.Mode.SOFT).addTickables(notes).addTickables(notes2);
const voice = f.Voice().setMode(Voice.Mode.SOFT).addTickables(notes1).addTickables(notes2);

f.Formatter().joinVoices([voice]).formatToStave([voice], stave, { stave: stave });

Expand Down
4 changes: 2 additions & 2 deletions tests/boundingbox_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { BoundingBox } from 'boundingbox';
const BoundingBoxTests = {
Start(): void {
QUnit.module('BoundingBox');
test('Initialization Test', BoundingBoxTests.initialization);
test('Merging Text', BoundingBoxTests.merging);
test('Initialization Test', this.initialization);
test('Merging Text', this.merging);
},

initialization(): void {
Expand Down
63 changes: 34 additions & 29 deletions tests/boundingboxcomputation_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,14 @@
//
// BoundingBoxComputation Tests

// TODO: Provide a unique names for each test, so we can render separate PNGs.
// See line 41 'Quadratic Test' and line 93 'Cubic Test'. We can append an incrementing counter.

import { VexFlowTests, TestOptions } from './vexflow_test_helpers';
import { BoundingBoxComputation } from 'boundingboxcomputation';
import { Glyph, OutlineCode } from 'glyph';
import { RenderContext } from 'types/common';

function createCanvas(options: TestOptions): RenderContext {
const points = options.params.points;

// Size the canvas to fit all the points and a small margin.
let w = points[0];
let h = points[1];
for (let i = 2; i < points.length; i += 2) {
w = Math.max(w, points[i]);
h = Math.max(h, points[i + 1]);
}

const f = VexFlowTests.makeFactory(options, w + 20, h + 20);
const ctx = f.getContext();
ctx.setLineCap('square');
return ctx;
}

function rect(ctx: RenderContext, style: string, lineWidth: number, x: number, y: number, w: number, h: number): void {
ctx.strokeStyle = style;
ctx.setLineWidth(lineWidth);
ctx.beginPath();
ctx.moveTo(x, y);
ctx.lineTo(x + w, y);
ctx.lineTo(x + w, y + h);
ctx.lineTo(x, y + h);
ctx.lineTo(x, y);
ctx.stroke();
}

const BoundingBoxComputationTests = {
Start(): void {
QUnit.module('BoundingBoxComputation');
Expand Down Expand Up @@ -237,4 +211,35 @@ const BoundingBoxComputationTests = {
},
};

//#region Helper Functions
function createCanvas(options: TestOptions): RenderContext {
const points = options.params.points;

// Size the canvas to fit all the points and a small margin.
let w = points[0];
let h = points[1];
for (let i = 2; i < points.length; i += 2) {
w = Math.max(w, points[i]);
h = Math.max(h, points[i + 1]);
}

const f = VexFlowTests.makeFactory(options, w + 20, h + 20);
const ctx = f.getContext();
ctx.setLineCap('square');
return ctx;
}

function rect(ctx: RenderContext, style: string, lineWidth: number, x: number, y: number, w: number, h: number): void {
ctx.strokeStyle = style;
ctx.setLineWidth(lineWidth);
ctx.beginPath();
ctx.moveTo(x, y);
ctx.lineTo(x + w, y);
ctx.lineTo(x + w, y + h);
ctx.lineTo(x, y + h);
ctx.lineTo(x, y);
ctx.stroke();
}
//#endregion Helper Functions

export { BoundingBoxComputationTests };
3 changes: 2 additions & 1 deletion tests/clef_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// Clef Tests

import { VexFlowTests, TestOptions } from './vexflow_test_helpers';
import { Note } from 'note';

const ClefTests = {
Start(): void {
Expand Down Expand Up @@ -107,7 +108,7 @@ const ClefTests = {
drawClefChange(options: TestOptions): void {
const f = VexFlowTests.makeFactory(options, 800, 180);
const stave = f.Stave().addClef('treble');
const notes = [
const notes: Note[] = [
f.StaveNote({ keys: ['c/4'], duration: '4', clef: 'treble' }),
f.ClefNote({ type: 'alto', options: { size: 'small' } }),
f.StaveNote({ keys: ['c/4'], duration: '4', clef: 'alto' }),
Expand Down
Loading

0 comments on commit f1723fc

Please sign in to comment.