Skip to content

Commit 47b229a

Browse files
committed
Refactor specs
- Create `spec_helper` - Disable random spec order to allow spec to pass - Update RuboCop - Use `described_class` (autofix `RSpec/DescribedClass` offenses) - Remove unused variables to fix the following warnings: ``` spec/cached_asset_file_spec.rb:51: warning: assigned but unused variable - known_document_0 spec/transformation_pipeline/transformations/transformation_spec.rb:19: warning: assigned but unused variable - svg ``` - Add SimpleCov for code coverage
1 parent 2d8c66c commit 47b229a

30 files changed

+220
-119
lines changed

.gitignore

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22
*.rbc
33
.bundle
44
.config
5+
.ruby-*
56
.yardoc
6-
Gemfile.lock
7-
InstalledFiles
8-
_yardoc
97
coverage
108
doc/
9+
Gemfile.lock
10+
InstalledFiles
1111
lib/bundler/man
1212
pkg
1313
rdoc
14+
spec/examples.txt
1415
spec/reports
1516
test/tmp
1617
test/version_tmp
1718
tmp
18-
.ruby-*
19+
_yardoc

.rspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--require spec_helper

.rubocop_todo.yml

Lines changed: 2 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gemfile

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ gem "pry"
1010
gem "rake"
1111
gem "rspec"
1212

13-
gem "rubocop", "1.66.1"
14-
gem "rubocop-packaging", "0.5.2"
15-
gem "rubocop-performance", "1.22.1"
16-
gem "rubocop-rake", "0.6.0"
17-
gem "rubocop-rspec", "3.0.5"
13+
gem "rubocop", "1.69.0", require: false
14+
gem "rubocop-packaging", "0.5.2", require: false
15+
gem "rubocop-performance", "1.23.0", require: false
16+
gem "rubocop-rake", "0.6.0", require: false
17+
gem "rubocop-rspec", "3.2.0", require: false
18+
19+
gem "simplecov", require: false

spec/asset_file_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,27 @@
33
require "inline_svg/finds_asset_paths"
44
require "inline_svg/asset_file"
55

6-
describe InlineSvg::AssetFile do
6+
RSpec.describe InlineSvg::AssetFile do
77
it "reads data from a file, after qualifying a full path" do
88
example_svg_path = File.expand_path(__FILE__, 'files/example.svg')
99
expect(InlineSvg::FindsAssetPaths).to receive(:by_filename).with('some filename').and_return example_svg_path
1010

11-
expect(InlineSvg::AssetFile.named('some filename')).to include('This is a test')
11+
expect(described_class.named('some filename')).to include('This is a test')
1212
end
1313

1414
it "complains when the file cannot be read" do
1515
allow(InlineSvg::FindsAssetPaths).to receive(:by_filename).and_return('/this/path/does/not/exist')
1616

1717
expect do
18-
InlineSvg::AssetFile.named('some missing file')
18+
described_class.named('some missing file')
1919
end.to raise_error InlineSvg::AssetFile::FileNotFound
2020
end
2121

2222
it "complains when the file path was not found" do
2323
allow(InlineSvg::FindsAssetPaths).to receive(:by_filename).and_return(nil)
2424

2525
expect do
26-
InlineSvg::AssetFile.named('some missing file')
26+
described_class.named('some missing file')
2727
end.to raise_error InlineSvg::AssetFile::FileNotFound
2828
end
2929
end

spec/cached_asset_file_spec.rb

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
require 'pathname'
44
require "inline_svg"
55

6-
describe InlineSvg::CachedAssetFile do
6+
RSpec.describe InlineSvg::CachedAssetFile do
77
let(:fixture_path) { Pathname.new(File.expand_path('files/static_assets', __dir__)) }
88

99
it "loads assets under configured paths" do
1010
known_document = File.read(fixture_path.join("assets0", "known-document.svg"))
1111

12-
asset_loader = InlineSvg::CachedAssetFile.new(paths: fixture_path.join("assets0"))
12+
asset_loader = described_class.new(paths: fixture_path.join("assets0"))
1313

