File: test_note.rb

package info (click to toggle)
sonic-pi 3.2.2~repack-8
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 71,872 kB
  • sloc: ruby: 30,548; cpp: 8,490; sh: 957; ansic: 461; erlang: 360; lisp: 141; makefile: 44
file content (182 lines) | stat: -rw-r--r-- 5,538 bytes parent folder | download | duplicates (4)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#--
# This file is part of Sonic Pi: http://sonic-pi.net
# Full project source: https://github.com/samaaron/sonic-pi
# License: https://github.com/samaaron/sonic-pi/blob/master/LICENSE.md
#
# Copyright 2013, 2014, 2015, 2016 by Sam Aaron (http://sam.aaron.name).
# All rights reserved.
#
# Permission is granted for use, copying, modification, and
# distribution of modified versions of this work as long as this
# notice is included.
#++

require_relative "./setup_test"
require_relative "../lib/sonicpi/note"

module SonicPi
  class NoteTester < Minitest::Test

    def test_resolution_of_nil
      assert_equal(nil, Note.resolve_midi_note(nil))
    end

    def test_resolution_of_numbers
      assert_equal(60, Note.resolve_midi_note(60))
      assert_equal(60.2, Note.resolve_midi_note(60.2))
    end

    def test_resolution_of_symbols
      assert_equal(12, Note.resolve_midi_note(:C, 0))
      assert_equal(0, Note.resolve_midi_note(:C, -1))
      assert_equal(60, Note.resolve_midi_note(:C, 4))
      assert_equal(60, Note.resolve_midi_note(:c, 4))
      assert_equal(61, Note.resolve_midi_note(:Cs, 4))
      assert_equal(61, Note.resolve_midi_note(:cs, 4))
      assert_equal(60, Note.resolve_midi_note(:C4))
      assert_equal(61, Note.resolve_midi_note(:Cs4))
      assert_equal(61, Note.resolve_midi_note(:cs4))
      assert_equal(60, Note.resolve_midi_note(:C4, 4))
      assert_equal(60, Note.resolve_midi_note(:C6, 4))
    end

    def test_resolution_of_strings
      assert_equal(12, Note.resolve_midi_note("C", 0))
      assert_equal(0, Note.resolve_midi_note("C", -1))
      assert_equal(60, Note.resolve_midi_note("C", 4))
      assert_equal(60, Note.resolve_midi_note("c", 4))
      assert_equal(61, Note.resolve_midi_note("Cs", 4))
      assert_equal(61, Note.resolve_midi_note("cs", 4))
      assert_equal(61, Note.resolve_midi_note("Cs4"))
      assert_equal(61, Note.resolve_midi_note("cs4"))
      assert_equal(60, Note.resolve_midi_note("C4"))
      assert_equal(60, Note.resolve_midi_note("C4", 4))
      assert_equal(60, Note.resolve_midi_note("C6", 4))
    end

    def test_resolution_of_strings_prefixed_with_colons
      assert_equal(12, Note.resolve_midi_note(":C", 0))
      assert_equal(0, Note.resolve_midi_note(":C", -1))
      assert_equal(60, Note.resolve_midi_note(":C", 4))
      assert_equal(60, Note.resolve_midi_note(":c", 4))
      assert_equal(61, Note.resolve_midi_note(":Cs", 4))
      assert_equal(61, Note.resolve_midi_note(":cs", 4))
      assert_equal(61, Note.resolve_midi_note(":Cs4"))
      assert_equal(61, Note.resolve_midi_note(":cs4"))
      assert_equal(60, Note.resolve_midi_note(":C4"))
      assert_equal(60, Note.resolve_midi_note(":C4", 4))
      assert_equal(60, Note.resolve_midi_note(":C6", 4))
    end

    def test_resolution_of_name
      assert_equal(:C, Note.resolve_note_name(60))
      assert_equal(:C, Note.resolve_note_name(:C4))
      assert_equal(:C, Note.resolve_note_name(:C))
      assert_equal(:C, Note.resolve_note_name(:C, 4))
      assert_equal(:C, Note.resolve_note_name(:C4, 4))
      assert_equal(:C, Note.resolve_note_name(60.2))
      assert_equal(:C, Note.resolve_note_name(60.2, 4))
    end

    def test_init_c4
      n = Note.new(:C4)
      assert_equal(4, n.octave)
      assert_equal(:C, n.pitch_class)
      assert_equal(0, n.interval)
      assert_equal(60, n.midi_note)
    end

    def test_init_c_4
      n = Note.new(:C, 4)
      assert_equal(4, n.octave)
      assert_equal(:C, n.pitch_class)
      assert_equal(0, n.interval)
      assert_equal(60, n.midi_note)
    end

    def test_init_Eb3
      n = Note.new(:Eb3)
      assert_equal(3, n.octave)
      assert_equal(:Eb, n.pitch_class)
      assert_equal(3, n.interval)
      assert_equal(51, n.midi_note)
    end

    def test_init_EB3
      n = Note.new(:EB3)
      assert_equal(3, n.octave)
      assert_equal(:Eb, n.pitch_class)
      assert_equal(3, n.interval)
      assert_equal(51, n.midi_note)
    end

    def test_init_EF3
      n = Note.new(:EF3)
      assert_equal(3, n.octave)
      assert_equal(:Eb, n.pitch_class)
      assert_equal(3, n.interval)
      assert_equal(51, n.midi_note)
    end

    def test_init_Fs_7
      n = Note.new(:Fs, 7)
      assert_equal(7, n.octave)
      assert_equal(:Fs, n.pitch_class)
      assert_equal(6, n.interval)
      assert_equal(102, n.midi_note)
    end

    def test_init_Fs3_7
      # The 3 in Fs3 should be overridden by
      # the explicit octave value 7
      n = Note.new(:Fs3, 7)
      assert_equal(7, n.octave)
      assert_equal(:Fs, n.pitch_class)
      assert_equal(6, n.interval)
      assert_equal(102, n.midi_note)
    end

    def test_init_error_sam
      assert_raises Note::InvalidNoteError do
        Note.new(:sam)
      end
    end

    def test_init_error_KF_4
      assert_raises Note::InvalidNoteError do
        Note.new(:KF, 4)
      end
    end

    def test_init_error_Ebb2
      assert_raises Note::InvalidNoteError do
        Note.new(:Ebb2!)
      end
    end

    def test_init_invalid_octave
      assert_raises Note::InvalidOctaveError do
        Note.new(:Eb, :foo)
      end

      assert_raises Note::InvalidOctaveError do
        Note.new(:Eb, 3.5)
      end

      assert_raises Note::InvalidOctaveError do
        Note.new(:Eb, 3.0)
      end
    end

    def test_c_flat_is_octave_lower
      cb = Note.new(:Cb4)
      assert_equal(4, cb.octave)
      assert_equal(:Cb, cb.pitch_class)
      assert_equal(-1, cb.interval)
      assert_equal(59, cb.midi_note)
    end

  end


end