Skip to content

Commit

Permalink
Add and use rubocop-rspec
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Nicholaides committed Mar 26, 2024
1 parent ffa4bea commit 63269b0
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 54 deletions.
7 changes: 7 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require: rubocop-rspec

AllCops:
TargetRubyVersion: 3.1
NewCops: enable
Expand All @@ -23,3 +25,8 @@ Style/NumericPredicate: { Enabled: false }

Layout/LineLength:
Max: 120


RSpec/NestedGroups: { Enabled: false }
RSpec/ExampleLength: { Enabled: false }
RSpec/MultipleExpectations: { Enabled: false }
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ gem "retest", "~> 1.11"
gem "yard", "~> 0.9.34"

gem "webrick", "~> 1.8"

gem "rubocop-rspec", "~> 2.27"
9 changes: 9 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ GEM
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.30.0)
parser (>= 3.2.1.0)
rubocop-capybara (2.20.0)
rubocop (~> 1.41)
rubocop-factory_bot (2.25.1)
rubocop (~> 1.41)
rubocop-rspec (2.27.1)
rubocop (~> 1.40)
rubocop-capybara (~> 2.17)
rubocop-factory_bot (~> 2.22)
ruby-progressbar (1.13.0)
string-similarity (2.1.0)
tty-option (0.3.0)
Expand All @@ -74,6 +82,7 @@ DEPENDENCIES
retest (~> 1.11)
rspec (~> 3.0)
rubocop (~> 1.21)
rubocop-rspec (~> 2.27)
webrick (~> 1.8)
yard (~> 0.9.34)

Expand Down
15 changes: 5 additions & 10 deletions spec/cecil/code_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,8 @@
end

describe "an object that responds to #<<" do
let :buffer do
buff = BasicObject.new
def buff.<<(value) = @appended_value = value
def buff.value = @appended_value
buff
end

let(:buffer_contents) { buffer.value }
let(:buffer) { [] }
let(:buffer_contents) { buffer.join }

it_behaves_like "a code receiver"
end
Expand All @@ -70,7 +64,7 @@ def buff.value = @appended_value
described_class.generate_string do
`hello world`
end
end.to_not output.to_stdout
end.not_to output.to_stdout
end
end

Expand Down Expand Up @@ -266,6 +260,7 @@ class Baby extends ~my Papa
CODE
end
end

describe "turning off the starting Regexp" do
def_syntax do
def placeholder_start_re = //
Expand Down Expand Up @@ -335,7 +330,7 @@ class Baby extends Papa
end

it "raises an error immediately when called so that the error message shows you which line it's on" do
Cecil::Code.generate_string do
described_class.generate_string do
expect do
`def my_func():
pass`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
require "cecil/indentation"
require_relative "../../helpers/reindent_helpers"
require_relative "../helpers/reindent_helpers"

RSpec.describe Cecil::Indentation do
include described_class
extend described_class

describe ".reindent" do
describe ".reindent" do # rubocop:disable RSpec/EmptyExampleGroup
def self.reindents(template_str, desc_more = " ", **kwargs)
describe "given#{desc_more}\"#{template_str}\"" do
template = IndentationTemplate.new(template_str)
Expand Down
4 changes: 2 additions & 2 deletions spec/cecil/lang/rust_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ def inspect(obj)
it "closes curly braces" do
code = described_class.generate_string do
`fn fibonacci() {`[] do
`recurse()`
`do_stuff()`
end
end

expect(code).to eq <<~CODE
fn fibonacci() {
recurse()
do_stuff()
}
CODE
end
Expand Down
16 changes: 13 additions & 3 deletions spec/cecil/lang/typescript_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "cecil/lang/typescript"

RSpec.describe Cecil::Lang::TypeScript do
RSpec.describe Cecil::Lang::TypeScript do # rubocop:disable RSpec/FilePath,RSpec/SpecFilePathFormat
it "indents with 2 spaces" do
code = described_class.generate_string do
`function $fn() {`["fibonacci"] do
Expand All @@ -18,13 +18,13 @@
it "closes curly braces" do
code = described_class.generate_string do
`function $fn() {`["fibonacci"] do
`recurse()`
`do_stuff()`
end
end

expect(code).to eq <<~CODE
function fibonacci() {
recurse()
do_stuff()
}
CODE
end
Expand Down Expand Up @@ -92,12 +92,15 @@
it "joins items with ' | '" do
expect(t(%w[a b c])).to eq "a | b | c"
end

it "converts inputs to strings" do
expect(t(["a", "b", :c])).to eq "a | b | c"
end

it "removes nils" do
expect(t(["a", nil, "b", :c])).to eq "a | b | c"
end

it "accepts single item" do
expect(t("a")).to eq "a"
end
Expand All @@ -115,12 +118,15 @@
it "joins items with ', '" do
expect(l(%w[a b c])).to eq "a, b, c"
end

it "converts inputs to strings" do
expect(l(["a", "b", :c])).to eq "a, b, c"
end

