From 7ab0d59588c1214da70ff868d0687ec11c21cfe0 Mon Sep 17 00:00:00 2001 From: Kyrylo Silin Date: Sat, 30 Mar 2019 01:55:03 +0200 Subject: [PATCH] color_printer_spec: add more specs Some code paths were not covered by unit tests. --- spec/color_printer_spec.rb | 57 +++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/spec/color_printer_spec.rb b/spec/color_printer_spec.rb index 6c2e72cb..13b2acf6 100644 --- a/spec/color_printer_spec.rb +++ b/spec/color_printer_spec.rb @@ -11,7 +11,7 @@ RSpec.describe Pry::ColorPrinter do end end - it "prints a string" do + it "prints a string with a newline" do described_class.pp(healthy_class.new, output) expect(output.string).to eq("string\n") end @@ -40,5 +40,60 @@ RSpec.describe Pry::ColorPrinter do .to match(/\A\e\[32m#\e\[0m\e\[0m\n\z/) end end + + context "when #inspect returns an object literal" do + let(:klass) do + Class.new do + def inspect + '#' + end + end + end + + it "prints the object inspect" do + described_class.pp(klass.new, output) + expect(output.string).to eq("\e[32m#\e[0m\n") + end + + context "and when SyntaxHighlighter returns a token starting with '\e'" do + before do + expect(Pry::SyntaxHighlighter).to receive(:keyword_token_color) + .and_return("\e[32m") + end + + it "prints the object as is" do + described_class.pp(klass.new, output) + expect(output.string).to eq("\e[32m#\e[0m\n") + end + end + + context "and when SyntaxHighlighter returns a token that doesn't start with '\e'" do + before do + expect(Pry::SyntaxHighlighter).to receive(:keyword_token_color) + .and_return('token') + end + + it "prints the object with escape characters" do + described_class.pp(klass.new, output) + expect(output.string) + .to eq("\e[0m\e[0;tokenm#\e[0m\n") + end + end + end + + context "when #inspect raises Pry::Pager::StopPaging" do + let(:klass) do + Class.new do + def inspect + raise Pry::Pager::StopPaging + end + end + end + + it "propagates the error" do + expect { described_class.pp(klass.new, output) } + .to raise_error(Pry::Pager::StopPaging) + end + end end end