-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnote.test.js
115 lines (94 loc) · 1.98 KB
/
note.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import test from 'ava'
import Note from '../src/note.js'
const possibleNotes = [
'C', 'C#',
'Db', 'D', 'D#',
'Eb', 'E',
'F', 'F#',
'Gb', 'G', 'G#',
'Ab', 'A', 'A#',
'Bb', 'B'
];
const noteMap = {
'c': 60,
'd': 62,
'e': 64,
'f': 65,
'g': 67,
'a': 69,
'b': 71,
'h': 71
};
const numMap = {
60: 'c',
61: 'c#',
62: 'd',
63: 'd#',
64: 'e',
65: 'f',
66: 'f#',
67: 'g',
68: 'g#',
69: 'a',
70: 'a#',
71: 'b'
};
function failTest(t, expected, actual, additional = "") {
t.fail(actual + " did not match expected " + expected +". Info: " + additional)
}
test(
'name to note', t => {
const n = new Note('C');
for(let x in noteMap) {
let expected = noteMap[x];
let actual = n.nameToNote(x);
if (expected !== actual) {
failTest(t, expected, actual);
}
}
t.pass();
});
test(
'Ocatves -2 to 8', t => {
for (let i = -2; i <= 8; ++i) {
let c = 60;
for (let index in possibleNotes) {
let note = possibleNotes[index];
if (note.substr(-1) === 'b') {
--c;
}
let expected = c + i*12;
if (0 !== i) {
note = note.concat(i.toString());
}
const n = new Note(note);
const actual = n.getNumber();
if (actual !== expected) {
failTest(t, expected, actual, note);
}
c++;
}
}
t.pass();
}
);
test('from number', t => {
for (let i = 24; i < 156; ++i) {
const actual = Note.fromNumber(i);
const octave = Math.floor((i-60) / 12);
const norm = i - octave * 12;
let note = numMap[norm].toUpperCase();
if (octave !== 0) {
note += octave.toString();
}
const expected = new Note(note);
const info = expected.name;
if (actual.octave !== expected.octave) {
failTest(t, expected.octave, actual.octave, info);
}
if (actual.name !== expected.name) {
failTest(t, expected.name, actual.name, info);
}
t.pass();
}
});