Skip to content

Commit

Permalink
CWM::Dialog: #title=nil, fewer public methods, tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
mvidner committed Jun 22, 2017
1 parent 09bb403 commit 7b22790
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 22 deletions.
51 changes: 29 additions & 22 deletions library/cwm/src/lib/cwm/dialog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ class Dialog
include Yast::I18n
include Yast::UIShortcuts

# @return [String,nil] Set a title, or keep the existing title
abstract_method :title
# @return [String,nil] The dialog title. `nil`: keep the existing title.
def title
nil
end

# @return [CWM::WidgetTerm]
abstract_method :contents
Expand All @@ -30,30 +32,12 @@ def self.run(*args)
# @return [Symbol]
def run
if should_open_dialog?
wizard_create_dialog { run_assuming_open }
wizard_create_dialog { cwm_show }
else
run_assuming_open
cwm_show
end
end

def wizard_create_dialog(&block)
Yast::Wizard.CreateDialog
block.call
ensure
Yast::Wizard.CloseDialog
end

def run_assuming_open
Yast::CWM.show(
contents,
caption: title,
back_button: replace_true(back_button, Yast::Label.BackButton),
abort_button: replace_true(abort_button, Yast::Label.AbortButton),
next_button: replace_true(next_button, Yast::Label.NextButton),
skip_store_for: skip_store_for
)
end

def should_open_dialog?
!Yast::Wizard.IsWizardDialog
end
Expand All @@ -80,12 +64,35 @@ def next_button
end

# @return [Array<Symbol>]
# Events for which `store` won't be called, see {CWMClass#show}
def skip_store_for
[]
end

private

# Create a wizard dialog, run the *block*, ensure the dialog is closed.
# @param block
def wizard_create_dialog(&block)
Yast::Wizard.CreateDialog
block.call
ensure
Yast::Wizard.CloseDialog
end

# Call {CWMClass#show} with appropriate arguments
# @return [Symbol] wizard sequencer symbol
def cwm_show
Yast::CWM.show(
contents,
caption: title,
back_button: replace_true(back_button, Yast::Label.BackButton),
abort_button: replace_true(abort_button, Yast::Label.AbortButton),
next_button: replace_true(next_button, Yast::Label.NextButton),
skip_store_for: skip_store_for
)
end

def replace_true(value, replacement)
if value == true
replacement
Expand Down
1 change: 1 addition & 0 deletions library/cwm/test/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ TESTS = \
abstract_widget_test.rb \
common_widgets_test.rb \
custom_widget_test.rb \
dialog_test.rb \
pager_test.rb \
replace_point_test.rb \
table_test.rb \
Expand Down
48 changes: 48 additions & 0 deletions library/cwm/test/dialog_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#! /usr/bin/env rspec
require_relative "test_helper"

require "cwm/dialog"
require "cwm/rspec"

describe "CWM::Dialog" do
class TestCWMDialog < CWM::Dialog
def contents
VBox()
end
end
subject { TestCWMDialog.new }

include_examples "CWM::Dialog"

describe ".run" do
it "opens a dialog when needed, and calls CWM#show" do
expect(Yast::Wizard).to receive(:IsWizardDialog).and_return(false)
expect(Yast::Wizard).to receive(:CreateDialog)
expect(Yast::Wizard).to receive(:CloseDialog)
expect(Yast::CWM).to receive(:show).and_return(:launch)

expect(subject.class.run).to eq(:launch)
end

it "does not open a dialog when not needed, and calls CWM#show" do
expect(Yast::Wizard).to receive(:IsWizardDialog).and_return(true)
expect(Yast::Wizard).to_not receive(:CreateDialog)
expect(Yast::Wizard).to_not receive(:CloseDialog)
expect(Yast::CWM).to receive(:show).and_return(:launch)

expect(subject.class.run).to eq(:launch)
end
end

describe "#replace_true" do
it "replaces true" do
expect(subject.send(:replace_true, true, :new)).to eq :new
end

it "does not replace others" do
expect(subject.send(:replace_true, nil, :new)).to eq nil
expect(subject.send(:replace_true, false, :new)).to eq false
expect(subject.send(:replace_true, :old, :new)).to eq :old
end
end
end
Empty file modified library/cwm/test/replace_point_test.rb
100644 → 100755
Empty file.

0 comments on commit 7b22790

Please sign in to comment.