midi-nibbler 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,4 +2,6 @@ dir = File.dirname(File.expand_path(__FILE__))
2
2
  $LOAD_PATH.unshift dir + "/../lib"
3
3
 
4
4
  require "minitest/autorun"
5
+ require "mocha/test_unit"
6
+ require "shoulda-context"
5
7
  require "nibbler"
@@ -0,0 +1,37 @@
1
+ require "helper"
2
+
3
+ class Nibbler::MessageLibraryTest < Minitest::Test
4
+
5
+ context "MessageLibrary" do
6
+
7
+ context ".adapter" do
8
+
9
+ context "Midilib" do
10
+
11
+ setup do
12
+ @adapter = Nibbler::MessageLibrary.adapter(:midilib)
13
+ end
14
+
15
+ should "set to midilib" do
16
+ assert_equal Nibbler::Midilib, @adapter
17
+ end
18
+
19
+ end
20
+
21
+ context "MIDIMessage" do
22
+
23
+ setup do
24
+ @adapter = Nibbler::MessageLibrary.adapter(:midi_message)
25
+ end
26
+
27
+ should "set to midi message" do
28
+ assert_equal Nibbler::MIDIMessage, @adapter
29
+ end
30
+
31
+ end
32
+
33
+ end
34
+
35
+ end
36
+
37
+ end
@@ -3,124 +3,220 @@ require "nibbler/midi-message"
3
3
 
4
4
  class Nibbler::MIDIMessageTest < Minitest::Test
5
5
 
6
- def test_note_off
7
- lib = Nibbler::MIDIMessage
8
- message = lib.note_off(0, 0x40, 0x40)
9
- assert_equal(MIDIMessage::NoteOff, message.class)
10
- assert_equal(0, message.channel)
11
- assert_equal(0x40, message.note)
12
- assert_equal(0x40, message.velocity)
13
- end
6
+ context "MIDIMessage" do
14
7
 
15
- def test_note_on
16
- lib = Nibbler::MIDIMessage
17
- message = lib.note_on(0x0, 0x40, 0x40)
18
- assert_equal(MIDIMessage::NoteOn, message.class)
19
- assert_equal(0, message.channel)
20
- assert_equal(0x40, message.note)
21
- assert_equal(0x40, message.velocity)
22
- end
8
+ setup do
9
+ @lib = Nibbler::MIDIMessage
10
+ end
23
11
 
24
- def test_polyphonic_aftertouch
25
- lib = Nibbler::MIDIMessage
26
- message = lib.polyphonic_aftertouch(0x1, 0x40, 0x40)
27
- assert_equal(MIDIMessage::PolyphonicAftertouch, message.class)
28
- assert_equal(1, message.channel)
29
- assert_equal(0x40, message.note)
30
- assert_equal(0x40, message.value)
31
- end
12
+ context "note off" do
32
13
 
33
- def test_control_change
34
- lib = Nibbler::MIDIMessage
35
- message = lib.control_change(0x2, 0x20, 0x20)
36
- assert_equal(MIDIMessage::ControlChange, message.class)
37
- assert_equal(message.channel, 2)
38
- assert_equal(0x20, message.index)
39
- assert_equal(0x20, message.value)
40
- end
14
+ setup do
15
+ @message = @lib.note_off(0, 0x40, 0x40)
16
+ end
41
17
 
42
- def test_program_change
43
- lib = Nibbler::MIDIMessage
44
- message = lib.program_change(0x3, 0x40)
45
- assert_equal(MIDIMessage::ProgramChange, message.class)
46
- assert_equal(3, message.channel)
47
- assert_equal(0x40, message.program)
48
- end
18
+ should "create correct message" do
19
+ assert_equal(MIDIMessage::NoteOff, @message.class)
20
+ assert_equal(0, @message.channel)
21
+ assert_equal(0x40, @message.note)
22
+ assert_equal(0x40, @message.velocity)
23
+ end
49
24
 
50
- def test_channel_aftertouch
51
- lib = Nibbler::MIDIMessage
52
- message = lib.channel_aftertouch(0x3, 0x50)
53
- assert_equal(MIDIMessage::ChannelAftertouch, message.class)
54
- assert_equal(3, message.channel)
55
- assert_equal(0x50, message.value)
56
- end
25
+ end
57
26
 
