Skip to content

Commit

Permalink
Change tests to improve formatting and make it consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrmurach committed Apr 1, 2021
1 parent 5d803ca commit 93413ca
Show file tree
Hide file tree
Showing 50 changed files with 333 additions and 342 deletions.
1 change: 0 additions & 1 deletion spec/unit/ask_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

RSpec.describe TTY::Prompt, "#ask" do

subject(:prompt) { TTY::Prompt::Test.new }

it "asks question" do
Expand Down
52 changes: 27 additions & 25 deletions spec/unit/block_paginator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,52 @@

RSpec.describe TTY::Prompt::BlockPaginator, "#paginate" do
it "ignores per_page when equal items " do
list = %w(a b c d)
list = %w[a b c d]
paginator = described_class.new(per_page: 4)

expect(paginator.paginate(list, 1).to_a).to eq([
["a",0],["b",1],["c",2],["d",3]])
["a", 0], ["b", 1], ["c", 2], ["d", 3]
])
end

it "ignores per_page when less items " do
list = %w(a b c d)
list = %w[a b c d]
paginator = described_class.new(per_page: 5)

expect(paginator.paginate(list, 1).to_a).to eq([
["a",0],["b",1],["c",2],["d",3]])
["a", 0], ["b", 1], ["c", 2], ["d", 3]
])
end

it "paginates items matching per_page count" do
list = %w(a b c d e f)
list = %w[a b c d e f]
paginator = described_class.new(per_page: 3)

expect(paginator.paginate(list, 1).to_a).to eq([["a",0], ["b",1], ["c",2]])
expect(paginator.paginate(list, 2).to_a).to eq([["a",0], ["b",1], ["c",2]])
expect(paginator.paginate(list, 3).to_a).to eq([["a",0], ["b",1], ["c",2]])
expect(paginator.paginate(list, 4).to_a).to eq([["d",3], ["e",4], ["f",5]])
expect(paginator.paginate(list, 5).to_a).to eq([["d",3], ["e",4], ["f",5]])
expect(paginator.paginate(list, 6).to_a).to eq([["d",3], ["e",4], ["f",5]])
expect(paginator.paginate(list, 7).to_a).to eq([["d",3], ["e",4], ["f",5]])
expect(paginator.paginate(list, 1).to_a).to eq([["a", 0], ["b", 1], ["c", 2]])
expect(paginator.paginate(list, 2).to_a).to eq([["a", 0], ["b", 1], ["c", 2]])
expect(paginator.paginate(list, 3).to_a).to eq([["a", 0], ["b", 1], ["c", 2]])
expect(paginator.paginate(list, 4).to_a).to eq([["d", 3], ["e", 4], ["f", 5]])
expect(paginator.paginate(list, 5).to_a).to eq([["d", 3], ["e", 4], ["f", 5]])
expect(paginator.paginate(list, 6).to_a).to eq([["d", 3], ["e", 4], ["f", 5]])
expect(paginator.paginate(list, 7).to_a).to eq([["d", 3], ["e", 4], ["f", 5]])
end

it "paginates items not matching per_page count" do
list = %w(a b c d e f g)
list = %w[a b c d e f g]
paginator = described_class.new(per_page: 3)

expect(paginator.paginate(list, 1).to_a).to eq([["a",0], ["b",1], ["c",2]])
expect(paginator.paginate(list, 2).to_a).to eq([["a",0], ["b",1], ["c",2]])
expect(paginator.paginate(list, 3).to_a).to eq([["a",0], ["b",1], ["c",2]])
expect(paginator.paginate(list, 4).to_a).to eq([["d",3], ["e",4], ["f",5]])
expect(paginator.paginate(list, 5).to_a).to eq([["d",3], ["e",4], ["f",5]])
expect(paginator.paginate(list, 6).to_a).to eq([["d",3], ["e",4], ["f",5]])
expect(paginator.paginate(list, 7).to_a).to eq([["g",6]])
expect(paginator.paginate(list, 8).to_a).to eq([["g",6]])
expect(paginator.paginate(list, 1).to_a).to eq([["a", 0], ["b", 1], ["c", 2]])
expect(paginator.paginate(list, 2).to_a).to eq([["a", 0], ["b", 1], ["c", 2]])
expect(paginator.paginate(list, 3).to_a).to eq([["a", 0], ["b", 1], ["c", 2]])
expect(paginator.paginate(list, 4).to_a).to eq([["d", 3], ["e", 4], ["f", 5]])
expect(paginator.paginate(list, 5).to_a).to eq([["d", 3], ["e", 4], ["f", 5]])
expect(paginator.paginate(list, 6).to_a).to eq([["d", 3], ["e", 4], ["f", 5]])
expect(paginator.paginate(list, 7).to_a).to eq([["g", 6]])
expect(paginator.paginate(list, 8).to_a).to eq([["g", 6]])
end

it "finds both start and end index for current selection" do
list = %w(a b c d e f g)
list = %w[a b c d e f g]
paginator = described_class.new(per_page: 3, default: 0)

