dxruby_sdl 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,8 +3,30 @@ require 'spec_helper'
3
3
 
4
4
  describe DXRubySDL::Input,
5
5
  'キーボード・ゲームパッド・マウスの入力を扱うモジュール' do
6
- before do
7
- DXRubySDL::Input.instance_variable_set('@joysticks', [])
6
+ def push_key_down_event(sym)
7
+ e = SDL::Event::KeyDown.new
8
+ e.press = true
9
+ e.sym = sym
10
+ e.mod = 0
11
+ e.unicode = 0
12
+ SDL::Event.push(e)
13
+ end
14
+
15
+ def push_key_up_event(sym)
16
+ e = SDL::Event::KeyUp.new
17
+ e.press = false
18
+ e.sym = sym
19
+ e.mod = 0
20
+ e.unicode = 0
21
+ SDL::Event.push(e)
22
+ end
23
+
24
+ def update_input_keys
25
+ expect {
26
+ DXRubySDL::Window.loop do
27
+ SDL::Event.push(SDL::Event::Quit.new)
28
+ end
29
+ }.to raise_error(SystemExit)
8
30
  end
9
31
 
10
32
  shared_context '.set_repeat' do
@@ -19,43 +41,41 @@ describe DXRubySDL::Input,
19
41
  end
20
42
 
21
43
  describe '.set_repeat', 'キーリピートを設定する' do
22
- subject { DXRubySDL::Input.set_repeat(15, 2) }
44
+ subject { described_class.set_repeat(15, 2) }
23
45
 
24
46
  include_context '.set_repeat'
25
47
 
26
48
  describe 'alias' do
27
49
  describe '.setRepeat' do
28
50
  it_behaves_like '.set_repeat' do
29
- subject { DXRubySDL::Input.setRepeat(15, 2) }
51
+ subject { described_class.setRepeat(15, 2) }
30
52
  end
31
53
  end
32
54
  end
33
55
  end
34
56
 
35
- shared_context 'push_key' do
57
+ shared_context 'push key_down events' do
36
58
  let(:_keys) { [] }
37
59
 
38
60
  before do
39
- allow(SDL::Key).to receive(:scan)
40
- allow(SDL::Key).to receive(:press?) { |key|
41
- [_keys].flatten.include?(key) ? true : false
42
- }
61
+ _keys.each { |key| push_key_down_event(key) }
62
+ update_input_keys
43
63
  end
44
64
  end
45
65
 
46
66
  describe '.x' do
47
- include_context 'push_key'
67
+ include_context 'push key_down events'
48
68
 
49
69
  subject { described_class.x }
50
70
 
51
71
  context '左カーソルが押されている場合' do
52
- let(:_keys) { SDL::Key::LEFT }
72
+ let(:_keys) { [SDL::Key::LEFT] }
53
73
 
54
74
  it { should eq(-1) }
55
75
  end
56
76
 
57
77
  context '右カーソルが押されている場合' do
58
- let(:_keys) { SDL::Key::RIGHT }
78
+ let(:_keys) { [SDL::Key::RIGHT] }
59
79
 
60
80
  it { should eq(1) }
61
81
  end
@@ -72,18 +92,18 @@ describe DXRubySDL::Input,
72
92
  end
73
93
 
74
94
  describe '.y' do
75
- include_context 'push_key'
95
+ include_context 'push key_down events'
76
96
 
77
97
  subject { described_class.y }
78
98
 
79
99
  context '上カーソルが押されている場合' do
80
- let(:_keys) { SDL::Key::UP }
100
+ let(:_keys) { [SDL::Key::UP] }
81
101
 
82
102
  it { should eq(-1) }
83
103
  end
84
104
 
85
105
  context '下カーソルが押されている場合' do
86
- let(:_keys) { SDL::Key::DOWN }
106
+ let(:_keys) { [SDL::Key::DOWN] }
87
107
 
88
108
  it { should eq(1) }
89
109
  end
@@ -100,7 +120,7 @@ describe DXRubySDL::Input,
100
120
  end
101
121
 
102
122
  shared_context '.pad_down?' do
103
- include_context 'push_key'
123
+ include_context 'push key_down events'
104
124
 
105
125
  context 'Joystickが接続されている場合' do
