From b65e989c3891a68ae2f64dfdc1e3fe3fc1d19187 Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Fri, 10 Apr 2015 06:11:33 +0000 Subject: [PATCH 1/5] Accept -l LAYOUT CLI option to set the layout --- features/steps/output_steps.rb | 1 + lib/uh/wm/cli.rb | 4 ++++ lib/uh/wm/env.rb | 2 +- spec/uh/wm/cli_spec.rb | 9 +++++++++ spec/uh/wm/env_spec.rb | 4 ++++ 5 files changed, 19 insertions(+), 1 deletion(-) diff --git a/features/steps/output_steps.rb b/features/steps/output_steps.rb index 330452a..6d23231 100644 --- a/features/steps/output_steps.rb +++ b/features/steps/output_steps.rb @@ -24,6 +24,7 @@ options: -v, --version enable verbose mode -d, --debug enable debug mode -r, --require PATH require ruby feature + -l, --layout LAYOUT specify layout eoh end diff --git a/lib/uh/wm/cli.rb b/lib/uh/wm/cli.rb index cc61bce..c52f073 100644 --- a/lib/uh/wm/cli.rb +++ b/lib/uh/wm/cli.rb @@ -63,6 +63,10 @@ module Uh require feature @env.log "Loaded `#{feature}' ruby feature" end + + opts.on '-l', '--layout LAYOUT', 'specify layout' do |layout| + @env.layout_class = self.class.const_get layout.to_sym + end end end end diff --git a/lib/uh/wm/env.rb b/lib/uh/wm/env.rb index 8c18592..fded52c 100644 --- a/lib/uh/wm/env.rb +++ b/lib/uh/wm/env.rb @@ -12,7 +12,7 @@ module Uh def_delegator :@output, :print attr_reader :output - attr_accessor :verbose, :debug + attr_accessor :verbose, :debug, :layout_class def initialize output @output = output diff --git a/spec/uh/wm/cli_spec.rb b/spec/uh/wm/cli_spec.rb index 86b4d2e..24cb07c 100644 --- a/spec/uh/wm/cli_spec.rb +++ b/spec/uh/wm/cli_spec.rb @@ -130,6 +130,15 @@ module Uh end end + context 'with layout option' do + let(:arguments) { %w[-l Object] } + + it 'assigns the layout class in the env' do + cli.parse_arguments! + expect(cli.env.layout_class).to eq Object + end + end + context 'with invalid option' do let(:arguments) { %w[--unknown-option] } diff --git a/spec/uh/wm/env_spec.rb b/spec/uh/wm/env_spec.rb index ea41de3..b80334b 100644 --- a/spec/uh/wm/env_spec.rb +++ b/spec/uh/wm/env_spec.rb @@ -13,6 +13,10 @@ module Uh expect(env).not_to be_debug end + it 'has no layout_class set' do + expect(env.layout_class).not_to be + end + describe '#verbose?' do context 'when verbose mode is disabled' do before { env.verbose = false } From a848e6b9369a71dde8c281161a47976670db7983 Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Fri, 10 Apr 2015 07:11:29 +0000 Subject: [PATCH 2/5] Instantiate and assign a Layout in the Runner --- lib/uh/wm/runner.rb | 4 +++- spec/uh/wm/runner_spec.rb | 19 +++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/uh/wm/runner.rb b/lib/uh/wm/runner.rb index cf16f54..bbeba0c 100644 --- a/lib/uh/wm/runner.rb +++ b/lib/uh/wm/runner.rb @@ -10,12 +10,14 @@ module Uh end end - attr_reader :env, :events, :manager + attr_reader :env, :events, :manager, :layout def initialize env, manager: nil, stopped: false + raise ArgumentError, 'missing env layout class' unless env.layout_class @env = env @events = Dispatcher.new @manager = manager || Manager.new(@events) + @layout = @env.layout_class.new @stopped = stopped end diff --git a/spec/uh/wm/runner_spec.rb b/spec/uh/wm/runner_spec.rb index 8a9a500..15cbbfa 100644 --- a/spec/uh/wm/runner_spec.rb +++ b/spec/uh/wm/runner_spec.rb @@ -1,8 +1,12 @@ +SomeLayout = Class.new + module Uh module WM RSpec.describe Runner do - let(:env) { Env.new(StringIO.new) } - subject(:runner) { described_class.new env } + let(:env) do + Env.new(StringIO.new).tap { |o| o.layout_class = SomeLayout } + end + subject(:runner) { described_class.new env } describe '.run' do subject(:run) { described_class.run env, stopped: true } @@ -49,6 +53,17 @@ module Uh it 'is not stopped' do expect(runner).not_to be_stopped end + + it 'assigns a new layout instance' do + expect(runner.layout).to be_an_instance_of SomeLayout + end + + context 'when the env has no layout set' do + before { env.layout_class = nil } + it 'raises an ArgumentError' do + expect { runner }.to raise_error WM::ArgumentError + end + end end describe '#stopped?' do From 616b14eae29f815155870ffa41933807a25ca3d3 Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Fri, 10 Apr 2015 07:53:08 +0000 Subject: [PATCH 3/5] Register layout event hooks --- lib/uh/wm/runner.rb | 7 +++++++ spec/uh/wm/runner_spec.rb | 10 +++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/uh/wm/runner.rb b/lib/uh/wm/runner.rb index bbeba0c..f54a6cd 100644 --- a/lib/uh/wm/runner.rb +++ b/lib/uh/wm/runner.rb @@ -31,6 +31,7 @@ module Uh def register_event_hooks register_manager_hooks + register_layout_event_hooks register_key_bindings_hooks end @@ -55,6 +56,12 @@ module Uh end end + def register_layout_event_hooks + @events.on(:connected) do |display| + @layout.register display + end + end + def register_key_bindings_hooks @events.on(:key, :q) { stop! } end diff --git a/spec/uh/wm/runner_spec.rb b/spec/uh/wm/runner_spec.rb index 15cbbfa..0d533fd 100644 --- a/spec/uh/wm/runner_spec.rb +++ b/spec/uh/wm/runner_spec.rb @@ -1,4 +1,6 @@ -SomeLayout = Class.new +SomeLayout = Class.new do + define_method(:register) { |*args| } +end module Uh module WM @@ -97,6 +99,12 @@ module Uh runner.events.emit :connected end + it 'registers layout hook for :connected event' do + runner.register_event_hooks + expect(runner.layout).to receive(:register).with :display + runner.events.emit :connected, args: :display + end + it 'registers key bindings event hooks' do runner.register_event_hooks expect(runner.events[:key, :q]).not_to be_empty From b9b2bff85387e5828c4b0a8dd83358a196bfc3ee Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Fri, 10 Apr 2015 17:37:19 +0000 Subject: [PATCH 4/5] Use Layout from uh-layout as default --- lib/uh/wm/env.rb | 9 +++++++++ lib/uh/wm/runner.rb | 9 +++++---- spec/uh/wm/env_spec.rb | 18 ++++++++++++++++++ spec/uh/wm/runner_spec.rb | 13 +------------ uh-wm.gemspec | 3 ++- 5 files changed, 35 insertions(+), 17 deletions(-) diff --git a/lib/uh/wm/env.rb b/lib/uh/wm/env.rb index fded52c..029ab97 100644 --- a/lib/uh/wm/env.rb +++ b/lib/uh/wm/env.rb @@ -26,6 +26,15 @@ module Uh !!@debug end + def layout + @layout ||= if layout_class + layout_class.new + else + require 'uh/layout' + ::Uh::Layout.new + end + end + def logger @logger ||= Logger.new(@output).tap do |o| o.level = debug? ? LOGGER_LEVEL_DEBUG : diff --git a/lib/uh/wm/runner.rb b/lib/uh/wm/runner.rb index f54a6cd..24c0675 100644 --- a/lib/uh/wm/runner.rb +++ b/lib/uh/wm/runner.rb @@ -10,14 +10,15 @@ module Uh end end - attr_reader :env, :events, :manager, :layout + extend Forwardable + def_delegator :@env, :layout + + attr_reader :env, :events, :manager def initialize env, manager: nil, stopped: false - raise ArgumentError, 'missing env layout class' unless env.layout_class @env = env @events = Dispatcher.new @manager = manager || Manager.new(@events) - @layout = @env.layout_class.new @stopped = stopped end @@ -58,7 +59,7 @@ module Uh def register_layout_event_hooks @events.on(:connected) do |display| - @layout.register display + layout.register display end end diff --git a/spec/uh/wm/env_spec.rb b/spec/uh/wm/env_spec.rb index b80334b..77cf3f3 100644 --- a/spec/uh/wm/env_spec.rb +++ b/spec/uh/wm/env_spec.rb @@ -53,6 +53,24 @@ module Uh end end + describe '#layout' do + context 'when a layout class is set' do + let(:some_layout) { Class.new } + + before { env.layout_class = some_layout } + + it 'returns a new instance of this layout class' do + expect(env.layout).to be_an_instance_of some_layout + end + end + + context 'when a layout class is not set' do + it 'returns an instance of the default layout' do + expect(env.layout).to be_an_instance_of ::Uh::Layout + end + end + end + describe '#logger' do it 'returns a logger' do expect(env.logger).to be_a Logger diff --git a/spec/uh/wm/runner_spec.rb b/spec/uh/wm/runner_spec.rb index 0d533fd..c56d94b 100644 --- a/spec/uh/wm/runner_spec.rb +++ b/spec/uh/wm/runner_spec.rb @@ -55,17 +55,6 @@ module Uh it 'is not stopped' do expect(runner).not_to be_stopped end - - it 'assigns a new layout instance' do - expect(runner.layout).to be_an_instance_of SomeLayout - end - - context 'when the env has no layout set' do - before { env.layout_class = nil } - it 'raises an ArgumentError' do - expect { runner }.to raise_error WM::ArgumentError - end - end end describe '#stopped?' do @@ -101,7 +90,7 @@ module Uh it 'registers layout hook for :connected event' do runner.register_event_hooks - expect(runner.layout).to receive(:register).with :display + expect(env.layout).to receive(:register).with :display runner.events.emit :connected, args: :display end diff --git a/uh-wm.gemspec b/uh-wm.gemspec index 48a48ad..ed98051 100644 --- a/uh-wm.gemspec +++ b/uh-wm.gemspec @@ -14,7 +14,8 @@ Gem::Specification.new do |s| s.test_files = s.files.grep /\A(spec|features)\// s.executables = s.files.grep(/\Abin\//) { |f| File.basename(f) } - s.add_dependency 'uh', '2.0.0.pre' + s.add_dependency 'uh', '2.0.0.pre' + s.add_dependency 'uh-layout', '0.2.0.pre' s.add_development_dependency 'aruba', '~> 0.6' s.add_development_dependency 'cucumber', '~> 2.0' From de3fca228e8753ced2cd94570aba53e2800b09b8 Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Fri, 10 Apr 2015 06:12:08 +0000 Subject: [PATCH 5/5] Register a Layout with display information --- features/layout/registration.feature | 13 +++++++++++++ features/steps/filesystem_steps.rb | 3 +++ features/steps/output_steps.rb | 8 ++++++++ 3 files changed, 24 insertions(+) create mode 100644 features/layout/registration.feature create mode 100644 features/steps/filesystem_steps.rb diff --git a/features/layout/registration.feature b/features/layout/registration.feature new file mode 100644 index 0000000..5a71c60 --- /dev/null +++ b/features/layout/registration.feature @@ -0,0 +1,13 @@ +Feature: layout registration + + Scenario: sends the #register message to the layout with the display + Given a file named layout.rb with: + """ + class Layout + def register display + puts display + end + end + """ + When I run uhwm with option -r./layout -l Layout + Then the current output must contain current display diff --git a/features/steps/filesystem_steps.rb b/features/steps/filesystem_steps.rb new file mode 100644 index 0000000..a951c6f --- /dev/null +++ b/features/steps/filesystem_steps.rb @@ -0,0 +1,3 @@ +Given /^a file named ([^ ]+) with:$/ do |path, content| + write_file path, content +end diff --git a/features/steps/output_steps.rb b/features/steps/output_steps.rb index 6d23231..46fc2d0 100644 --- a/features/steps/output_steps.rb +++ b/features/steps/output_steps.rb @@ -31,3 +31,11 @@ end Then /^the current output must match \/([^\/]+)\/([a-z]*)$/ do |pattern, options| uhwm_wait_output Regexp.new(pattern, options) end + +Then /^the current output must contain:$/ do |content| + uhwm_wait_output content.to_s +end + +Then /^the current output must contain current display$/ do + uhwm_wait_output ENV['DISPLAY'] +end