paginator.paginate(list, 3)
Expand All @@ -66,14 +68,14 @@
end

it "starts with default selection" do
list = %w(a b c d e f g)
list = %w[a b c d e f g]
paginator = described_class.new(per_page: 3, default: 3)

expect(paginator.paginate(list, 4).to_a).to eq([["d",3], ["e",4], ["f",5]])
expect(paginator.paginate(list, 4).to_a).to eq([["d", 3], ["e", 4], ["f", 5]])
end

it "doesn't accept invalid pagination" do
list = %w(a b c d e f g)
list = %w[a b c d e f g]

paginator = described_class.new(per_page: 0)

Expand Down
16 changes: 8 additions & 8 deletions spec/unit/choice/eql_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@

RSpec.describe TTY::Prompt::Choice, "#==" do
it "is true with the same name and value attributes" do
expect(described_class.new(:large, 1)).
to eq(described_class.new(:large, 1))
expect(described_class.new(:large, 1))
.to eq(described_class.new(:large, 1))
end

it "is false with different name attribute" do
expect(described_class.new(:large, 1)).
not_to eq(described_class.new(:medium, 1))
expect(described_class.new(:large, 1))
.not_to eq(described_class.new(:medium, 1))
end

it "is false with different value attribute" do
expect(described_class.new(:large, 1)).
not_to eq(described_class.new(:large, 2))
expect(described_class.new(:large, 1))
.not_to eq(described_class.new(:large, 2))
end

it "is false with different key attribute" do
expect(described_class.new(:large, 1, key: "j")).
not_to eq(described_class.new(:large, 2, key: "k"))
expect(described_class.new(:large, 1, key: "j"))
.not_to eq(described_class.new(:large, 2, key: "k"))
end

it "is false with non-choice object" do
Expand Down
12 changes: 6 additions & 6 deletions spec/unit/choice/from_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@

it "creates choice from a hash with a value" do
expected_choice = described_class.new("large", 1)
choice = described_class.from({ large: 1 })
choice = described_class.from({large: 1})

expect(choice).to eq(expected_choice)
expect(choice.name).to eq("large")
Expand All @@ -72,7 +72,7 @@

it "creates choice from a hash with a nil value" do
expected_choice = described_class.new("large", nil)
choice = described_class.from({ large: nil })
choice = described_class.from({large: nil})

expect(choice).to eq(expected_choice)
expect(choice.name).to eq("large")
Expand All @@ -81,7 +81,7 @@

it "creates choice from an array with key-value pair" do
expected_choice = described_class.new("large", 1)
choice = described_class.from([{ "large" => 1 }])
choice = described_class.from([{"large" => 1}])

expect(choice).to eq(expected_choice)
expect(choice.name).to eq("large")
Expand All @@ -90,7 +90,7 @@

it "creates choice from an array with a hash with name and value keys" do
expected_choice = described_class.new("large", 1)
choice = described_class.from([{ name: "large", value: 1 }])
choice = described_class.from([{name: "large", value: 1}])

expect(choice).to eq(expected_choice)
expect(choice.name).to eq("large")
Expand All @@ -99,15 +99,15 @@

it "creates choice from an array with a hash without value key" do
expected_choice = described_class.new("large", "large")
choice = described_class.from([{ name: "large" }])
choice = described_class.from([{name: "large"}])

expect(choice).to eq(expected_choice)
expect(choice.name).to eq("large")
expect(choice.value).to eq("large")
end

it "creates choice from a hash with name, value and key keys" do
default = { key: "h", name: "Help", value: :help }
default = {key: "h", name: "Help", value: :help}
expected_choice = described_class.new("Help", :help, key: "h")
choice = described_class.from(default)

Expand Down
4 changes: 2 additions & 2 deletions spec/unit/choices/each_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# frozen_string_literal: true

RSpec.describe TTY::Prompt::Choices, ".each" do
RSpec.describe TTY::Prompt::Choices, "#each" do
it "iterates over collection" do
choices = described_class[:large, :medium, :small]
actual = []
choices.each do |choice|
actual << choice.name
end
expect(actual).to eq([:large, :medium, :small])
expect(actual).to eq(%i[large medium small])
expect(choices.each).to be_kind_of(Enumerator)
end
end
12 changes: 6 additions & 6 deletions spec/unit/choices/find_by_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
RSpec.describe TTY::Prompt::Choices, "#find_by" do
let(:collection) {
[
{ name: "large", value: "L", key: "l" },
{ name: "medium", value: "M", key: "m" },
{ name: "small", value: "S", key: "s" }
{name: "large", value: "L", key: "l"},
{name: "medium", value: "M", key: "m"},
{name: "small", value: "S", key: "s"}
]
}

Expand All @@ -15,19 +15,19 @@
end

it "finds a matching choice by :name key" do
choice = TTY::Prompt::Choice.from({ name: "small", value: "S", key: "s" })
choice = TTY::Prompt::Choice.from({name: "small", value: "S", key: "s"})
choices = described_class[*collection]
expect(choices.find_by(:name, "small")).to eq(choice)
end