106
126
  let(:joystick) {
@@ -141,7 +161,7 @@ describe DXRubySDL::Input,
141
161
  end
142
162
 
143
163
  context "#{_key}キーが押されている場合" do
144
- let(:_keys) { SDL::Key.const_get(_key) }
164
+ let(:_keys) { [SDL::Key.const_get(_key)] }
145
165
 
146
166
  it { should be_true }
147
167
  end
@@ -173,7 +193,7 @@ describe DXRubySDL::Input,
173
193
  let(:button_code) { DXRubySDL.const_get(_button) }
174
194
 
175
195
  context "#{_key}キーが押されている場合" do
176
- let(:_keys) { SDL::Key.const_get(_key) }
196
+ let(:_keys) { [SDL::Key.const_get(_key)] }
177
197
 
178
198
  it { should be_true }
179
199
  end
@@ -201,12 +221,7 @@ describe DXRubySDL::Input,
201
221
  end
202
222
 
203
223
  shared_context '.pad_push?' do
204
- include_context 'push_key'
205
-
206
- before do
207
- DXRubySDL::Input.instance_variable_set('@current_key_state', Set.new)
208
- DXRubySDL::Input.instance_variable_set('@last_key_state', Set.new)
209
- end
224
+ include_context 'push key_down events'
210
225
 
211
226
  context 'Joystickが接続されている場合' do
212
227
  let(:joystick) {
@@ -247,7 +262,7 @@ describe DXRubySDL::Input,
247
262
  end
248
263
 
249
264
  context "#{_key}キーが押されている場合" do
250
- let(:_keys) { SDL::Key.const_get(_key) }
265
+ let(:_keys) { [SDL::Key.const_get(_key)] }
251
266
 
252
267
  it { should be_true }
253
268
  end
@@ -279,7 +294,7 @@ describe DXRubySDL::Input,
279
294
  let(:button_code) { DXRubySDL.const_get(_button) }
280
295
 
281
296
  context "#{_key}キーが押されている場合" do
282
- let(:_keys) { SDL::Key.const_get(_key) }
297
+ let(:_keys) { [SDL::Key.const_get(_key)] }
283
298
 
284
299
  it { should be_true }
285
300
  end
@@ -343,10 +358,10 @@ describe DXRubySDL::Input,
343
358
  end
344
359
 
345
360
  shared_context '.key_down?' do
346
- include_context 'push_key'
361
+ include_context 'push key_down events'
347
362
 
348
363
  context 'ESCAPEキーが押されている場合' do
349
- let(:_keys) { SDL::Key::ESCAPE }
364
+ let(:_keys) { [SDL::Key::ESCAPE] }
350
365
  let(:key_code) { DXRubySDL::K_ESCAPE }
351
366
 
352
367
  it { should be_true }
@@ -368,26 +383,20 @@ describe DXRubySDL::Input,
368
383
  end
369
384
 
370
385
  shared_context '.key_push?' do
371
- include_context 'push_key'
386
+ include_context 'push key_down events'
372
387
 
373
388
  subject { described_class.send(method, key_code) }
374
389
 
375
- before do
376
- DXRubySDL::Input.instance_variable_set('@current_key_state', Set.new)
377
- DXRubySDL::Input.instance_variable_set('@last_key_state', Set.new)
378
- end
379
-
380
390
  context 'ESCAPEキーが押されている場合' do
381
- let(:_keys) { SDL::Key::ESCAPE }
391
+ let(:_keys) { [SDL::Key::ESCAPE] }
382
392
  let(:key_code) { DXRubySDL::K_ESCAPE }
383
393
 
384
394
  it { should be_true }
385
395
 
386
396
  context 'キーが押しっぱなしの場合' do
387
397
  before do
388
- last_key_state =
389
- DXRubySDL::Input.instance_variable_get('@last_key_state')
390
- last_key_state.add(*_keys)
398
+ down_keys = described_class.instance_variable_get('@down_keys')
399
+ down_keys.add(key_code)
391
400
  end
392
401
 
393
402
  it { should be_false }
@@ -395,7 +404,7 @@ describe DXRubySDL::Input,
395
404
 
396
405
  context 'キーが押しっぱなしだが、' \
397
406
  'Input.key_push?をメインループで毎回処理しない場合' do
398
- it '最初の1回以外は全てfalseを返す' do
407
+ specify '最初の1回以外は全てfalseを返す' do
399
408
  begin
400
409
  first = true
401
410
  i = 0
@@ -528,8 +537,8 @@ describe DXRubySDL::Input,
528
537
  shared_context '.mouse_push?' do
529
538
  context '直前にマウスのボタンを押していない場合' do
530
539
  before do
531
- DXRubySDL::Input.instance_variable_set('@last_mouse_state',
532
- [0, 0, false, false, false])
540
+ described_class.instance_variable_set('@last_mouse_state',
541
+ [0, 0, false, false, false])
533
542
  end
534
543
 
535
544
  context 'マウスの左ボタンを押している場合' do
@@ -610,8 +619,8 @@ describe DXRubySDL::Input,
610
619
 
611
620
  context '直前にマウスのボタンを全て押していた場合' do
612
621
  before do
613
- DXRubySDL::Input.instance_variable_set('@last_mouse_state',
614
- [0, 0, true, true, true])
622
+ described_class.instance_variable_set('@last_mouse_state',
623
+ [0, 0, true, true, true])
615
624
  end
616
625
 
617
626
  context 'マウスの左ボタンを押している場合' do
@@ -704,4 +713,34 @@ describe DXRubySDL::Input,
704
713
  end
705
714
  end
706
715
  end
716
+
717
+ describe '.keys' do
718
+ subject {
719
+ update_input_keys
720
+ described_class.keys
721
+ }
722
+
723
+ context 'スペースキー、上カーソルキー、Aキーを押している場合' do
724
+ before do
725
+ push_key_down_event(SDL::Key::SPACE)
726
+ push_key_down_event(SDL::Key::UP)
727
+ push_key_down_event(SDL::Key::DOWN)
728
+ push_key_up_event(SDL::Key::DOWN)
729
+ push_key_down_event(SDL::Key::A)
730
+ end
731
+
732
+ it {
733
+ should include(DXRubySDL::K_SPACE, DXRubySDL::K_UP, DXRubySDL::K_A)
734
+ }
735
+ end
736
+
737
+ context '下カーソルキーを押したあとに離した場合' do
738
+ before do
739
+ push_key_down_event(SDL::Key::DOWN)
740
+ push_key_up_event(SDL::Key::DOWN)
741
+ end
742
+
743
+ it { should be_empty }
744
+ end
745
+ end
707
746
  end
File without changes