58
- def test_pitch_bend
59
- lib = Nibbler::MIDIMessage
60
- message = lib.pitch_bend(0x0, 0x20, 0x00) # center
61
- assert_equal(MIDIMessage::PitchBend, message.class)
62
- assert_equal(0, message.channel)
63
- assert_equal(0x20, message.low)
64
- assert_equal(0x00, message.high)
65
- end
27
+ context "note on" do
66
28
 
67
- def test_system_exclusive_command
68
- lib = Nibbler::MIDIMessage
69
- message = lib.system_exclusive(0xF0, 0x41, 0x10, 0x42, 0x12, 0x40, 0x00, 0x7F, 0x00, 0x41, 0xF7)
70
- assert_equal(MIDIMessage::SystemExclusive::Command, message.class)
71
- assert_equal([0xF0, [0x41, 0x10, 0x42], 0x12, [0x40, 0x00, 0x7F], [0x00], 0x41, 0xF7], message.to_a)
72
- assert_equal([0xF0, 0x41, 0x10, 0x42, 0x12, 0x40, 0x00, 0x7F, 0x00, 0x41, 0xF7], message.to_bytes)
73
- assert_equal("F04110421240007F0041F7", message.to_hex_s)
74
- end
29
+ setup do
30
+ @message = @lib.note_on(0x0, 0x40, 0x40)
31
+ end
75
32
 
76
- def test_system_exclusive_request
77
- lib = Nibbler::MIDIMessage
78
- message = lib.system_exclusive(0xF0, 0x41, 0x10, 0x42, 0x11, 0x40, 0x00, 0x7F, 0x00, 0x41, 0xF7)
79
- assert_equal(MIDIMessage::SystemExclusive::Request, message.class)
80
- assert_equal([0xF0, [0x41, 0x10, 0x42], 0x11, [0x40, 0x00, 0x7F], [0x00, 0x00, 0x00], 0x41, 0xF7], message.to_a)
81
- assert_equal([0xF0, 0x41, 0x10, 0x42, 0x11, 0x40, 0x00, 0x7F, 0x00, 0x00, 0x00, 0x41, 0xF7], message.to_bytes)
82
- assert_equal("F04110421140007F00000041F7", message.to_hex_s)
83
- end
33
+ should "create correct message" do
34
+ assert_equal(MIDIMessage::NoteOn, @message.class)
35
+ assert_equal(0, @message.channel)
36
+ assert_equal(0x40, @message.note)
37
+ assert_equal(0x40, @message.velocity)
38
+ end
84
39
 
85
- def test_system_exclusive_node
86
- lib = Nibbler::MIDIMessage
87
- message = lib.system_exclusive(0xF0, 0x41, 0x10, 0x42, 0x12, 0x40, 0x00, 0x7F, 0x00, 0x41, 0xF7)
88
- node = message.node
89
- assert_equal(MIDIMessage::SystemExclusive::Node, node.class)
90
- assert_equal(0x41, node.manufacturer_id)
91
- assert_equal(0x42, node.model_id)
92
- assert_equal(0x10, node.device_id)
93
- end
40
+ end
94
41
 
95
- def test_system_common_generic_3_bytes
96
- lib = Nibbler::MIDIMessage
97
- message = lib.system_common(0x1, 0x50, 0xA0)
98
- assert_equal(MIDIMessage::SystemCommon, message.class)
99
- assert_equal(1, message.status[1])
100
- assert_equal(0x50, message.data[0])
101
- assert_equal(0xA0, message.data[1])
102
- end
42
+ context "polyphonic aftertouch" do
103
43
 
104
- def test_system_common_generic_2_bytes
105
- nibbler = Nibbler.new
106
- message = nibbler.parse(0xF1, 0x50)
107
- assert_equal(MIDIMessage::SystemCommon, message.class)
108
- assert_equal(1, message.status[1])
109
- assert_equal(0x50, message.data[0])
110
- end
44
+ setup do
45
+ @message = @lib.polyphonic_aftertouch(0x1, 0x40, 0x40)
46
+ end
111
47
 