it "removes nils" do
expect(l(["a", nil, "b", :c])).to eq "a, b, c"
end

it "accepts single item" do
expect(l("a")).to eq "a"
end
Expand All @@ -144,15 +150,19 @@
it "converts input to string without quotes" do
expect(s("hello world")).to eq "hello world"
end

it "escapes double quotes" do
expect(s('hello "world"')).to eq 'hello \\"world\\"'
end

it "escapes single quotes" do
expect(s("hello 'world'")).to eq "hello \\'world\\'"
end

it "escapes backticks" do
expect(s("hello `world`")).to eq "hello \\`world\\`"
end

it "dollar signs" do
expect(s("hello ${world}")).to eq "hello \\${world}"
end
Expand Down
2 changes: 1 addition & 1 deletion spec/cecil/text_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require "cecil/placeholder"

RSpec.describe Cecil::Text do
include Cecil::Text
include described_class

describe ".scan_for_re_matches" do
it "returns matches" do
Expand Down
71 changes: 35 additions & 36 deletions spec/cecil_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
RSpec.describe Cecil do
RSpec.describe Cecil do # rubocop:disable RSpec/MultipleDescribes
it "has a version number" do
expect(Cecil::VERSION).not_to be nil
expect(Cecil::VERSION).not_to be_nil
end

describe "nesting generation calls" do
Expand Down Expand Up @@ -159,11 +159,11 @@ def blank?(str) = str =~ /^\s*$/
end

it "errors on positional and named placeholders" do
cecil do
`line { $code } [ $another ]`["my code", another: "more code"]
end

raises(/expects/i)
expect do
code do
`line { $code } [ $another ]`["my code", another: "more code"]
end
end.to raise_error(/expects/i)
end

it "errors on unmatched placeholders given positional arguments" do
Expand All @@ -174,7 +174,7 @@ def blank?(str) = str =~ /^\s*$/
end.to raise_error(/mismatch/i)
end

it "errors on unmatched placeholders given keyword arguments " do
it "errors on unmatched placeholders given keyword arguments" do
expect do
code do
`line { $code } [ $another ]`[code: "my code"]
Expand All @@ -190,31 +190,31 @@ def blank?(str) = str =~ /^\s*$/
end.to raise_error(/mismatch/i)
end

it "errors on unmatched placeholder values given keyword arguments " do
it "errors on unmatched placeholder values given extra keyword arguments" do
expect do
code do
`line { $code }`[code: "my code", more: "some extra"]
end
end.to raise_error(/mismatch/i)
end

it "errors on unmatched placeholder values given keyword arguments " do
it "errors on unmatched placeholder values given keyword arguments with different names" do
expect do
code do
`line { $code }`[wrong_name: "my code"]
end
end.to raise_error(/mismatch/i)
end

it "errors on unmatched placeholder values given keyword arguments " do
it "errors on unmatched placeholder values given no arguments" do
expect do
code do
`line { $code }`[]
end
end.to raise_error(/mismatch/i)
end

it "errors on unmatched placeholder values given keyword arguments " do
it "errors on neglect to call #[]" do
expect do
code do
`line { $code }`
Expand Down Expand Up @@ -330,17 +330,16 @@ class BasicCurlySquareFactory
CODE
end
end

describe "indentation" do
it "indents blocks" do
cecil do
expect_code do
`start outer`
`inner {`[] do
`content`
end
`end outer`
end

outputs <<~CODE
end.to eq <<~CODE
start outer
inner {
content
Expand Down Expand Up @@ -631,21 +630,19 @@ class C {}
end
end

describe "customizing configuration" do
it "works" do
require "cecil/lang/typescript"
expect(
Cecil::Lang::TypeScript.generate_string do
`function($args) {`[l %w[a b c]] do
`doStuff()`
end
it "can have configuaration customized" do
require "cecil/lang/typescript"
expect(
Cecil::Lang::TypeScript.generate_string do
`function($args) {`[l %w[a b c]] do
`doStuff()`
end
).to eq <<~CODE
function(a, b, c) {
doStuff()
}
CODE
end
end
).to eq <<~CODE
function(a, b, c) {
doStuff()
}
CODE
end

describe "ambiguous indentation" do
Expand All @@ -672,20 +669,22 @@ class C {}

def object_method = "OBJECT METHOD"

RSpec.describe Cecil do
describe "block scope" do
RSpec.describe Cecil, "block scope method access" do # rubocop:disable RSpec/DescribeMethod
describe "global scope" do
it "has access to methods defined in the global scope" do
expect(code do
expect_code do
src object_method
end.strip).to eq object_method
end.to eq "OBJECT METHOD\n"
end
end

describe "local scope" do
def receiver_method = "RECEIVER METHOD"

it "has access to methods defined in the block's scope" do
expect(code do
expect_code do
src receiver_method
end.strip).to eq receiver_method
end.to eq "RECEIVER METHOD\n"
end
end
end

0 comments on commit 63269b0

Please sign in to comment.