Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
timriley committed Dec 2, 2015
0 parents commit becec49
Show file tree
Hide file tree
Showing 15 changed files with 334 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/.yardoc
/doc
/coverage
/spec/examples.txt
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--require spec_helper
37 changes: 37 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
AllCops:
Exclude:
- spec/**/*.rb
Metrics/LineLength:
Max: 140
Style/AndOr:
Enabled: false
Style/BlockDelimiters:
EnforcedStyle: semantic
IgnoredMethods:
# Defaults:
- lambda
- proc
- it
# Ours:
- MatchEitherResult
- match
- specify
Style/Lambda:
Enabled: false
Style/LambdaCall:
Enabled: false
Style/MultilineBlockChain:
Enabled: false
Style/SignalException:
Enabled: false
Style/SpaceInsideHashLiteralBraces:
EnforcedStyle: no_space
Style/StabbyLambdaParentheses:
Enabled: false
Style/StringLiterals:
EnforcedStyle: double_quotes

# Specific exclusions
Style/MethodName:
Exclude:
- lib/either_result_matcher.rb
1 change: 1 addition & 0 deletions .yardopts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--markup=markdown
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source "https://rubygems.org"

# Gem dependencies are specified in either_result_matcher.gemspec
gemspec
62 changes: 62 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
PATH
remote: .
specs:
either_result_matcher (0.1.0)
kleisli

GEM
remote: https://rubygems.org/
specs:
ast (2.1.0)
astrolabe (1.3.1)
parser (~> 2.2)
diff-lcs (1.2.5)
docile (1.1.5)
json (1.8.3)
kleisli (0.2.6)
parser (2.2.3.0)
ast (>= 1.1, < 3.0)
powerpack (0.1.1)
rainbow (2.0.0)
rake (10.4.2)
rspec (3.3.0)
rspec-core (~> 3.3.0)
rspec-expectations (~> 3.3.0)
rspec-mocks (~> 3.3.0)
rspec-core (3.3.2)
rspec-support (~> 3.3.0)
rspec-expectations (3.3.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.3.0)
rspec-mocks (3.3.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.3.0)
rspec-support (3.3.0)
rubocop (0.34.2)
astrolabe (~> 1.3)
parser (>= 2.2.2.5, < 3.0)
powerpack (~> 0.1)
rainbow (>= 1.99.1, < 3.0)
ruby-progressbar (~> 1.4)
ruby-progressbar (1.7.5)
simplecov (0.10.0)
docile (~> 1.1.0)
json (~> 1.8)
simplecov-html (~> 0.10.0)
simplecov-html (0.10.0)
yard (0.8.7.6)

PLATFORMS
ruby

DEPENDENCIES
bundler (~> 1.10)
either_result_matcher!
rake (~> 10.4.2)
rspec (~> 3.3.0)
rubocop (~> 0.34.2)
simplecov (~> 0.10.0)
yard

BUNDLED WITH
1.10.6
9 changes: 9 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License (MIT)