1414
expect(asset_loader.named("known-document.svg")).to eq(known_document)
1515
end
1616

1717
it "does not include assets outside of configured paths" do
18-
asset_loader = InlineSvg::CachedAssetFile.new(paths: fixture_path.join("assets0"))
18+
asset_loader = described_class.new(paths: fixture_path.join("assets0"))
1919

2020
expect(fixture_path.join("assets1", "other-document.svg")).to be_file
2121
expect do
@@ -29,7 +29,7 @@
2929

3030
expect(known_document_0).not_to eq(known_document_1)
3131

32-
asset_loader = InlineSvg::CachedAssetFile.new(paths: fixture_path)
32+
asset_loader = described_class.new(paths: fixture_path)
3333

3434
expect(known_document_0).to eq(asset_loader.named("assets0/known-document.svg"))
3535
expect(known_document_1).to eq(asset_loader.named("assets1/known-document.svg"))
@@ -41,17 +41,16 @@
4141

4242
expect(known_document).not_to eq(known_document_2)
4343

44-
asset_loader = InlineSvg::CachedAssetFile.new(paths: fixture_path.join("assets0"), filters: /\.svg/)
44+
asset_loader = described_class.new(paths: fixture_path.join("assets0"), filters: /\.svg/)
4545

4646
expect(asset_loader.named("known-document")).to eq(known_document)
4747
expect(asset_loader.named("known-document-two")).to eq(known_document_2)
4848
end
4949

5050
it "filters wanted files by simple string matching" do
51-
known_document_0 = File.read(fixture_path.join("assets0", "known-document.svg"))
5251
known_document_1 = File.read(fixture_path.join("assets1", "known-document.svg"))
5352

54-
asset_loader = InlineSvg::CachedAssetFile.new(paths: fixture_path, filters: "assets1")
53+
asset_loader = described_class.new(paths: fixture_path, filters: "assets1")
5554

5655
expect do
5756
asset_loader.named("assets0/known-document.svg")
@@ -63,7 +62,7 @@
6362
it "filters wanted files by regex matching" do
6463
known_document_1 = File.read(fixture_path.join("assets1", "known-document.svg"))
6564

66-
asset_loader = InlineSvg::CachedAssetFile.new(paths: fixture_path, filters: ["assets1", /\.svg/])
65+
asset_loader = described_class.new(paths: fixture_path, filters: ["assets1", /\.svg/])
6766

6867
expect do
6968
asset_loader.named("assets1/some-file.txt")

spec/finds_asset_paths_spec.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
require 'pathname'
44
require "inline_svg"
55

6-
describe InlineSvg::FindsAssetPaths do
6+
RSpec.describe InlineSvg::FindsAssetPaths do
77
context "when sprockets finder returns an object which supports only the pathname method" do
88
it "returns fully qualified file paths from Sprockets" do
99
sprockets = double('SprocketsDouble')
@@ -15,7 +15,7 @@
1515
config.asset_finder = sprockets
1616
end
1717

18-
expect(InlineSvg::FindsAssetPaths.by_filename('some-file')).to eq Pathname('/full/path/to/some-file')
18+
expect(described_class.by_filename('some-file')).to eq Pathname('/full/path/to/some-file')
1919
end
2020
end
2121

@@ -30,7 +30,7 @@
3030
config.asset_finder = sprockets
3131
end
3232

33-
expect(InlineSvg::FindsAssetPaths.by_filename('some-file')).to eq Pathname('/full/path/to/some-file')
33+
expect(described_class.by_filename('some-file')).to eq Pathname('/full/path/to/some-file')
3434
end
3535
end
3636

@@ -44,7 +44,7 @@
4444
config.asset_finder = sprockets
4545
end
4646

47-
expect(InlineSvg::FindsAssetPaths.by_filename('some-file')).to be_nil
47+
expect(described_class.by_filename('some-file')).to be_nil
4848
end
4949
end
5050

