Skip to content

Commit

Permalink
Change tests to improve formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrmurach committed May 3, 2021
1 parent 1524374 commit b19c042
Show file tree
Hide file tree
Showing 26 changed files with 126 additions and 172 deletions.
14 changes: 7 additions & 7 deletions spec/spec_helper.rb
@@ -1,22 +1,22 @@
# frozen_string_literal: true

if ENV["COVERAGE"] == "true"
require 'simplecov'
require 'coveralls'
require "simplecov"
require "coveralls"

SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([
SimpleCov::Formatter::HTMLFormatter,
Coveralls::SimpleCov::Formatter
])

SimpleCov.start do
command_name 'spec'
add_filter 'spec'
command_name "spec"
add_filter "spec"
end
end

require 'tty-spinner'
require 'stringio'
require "tty-spinner"
require "stringio"

class StringIO
def tty?
Expand All @@ -41,7 +41,7 @@ def tty?
config.warnings = true

if config.files_to_run.one?
config.default_formatter = 'doc'
config.default_formatter = "doc"
end

config.profile_examples = 2
Expand Down
8 changes: 3 additions & 5 deletions spec/unit/auto_spin_spec.rb
@@ -1,7 +1,5 @@
# coding: utf-8

RSpec.describe TTY::Spinner, '#auto_spin' do
let(:output) { StringIO.new('', 'w+') }
RSpec.describe TTY::Spinner, "#auto_spin" do
let(:output) { StringIO.new("", "w+") }

it "starts and auto spins" do
spinner = TTY::Spinner.new(output: output, interval: 100)
Expand All @@ -18,7 +16,7 @@
spinner = TTY::Spinner.new(output: output, hide_cursor: true)

spinner.auto_spin {
raise 'boom'
raise "boom"
}

output.rewind
Expand Down
8 changes: 3 additions & 5 deletions spec/unit/clear_spec.rb
@@ -1,12 +1,10 @@
# encoding: utf-8

RSpec.describe TTY::Spinner, ':clear' do
let(:output) { StringIO.new('', 'w+') }
RSpec.describe TTY::Spinner, ":clear" do
let(:output) { StringIO.new("", "w+") }

