Skip to content

Commit

Permalink
Chore: Fix Rubocop Offenses in spec/helpers and spec/slither (#22)
Browse files Browse the repository at this point in the history
* Chore[rubocop]: Set HashSyntax EnforcedShorthandSyntax: never

* Chore[rubocop]: Fix offesnes in spec/slither/column_spec

* Chore[rubocop]: Fix offesnes in spec/helpers/definition_helper

* Chore[rubocop]: Add comments to empty block on definition_spec

* Chore[rubocop]: Fix offenses in spec/slither/generator_spec

* Chore[rubocop]: Fix offenses in spec/slither/parser_spec

* Chore[rubocop]: Fix offenses in spec/slither/section_spec
  • Loading branch information
JorgeGarciaxyz authored Oct 13, 2023
1 parent eef76f0 commit 8c4f887
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 42 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ Metrics/MethodLength:
Style/Documentation:
Enabled: false

Style/HashSyntax:
EnforcedShorthandSyntax: never

RSpec/ContextWording:
Enabled: false
RSpec/ExampleLength:
Expand Down
6 changes: 4 additions & 2 deletions spec/helpers/definition_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

module DefinitionHelper
# rubocop:disable Metrics/AbcSize
def simple_definition
Slither.define :simple, by_bytes: false do |d|
# This is a template section that can be reused in other sections
Expand All @@ -18,7 +19,7 @@ def simple_definition
end

d.body do |body|
body.trap { |line| line[0, 4] =~ /[^(HEAD|FOOT)]/ }
body.trap { |line| line[0, 4] =~ /[^(HEAD|FOT)]/ }
body.column :id, 10, type: :integer
body.column :name, 10, align: :left
body.spacer 3
Expand All @@ -32,9 +33,10 @@ def simple_definition
end
end
end
# rubocop:enable Metrics/AbcSize

def simple_definition_test_data
{
{
header: [
{ record_type: "HEAD", company_id: "ABC" }
],
Expand Down
8 changes: 4 additions & 4 deletions spec/slither/column_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@
let(:options) { { padding: :space } }

it "respect the format" do
expect(subject.format(25)).to eq(' 25')
expect(subject.format(25)).to eq(" 25")
end
end

Expand Down Expand Up @@ -280,13 +280,13 @@
end

context "with format" do
let(:options) { { type: :float, format: "%.3f"} }
let(:options) { { type: :float, format: "%.3f" } }
let(:length) { 10 }

it "support the type with its format" do
expect(subject.format(234.45)).to eq(" 234.450")
expect(subject.format('234.4500')).to eq(" 234.450")
expect(subject.format('3')).to eq(" 3.000")
expect(subject.format("234.4500")).to eq(" 234.450")
expect(subject.format("3")).to eq(" 3.000")
end

context "alignment and padding" do
Expand Down
24 changes: 18 additions & 6 deletions spec/slither/definition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@
subject { described_class.new }

it "defaults to :right if is not specified" do
subject.section("name") {}
subject.section("name") do
# Empty block
end

section = subject.sections.first

expect(section.options[:align]).to eq(:right)
end

it "override default if :align is passed to the section" do
subject.section("name", align: :left) {}
subject.section("name", align: :left) do
# Empty block
end

section = subject.sections.first

Expand Down Expand Up @@ -63,10 +67,14 @@
end

it "does not create duplicate section names" do
subject.section(:header) {}
subject.section(:header) do
# Empty block
end

expect do
subject.section(:header) {}
subject.section(:header) do
# Empty block
end
end.to raise_error(ArgumentError)
end

Expand All @@ -85,12 +93,16 @@
it "create a new section" do
expect(Slither::Section).to receive(:new)

subject.template(:row) {}
subject.template(:row) do
# Empty block
end
end

it "add a section to the templates collection" do
expect do
subject.template(:row) {}
subject.template(:row) do
# Empty block
end
end.to change { subject.templates.count }.by(1)
end

Expand Down
16 changes: 8 additions & 8 deletions spec/slither/generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
let(:definition) do
Slither.define :test do |d|
d.header do |h|
h.trap { |line| line[0,4] == 'HEAD' }
h.trap { |line| line[0, 4] == "HEAD" }
h.column :type, 4
h.column :file_id, 10
end

d.body do |b|
b.trap { |line| line[0,4] =~ /[^(HEAD|FOOT)]/ }
b.trap { |line| line[0, 4] =~ /[^(HEAD|FOOT)]/ } # rubocop:disable Lint/DuplicateRegexpCharacterClassElement
b.column :first, 10
b.column :last, 10
end

d.footer do |f|
f.trap { |line| line[0,4] == 'FOOT' }
f.trap { |line| line[0, 4] == "FOOT" }
f.column :type, 4
f.column :file_id, 10
end
Expand All @@ -28,12 +28,12 @@
describe ".generate" do
let(:data) do
{
:header => [ {:type => "HEAD", :file_id => "1" }],
:body => [
{:first => "Paul", :last => "Hewson" },
{:first => "Dave", :last => "Evans" }
header: [{ type: "HEAD", file_id: "1" }],
body: [
{ first: "Paul", last: "Hewson" },
{ first: "Dave", last: "Evans" }
],
:footer => [ {:type => "FOOT", :file_id => "1" }]
footer: [{ type: "FOOT", file_id: "1" }]
}
end

Expand Down
24 changes: 12 additions & 12 deletions spec/slither/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,52 +192,52 @@
describe "private methods" do
describe "#remove_new_lines!" do
it "returns true for file starting in newlines or EOF" do
expect(subject.send(:remove_newlines!)).to eq(true)
expect(subject.send(:remove_newlines!)).to be(true)

io.string = "\nXYZ"
expect(subject.send(:remove_newlines!)).to eq(true)
expect(subject.send(:remove_newlines!)).to be(true)

io.string = "\r\n"
expect(subject.send(:remove_newlines!)).to eq(true)
expect(subject.send(:remove_newlines!)).to be(true)

io.string = "\n\n\n\nXYZ\n"
expect(subject.send(:remove_newlines!)).to eq(true)
expect(subject.send(:remove_newlines!)).to be(true)

io.string = ""
expect(subject.send(:remove_newlines!)).to eq(true)
expect(subject.send(:remove_newlines!)).to be(true)
end

it "return false for any other first characters" do
io.string = "XYZ\nxyz"
expect(subject.send(:remove_newlines!)).to eq(false)
expect(subject.send(:remove_newlines!)).to be(false)

io.string = " \nxyz"
expect(subject.send(:remove_newlines!)).to eq(false)
expect(subject.send(:remove_newlines!)).to be(false)

io.string = "!YZxyz\n"
expect(subject.send(:remove_newlines!)).to eq(false)
expect(subject.send(:remove_newlines!)).to be(false)
end

it "leaves first non-newline char in place" do
io.string = "\nXYZ"
expect(subject.send(:remove_newlines!)).to eq(true)
expect(subject.send(:remove_newlines!)).to be(true)

first_character = io.getc
expect(first_character).to eq("X")

expect(subject.send(:remove_newlines!)).to eq(false)
expect(subject.send(:remove_newlines!)).to be(false)
end
end

describe "#newline?" do
it "is true for \n or \r and false otherwise" do
["\n", "\r"].each do |e|
# ord = https://apidock.com/rails/String/ord
expect(subject.send(:newline?, e.ord)).to eq(true)
expect(subject.send(:newline?, e.ord)).to be(true)
end

["", nil, "meep"].each do |e|
expect(subject.send(:newline?, e)).to eq(false)
expect(subject.send(:newline?, e)).to be(false)
end
end
end
Expand Down
18 changes: 8 additions & 10 deletions spec/slither/section_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
end.to raise_error(Slither::DuplicateColumnNameError)
end

it "should allow duplicate column names that are reserved (i.e. spacer)" do
it "allow duplicate column names that are reserved (i.e. spacer)" do
subject.spacer(10)

expect do
Expand Down Expand Up @@ -82,15 +82,15 @@
trap = subject.instance_variable_get(:@trap)

expect(trap).to be_a(Proc)
expect(trap.call(4)).to eq(true)
expect(trap.call(4)).to be(true)
end
end

it "should try to match a line using the trap" do
it "try to match a line using the trap" do
subject.trap { |line| line == "hello" }

expect(subject.match("hello")).to eq(true)
expect(subject.match("goodbye")).to eq(false)
expect(subject.match("hello")).to be(true)
expect(subject.match("goodbye")).to be(false)
end
end

Expand All @@ -102,8 +102,8 @@
context "when template does not exist" do
it "raise an error if the template is not found on the definition" do
expect do
subject.template(:none).to raise_error(ArgumentError)
end
subject.template(:none).to
end.to raise_error(ArgumentError)
end
end

Expand Down Expand Up @@ -136,7 +136,7 @@
end

describe "#format" do
let(:data) { { id: 3, name: "Ryan"} }
let(:data) { { id: 3, name: "Ryan" } }

it "transform the 'data' hash to the expected format based on the columns width" do
subject.column(:id, 5)
Expand Down Expand Up @@ -198,6 +198,4 @@
end
end
end


end

0 comments on commit 8c4f887

Please sign in to comment.