112
- def test_system_common_generic_1_byte
113
- nibbler = Nibbler.new
114
- message = nibbler.parse(0xF1)
115
- assert_equal(MIDIMessage::SystemCommon, message.class)
116
- assert_equal(1, message.status[1])
117
- end
48
+ should "create correct message" do
49
+ assert_equal(MIDIMessage::PolyphonicAftertouch, @message.class)
50
+ assert_equal(1, @message.channel)
51
+ assert_equal(0x40, @message.note)
52
+ assert_equal(0x40, @message.value)
53
+ end
54
+
55
+ end
56
+
57
+ context "control change" do
58
+
59
+ setup do
60
+ @message = @lib.control_change(0x2, 0x20, 0x20)
61
+ end
62
+
63
+ should "create correct message" do
64
+ assert_equal(MIDIMessage::ControlChange, @message.class)
65
+ assert_equal(@message.channel, 2)
66
+ assert_equal(0x20, @message.index)
67
+ assert_equal(0x20, @message.value)
68
+ end
69
+
70
+ end
71
+
72
+ context "program change" do
73
+
74
+ setup do
75
+ @message = @lib.program_change(0x3, 0x40)
76
+ end
77
+
78
+ should "create correct message" do
79
+ assert_equal(MIDIMessage::ProgramChange, @message.class)
80
+ assert_equal(3, @message.channel)
81
+ assert_equal(0x40, @message.program)
82
+ end
83
+
84
+ end
85
+
86
+ context "channel aftertouch" do
87
+
88
+ setup do
89
+ @message = @lib.channel_aftertouch(0x3, 0x50)
90
+ end
91
+
92
+ should "create correct message" do
93
+ assert_equal(MIDIMessage::ChannelAftertouch, @message.class)
94
+ assert_equal(3, @message.channel)
95
+ assert_equal(0x50, @message.value)
96
+ end
97
+
98
+ end
99
+
100
+ context "pitch bend" do
101
+
102
+ setup do
103
+ @message = @lib.pitch_bend(0x0, 0x20, 0x00) # center
104
+ end
105
+
106
+ should "create correct message" do
107
+ assert_equal(MIDIMessage::PitchBend, @message.class)
108
+ assert_equal(0, @message.channel)
109
+ assert_equal(0x20, @message.low)
110
+ assert_equal(0x00, @message.high)
111
+ end
112
+
113
+ end
114
+
115
+ context "system exclusive command" do
116
+
117
+ setup do
118
+ @message = @lib.system_exclusive(0xF0, 0x41, 0x10, 0x42, 0x12, 0x40, 0x00, 0x7F, 0x00, 0x41, 0xF7)
119
+ end
120
+
121
+ should "create correct message" do
122
+ assert_equal(MIDIMessage::SystemExclusive::Command, @message.class)
123
+ assert_equal([0xF0, [0x41, 0x10, 0x42], 0x12, [0x40, 0x00, 0x7F], [0x00], 0x41, 0xF7], @message.to_a)
124
+ assert_equal([0xF0, 0x41, 0x10, 0x42, 0x12, 0x40, 0x00, 0x7F, 0x00, 0x41, 0xF7], @message.to_bytes)
125
+ assert_equal("F04110421240007F0041F7", @message.to_hex_s)
126
+ end
127
+
128
+ end
129
+
130
+ context "system exclusive request" do
131
+
132
+ setup do
133
+ @message = @lib.system_exclusive(0xF0, 0x41, 0x10, 0x42, 0x11, 0x40, 0x00, 0x7F, 0x00, 0x41, 0xF7)
134
+ end
135
+
136
+ should "create correct message" do
137
+ assert_equal(MIDIMessage::SystemExclusive::Request, @message.class)
138
+ assert_equal([0xF0, [0x41, 0x10, 0x42], 0x11, [0x40, 0x00, 0x7F], [0x00, 0x00, 0x00], 0x41, 0xF7], @message.to_a)
139
+ assert_equal([0xF0, 0x41, 0x10, 0x42, 0x11, 0x40, 0x00, 0x7F, 0x00, 0x00, 0x00, 0x41, 0xF7], @message.to_bytes)
140
+ assert_equal("F04110421140007F00000041F7", @message.to_hex_s)
141
+ end
142
+
143
+ end
144
+
145
+ context "system exclusive node" do
146
+
147
+ setup do
148
+ @message = @lib.system_exclusive(0xF0, 0x41, 0x10, 0x42, 0x12, 0x40, 0x00, 0x7F, 0x00, 0x41, 0xF7)
149
+ @node = @message.node
150
+ end
151
+
152
+ should "create correct message" do
153
+ assert_equal(MIDIMessage::SystemExclusive::Node, @node.class)
154
+ assert_equal(0x41, @node.manufacturer_id)
155
+ assert_equal(0x42, @node.model_id)
156
+ assert_equal(0x10, @node.device_id)
157
+ end
158
+
159
+ end
160
+
161
+ context "system realtime" do
162
+
163
+ setup do
164
+ @message = @lib.system_realtime(0x8)
165
+ end
166
+
167
+ should "create correct message" do
168
+ assert_equal(MIDIMessage::SystemRealtime, @message.class)
169
+ assert_equal(8, @message.id)
170
+ end
171
+
172
+ end
173
+
174
+ context "system common" do
175
+
176
+ context "1 byte" do
177
+
178
+ setup do
179
+ @message = @lib.system_common(0x1)
180
+ end
181
+
182
+ should "create correct message" do
183
+ assert_equal(MIDIMessage::SystemCommon, @message.class)
184
+ assert_equal(1, @message.status[1])
185
+ end
186
+
187
+ end
188
+
189
+ context "2 bytes" do
190
+
191
+ setup do
192
+ @message = @lib.system_common(0x1, 0x50)
193
+ end
194
+
195
+ should "create correct message" do
196
+ assert_equal(MIDIMessage::SystemCommon, @message.class)
197
+ assert_equal(1, @message.status[1])
198
+ assert_equal(0x50, @message.data[0])
199
+ end
200
+
201
+ end
202
+
203
+ context "3 bytes" do
204
+
205
+ setup do
206
+ @message = @lib.system_common(0x1, 0x50, 0xA0)
207
+ end
208
+
209
+ should "create correct message" do
210
+ assert_equal(MIDIMessage::SystemCommon, @message.class)
211
+ assert_equal(1, @message.status[1])
212
+ assert_equal(0x50, @message.data[0])
213
+ assert_equal(0xA0, @message.data[1])
214
+ end
215
+
216
+ end
217
+
218
+ end
118
219
 