it "finds a matching choice by :value key" do
choice = TTY::Prompt::Choice.from({ name: "medium", value: "M", key: "m" })
choice = TTY::Prompt::Choice.from({name: "medium", value: "M", key: "m"})
choices = described_class[*collection]
expect(choices.find_by(:value, "M")).to eq(choice)
end

it "finds a matching choice by :key key" do
choice = TTY::Prompt::Choice.from({ name: "large", value: "L", key: "l" })
choice = TTY::Prompt::Choice.from({name: "large", value: "L", key: "l"})
choices = described_class[*collection]
expect(choices.find_by(:key, "l")).to eq(choice)
end
Expand Down
6 changes: 3 additions & 3 deletions spec/unit/choices/new_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

RSpec.describe TTY::Prompt::Choices, ".new" do
it "creates choices collection" do
choice_1 = TTY::Prompt::Choice.from(:label1)
choice_2 = TTY::Prompt::Choice.from(:label2)
choice1 = TTY::Prompt::Choice.from(:label1)
choice2 = TTY::Prompt::Choice.from(:label2)
collection = described_class[:label1, :label2]
expect(collection).to eq([choice_1, choice_2])
expect(collection).to eq([choice1, choice2])
end
end
4 changes: 2 additions & 2 deletions spec/unit/choices/pluck_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

RSpec.describe TTY::Prompt::Choices, "#pluck" do
it "plucks choice by key name" do
collection = [{name: "large"},{name: "medium"},{name: "small"}]
collection = [{name: "large"}, {name: "medium"}, {name: "small"}]
choices = described_class[*collection]
expect(choices.pluck(:name)).to eq(["large", "medium", "small"])
expect(choices.pluck(:name)).to eq(%w[large medium small])
end
end
7 changes: 3 additions & 4 deletions spec/unit/collect_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

RSpec.describe TTY::Prompt, "#collect" do

subject(:prompt) { TTY::Prompt::Test.new }

def collect(&block)
Expand All @@ -20,7 +19,7 @@ def collect(&block)
end

context "when receiving multiple answers" do
let(:colors) { %w(red blue yellow) }
let(:colors) { %w[red blue yellow] }

before do
subject.input << "y\r" + colors.join("\ry\r") + "\rn\r"
Expand All @@ -38,13 +37,13 @@ def collect(&block)
key(:colors).values { key(:name).ask("color:") }
end
expect(result[:count]).to eq(3)
expect(result[:colors]).to eq(colors.map { |c| { name: c } })
expect(result[:colors]).to eq(colors.map { |c| {name: c} })
end

context "with multiple keys" do
let(:colors) { ["red\rblue", "yellow\rgreen"] }
let(:expected_pairs) do
colors.map { |s| Hash[%i(hot cold).zip(s.split("\r"))] }
colors.map { |s| Hash[%i[hot cold].zip(s.split("\r"))] }
end

it "collects into the appropriate keys" do
Expand Down
3 changes: 1 addition & 2 deletions spec/unit/converters/convert_bool_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# frozen_string_literal: true

RSpec.describe TTY::Prompt::Question, "convert bool" do

subject(:prompt) { TTY::Prompt::Test.new}
subject(:prompt) { TTY::Prompt::Test.new }

it "fails to convert boolean" do
prompt.input << "x"
Expand Down
3 changes: 1 addition & 2 deletions spec/unit/converters/convert_custom_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
# frozen_string_literal: true

RSpec.describe TTY::Prompt::Question, "convert custom" do

subject(:prompt) { TTY::Prompt::Test.new }

it "converts response with custom conversion" do
prompt.input << "one,two,three\n"
prompt.input.rewind
conversion = proc { |input| input.split(/,\s*/) }
answer = prompt.ask("Ingredients? (comma sep list)", convert: conversion)
expect(answer).to eq(["one","two","three"])
expect(answer).to eq(%w[one two three])
end
end
3 changes: 1 addition & 2 deletions spec/unit/converters/convert_date_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# frozen_string_literal: true

RSpec.describe TTY::Prompt::Question, "convert date" do

subject(:prompt) { TTY::Prompt::Test.new}
subject(:prompt) { TTY::Prompt::Test.new }

it "fails to convert date" do
prompt.input << "x"
Expand Down
1 change: 0 additions & 1 deletion spec/unit/converters/convert_number_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

RSpec.describe TTY::Prompt::Question, "convert numbers" do

subject(:prompt) { TTY::Prompt::Test.new }

it "fails to convert integer" do
Expand Down
3 changes: 1 addition & 2 deletions spec/unit/converters/convert_range_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# frozen_string_literal: true

RSpec.describe TTY::Prompt::Question, "convert range" do

subject(:prompt) { TTY::Prompt::Test.new}
subject(:prompt) { TTY::Prompt::Test.new }

it "converts with valid range" do
prompt.input << "20-30"
Expand Down
Loading

0 comments on commit 93413ca

Please sign in to comment.