spec/lib/config/searcher_spec.rb in fusuma-2.0.4 vs spec/lib/config/searcher_spec.rb in fusuma-2.0.5

- old
+ new

@@ -5,83 +5,165 @@ require './lib/fusuma/config/searcher' # spec for Config module Fusuma RSpec.describe Config::Searcher do - let(:keymap) do - { - 'swipe' => { - 3 => { - 'left' => { 'command' => 'alt+Left' }, - 'right' => { 'command' => 'alt+Right' } - }, - 4 => { - 'left' => { 'command' => 'super+Left' }, - 'right' => { 'command' => 'super+Right' } - } - }, - 'pinch' => { - 'in' => { 'command' => 'ctrl+plus' }, - 'out' => { 'command' => 'ctrl+minus' } - } - } - end + around do |example| + ConfigHelper.load_config_yml = <<~CONFIG + swipe: + 3: + left: + command: 'alt+Left' + right: + command: 'alt+Right' + 4: + left: + command: 'super+Left' + right: + command: 'super+Right' + pinch: + in: + command: 'ctrl+plus' + out: + command: 'ctrl+minus' + CONFIG - let(:keymap_without_finger) do - { - 'swipe' => { - 'left' => { 'command' => 'alt+Left' } - } - } - end + example.run - describe '.custom_path=' do - before { Singleton.__init__(Config) } - it 'should reload keymap file' do - keymap = Config.instance.keymap - Config.custom_path = './spec/lib/dummy_config.yml' - custom_keymap = Config.instance.keymap - expect(keymap).not_to eq custom_keymap - end + ConfigHelper.clear_config_yml end describe '.search' do let(:index) { nil } - subject { Config::Searcher.new.search(index, location: keymap.deep_symbolize_keys) } + let(:location) { Config.instance.keymap[0] } + let(:search) { Config::Searcher.new.search(index, location: location) } context 'index correct order' do let(:index) { Config::Index.new %w[pinch in command] } - it { is_expected.to eq 'ctrl+plus' } + it { expect(Config::Searcher.new.search(index, location: location)).to eq 'ctrl+plus' } end - context 'index include skippable key' do - let(:index) do - Config::Index.new [ - Config::Index::Key.new('pinch'), - Config::Index::Key.new(2, skippable: true), - Config::Index::Key.new('out'), - Config::Index::Key.new('command') - ] - end - it { expect(Config::Searcher.skip { subject }).to eq 'ctrl+minus' } + context 'index incorrect order' do + let(:index) { Config::Index.new %w[in pinch 2 command] } + it { expect(Config::Searcher.new.search(index, location: location)).not_to eq 'ctrl+plus' } end - context 'index include skippable key at first' do - let(:index) do - Config::Index.new [ - Config::Index::Key.new(:hoge, skippable: true), - Config::Index::Key.new(:fuga, skippable: true), - Config::Index::Key.new('pinch'), - Config::Index::Key.new('in'), - Config::Index::Key.new(:piyo, skippable: true), - Config::Index::Key.new('command') - ] + context 'with Skip condtions' do + context 'when index includes skippable key' do + let(:index) do + Config::Index.new [ + Config::Index::Key.new('pinch'), + Config::Index::Key.new(2, skippable: true), + Config::Index::Key.new('out'), + Config::Index::Key.new('command') + ] + end + it 'detects ctrl+minus with skip' do + condition, value = Config::Searcher.find_condition do + Config::Searcher.new.search(index, location: location) + end + expect([condition, value]).to eq([:skip, 'ctrl+minus']) + end end - it { expect(Config::Searcher.skip { subject }).to eq 'ctrl+plus' } - end - context 'index incorrect order' do - let(:index) { Config::Index.new %w[in pinch 2 command] } - it { is_expected.not_to eq 'ctrl+plus' } + context 'when index includes skippable key at first' do + let(:index) do + Config::Index.new [ + Config::Index::Key.new(:hoge, skippable: true), + Config::Index::Key.new(:fuga, skippable: true), + Config::Index::Key.new('pinch'), + Config::Index::Key.new('in'), + Config::Index::Key.new(:piyo, skippable: true), + Config::Index::Key.new('command') + ] + end + it 'detects ctrl+plus with skip' do + condition, value = Config::Searcher.find_condition do + Config::Searcher.new.search(index, location: location) + end + expect([condition, value]).to eq([:skip, 'ctrl+plus']) + end + end + + context 'with begin/update/end' do + around do |example| + ConfigHelper.load_config_yml = <<~CONFIG + swipe: + 3: + begin: + command: 'echo begin' + update: + command: 'echo update' + end: + command: 'echo end' + keypress: + LEFTCTRL: + command: 'echo end+ctrl' + CONFIG + + example.run + + ConfigHelper.clear_config_yml + end + + context 'without keypress' do + let(:index) do + Config::Index.new [ + Config::Index::Key.new(:swipe), + Config::Index::Key.new(3), + Config::Index::Key.new('left', skippable: true), + Config::Index::Key.new('end'), + Config::Index::Key.new('command') + ] + end + + it 'detects with skip' do + condition, value = Config::Searcher.find_condition do + Config::Searcher.new.search(index, location: location) + end + expect([condition, value]).to eq([:skip, 'echo end']) + end + end + context 'with keypress' do + context 'with valid key existing in config.yml' do + let(:index) do + Config::Index.new [ + Config::Index::Key.new(:swipe), + Config::Index::Key.new(3), + Config::Index::Key.new('left', skippable: true), + Config::Index::Key.new('end'), + Config::Index::Key.new('keypress', skippable: true), + Config::Index::Key.new('LEFTCTRL', skippable: true), + Config::Index::Key.new('command') + ] + end + it 'detects end+ctrl with skip' do + condition, value = Config::Searcher.find_condition do + Config::Searcher.new.search(index, location: location) + end + expect([condition, value]).to eq([:skip, 'echo end+ctrl']) + end + end + context 'with non-existing key not existing in config.yml' do + let(:index) do + Config::Index.new [ + Config::Index::Key.new(:swipe), + Config::Index::Key.new(3), + Config::Index::Key.new('up', skippable: true), + Config::Index::Key.new('end'), + Config::Index::Key.new('keypress', skippable: true), + Config::Index::Key.new('LEFTSHIFT', skippable: true), # Invalid key + Config::Index::Key.new('command') + ] + end + it 'detects end with skip (fallback to no keypress)' do + condition, value = Config::Searcher.find_condition do + Config::Searcher.new.search(index, location: location) + end + expect([condition, value]).to eq([:skip, 'echo end']) + end + end + end + end end end describe 'private_method: :cache' do it 'should cache command' do