Refactor specs with factory helper methods
This commit is contained in:
parent
2cad5da620
commit
47bc9c9da2
@ -2,7 +2,11 @@ require 'headless'
|
|||||||
|
|
||||||
require 'uh/wm'
|
require 'uh/wm'
|
||||||
|
|
||||||
|
Dir['spec/support/**/*.rb'].map { |e| require e.gsub 'spec/', '' }
|
||||||
|
|
||||||
RSpec.configure do |config|
|
RSpec.configure do |config|
|
||||||
|
config.include Factories
|
||||||
|
|
||||||
config.expect_with :rspec do |expectations|
|
config.expect_with :rspec do |expectations|
|
||||||
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
||||||
end
|
end
|
||||||
|
23
spec/support/factories.rb
Normal file
23
spec/support/factories.rb
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
module Factories
|
||||||
|
def build_geo x = 0, y = 0, width = 640, height = 480
|
||||||
|
Uh::Geo.new(x, y, width, height)
|
||||||
|
end
|
||||||
|
|
||||||
|
def mock_event type = :xany, **options
|
||||||
|
double 'event', type: type, **options
|
||||||
|
end
|
||||||
|
|
||||||
|
def mock_event_key_press key, modifier_mask
|
||||||
|
mock_event :key_press,
|
||||||
|
key: 'f',
|
||||||
|
modifier_mask: modifier_mask
|
||||||
|
end
|
||||||
|
|
||||||
|
def mock_window override_redirect: false
|
||||||
|
instance_spy Uh::Window, 'window',
|
||||||
|
to_s: 'wid',
|
||||||
|
name: 'wname',
|
||||||
|
wclass: 'wclass',
|
||||||
|
override_redirect?: override_redirect
|
||||||
|
end
|
||||||
|
end
|
@ -1,11 +1,8 @@
|
|||||||
module Uh
|
module Uh
|
||||||
module WM
|
module WM
|
||||||
RSpec.describe Client do
|
RSpec.describe Client do
|
||||||
let(:geo) { Geo.new(0, 0, 640, 480) }
|
let(:window) { mock_window }
|
||||||
let(:window) do
|
let(:geo) { build_geo }
|
||||||
instance_spy Window, 'window', to_s: 'wid',
|
|
||||||
name: 'wname', wclass: 'wclass'
|
|
||||||
end
|
|
||||||
subject(:client) { described_class.new window, geo }
|
subject(:client) { described_class.new window, geo }
|
||||||
|
|
||||||
it 'is not visible' do
|
it 'is not visible' do
|
||||||
|
@ -2,7 +2,7 @@ module Uh
|
|||||||
module WM
|
module WM
|
||||||
RSpec.describe Manager do
|
RSpec.describe Manager do
|
||||||
let(:block) { proc { } }
|
let(:block) { proc { } }
|
||||||
let(:window) { instance_spy Window, override_redirect?: false }
|
let(:window) { mock_window }
|
||||||
let(:events) { Dispatcher.new }
|
let(:events) { Dispatcher.new }
|
||||||
let(:modifier) { :mod1 }
|
let(:modifier) { :mod1 }
|
||||||
let(:display) { Display.new }
|
let(:display) { Display.new }
|
||||||
@ -107,13 +107,13 @@ module Uh
|
|||||||
context 'with new window' do
|
context 'with new window' do
|
||||||
it 'sends a configure event to the window with a default geo' do
|
it 'sends a configure event to the window with a default geo' do
|
||||||
expect(window)
|
expect(window)
|
||||||
.to receive(:configure_event).with(Geo.new(0, 0, 320, 240))
|
.to receive(:configure_event).with(build_geo 0, 0, 320, 240)
|
||||||
manager.configure window
|
manager.configure window
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when :configure event returns a geo' do
|
context 'when :configure event returns a geo' do
|
||||||
it 'sends a configure event with geo returned by event' do
|
it 'sends a configure event with geo returned by event' do
|
||||||
geo = Geo.new(0, 0, 42, 42)
|
geo = build_geo 0, 0, 42, 42
|
||||||
events.on(:configure) { geo }
|
events.on(:configure) { geo }
|
||||||
expect(window).to receive(:configure_event).with geo
|
expect(window).to receive(:configure_event).with geo
|
||||||
manager.configure window
|
manager.configure window
|
||||||
@ -249,7 +249,7 @@ module Uh
|
|||||||
end
|
end
|
||||||
|
|
||||||
describe '#handle_pending_events' do
|
describe '#handle_pending_events' do
|
||||||
let(:event) { double 'event' }
|
let(:event) { mock_event }
|
||||||
|
|
||||||
context 'when an event is pending on display' do
|
context 'when an event is pending on display' do
|
||||||
before do
|
before do
|
||||||
@ -277,7 +277,7 @@ module Uh
|
|||||||
end
|
end
|
||||||
|
|
||||||
describe '#handle' do
|
describe '#handle' do
|
||||||
let(:event) { double 'event', type: :any }
|
let(:event) { mock_event }
|
||||||
|
|
||||||
it 'emits :xevent event with the X event' do
|
it 'emits :xevent event with the X event' do
|
||||||
events.on :xevent, &block
|
events.on :xevent, &block
|
||||||
@ -285,13 +285,8 @@ module Uh
|
|||||||
end
|
end
|
||||||
|
|
||||||
context 'when key_press event is given' do
|
context 'when key_press event is given' do
|
||||||
let(:mod_mask) { KEY_MODIFIERS[modifier] }
|
let(:mod_mask) { KEY_MODIFIERS[modifier] }
|
||||||
let(:event) do
|
let(:event) { mock_event_key_press 'f', mod_mask }
|
||||||
double 'event',
|
|
||||||
type: :key_press,
|
|
||||||
key: 'f',
|
|
||||||
modifier_mask: mod_mask
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'emits :key event with the corresponding key' do
|
it 'emits :key event with the corresponding key' do
|
||||||
events.on(:key, :f) { throw :key_press_code }
|
events.on(:key, :f) { throw :key_press_code }
|
||||||
@ -309,9 +304,7 @@ module Uh
|
|||||||
end
|
end
|
||||||
|
|
||||||
context 'when configure request event is given' do
|
context 'when configure request event is given' do
|
||||||
let(:event) do
|
let(:event) { mock_event :configure_request, window: :window }
|
||||||
double 'event', type: :configure_request, window: :window
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'configures the event window' do
|
it 'configures the event window' do
|
||||||
expect(manager).to receive(:configure).with :window
|
expect(manager).to receive(:configure).with :window
|
||||||
@ -320,7 +313,7 @@ module Uh
|
|||||||
end
|
end
|
||||||
|
|
||||||
context 'when destroy_notify event is given' do
|
context 'when destroy_notify event is given' do
|
||||||
let(:event) { double 'event', type: :destroy_notify, window: :window }
|
let(:event) { mock_event :destroy_notify, window: :window }
|
||||||
|
|
||||||
it 'destroy the event window' do
|
it 'destroy the event window' do
|
||||||
expect(manager).to receive(:destroy).with :window
|
expect(manager).to receive(:destroy).with :window
|
||||||
@ -329,7 +322,7 @@ module Uh
|
|||||||
end
|
end
|
||||||
|
|
||||||
context 'when map_request event is given' do
|
context 'when map_request event is given' do
|
||||||
let(:event) { double 'event', type: :map_request, window: :window }
|
let(:event) { mock_event :map_request, window: :window }
|
||||||
|
|
||||||
it 'maps the event window' do
|
it 'maps the event window' do
|
||||||
expect(manager).to receive(:map).with :window
|
expect(manager).to receive(:map).with :window
|
||||||
@ -338,7 +331,7 @@ module Uh
|
|||||||
end
|
end
|
||||||
|
|
||||||
context 'when unmap_notify event is given' do
|
context 'when unmap_notify event is given' do
|
||||||
let(:event) { double 'event', type: :unmap_notify, window: :window }
|
let(:event) { mock_event :unmap_notify, window: :window }
|
||||||
|
|
||||||
it 'unmap the event window' do
|
it 'unmap the event window' do
|
||||||
expect(manager).to receive(:unmap).with :window
|
expect(manager).to receive(:unmap).with :window
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
SomeLayout = Class.new do
|
SomeLayout = Class.new do
|
||||||
|
include Factories
|
||||||
|
|
||||||
define_method(:register) { |*args| }
|
define_method(:register) { |*args| }
|
||||||
define_method(:suggest_geo) { Uh::Geo.new(0, 0, 42, 42) }
|
define_method(:suggest_geo) { build_geo 0, 0, 42, 42 }
|
||||||
define_method(:<<) { |*args| }
|
define_method(:<<) { |*args| }
|
||||||
define_method(:remove) { |*args| }
|
define_method(:remove) { |*args| }
|
||||||
end
|
end
|
||||||
@ -82,7 +84,7 @@ module Uh
|
|||||||
it 'registers for :configure event' do
|
it 'registers for :configure event' do
|
||||||
runner.register_event_hooks
|
runner.register_event_hooks
|
||||||
expect(runner.events.emit :configure, args: :window)
|
expect(runner.events.emit :configure, args: :window)
|
||||||
.to eq Geo.new(0, 0, 42, 42)
|
.to eq build_geo 0, 0, 42, 42
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'registers for :manage event' do
|
it 'registers for :manage event' do
|
||||||
|
Loading…
x
Reference in New Issue
Block a user