it "clears output when done" do
spinner = TTY::Spinner.new(clear: true, output: output)
3.times { spinner.spin }
spinner.stop('Done!')
spinner.stop("Done!")
output.rewind
expect(output.read).to eq([
"\e[1G|",
Expand Down
12 changes: 5 additions & 7 deletions spec/unit/error_spec.rb
@@ -1,7 +1,5 @@
# coding: utf-8

RSpec.describe TTY::Spinner, '#error' do
let(:output) { StringIO.new('', 'w+') }
RSpec.describe TTY::Spinner, "#error" do
let(:output) { StringIO.new("", "w+") }

it "marks spinner as error" do
spinner = TTY::Spinner.new(output: output)
Expand All @@ -22,7 +20,7 @@
it "marks spinner as error with message" do
spinner = TTY::Spinner.new(output: output)
3.times { spinner.spin }
spinner.error('Error')
spinner.error("Error")
output.rewind
expect(output.read).to eq([
"\e[1G|",
Expand All @@ -36,8 +34,8 @@
end

it "changes error spinner marker" do
spinner = TTY::Spinner.new(error_mark: 'x', output: output)
spinner.error('(error)')
spinner = TTY::Spinner.new(error_mark: "x", output: output)
spinner.error("(error)")
output.rewind
expect(output.read).to eq("\e[0m\e[2K\e[1Gx (error)\n")

Expand Down
10 changes: 4 additions & 6 deletions spec/unit/events_spec.rb
@@ -1,7 +1,5 @@
# coding: utf-8

RSpec.describe TTY::Spinner, 'events' do
let(:output) { StringIO.new('', 'w+') }
RSpec.describe TTY::Spinner, "events" do
let(:output) { StringIO.new("", "w+") }

it "emits :done event" do
events = []
Expand All @@ -21,7 +19,7 @@

spinner.success

expect(events).to match_array([:done, :success])
expect(events).to match_array(%i[done success])
end

it "emits :error event" do
Expand All @@ -32,6 +30,6 @@

spinner.error

expect(events).to match_array([:done, :error])
expect(events).to match_array(%i[done error])
end
end
6 changes: 2 additions & 4 deletions spec/unit/frames_spec.rb
@@ -1,7 +1,5 @@
# encoding: utf-8

RSpec.describe TTY::Spinner, ':frames' do
let(:output) { StringIO.new('', 'w+') }
RSpec.describe TTY::Spinner, ":frames" do
let(:output) { StringIO.new("", "w+") }

it "uses custom frames from string" do
frames = ".o0@*"
Expand Down
10 changes: 4 additions & 6 deletions spec/unit/hide_cursor_spec.rb
@@ -1,7 +1,5 @@
# coding: utf-8

RSpec.describe TTY::Spinner, ':hide_cursor' do
let(:output) { StringIO.new('', 'w+') }
RSpec.describe TTY::Spinner, ":hide_cursor" do
let(:output) { StringIO.new("", "w+") }

it "hides cursor" do
spinner = TTY::Spinner.new(output: output, hide_cursor: true)
Expand All @@ -22,7 +20,7 @@
it "restores cursor on success" do
spinner = TTY::Spinner.new(output: output, hide_cursor: true)
4.times { spinner.spin }
spinner.success('success')
spinner.success("success")
output.rewind
expect(output.read).to eq([
"\e[?25l\e[1G|",
Expand All @@ -38,7 +36,7 @@
it "restores cursor on error" do
spinner = TTY::Spinner.new(output: output, hide_cursor: true)
4.times { spinner.spin }
spinner.error('error')
spinner.error("error")
output.rewind
expect(output.read).to eq([
"\e[?25l\e[1G|",
Expand Down
4 changes: 1 addition & 3 deletions spec/unit/job_spec.rb
@@ -1,6 +1,4 @@
# encoding: utf-8

RSpec.describe TTY::Spinner, '#job' do
RSpec.describe TTY::Spinner, "#job" do
it "adds and executes job" do
spinner = TTY::Spinner.new("[:spinner] :title")
called = []
Expand Down
6 changes: 2 additions & 4 deletions spec/unit/join_spec.rb
@@ -1,7 +1,5 @@
# coding: utf-8

RSpec.describe TTY::Spinner, '#join' do
let(:output) { StringIO.new('', 'w+') }
RSpec.describe TTY::Spinner, "#join" do
let(:output) { StringIO.new("", "w+") }

it "raises exception when not spinning" do
spinner = TTY::Spinner.new(output: output)
Expand Down
6 changes: 3 additions & 3 deletions spec/unit/log_spec.rb
@@ -1,5 +1,5 @@
RSpec.describe TTY::Spinner, "#log" do
let(:output) { StringIO.new('', 'w+') }
let(:output) { StringIO.new("", "w+") }

it "logs a message above a spinner" do
spinner = TTY::Spinner.new(output: output)
Expand All @@ -18,7 +18,7 @@
"\e[2K\e[1Gfoo\n",
"\e[2K\e[1Gbar\n",
"\e[1G/",
"\e[1G/",
"\e[1G/"
].join)
end

Expand All @@ -37,7 +37,7 @@
"\e[1G|",
"\e[2K\e[1Gfoo\n",
"\e[1G/",
"\e[1G/",
"\e[1G/"
].join)
end

Expand Down
12 changes: 5 additions & 7 deletions spec/unit/multi/auto_spin_spec.rb
@@ -1,7 +1,5 @@
# encoding: utf-8

RSpec.describe TTY::Spinner::Multi, '#auto_spin' do
let(:output) { StringIO.new('', 'w+') }
RSpec.describe TTY::Spinner::Multi, "#auto_spin" do
let(:output) { StringIO.new("", "w+") }

it "doesn't auto spin top level spinner" do
spinners = TTY::Spinner::Multi.new("Top level spinner", output: output)
Expand All @@ -24,11 +22,11 @@
spinners = TTY::Spinner::Multi.new("top", output: output)
jobs = []

spinners.register("one") { |sp| jobs << 'one'; sp.success }
spinners.register("two") { |sp| jobs << 'two'; sp.success }
spinners.register("one") { |sp| jobs << "one"; sp.success }
spinners.register("two") { |sp| jobs << "two"; sp.success }

spinners.auto_spin

expect(jobs).to match_array(['one', 'two'])
expect(jobs).to match_array(%w[one two])
end
end
24 changes: 11 additions & 13 deletions spec/unit/multi/error_spec.rb
@@ -1,9 +1,7 @@
# encoding: utf-8
RSpec.describe TTY::Spinner::Multi, "#error" do
let(:output) { StringIO.new("", "w+") }

RSpec.describe TTY::Spinner::Multi, '#error' do
let(:output) { StringIO.new('', 'w+') }

it 'stops registerd multi spinner and emits an :error message' do
it "stops registerd multi spinner and emits an :error message" do
spinners = TTY::Spinner::Multi.new(":spinner", output: output)
callbacks = []
sp1 = spinners.register "[:spinner] one"
Expand All @@ -20,10 +18,10 @@

expect(sp1.error?).to eq(true)
expect(sp2.error?).to eq(true)
expect(callbacks).to match_array([:done, :error])
expect(callbacks).to match_array(%i[done error])
end

it 'stops unregistered top level spinner and emits an :error message' do
it "stops unregistered top level spinner and emits an :error message" do
spinners = TTY::Spinner::Multi.new(output: output)
callbacks = []
sp1 = spinners.register "[:spinner] one"
Expand All @@ -40,10 +38,10 @@

expect(sp1.error?).to eq(true)
expect(sp2.error?).to eq(true)
expect(callbacks).to match_array([:done, :error])
expect(callbacks).to match_array(%i[done error])
end

it 'stops registed spinners under top level and emits an error message' do
it "stops registed spinners under top level and emits an error message" do
spinners = TTY::Spinner::Multi.new(":spinner", output: output)
callbacks = []
sp1 = spinners.register "[:spinner] one"
Expand All @@ -60,10 +58,10 @@
sp2.error

expect(spinners.error?).to eq(true)
expect(callbacks).to match_array([:done, :error])
expect(callbacks).to match_array(%i[done error])
end

it 'stops registed spinners under top level and emits an error message' do
it "stops registed spinners under top level and emits an error message" do
spinners = TTY::Spinner::Multi.new(output: output)
callbacks = []
sp1 = spinners.register "[:spinner] one"
Expand All @@ -80,10 +78,10 @@
sp2.error

expect(spinners.error?).to eq(true)
expect(callbacks).to match_array([:done, :error])
expect(callbacks).to match_array(%i[done error])
end

it 'returns true when any spinner failed' do
it "returns true when any spinner failed" do
spinners = TTY::Spinner::Multi.new(output: output)
sp1 = spinners.register("one")
sp2 = spinners.register("two")
Expand Down
20 changes: 9 additions & 11 deletions spec/unit/multi/line_inset_spec.rb
@@ -1,14 +1,12 @@
# encoding: utf-8

RSpec.describe TTY::Spinner::Multi, '#line_inset' do
let(:output) { StringIO.new('', 'w+') }
RSpec.describe TTY::Spinner::Multi, "#line_inset" do
let(:output) { StringIO.new("", "w+") }

it "doesn't create inset when no top level spinner" do
spinners = TTY::Spinner::Multi.new(output: output)

spinner = spinners.register 'example'
spinner = spinners.register "example"

expect(spinners.line_inset(spinner)).to eq('')
expect(spinners.line_inset(spinner)).to eq("")
end

it "defaults to the empty string for the top level spinner" do
Expand All @@ -21,8 +19,8 @@
it "returns four spaces when there is a top level spinner" do
spinners = TTY::Spinner::Multi.new("Top level spinner", output: output)

spinners.register 'middle'
spinners.register 'bottom'
spinners.register "middle"
spinners.register "bottom"

expect(spinners.line_inset(2))
.to eq(TTY::Spinner::Multi::DEFAULT_INSET[:middle])
Expand All @@ -31,8 +29,8 @@
it "decorates last spinner" do
spinners = TTY::Spinner::Multi.new("Top spinner", output: output)

spinners.register 'middle'
spinners.register 'bottom'
spinners.register "middle"
spinners.register "bottom"

expect(spinners.line_inset(3))
.to eq(TTY::Spinner::Multi::DEFAULT_INSET[:bottom])
Expand All @@ -45,7 +43,7 @@
style: {
top: ". ",
middle: "--",
bottom: "---",
bottom: "---"
}
}
spinners = TTY::Spinner::Multi.new("Top level spinner", opts)
Expand Down
10 changes: 4 additions & 6 deletions spec/unit/multi/on_spec.rb
@@ -1,13 +1,11 @@
# encoding: utf-8
RSpec.describe TTY::Spinner::Multi, "#on" do
let(:output) { StringIO.new("", "w+") }

RSpec.describe TTY::Spinner::Multi, '#on' do
let(:output) { StringIO.new('', 'w+') }

it 'fails to register a callback with invalid event name' do
it "fails to register a callback with invalid event name" do
spinners = TTY::Spinner::Multi.new(output: output)

expect {
spinners.on(:unknown_event) { }
spinners.on(:unknown_event) { :noop }
}.to raise_error(ArgumentError, /The event unknown_event does not exist/)
end
end
13 changes: 6 additions & 7 deletions spec/unit/multi/register_spec.rb
@@ -1,7 +1,5 @@
# coding: utf-8

RSpec.describe TTY::Spinner::Multi, '#register' do
let(:output) { StringIO.new('', 'w+') }
RSpec.describe TTY::Spinner::Multi, "#register" do
let(:output) { StringIO.new("", "w+") }

it "registers a TTY::Spinner instance from a pattern" do
spinners = TTY::Spinner::Multi.new(output: output, interval: 100)
Expand All @@ -27,8 +25,8 @@
it "raises an erro when given neither a string or spinner instance" do
spinners = TTY::Spinner::Multi.new(output: output, interval: 100)

expect { spinners.register [] }.
to raise_error(
expect { spinners.register [] }
.to raise_error(
ArgumentError,
"Expected a pattern or spinner, got: Array"
)
Expand All @@ -42,6 +40,7 @@

spinners.register "[:spinner]"

expect(TTY::Spinner).to have_received(:new).with("[:spinner]", {interval: 100, output: output})
expect(TTY::Spinner).to have_received(:new)
.with("[:spinner]", {interval: 100, output: output})
end
end

0 comments on commit b19c042

Please sign in to comment.