Skip to content

Commit

Permalink
Chore[rubocop]: Fix rubocop offenses on slither_spec
Browse files Browse the repository at this point in the history
  • Loading branch information
JorgeGarciaxyz committed Oct 13, 2023
1 parent 8c4f887 commit 7abbb89
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 27 deletions.
24 changes: 15 additions & 9 deletions lib/slither.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand Down
34 changes: 16 additions & 18 deletions spec/slither_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7abbb89

Please sign in to comment.