2014-12-31 15:59:32 -05:00
|
|
|
require 'spec_helper'
|
2011-11-08 17:50:16 -05:00
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
RSpec.describe 'AwesomePrint' do
|
2011-11-08 17:50:16 -05:00
|
|
|
def stub_tty!(output = true, stream = STDOUT)
|
|
|
|
if output
|
|
|
|
stream.instance_eval { def tty?; true; end }
|
|
|
|
else
|
|
|
|
stream.instance_eval { def tty?; false; end }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
describe 'colorization' do
|
2011-11-08 17:50:16 -05:00
|
|
|
PLAIN = '[ 1, :two, "three", [ nil, [ true, false ] ] ]'
|
|
|
|
COLORIZED = "[ \e[1;34m1\e[0m, \e[0;36m:two\e[0m, \e[0;33m\"three\"\e[0m, [ \e[1;31mnil\e[0m, [ \e[1;32mtrue\e[0m, \e[1;31mfalse\e[0m ] ] ]"
|
|
|
|
|
|
|
|
before do
|
2016-11-08 00:07:03 -05:00
|
|
|
ENV['TERM'] = 'xterm-colors'
|
2011-11-08 17:50:16 -05:00
|
|
|
ENV.delete('ANSICON')
|
2016-11-08 21:39:25 -05:00
|
|
|
@arr = [1, :two, 'three', [nil, [true, false]]]
|
2011-11-08 17:50:16 -05:00
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
describe 'default settings (no forced colors)' do
|
2011-11-08 17:50:16 -05:00
|
|
|
before do
|
|
|
|
AwesomePrint.force_colors! false
|
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'colorizes tty processes by default' do
|
2011-11-08 17:50:16 -05:00
|
|
|
stub_tty!
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(@arr.ai(multiline: false)).to eq(COLORIZED)
|
2011-11-08 17:50:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "colorizes processes with ENV['ANSICON'] by default" do
|
|
|
|
begin
|
|
|
|
stub_tty!
|
2016-11-09 01:04:02 -05:00
|
|
|
term = ENV['ANSICON']
|
|
|
|
ENV['ANSICON'] = '1'
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(@arr.ai(multiline: false)).to eq(COLORIZED)
|
2011-11-08 17:50:16 -05:00
|
|
|
ensure
|
|
|
|
ENV['ANSICON'] = term
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'does not colorize tty processes running in dumb terminals by default' do
|
2011-11-08 17:50:16 -05:00
|
|
|
begin
|
|
|
|
stub_tty!
|
2016-11-09 01:04:02 -05:00
|
|
|
term = ENV['TERM']
|
|
|
|
ENV['TERM'] = 'dumb'
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(@arr.ai(multiline: false)).to eq(PLAIN)
|
2011-11-08 17:50:16 -05:00
|
|
|
ensure
|
|
|
|
ENV['TERM'] = term
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'does not colorize subprocesses by default' do
|
2011-11-08 17:50:16 -05:00
|
|
|
begin
|
|
|
|
stub_tty! false
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(@arr.ai(multiline: false)).to eq(PLAIN)
|
2011-11-08 17:50:16 -05:00
|
|
|
ensure
|
|
|
|
stub_tty!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
describe 'forced colors override' do
|
2011-11-08 17:50:16 -05:00
|
|
|
before do
|
|
|
|
AwesomePrint.force_colors!
|
|
|
|
end
|
2016-08-27 09:43:32 -04:00
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'still colorizes tty processes' do
|
2011-11-08 17:50:16 -05:00
|
|
|
stub_tty!
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(@arr.ai(multiline: false)).to eq(COLORIZED)
|
2011-11-08 17:50:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "colorizes processes with ENV['ANSICON'] set to 0" do
|
|
|
|
begin
|
|
|
|
stub_tty!
|
2016-11-09 01:04:02 -05:00
|
|
|
term = ENV['ANSICON']
|
|
|
|
ENV['ANSICON'] = '1'
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(@arr.ai(multiline: false)).to eq(COLORIZED)
|
2011-11-08 17:50:16 -05:00
|
|
|
ensure
|
|
|
|
ENV['ANSICON'] = term
|
|
|
|
end
|
|
|
|
end
|
2016-08-27 09:43:32 -04:00
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'colorizes dumb terminals' do
|
2011-11-08 17:50:16 -05:00
|
|
|
begin
|
|
|
|
stub_tty!
|
2016-11-09 01:04:02 -05:00
|
|
|
term = ENV['TERM']
|
|
|
|
ENV['TERM'] = 'dumb'
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(@arr.ai(multiline: false)).to eq(COLORIZED)
|
2011-11-08 17:50:16 -05:00
|
|
|
ensure
|
|
|
|
ENV['TERM'] = term
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'colorizes subprocess' do
|
2011-11-08 17:50:16 -05:00
|
|
|
begin
|
|
|
|
stub_tty! false
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(@arr.ai(multiline: false)).to eq(COLORIZED)
|
2011-11-08 17:50:16 -05:00
|
|
|
ensure
|
|
|
|
stub_tty!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|