119
- def test_system_realtime
120
- nibbler = Nibbler.new
121
- message = nibbler.parse(0xF8)
122
- assert_equal(MIDIMessage::SystemRealtime, message.class)
123
- assert_equal(8, message.id)
124
220
  end
125
221
 
126
222
  end
@@ -2,182 +2,254 @@ require "helper"
2
2
 
3
3
  class Nibbler::ParserTest < Minitest::Test
4
4
 
5
- def test_lookahead
6
- parser = Nibbler::Parser.new
7
- num = 6
8
- parser.instance_variable_set("@buffer", ["9", "0", "4", "0", "5", "0"])
9
- parser.send(:populate_current)
10
- output = parser.send(:lookahead, num) { |nibble_2, bytes| [nibble_2, bytes] }
11
- assert_equal([0,[0x90, 0x40, 0x50]], output[:message])
12
- assert_equal(["9", "0", "4", "0", "5", "0"], output[:processed])
13
- assert_equal([], parser.instance_variable_get("@current"))
14
- end
5
+ context "Parser" do
15
6
 
16
- def test_lookahead_trailing
17
- parser = Nibbler::Parser.new
18
- num = 6
19
- parser.instance_variable_set("@buffer", ["9", "0", "4", "0", "5", "0", "5", "0"])
20
- parser.send(:populate_current)
21
- output = parser.send(:lookahead, num) { |nibble_2, bytes| [nibble_2, bytes] }
22
- assert_equal([0,[0x90, 0x40, 0x50]], output[:message])
23
- assert_equal(["9", "0", "4", "0", "5", "0"], output[:processed])
24
- assert_equal(["5", "0"], parser.instance_variable_get("@current"))
25
- end
7
+ setup do
8
+ @library = Nibbler::MessageLibrary.adapter
9
+ @parser = Nibbler::Parser.new(@library)
10
+ end
11
+
12
+ context "#lookahead" do
13
+
14
+ context "basic" do
15
+
16
+ setup do
17
+ @parser.instance_variable_set("@buffer", ["9", "0", "4", "0", "5", "0"])
18
+ fragment = @parser.send(:get_fragment, 0)
19
+ @output = @parser.send(:lookahead, fragment, Nibbler::MessageBuilder.for_channel_message(@library, 0x9))
20
+ end
21
+
22
+ should "return proper message" do
23
+ assert_equal([0x90, 0x40, 0x50], @output[:message].to_a)
24
+ assert_equal(["9", "0", "4", "0", "5", "0"], @output[:processed])
25
+ end
26
+
27
+ end
28
+
29
+ context "with trailing nibbles" do
30
+
31
+ setup do
32
+ @parser.instance_variable_set("@buffer", ["9", "0", "4", "0", "5", "0", "5", "0"])
33
+ fragment = @parser.send(:get_fragment, 0)
34
+ @output = @parser.send(:lookahead, fragment, Nibbler::MessageBuilder.for_channel_message(@library, 0x9))
35
+ end
36
+
37
+ should "disregard trailing nibbles and return proper messages" do
38
+ assert_equal([0x90, 0x40, 0x50], @output[:message].to_a)
39
+ assert_equal(["9", "0", "4", "0", "5", "0"], @output[:processed])
40
+ end
41
+
42
+ end
43
+
44
+ context "incomplete" do
45
+
46
+ setup do
47
+ @parser.instance_variable_set("@buffer", ["9", "0", "4"])
48
+ fragment = @parser.send(:get_fragment, 0)
49
+ @output = @parser.send(:lookahead, fragment, Nibbler::MessageBuilder.for_channel_message(@library, 0x9))
50
+ end
51
+
52
+ should "not return anything" do
53
+ assert_nil @output
54
+ end
55
+
56
+ end
26
57
 