Copyright © 2015 [Icelab](http://icelab.com.au/).

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Either Result Matcher

## Usage

```ruby
result = Right("some result")

MatchEitherResult(result) do |m|
m.success do |v|
"Success: #{v}"
end

m.failure do |v|
"Failure: #{v}"
end
end
```
12 changes: 12 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require "bundler/gem_tasks"

require "rspec/core/rake_task"
RSpec::Core::RakeTask.new

require "rubocop/rake_task"
RuboCop::RakeTask.new

task default: :ci

desc "Run the test suite"
task ci: %w(rubocop spec)
26 changes: 26 additions & 0 deletions either_result_matcher.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
lib = File.expand_path("../lib", __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require "either_result_matcher/version"

Gem::Specification.new do |spec|
spec.name = "either_result_matcher"
spec.version = EitherResultMatcher::VERSION
spec.authors = ["Tim Riley"]
spec.email = ["[email protected]"]
spec.license = "MIT"

spec.summary = "Expressive, all-in-one match API for Kleisli Either monads"
spec.homepage = "https://github.com/icelab/either_result_matcher"

spec.files = Dir["README.md", "LICENSE.md", "Gemfile*", "Rakefile", "lib/**/*", "spec/**/*"]
spec.require_paths = ["lib"]

spec.add_runtime_dependency "kleisli"

spec.add_development_dependency "bundler", "~> 1.10"
spec.add_development_dependency "rake", "~> 10.4.2"
spec.add_development_dependency "rspec", "~> 3.3.0"
spec.add_development_dependency "rubocop", "~> 0.34.2"
spec.add_development_dependency "simplecov", "~> 0.10.0"
spec.add_development_dependency "yard"
end
5 changes: 5 additions & 0 deletions lib/either_result_matcher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require "either_result_matcher/matcher"

def MatchEitherResult(result, &block)
block.call(EitherResultMatcher::Matcher.new(result))
end
22 changes: 22 additions & 0 deletions lib/either_result_matcher/matcher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require "kleisli"

module EitherResultMatcher
class Matcher
attr_reader :result
attr_reader :output

def initialize(result)
@result = result
end

def success(&block)
return output unless result.is_a?(Kleisli::Either::Right)
@output = block.call(result.value)
end

def failure(&block)
return output unless result.is_a?(Kleisli::Either::Left)
@output = block.call(result.value)
end
end
end
3 changes: 3 additions & 0 deletions lib/either_result_matcher/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module EitherResultMatcher
VERSION = "0.1.0".freeze
end
31 changes: 31 additions & 0 deletions spec/integration/match_either_result_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require "kleisli"

RSpec.describe "MatchEitherResult" do
subject(:match) {
MatchEitherResult(result) do |m|
m.success do |v|
"Matched success: #{v}"
end

m.failure do |v|
"Matched failure: #{v}"
end
end
}

context "successful result" do
let(:result) { Right("a success") }

it "matches on success" do
expect(match).to eq "Matched success: a success"
end
end

context "failed result" do
let(:result) { Left("a failure") }

it "matches on failure" do
expect(match).to eq "Matched failure: a failure"
end
end
end
99 changes: 99 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
require "simplecov"
SimpleCov.minimum_coverage 100
SimpleCov.start do
add_filter "/spec/"
end

require "either_result_matcher"

# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec files by default. This means that files in spec/support that end
# in _spec.rb will both be required and run as specs, causing the specs to be
# run twice. It is recommended that you do not name files matching this glob to
# end with _spec.rb. You can configure this pattern with the --pattern
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
#
# The following line is provided for convenience purposes. It has the downside
# of increasing the boot-up time by auto-requiring all files in the support
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
Dir[File.join(File.dirname(__FILE__), "support/**/*.rb")].each do |f| require f end

# This file was generated by the `rspec --init` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# The generated `.rspec` file contains `--require spec_helper` which will cause
# this file to always be loaded, without a need to explicitly require it in any
# files.
#
# Given that it is always loaded, you are encouraged to keep this file as
# light-weight as possible. Requiring heavyweight dependencies from this file
# will add to the boot time of your test suite on EVERY test run, even for an
# individual file that may not need all of that loaded. Instead, consider making
# a separate helper file that requires the additional dependencies and performs
# the additional setup, and require it from the spec files that actually need
# it.
#
# The `.rspec` file also contains a few flags that are not defaults but that
# users commonly want.
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
# rspec-expectations config goes here. You can use an alternate
# assertion/expectation library such as wrong or the stdlib/minitest
# assertions if you prefer.
config.expect_with :rspec do |expectations|
# This option will default to `true` in RSpec 4. It makes the `description`
# and `failure_message` of custom matchers include text for helper methods
# defined using `chain`, e.g.:
# be_bigger_than(2).and_smaller_than(4).description
# # => "be bigger than 2 and smaller than 4"
# ...rather than:
# # => "be bigger than 2"
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end

# rspec-mocks config goes here. You can use an alternate test double
# library (such as bogus or mocha) by changing the `mock_with` option here.
config.mock_with :rspec do |mocks|
# Prevents you from mocking or stubbing a method that does not exist on
# a real object. This is generally recommended, and will default to
# `true` in RSpec 4.
mocks.verify_partial_doubles = true
end

# Allows RSpec to persist some state between runs in order to support
# the `--only-failures` and `--next-failure` CLI options. We recommend
# you configure your source control system to ignore this file.
config.example_status_persistence_file_path = "spec/examples.txt"

# Limits the available syntax to the non-monkey patched syntax that is
# recommended.
config.disable_monkey_patching!

# This setting enables warnings. It's recommended, but in some cases may
# be too noisy due to issues in dependencies.
config.warnings = true

# Many RSpec users commonly either run the entire suite or an individual
# file, and it's useful to allow more verbose output when running an
# individual spec file.
if config.files_to_run.one?
# Use the documentation formatter for detailed output,
# unless a formatter has already been configured
# (e.g. via a command-line flag).
config.default_formatter = "doc"
end

# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = :random

# Seed global randomization in this process using the `--seed` CLI option.
# Setting this allows you to use `--seed` to deterministically reproduce
# test failures related to randomization by passing the same `--seed` value
# as the one that triggered the failure.
Kernel.srand config.seed
end

0 comments on commit becec49

Please sign in to comment.