File: selected_choices_spec.rb

package info (click to toggle)
ruby-tty-prompt 0.23.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,452 kB
  • sloc: ruby: 8,847; makefile: 4
file content (73 lines) | stat: -rw-r--r-- 1,783 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# frozen_string_literal: true

RSpec.describe TTY::Prompt::SelectedChoices do
  it "inserts choices by the index order" do
    choices = %w[A B C D E F]
    selected = described_class.new

    expect(selected.to_a).to eq([])
    expect(selected.size).to eq(0)

    selected.insert(5, "F")
    selected.insert(1, "B")
    selected.insert(3, "D")
    selected.insert(0, "A")
    selected.insert(4, "E")
    selected.insert(2, "C")

    expect(selected.to_a).to eq(choices)
    expect(selected.size).to eq(6)

    expect(selected.delete_at(3)).to eq("D")
  end

  it "initializes with selected choices" do
    choices = %w[A B C D E F]
    selected = described_class.new(choices, (0...choices.size).to_a)

    expect(selected.to_a).to eq(choices)
    expect(selected.size).to eq(6)

    choice = selected.delete_at(3)
    expect(choice).to eq("D")

    expect(selected.to_a).to eq(%w[A B C E F])
    expect(selected.size).to eq(5)
  end

  it "inserts and deletes choices" do
    selected = described_class.new

    selected.insert(5, "F")
    selected.insert(1, "B")
    selected.insert(3, "D")
    selected.insert(0, "A")

    expect(selected.to_a).to eq(%w[A B D F])
    expect(selected.size).to eq(4)

    choice = selected.delete_at(3)
    expect(choice).to eq("D")
    expect(selected.to_a).to eq(%w[A B F])
    expect(selected.size).to eq(3)

    selected.insert(4, "E")
    choice = selected.delete_at(-999)

    expect(choice).to eq(nil)
    expect(selected.to_a).to eq(%w[A B E F])
    expect(selected.size).to eq(4)
  end

  it "clears choices" do
    selected = described_class.new(%w[B D F])

    expect(selected.to_a).to eq(%w[B D F])
    expect(selected.size).to eq(3)

    selected.clear

    expect(selected.to_a).to eq([])
    expect(selected.size).to eq(0)
  end
end