Skip to content

Instantly share code, notes, and snippets.

@DannyBen
Last active July 19, 2020 07:34
Show Gist options
  • Select an option

  • Save DannyBen/6ad4ade97961856dc63efe3138046fcc to your computer and use it in GitHub Desktop.

Select an option

Save DannyBen/6ad4ade97961856dc63efe3138046fcc to your computer and use it in GitHub Desktop.

Revisions

  1. DannyBen revised this gist Jul 19, 2020. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    Run with:

    ```shell
    $ bundle
    $ bundle exec rspec tty-prompt-test.rb
    ```
  2. DannyBen created this gist Jul 19, 2020.
    8 changes: 8 additions & 0 deletions Gemfile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    # frozen_string_literal: true

    source "https://rubygems.org"

    git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }

    gem "tty-prompt", github: 'piotrmurach/tty-prompt'
    gem "rspec"
    5 changes: 5 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    Run with:

    ```shell
    $ bundle exec rspec tty-prompt-test.rb
    ```
    45 changes: 45 additions & 0 deletions tty-prompt-test.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    require 'tty-prompt'
    require "tty/prompt/test"

    class Subject
    def prompt
    @prompt ||= TTY::Prompt.new
    end

    def run
    "#{name} #{destiny}"
    end

    def name
    prompt.ask "Name:"
    end

    def destiny
    prompt.select("Destiny:", %w(Scorpion Kano Jax))
    end
    end

    describe Subject do
    let(:prompt) { TTY::Prompt::Test.new }

    before do
    prompt.on :keypress do |e|
    prompt.trigger :keyup if e.value == "k"
    prompt.trigger :keydown if e.value == "j"
    end

    allow(subject).to receive(:prompt).and_return prompt
    end

    it "works" do
    prompt.input << "Danny" << "\n" << "j" << "\n"
    prompt.input.rewind
    expect(subject.run).to eq "Danny Kano"
    end

    it "works for sure" do
    prompt.input << "Danny" << "\n" << "j" << "j" << "\n"
    prompt.input.rewind
    expect(subject.run).to eq "Danny Jax"
    end
    end