Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1c96ede
commit aabae85
Showing
9 changed files
with
141 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
AllCops: | ||
TargetRubyVersion: 2.6 | ||
TargetRubyVersion: 2.7 | ||
|
||
Style/StringLiterals: | ||
Enabled: true | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
module Rspec | ||
module RSpec | ||
module ContextHelper | ||
VERSION = "0.1.0" | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
require "pry" | ||
|
||
require "rspec/context_helper" | ||
|
||
RSpec.configure do |config| | ||
|