Skip to content

Commit

Permalink
feat: implement context helper
Browse files Browse the repository at this point in the history
  • Loading branch information
masaakiaoyagi committed May 22, 2022
1 parent 1c96ede commit aabae85
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
@@ -1,5 +1,5 @@
AllCops:
TargetRubyVersion: 2.6
TargetRubyVersion: 2.7

Style/StringLiterals:
Enabled: true
Expand Down
2 changes: 1 addition & 1 deletion LICENSE.txt
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2022 TODO: Write your name
Copyright (c) 2022 Masaaki Aoyagi <masaaki.aoyagi@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
4 changes: 2 additions & 2 deletions README.md
@@ -1,4 +1,4 @@
# Rspec::ContextHelper
# RSpec::ContextHelper

Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/rspec/context_helper`. To experiment with that code, run `bin/console` for an interactive prompt.

Expand Down Expand Up @@ -34,4 +34,4 @@ The gem is available as open source under the terms of the [MIT License](https:/

## Code of Conduct

Everyone interacting in the Rspec::ContextHelper project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/rspec-context_helper/blob/master/CODE_OF_CONDUCT.md).
Everyone interacting in the RSpec::ContextHelper project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/rspec-context_helper/blob/master/CODE_OF_CONDUCT.md).
44 changes: 41 additions & 3 deletions lib/rspec/context_helper.rb
@@ -1,10 +1,48 @@
# frozen_string_literal: true

require "rspec"

require_relative "context_helper/version"

module Rspec
module RSpec
module ContextHelper
class Error < StandardError; end
# Your code goes here...
def context_with(description = nil, metadata: nil, shared: nil, **values, &block)
description ||= to_sentence(values.map { |k, v| "#{k} is #{v.inspect}" }).then do |desc|
desc.empty? ? desc : "when #{desc}"
end

context description do
values.each do |key, value|
let(key) do
value.is_a?(::Proc) ? instance_exec(&value) : value
end
end
instance_exec(&block)
end
end

def example_with(description = nil, metadata: nil, shared: nil, **values, &block)
context_with(description, metadata: metadata, shared: shared, **values) do
example { instance_exec(&block) }
end
end

private def to_sentence(words)
case words.size
when 0
""
when 1
words[0]
when 2
words.join(" and ")
else
*rest, last = words
[rest.join(", "), last].join(" and ")
end
end
end
end

RSpec.configure do |config|
config.extend RSpec::ContextHelper
end
2 changes: 1 addition & 1 deletion lib/rspec/context_helper/version.rb
@@ -1,6 +1,6 @@
# frozen_string_literal: true

module Rspec
module RSpec
module ContextHelper
VERSION = "0.1.0"
end
Expand Down
24 changes: 11 additions & 13 deletions rspec-context_helper.gemspec
Expand Up @@ -4,21 +4,21 @@ require_relative "lib/rspec/context_helper/version"

Gem::Specification.new do |spec|
spec.name = "rspec-context_helper"
spec.version = Rspec::ContextHelper::VERSION
spec.authors = ["TODO: Write your name"]
spec.email = ["TODO: Write your email address"]
spec.version = RSpec::ContextHelper::VERSION
spec.authors = ["Masaaki Aoyagi"]
spec.email = ["masaaki.aoyagi@gmail.com"]

spec.summary = "TODO: Write a short summary, because RubyGems requires one."
spec.description = "TODO: Write a longer description or delete this line."
spec.homepage = "TODO: Put your gem's website or public repo URL here."
spec.summary = "context helpers for RSpec"
spec.description = "context helpers for RSpec"
spec.homepage = "https://github.com/masaakiaoyagi/rspec-context_helper.rb"
spec.license = "MIT"
spec.required_ruby_version = ">= 2.6.0"
spec.required_ruby_version = ">= 2.7.0"

spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"

spec.metadata["homepage_uri"] = spec.homepage
spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
spec.metadata["source_code_uri"] = "https://github.com/masaakiaoyagi/rspec-context_helper.rb"
spec.metadata["changelog_uri"] = "https://github.com/masaakiaoyagi/rspec-context_helper.rb"

# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
Expand All @@ -31,9 +31,7 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

# Uncomment to register a new dependency of your gem
# spec.add_dependency "example-gem", "~> 1.0"
spec.add_dependency "rspec", "~> 3.11"

# For more information and examples about making a new gem, check out our
# guide at: https://bundler.io/guides/creating_gem.html
spec.add_dependency "pry-byebug"
end
2 changes: 1 addition & 1 deletion sig/rspec/context_helper.rbs
@@ -1,4 +1,4 @@
module Rspec
module RSpec
module ContextHelper
VERSION: String
# See the writing guide of rbs: https://github.com/ruby/rbs#guides
Expand Down
86 changes: 81 additions & 5 deletions spec/rspec/context_helper_spec.rb
@@ -1,11 +1,87 @@
# frozen_string_literal: true

RSpec.describe Rspec::ContextHelper do
it "has a version number" do
expect(Rspec::ContextHelper::VERSION).not_to be nil
RSpec.describe RSpec::ContextHelper do
let(:description) { self.class.description }

describe ".context_with" do
describe "description" do
context_with do
example { expect(description).to eq("") }
end

context_with "message" do
example { expect(description).to eq("message") }
end

context_with a: "1" do
example { expect(description).to eq(%(when a is "1")) }
end

context_with a: "1", b: "2" do
example { expect(description).to eq(%(when a is "1" and b is "2")) }
end

context_with a: "1", b: "2", c: "3" do
example { expect(description).to eq(%(when a is "1", b is "2" and c is "3")) }
end
end

context_with do
example {}
end

context_with "message" do
example {}
end

context_with a: "1" do
example { expect(a).to eq("1") }
end

context_with "message", a: -> { b }, b: "2", c: -> { -> {} } do
example { expect(a).to eq("2") }
example { expect(b).to eq("2") }
example { expect(c).to be_an_instance_of(Proc) }
end
end

it "does something useful" do
expect(false).to eq(true)
describe ".example_with" do
describe "description" do
example_with do
expect(description).to eq("")
end

example_with "message" do
expect(description).to eq("message")
end

example_with a: "1" do
expect(description).to eq(%(when a is "1"))
end

example_with a: "1", b: "2" do
expect(description).to eq(%(when a is "1" and b is "2"))
end

example_with a: "1", b: "2", c: "3" do
expect(description).to eq(%(when a is "1", b is "2" and c is "3"))
end
end

example_with do
end

example_with "message" do
end

example_with a: "1" do
expect(a).to eq("1")
end

example_with "message", a: -> { b }, b: "2", c: -> { -> {} } do
expect(a).to eq("2")
expect(b).to eq("2")
expect(c).to be_an_instance_of(Proc)
end
end
end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require "pry"

require "rspec/context_helper"

RSpec.configure do |config|
Expand Down

0 comments on commit aabae85

Please sign in to comment.