27
- def test_lookahead_too_short
28
- parser = Nibbler::Parser.new
29
- num = 6
30
- parser.instance_variable_set("@buffer", ["9", "0", "4"])
31
- parser.send(:populate_current)
32
- output = parser.send(:lookahead, num) do |nibble_2, bytes|
33
- {
34
- :message => nibble_2,
35
- :processed => bytes
36
- }
37
58
  end
38
59
 
39
- assert_nil output
40
- assert_equal(["9", "0", "4"], parser.instance_variable_get("@current"))
41
- end
60
+ context "#lookahead_for_sysex" do
42
61
 
43
- def test_lookahead_sysex
44
- parser = Nibbler::Parser.new
45
- parser.instance_variable_set("@buffer", "F04110421240007F0041F750".split(//))
46
- parser.send(:populate_current)
47
- output = parser.send(:lookahead_sysex) { |b| b }
48
- assert_equal([0xF0, 0x41, 0x10, 0x42, 0x12, 0x40, 0x00, 0x7F, 0x00, 0x41, 0xF7], output[:message])
49
- assert_equal("F04110421240007F0041F7".split(//), output[:processed])
50
- assert_equal(["5", "0"], parser.instance_variable_get("@current"))
51
- end
62
+ context "basic" do
52
63
 
53
- def test_lookahead_sysex_too_short
54
- parser = Nibbler::Parser.new
55
- parser.instance_variable_set("@buffer", ["9", "0", "4"])
56
- parser.send(:populate_current)
57
- output = parser.send(:lookahead_sysex) { |b| b }
64
+ setup do
65
+ @parser.instance_variable_set("@buffer", "F04110421240007F0041F750".split(//))
66
+ fragment = @parser.send(:get_fragment, 0)
67
+ @output = @parser.send(:lookahead_for_sysex, fragment)
68
+ end
58
69
 
59
- assert_nil output
60
- assert_equal(["9", "0", "4"], parser.instance_variable_get("@current"))
61
- end
70
+ should "return proper message" do
71
+ assert_equal([0xF0, 0x41, 0x10, 0x42, 0x12, 0x40, 0x00, 0x7F, 0x00, 0x41, 0xF7], @output[:message].to_a.flatten)
72
+ assert_equal("F04110421240007F0041F7".split(//), @output[:processed])
73
+ end
62
74
 
63
- def test_process
64
- parser = Nibbler::Parser.new
65
- short = ["9", "0", "4", "0", "5", "0", "5", "0"]
66
- output = parser.send(:process, short)
75
+ end
67
76
 
68
- assert_equal(::MIDIMessage::NoteOn, output[:messages].first.class)
69
- assert_equal(["5", "0"], parser.buffer)
70
- assert_equal(["9", "0", "4", "0", "5", "0"], output[:processed])
71
- end
77
+ context "incomplete" do
72
78
 
73
- def test_process_running_status
74
- parser = Nibbler::Parser.new
75
- two_msgs = ["9", "0", "4", "0", "5", "0", "4", "0", "6", "0"]
76
- output = parser.send(:process, two_msgs)
79
+ setup do
80
+ @parser.instance_variable_set("@buffer", ["9", "0", "4"])
81
+ fragment = @parser.send(:get_fragment, 0)
82
+ @output = @parser.send(:lookahead_for_sysex, fragment)
83
+ end
77
84
 
78
- refute_nil output
79
- assert_equal(::MIDIMessage::NoteOn, output[:messages][0].class)
80
- #assert_equal(::MIDIMessage::NoteOn, output[:messages][1].class)
81
- assert_equal([], parser.buffer)
82
- assert_equal(["9", "0", "4", "0", "5", "0", "4", "0", "6", "0"], output[:processed])
83
- end
85
+ should "not return anything" do
86
+ assert_nil @output
87
+ end
84
88
 
85
- def test_process_multiple_overlapping_calls
86
- parser = Nibbler::Parser.new
87
- short = ["9", "0", "4", "0", "5", "0", "9", "0"]
88
- short2 = ["3", "0", "2", "0", "1", "0"]
89
+ end
90
+ end
89
91
 
90
- output = parser.send(:process, short)
92
+ context "#process" do
91
93
 
92
- refute_nil output
93
- assert_equal(::MIDIMessage::NoteOn, output[:messages].first.class)
94
- assert_equal(["9", "0"], parser.buffer)
95
- assert_equal(["9", "0", "4", "0", "5", "0"], output[:processed])
94
+ context "basic" do
96
95
 
97
- output2 = parser.send(:process, short2)
96
+ setup do
97
+ short = ["9", "0", "4", "0", "5", "0", "5", "0"]
98
+ @output = @parser.send(:process, short)
99
+ end
98
100
 
99
- refute_nil output2
100
- assert_equal(::MIDIMessage::NoteOn, output2[:messages].first.class)
101
- assert_equal(["1", "0"], parser.buffer)
102
- assert_equal(["9", "0", "3", "0", "2", "0"], output2[:processed])
103
- end
101
+ should "return proper message" do
102
+ assert_equal(::MIDIMessage::NoteOn, @output[:messages].first.class)
103
+ assert_equal(["9", "0", "4", "0", "5", "0"], @output[:processed])
104
+ end
104
105
 
105
- def test_nibbles_to_message_leading
106
- parser = Nibbler::Parser.new
107
- short = ["5", "0", "9", "0", "4", "0", "5", "0"]
108
- parser.instance_variable_set("@buffer", short)
109
- parser.send(:populate_current)
110
- output = parser.send(:nibbles_to_message)
106
+ should "have trailing nibbles in buffer" do
107
+ assert_equal(["5", "0"], @parser.buffer)
108
+ end
111
109
 
112
- assert_nil output
113
- assert_equal(["5", "0", "9", "0", "4", "0", "5", "0"], parser.buffer)
114
- end
110
+ end
115
111
 
116
- def test_nibbles_to_message_trailing
117
- parser = Nibbler::Parser.new
118
- short = ["9", "0", "4", "0", "5", "0", "5", "0"]
119
- parser.instance_variable_set("@buffer", short)
120
- parser.send(:populate_current)
121
- output = parser.send(:nibbles_to_message)
122
-
123
- refute_nil output
124
- assert_equal(::MIDIMessage::NoteOn, output[:message].class)
125
- assert_equal(["5", "0"], parser.instance_variable_get("@current"))
126
- assert_equal(["9", "0", "4", "0", "5", "0"], output[:processed])
127
- end
112
+ context "with running status" do
128
113
 
129
- def test_nibbles_to_message
130
- parser = Nibbler::Parser.new
131
- short = ["9", "0", "4", "0", "5", "0", "5", "0"]
132
- parser.instance_variable_set("@buffer", short)
133
- parser.send(:populate_current)
134
- output = parser.send(:nibbles_to_message)
135
-
136
- refute_nil output
137
- assert_equal(::MIDIMessage::NoteOn, output[:message].class)
138
- assert_equal(["5", "0"], parser.instance_variable_get("@current"))
139
- assert_equal(["9", "0", "4", "0", "5", "0"], output[:processed])
140
- end
114
+ setup do
115
+ two_msgs = ["9", "0", "4", "0", "5", "0", "4", "0", "6", "0"]
116
+ @output = @parser.send(:process, two_msgs)
117
+ end
141
118
 
142
- def test_nibbles_to_message_running_status
143
- parser = Nibbler::Parser.new
144
- short = ["9", "0", "4", "0", "5", "0"]
145
- parser.instance_variable_set("@buffer", short)
146
- parser.send(:populate_current)
147
- output = parser.send(:nibbles_to_message)
119
+ should "return proper message" do
120
+ refute_nil @output
121
+ assert_equal(::MIDIMessage::NoteOn, @output[:messages][0].class)
122
+ assert_equal(::MIDIMessage::NoteOn, @output[:messages][1].class)
123
+ assert_equal(["9", "0", "4", "0", "5", "0", "4", "0", "6", "0"], @output[:processed])
124
+ end
148
125
 
149
- refute_nil output
150
- assert_equal(::MIDIMessage::NoteOn, output[:message].class)
126
+ should "not have anything left in the buffer" do
127
+ assert_empty(@parser.buffer)
128
+ end
151
129
 
152
- running_status = ["5", "0", "6", "0"]
153
- parser.instance_variable_set("@buffer", running_status)
154
- parser.send(:populate_current)
155
- output = parser.send(:nibbles_to_message)
130
+ end
156
131
 
157
- refute_nil output
158
- assert_equal(::MIDIMessage::NoteOn, output[:message].class)
159
- assert_equal(["5", "0", "6", "0"], output[:processed])
160
- end
132
+ context "with multiple overlapping calls" do
161
133
 
162
- def test_nibbles_to_message_sysex
163
- parser = Nibbler::Parser.new
164
- sysex = "F04110421240007F0041F750".split(//)
165
- parser.instance_variable_set("@buffer", sysex)
166
- parser.send(:populate_current)
167
- output = parser.send(:nibbles_to_message)
168
-
169
- refute_nil output
170
- assert_equal(::MIDIMessage::SystemExclusive::Command, output[:message].class)
171
- assert_equal(["5", "0"], parser.instance_variable_get("@current"))
172
- assert_equal("F04110421240007F0041F7".split(//), output[:processed])
173
- end
134
+ setup do
135
+ @short = ["9", "0", "4", "0", "5", "0", "9", "0"]
136
+ @short2 = ["3", "0", "2", "0", "1", "0"]
137
+ end
174
138
 
175
- def test_message_library
176
- parser = Nibbler::Parser.new(:message_lib => :midilib)
177
- assert_equal Nibbler::Midilib, parser.instance_variable_get("@message")
139
+ should "return proper messages and have trailing nibbles in buffer" do
140
+ @output = @parser.send(:process, @short)
141
+
142
+ refute_nil @output
143
+ assert_equal(::MIDIMessage::NoteOn, @output[:messages].first.class)
144
+ assert_equal(["9", "0", "4", "0", "5", "0"], @output[:processed])
145
+ assert_equal(["9", "0"], @parser.buffer)
146
+
147
+ @output2 = @parser.send(:process, @short2)
148
+
149
+ refute_nil @output2
150
+ assert_equal(::MIDIMessage::NoteOn, @output2[:messages].first.class)
151
+ assert_equal(["9", "0", "3", "0", "2", "0"], @output2[:processed])
152
+ assert_equal(["1", "0"], @parser.buffer)
153
+ end
154
+
155
+ end
156
+
157
+ end
158
+
159
+ context "#nibbles_to_message" do
160
+
161
+ context "basic" do
162
+
163
+ setup do
164
+ short = ["9", "0", "4", "0", "5", "0", "5", "0"]
165
+ @parser.instance_variable_set("@buffer", short)
166
+ fragment = @parser.send(:get_fragment, 0)
167
+ @output = @parser.send(:nibbles_to_message, fragment)
168
+ end
169
+
170
+ should "return proper message" do
171
+ refute_nil @output
172
+ assert_equal(::MIDIMessage::NoteOn, @output[:message].class)
173
+ assert_equal(["9", "0", "4", "0", "5", "0"], @output[:processed])
174
+ end
175
+
176
+ end
177
+
178
+ context "with leading nibbles" do
179
+
180
+ setup do
181
+ short = ["5", "0", "9", "0", "4", "0", "5", "0"]
182
+ @parser.instance_variable_set("@buffer", short)
183
+ fragment = @parser.send(:get_fragment, 0)
184
+ @output = @parser.send(:nibbles_to_message, fragment)
185
+ end
186
+
187
+ should "not do anything" do
188
+ assert_nil @output
189
+ assert_equal(["5", "0", "9", "0", "4", "0", "5", "0"], @parser.buffer)
190
+ end
191
+
192
+ end
193
+
194
+ context "with trailing nibbles" do
195
+
196
+ setup do
197
+ short = ["9", "0", "4", "0", "5", "0", "5", "0"]
198
+ @parser.instance_variable_set("@buffer", short)
199
+ fragment = @parser.send(:get_fragment, 0)
200
+ @output = @parser.send(:nibbles_to_message, fragment)
201
+ end
202
+
203
+ should "return proper message" do
204
+ refute_nil @output
205
+ assert_equal(::MIDIMessage::NoteOn, @output[:message].class)
206
+ assert_equal(["9", "0", "4", "0", "5", "0"], @output[:processed])
207
+ end
208
+
209
+ end
210
+
211
+ context "with running status" do
212
+
213
+ setup do
214
+ short = ["9", "0", "4", "0", "5", "0"]
215
+ @parser.instance_variable_set("@buffer", short)
216
+ fragment = @parser.send(:get_fragment, 0)
217
+ @output = @parser.send(:nibbles_to_message, fragment)
218
+ refute_nil @output
219
+ assert_equal(::MIDIMessage::NoteOn, @output[:message].class)
220
+ running_status = ["5", "0", "6", "0"]
221
+ @parser.instance_variable_set("@buffer", running_status)
222
+ fragment = @parser.send(:get_fragment, 0)
223
+ @output = @parser.send(:nibbles_to_message, fragment)
224
+ end
225
+
226
+ should "return proper message" do
227
+ refute_nil @output
228
+ assert_equal(::MIDIMessage::NoteOn, @output[:message].class)
229
+ assert_equal(["5", "0", "6", "0"], @output[:processed])
230
+ end
231
+
232
+ end
233
+
234
+ context "sysex" do
235
+
236
+ setup do
237
+ sysex = "F04110421240007F0041F750".split(//)
238
+ @parser.instance_variable_set("@buffer", sysex)
239
+ fragment = @parser.send(:get_fragment, 0)
240
+ @output = @parser.send(:nibbles_to_message, fragment)
241
+ end
242
+
243
+ should "return proper message" do
244
+ refute_nil @output
245
+ assert_equal(::MIDIMessage::SystemExclusive::Command, @output[:message].class)
246
+ assert_equal("F04110421240007F0041F7".split(//), @output[:processed])
247
+ end
248
+
249
+ end
250
+
251
+ end
178
252
 
179
- parser = Nibbler::Parser.new
180
- assert_equal Nibbler::MIDIMessage, parser.instance_variable_get("@message")
181
253
  end
182
254
 
183
255
  end