@@ -59,7 +59,7 @@
5959
config.asset_finder = propshaft
6060
end
6161

62-
expect(InlineSvg::FindsAssetPaths.by_filename('some-file')).to eq Pathname('/full/path/to/some-file')
62+
expect(described_class.by_filename('some-file')).to eq Pathname('/full/path/to/some-file')
6363
end
6464
end
6565

@@ -74,7 +74,7 @@
7474
config.asset_finder = shakapacker
7575
end
7676

77-
expect(InlineSvg::FindsAssetPaths.by_filename('some-file')).to eq Pathname('/full/path/to/some-file')
77+
expect(described_class.by_filename('some-file')).to eq Pathname('/full/path/to/some-file')
7878
end
7979
end
8080

@@ -89,7 +89,7 @@
8989
config.asset_finder = shakapacker
9090
end
9191

92-
expect(InlineSvg::FindsAssetPaths.by_filename('some-file')).to eq Pathname('https://my-fancy-domain.test/full/path/to/some-file')
92+
expect(described_class.by_filename('some-file')).to eq Pathname('https://my-fancy-domain.test/full/path/to/some-file')
9393
end
9494
end
9595
end

spec/helpers/inline_svg_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def transform(doc)
1111
end
1212
end
1313

14-
describe InlineSvg::ActionView::Helpers do
14+
RSpec.describe InlineSvg::ActionView::Helpers do
1515
let(:helper) { (Class.new { include InlineSvg::ActionView::Helpers }).new }
1616

1717
shared_examples "inline_svg helper" do |helper_method:|

spec/id_generator_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
require "inline_svg/id_generator"
44

5-
describe InlineSvg::IdGenerator do
5+
RSpec.describe InlineSvg::IdGenerator do
66
it "generates a hexencoded ID based on a salt and a random value" do
77
randomizer = -> { "some-random-value" }
88

9-
expect(InlineSvg::IdGenerator.generate("some-base", "some-salt", randomness: randomizer))
9+
expect(described_class.generate("some-base", "some-salt", randomness: randomizer))
1010
.to eq("at2c17mkqnvopy36iccxspura7wnreqf")
1111
end
1212
end

spec/inline_svg_spec.rb

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,53 +19,53 @@ class MyCustomAssetFile
1919
def self.named(filename); end
2020
end
2121

22-
describe InlineSvg do
22+
RSpec.describe InlineSvg do
2323
describe "configuration" do
2424
context "when a block is not given" do
2525
it "complains" do
2626
expect do
27-
InlineSvg.configure
27+
described_class.configure
2828
end.to raise_error(InlineSvg::Configuration::Invalid)
2929
end
3030
end
3131

3232
context "asset finder" do
3333
it "allows an asset finder to be assigned" do
3434
sprockets = double('SomethingLikeSprockets', find_asset: 'some asset')
35-
InlineSvg.configure do |config|
35+
described_class.configure do |config|
3636
config.asset_finder = sprockets
3737
end
3838

39-
expect(InlineSvg.configuration.asset_finder).to eq sprockets
39+
expect(described_class.configuration.asset_finder).to eq sprockets
4040
end
4141

4242
it "falls back to StaticAssetFinder when the provided asset finder does not implement #find_asset" do
43-
InlineSvg.configure do |config|
43+
described_class.configure do |config|
4444
config.asset_finder = 'Not a real asset finder'
4545
end
4646

47-
expect(InlineSvg.configuration.asset_finder).to eq InlineSvg::StaticAssetFinder
47+
expect(described_class.configuration.asset_finder).to eq InlineSvg::StaticAssetFinder
4848
end
4949
end
5050

5151
context "configuring a custom asset file" do
5252
it "falls back to the built-in asset file implementation by default" do
53-
expect(InlineSvg.configuration.asset_file).to eq(InlineSvg::AssetFile)
53+
expect(described_class.configuration.asset_file).to eq(InlineSvg::AssetFile)
5454
end
5555

