From 7abbb8909b77332b57fffd5b06207da9e7858f31 Mon Sep 17 00:00:00 2001 From: Jorge Garcia Date: Fri, 13 Oct 2023 17:34:00 -0600 Subject: [PATCH] Chore[rubocop]: Fix rubocop offenses on slither_spec --- lib/slither.rb | 24 +++++++++++++++--------- spec/slither_spec.rb | 34 ++++++++++++++++------------------ 2 files changed, 31 insertions(+), 27 deletions(-) diff --git a/lib/slither.rb b/lib/slither.rb index d3ab090..92aaace 100644 --- a/lib/slither.rb +++ b/lib/slither.rb @@ -1,16 +1,14 @@ # frozen_string_literal: true -require_relative 'slither/column' -require_relative 'slither/definition' -require_relative 'slither/generator' -require_relative 'slither/parser' -require_relative 'slither/section' +require_relative "slither/column" +require_relative "slither/definition" +require_relative "slither/generator" +require_relative "slither/parser" +require_relative "slither/section" require_relative "slither/version" module Slither class Error < StandardError; end - # Your code goes here... - class DuplicateColumnNameError < StandardError; end class RequiredSectionNotFoundError < StandardError; end class RequiredSectionEmptyError < StandardError; end @@ -19,7 +17,13 @@ class ColumnMismatchError < StandardError; end class LineWrongSizeError < StandardError; end class SectionsNotSameLengthError < StandardError; end - + # Define a Slither's definition to parse a file. + # + # name - String name of the definition, this should be unique. + # options - Hash of options to pass to the definition. + # Ex: by_bytes: true, to parse by bytes + # Ex: align: :left, to align the columns to the left + # block - Block to define the sections of the definition. See README.md for more info. def self.define(name, options = {}, &block) definition = Definition.new(options) yield(definition) @@ -31,13 +35,14 @@ def self.define(name, options = {}, &block) def self.generate(definition_name, data) definition = definition(definition_name) raise ArgumentError, "Definition name '#{name}' was not found." unless definition + generator = Generator.new(definition) generator.generate(data) end # Writes the File def self.write(filename, definition_name, data) - File.open(filename, 'w') do |f| + File.open(filename, "w") do |f| f.write generate(definition_name, data) end end @@ -52,6 +57,7 @@ def self.parse(filename, definition_name) def self.parseIo(io, definition_name) definition = definition(definition_name) raise ArgumentError, "Definition name '#{definition_name}' was not found." unless definition + parser = Parser.new(definition, io) definition.options[:by_bytes] ? parser.parse_by_bytes : parser.parse end diff --git a/spec/slither_spec.rb b/spec/slither_spec.rb index 3d82d8a..b50850f 100644 --- a/spec/slither_spec.rb +++ b/spec/slither_spec.rb @@ -8,38 +8,35 @@ describe ".define" do it "creates a new definition using the specified name and options" do - definition = double("definition") - - expect(subject).to receive(:define).with(name, options).and_return(definition) - - subject.define(name, options) - end - - it "pass the definition to the block" do - yielded = nil - - described_class.define(name) do |y| - yielded = y + subject.define(name, options) do + # Empty block end - expect(yielded).to be_a(Slither::Definition) + definition = subject.send(:definitions)[name] + + expect(definition).to be_a(Slither::Definition) + expect(definition.options).to eq(options.merge(by_bytes: true)) end it "adds the definition to the internal definition count" do expect do - subject.define("new_definition", options) {} + # NOTE: as @@definitions is a class variable that's shared across the specs, + # so we need to ensure we add a new one. + subject.define("#{name}-1", options) do + # Empty block + end end.to change { subject.send(:definitions).count }.by(1) end end describe ".generate" do - it "should raise an error if the definition name is not found" do + it "raise an error if the definition name is not found" do expect do subject.generate(:not_found_definition, {}) end.to raise_error(ArgumentError) end - it "should output a string" do + it "output a string" do simple_definition expect( @@ -54,7 +51,8 @@ it "write a file" do simple_definition - file = double("file") + file = instance_double(File, "file") + allow(File).to receive(:open).with(file_name, "w").and_yield(file) expect(file).to receive(:write) @@ -89,7 +87,7 @@ simple_definition expect( - Slither.parse(simple_definition_file, :simple) + subject.parse(simple_definition_file, :simple) ).to eq(simple_definition_test_data) end end