Sha256: fb4354efecdb612d9c180eb853be1ab9da3057663fd173c9f7b674431c57c39a

Contents?: true

Size: 1.88 KB

Versions: 5

Compression:

Stored size: 1.88 KB

Contents

# -*- encoding: utf-8 -*-

require 'spec_helper'

describe TTY::Shell::Question, '#read_password' do
  let(:input)  { StringIO.new }
  let(:output) { StringIO.new }
  let(:shell) { TTY::Shell.new(input, output) }

  context 'with no mask' do
    it 'asks with echo on' do
      input << "password"
      input.rewind
      q = shell.ask("What is your password: ").echo(true)
      expect(q.read).to eql("password")
      expect(output.string).to eql('What is your password: ')
      expect(q.mask?).to be_false
    end

    it 'asks with echo off' do
      input << "password"
      input.rewind
      q = shell.ask("What is your password: ").echo(false)
      expect(q.read).to eql("password")
      expect(output.string).to eql('What is your password: ')
      expect(q.mask?).to be_false
    end
  end

  context 'with mask' do
    it 'masks output with character' do
      input << "password\n"
      input.rewind
      q = shell.ask("What is your password: ").mask('*')
      expect(q.read).to eql("password")
      expect(output.string).to eql('What is your password: ********')
      expect(q.mask?).to be_true
    end

    it 'ignores mask if echo is off' do
      input << "password"
      input.rewind
      q = shell.ask("What is your password: ").echo(false).mask('*')
      expect(q.read).to eql("password")
      expect(output.string).to eql('What is your password: ')
      expect(q.mask?).to be_true
    end
  end

  context 'with mask and echo as options' do
    it 'asks with options' do
      input << "password"
      input.rewind
      q = shell.ask("What is your password: ", :echo => false, :mask => '*')
      expect(q.read_password).to eql("password")
    end

    it 'asks with block' do
      input << "password"
      input.rewind
      q = shell.ask "What is your password: " do
        echo  false
        mask '*'
      end
      expect(q.read_password).to eql("password")
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
tty-0.0.11 spec/tty/shell/response/read_spec.rb
tty-0.0.10 spec/tty/shell/response/read_spec.rb
tty-0.0.9 spec/tty/shell/response/read_spec.rb
tty-0.0.8 spec/tty/shell/response/read_spec.rb
tty-0.0.7 spec/tty/shell/response/read_spec.rb