Aruba gem - Matcher

Aruba の Matcher 周りの機能についてまとめます。

## <i class='fa fa-flask'></i> サンプル
### <i class='fa fa-check'></i> include_an_object
RSpecの [`include` matcher](http://www.relishapp.com/rspec/rspec-expectations/v/3-5/docs/built-in-matchers/include-matcher) とはどう違うのだろう？

```ruby
require 'spec_helper'

RSpec.describe [2, 4, 'a', 11] do
  it { is_expected.to include_an_object( be_odd ) }
  it { is_expected.to include_an_object( be_an(String) ) }
  it { is_expected.not_to include_an_object( be_between(0, 1).inclusive ) }
  it { is_expected.to include_an_object( be_between(0, 2).inclusive ) }
  it { is_expected.not_to include_an_object( be_between(0, 2).exclusive ) }
  it { is_expected.to include_an_object( be_between(0, 3).exclusive ) }
  it { is_expected.to include_an_object( be < 10 ) }
  it { is_expected.to include_an_object( eq 'a' ) }
  it { is_expected.not_to include_an_object( eq 'b' ) }
end
```

* 実行結果

```sh
$ rspec -fd -c spec/matchers/include_an_object_spec.rb

[2, 4, "a", 11]
  should include an object be odd
  should include an object be a kind of String
  should not include an object be between 0 and 1 (inclusive)
  should include an object be between 0 and 2 (inclusive)
  should not include an object be between 0 and 2 (exclusive)
  should include an object be between 0 and 3 (exclusive)
  should include an object be < 10
  should include an object eq "a"
  should not include an object eq "b"

Finished in 0.00641 seconds (files took 0.22814 seconds to load)
9 examples, 0 failures
```

### <i class='fa fa-check'></i> have_sub_directory

```ruby
require 'spec_helper'

RSpec.describe 'have_sub_directory', type: :aruba do
  context 'single dir' do
    let(:directory) { 'dir' }
    let(:sub_directory) { 'sub' }

    before(:each) { create_directory(File.join(directory, sub_directory)) }
    before(:each) { create_directory(File.join(directory, 'sub2')) }

    it { expect(directory).to have_sub_directory sub_directory }
  end

  context 'multi dir' do
    let(:directories) { %w(dir1 dir2) }
    let(:sub_directory) { 'sub' }

    before(:each) do
      directories.each { |d| create_directory(File.join(d, sub_directory)) }
    end

    it { expect(directories).to all have_sub_directory sub_directory }
  end
end
```

* 実行結果

```sh
$ rspec -fd -c spec/matchers/have_sub_directory_spec.rb

have_sub_directory
  single dir
    should have sub directory "sub"
  multi dir
    should all have sub directory "sub"

Finished in 0.0108 seconds (files took 0.22751 seconds to load)
2 examples, 0 failures
```

### <i class='fa fa-check'></i> have_file_content

```ruby
require 'spec_helper'

RSpec.describe 'have_file_content', type: :aruba do
  let(:file) { 'hoge.md' }
  let(:content) { 'hoge hige hage' }

  before(:each) { write_file(file, content) }

  it { expect(file).to have_file_content content }
  it { expect(file).to have_file_content /h.ge/ }
  it { expect(file).to have_file_content end_with('ge') }
end
```

* 実行結果

```sh
$ rspec -fd -c spec/matchers/have_file_content_spec.rb

have_file_content
  should have file content: "hoge hige hage"
  should have file content: /h.ge/
  should have file content: end with "ge"

Finished in 0.01209 seconds (files took 0.22188 seconds to load)
3 examples, 0 failures
```

### <i class='fa fa-check'></i> be_an_existing_executable

```ruby
require 'spec_helper'

RSpec.describe 'be_an_existing_executable', type: :aruba do
  context 'executable' do
    let(:file) { 'file1.txt' }
    before(:each) { touch(file) }
    before(:each) { chmod(0755, file) }

    it { expect(file).to be_an_existing_executable }
  end

  context 'can not executable' do
    let(:file) { 'file2.txt' }
    before(:each) { touch(file) }
    before(:each) { chmod(0444, file) }

    it { expect(file).not_to be_an_existing_executable }
  end
end
```

* 実行結果

```sh
$ rspec -fd -c spec/matchers/be_an_existing_executable_spec.rb

be_an_existing_executable
  executable
    should be an existing executable
  can not executable
    should not be an existing executable

Finished in 0.00713 seconds (files took 0.22001 seconds to load)
2 examples, 0 failures
```

### <i class='fa fa-check'></i> be_an_existing_file

```ruby
require 'spec_helper'

RSpec.describe 'be_an_existing_file', :type => :aruba do
  context 'exist' do
    let(:file) { 'hoge.txt' }
    before(:each) { touch(file) }
    it { expect(file).to be_an_existing_file }
  end

  context 'not exist' do
    let(:file) { 'hoge.txt' }
    it { expect(file).not_to be_an_existing_file }
  end
end
```

* 実行結果

```sh
rspec -fd -c spec/matchers/be_an_existing_file_spec.rb

be_an_existing_file
 exist
   should be an existing file
 not exist
   should not be an existing file

Finished in 0.0073 seconds (files took 0.21953 seconds to load)
2 examples, 0 failures
```

### <i class='fa fa-check'></i> have_file_size

```ruby
require 'spec_helper'

RSpec.describe 'have_file_size', :type => :aruba do
  let(:file) { 'hoge.txt' }
  let(:size) { 11 }

  before(:each) { write_fixed_size_file(file, size) }

  it { expect(file).to have_file_size size }
end
```

* 実行結果

```sh
$ rspec -fd -c spec/matchers/have_file_size_spec.rb

have_file_size
 should have file size 11

Finished in 0.00449 seconds (files took 0.22108 seconds to load)
1 example, 0 failures
```

### <i class='fa fa-check'></i> timeout

```ruby
require 'spec_helper'

RSpec.describe 'timeout', type: :aruba do
  context 'timeout' do
    before(:each) { aruba.config.exit_timeout = 1 }
    before(:each) { run('sleep 2') }

    it { expect(last_command_started).to run_too_long }
  end

  context 'in time' do
    before(:each) { aruba.config.exit_timeout = 4 }
    before(:each) { run('sleep 2') }

    it { expect(last_command_started).not_to run_too_long }
  end
end
```

* 実行結果

```sh
$ rspec -fd -c spec/matchers/timeout_spec.rb

timeout
 timeout
   should run too long
 in time
   should not run too long

Finished in 3.22 seconds (files took 0.21704 seconds to load)
2 examples, 0 failures
```

### <i class='fa fa-check'></i> be_an_existing_path

```ruby
require 'spec_helper'

RSpec.describe 'be_an_existing_path', :type => :aruba do
  context 'exist' do
    let(:path) { 'hoge.txt' }
    before(:each) { touch(path) }
    it { expect(path).to be_an_existing_path }
  end

  context 'not exist' do
    let(:path) { 'hoge.txt' }
    it { expect(path).not_to be_an_existing_path }
  end
end
```

* 実行結果

```sh
$ rspec -fd -c spec/matchers/be_an_existing_path_spec.rb

be_an_existing_path
  exist
    should be an existing path
  not exist
    should not be an existing path

Finished in 0.00685 seconds (files took 0.21496 seconds to load)
2 examples, 0 failures
```

### <i class='fa fa-check'></i> have_permissions

```ruby
require 'spec_helper'

RSpec.describe 'have_permissions', :type => :aruba do
  let(:file) { 'hoge.txt' }
  let(:permissions) { 0544 }

  before(:each) { touch(file) }
  before(:each) { chmod(permissions, file) }

  it { expect(file).to have_permissions permissions }
  it { expect(file).not_to have_permissions 777 }
end
```

* 実行結果

```sh
$ rspec -fd -c spec/matchers/have_permissions_spec.rb

have_permissions
  should have permissions 356
  should not have permissions 777

Finished in 0.00732 seconds (files took 0.20833 seconds to load)
2 examples, 0 failures
```

### <i class='fa fa-check'></i> be_an_absolute_path

```ruby
require 'spec_helper'

RSpec.describe 'be_an_absolute_path', :type => :aruba do
  context 'absolute' do
    let(:path) { '/path/to/hoge.txt' }
    it { expect(path).to be_an_absolute_path }
  end

  context 'relative' do
    let(:path) { 'hoge.txt' }
    it { expect(path).not_to be_an_absolute_path }
  end
end
```

* 実行結果

```sh
$ rspec -fd -c spec/matchers/be_an_absolute_path_spec.rb

be_an_absolute_path
  absolute
    should be an absolute path
  relative
    should not be an absolute path

Finished in 0.0083 seconds (files took 0.23092 seconds to load)
2 examples, 0 failures
```

## <i class="fa fa-book" style="font-size:1em;"></i> 外部資料
* <i class='fa fa-external-link'></i> [`include_an_object` Matcher - Aruba - docs](http://www.relishapp.com/cucumber/aruba/v/0-11-0/docs/collection/include-an-object-matcher)
* <i class='fa fa-external-link'></i> [Check if directory has given sub directories - Aruba - docs](http://www.relishapp.com/cucumber/aruba/v/0-11-0/docs/directory/check-if-directory-has-given-sub-directories)
* <i class='fa fa-external-link'></i> [Check if file has content - Aruba - docs](http://www.relishapp.com/cucumber/aruba/v/0-11-0/docs/file/check-if-file-has-content)
* <i class='fa fa-external-link'></i> [Check if path exists and is an executable file - Aruba - docs](http://www.relishapp.com/cucumber/aruba/v/0-11-0/docs/file/check-if-path-exists-and-is-an-executable-file)
* <i class='fa fa-external-link'></i> [Check if path exists and is file - Aruba - docs](http://www.relishapp.com/cucumber/aruba/v/0-11-0/docs/file/check-if-path-exists-and-is-file)
* <i class='fa fa-external-link'></i> [Check if path has size - Aruba - docs](http://www.relishapp.com/cucumber/aruba/v/0-11-0/docs/file/check-if-path-has-size)
* <i class='fa fa-external-link'></i> [Check if a timeout occured during command execution - Aruba - docs](http://www.relishapp.com/cucumber/aruba/v/0-11-0/docs/matchers/check-if-a-timeout-occured-during-command-execution)
* <i class='fa fa-external-link'></i> [Check if path exists - Aruba - docs](http://www.relishapp.com/cucumber/aruba/v/0-11-0/docs/path/check-if-path-exists)
* <i class='fa fa-external-link'></i> [Check if path has permissions in filesystem - Aruba - docs](http://www.relishapp.com/cucumber/aruba/v/0-11-0/docs/path/check-if-path-has-permissions-in-filesystem)
* <i class='fa fa-external-link'></i> [Check if path is absolute - Aruba - docs](http://www.relishapp.com/cucumber/aruba/v/0-11-0/docs/path/check-if-path-is-absolute)