Run with:
$ bundle
$ bundle exec rspec tty-prompt-test.rb| # 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 "tty-testing", github: 'DanielVartanov/tty-testing' | |
| gem "rspec" |
| require 'tty-prompt' | |
| require "tty/prompt/test" | |
| class Subject | |
| def initialize(prompt=nil) | |
| @prompt = prompt | |
| end | |
| def run | |
| prompt.say "#{name} #{destiny}" | |
| end | |
| def name | |
| prompt.ask "Name:" | |
| end | |
| def destiny | |
| prompt.select("Destiny:", %w(Scorpion Kano Jax)) | |
| end | |
| protected | |
| def prompt | |
| @prompt ||= TTY::Prompt.new | |
| end | |
| end | |
| describe Subject do | |
| let(:prompt) { TTY::Prompt::Test.new } | |
| subject do | |
| Subject.new(prompt) | |
| end | |
| before do | |
| prompt.on :keypress do |e| | |
| prompt.trigger :keyup if e.value == "k" | |
| prompt.trigger :keydown if e.value == "j" | |
| end | |
| end | |
| it "works" do | |
| prompt.input << "Danny" << "\n" << "j" << "\n" | |
| prompt.input.rewind | |
| subject.run | |
| expect(prompt.output.string).to end_with "Danny Kano\n" | |
| end | |
| it "works for sure" do | |
| prompt.input << "Danny" << "\n" << "j" << "j" << "\n" | |
| prompt.input.rewind | |
| subject.run | |
| expect(prompt.output.string).to end_with "Danny Jax\n" | |
| end | |
| end |
| require "tty/prompt" | |
| require "tty/testing" | |
| class Subject | |
| def initialize(input, output) | |
| @input = input | |
| @output = output | |
| end | |
| def run | |
| prompt.say "#{name} #{destiny}" | |
| end | |
| def name | |
| prompt.ask "Name:" | |
| end | |
| def destiny | |
| prompt.select("Destiny:", %w(Scorpion Kano Jax)) | |
| end | |
| protected | |
| def prompt | |
| @prompt ||= TTY::Prompt.new(input: @input, output: @output) | |
| end | |
| end | |
| describe Subject do | |
| let(:app) do | |
| TTY::Testing.app_wrapper do |input, output| | |
| Subject.new(input, output).run | |
| end | |
| end | |
| before { app.run! } | |
| it "works" do | |
| app.input.puts "Danny" | |
| app.input.puts "\e[B" # press arrow down and enter | |
| expect(app.output).to end_with "Danny Kano\n" | |
| end | |
| it "works for sure" do | |
| app.input.puts "Danny" | |
| app.input.puts "\e[B\e[B" # press arrow down twice and enter | |
| expect(app.output).to end_with "Danny Jax\n" | |
| end | |
| end |