5656
it "adds a collaborator that meets the interface specification" do
57-
InlineSvg.configure do |config|
57+
described_class.configure do |config|
5858
config.asset_file = MyCustomAssetFile
5959
end
6060

61-
expect(InlineSvg.configuration.asset_file).to eq MyCustomAssetFile
61+
expect(described_class.configuration.asset_file).to eq MyCustomAssetFile
6262
end
6363

6464
it "rejects a collaborator that does not conform to the interface spec" do
6565
bad_asset_file = double("bad_asset_file")
6666

6767
expect do
68-
InlineSvg.configure do |config|
68+
described_class.configure do |config|
6969
config.asset_file = bad_asset_file
7070
end
7171
end.to raise_error(InlineSvg::Configuration::Invalid, /asset_file should implement the #named method/)
@@ -75,7 +75,7 @@ def self.named(filename); end
7575
bad_asset_file = double("bad_asset_file", named: nil)
7676

7777
expect do
78-
InlineSvg.configure do |config|
78+
described_class.configure do |config|
7979
config.asset_file = bad_asset_file
8080
end
8181
end.to raise_error(InlineSvg::Configuration::Invalid, /asset_file should implement the #named method with arity 1/)
@@ -84,42 +84,42 @@ def self.named(filename); end
8484

8585
context "configuring the default svg-not-found class" do
8686
it "sets the class name" do
87-
InlineSvg.configure do |config|
87+
described_class.configure do |config|
8888
config.svg_not_found_css_class = 'missing-svg'
8989
end
9090

91-
expect(InlineSvg.configuration.svg_not_found_css_class).to eq 'missing-svg'
91+
expect(described_class.configuration.svg_not_found_css_class).to eq 'missing-svg'
9292
end
9393
end
9494

9595
context "configuring custom transformation" do
9696
it "allows a custom transformation to be added" do
97-
InlineSvg.configure do |config|
97+
described_class.configure do |config|
9898
config.add_custom_transformation(attribute: :my_transform, transform: MyCustomTransform)
9999
end
100100

101-
expect(InlineSvg.configuration.custom_transformations).to eq({ my_transform: { attribute: :my_transform, transform: MyCustomTransform } })
101+
expect(described_class.configuration.custom_transformations).to eq({ my_transform: { attribute: :my_transform, transform: MyCustomTransform } })
102102
end
103103

104104
it "rejects transformations that do not implement .create_with_value" do
105105
expect do
106-
InlineSvg.configure do |config|
106+
described_class.configure do |config|
107107
config.add_custom_transformation(attribute: :irrelevant, transform: MyInvalidCustomTransformKlass)
108108
end
109109
end.to raise_error(InlineSvg::Configuration::Invalid, /#{MyInvalidCustomTransformKlass} should implement the .create_with_value and #transform methods/o)
110110
end
111111

112112
it "rejects transformations that does not implement #transform" do
113113
expect do
114-
InlineSvg.configure do |config|
114+
described_class.configure do |config|
115115
config.add_custom_transformation(attribute: :irrelevant, transform: MyInvalidCustomTransformInstance)
116116
end
117117
end.to raise_error(InlineSvg::Configuration::Invalid, /#{MyInvalidCustomTransformInstance} should implement the .create_with_value and #transform methods/o)
118118
end
119119

120120
it "rejects transformations that are not classes" do
121121
expect do
122-
InlineSvg.configure do |config|
122+
described_class.configure do |config|
123123
config.add_custom_transformation(attribute: :irrelevant, transform: :not_a_class)
124124
end
125125
end.to raise_error(InlineSvg::Configuration::Invalid, /not_a_class should implement the .create_with_value and #transform methods/)

spec/io_resource_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
require "stringio"
55
require "tempfile"
66

7-
describe InlineSvg::IOResource do
7+
RSpec.describe InlineSvg::IOResource do
88
it "support api methods" do
99
is_expected.to respond_to(:===, :read)
1010
end

0 commit comments

Comments
 (0)