/vendor/gems/ruby/1.9.1/gems/pry-0.9.8.4/test/test_default_commands/test_input.rb

https://github.com/umarsheikh/gold-notes · Ruby · 405 lines · 325 code · 68 blank · 12 comment · 21 complexity · b54c48963afed4c58af07c31fe62c71d MD5 · raw file

  1. require 'helper'
  2. describe "Pry::DefaultCommands::Input" do
  3. describe "amend-line" do
  4. it 'should correctly amend the last line of input when no line number specified ' do
  5. str_output = StringIO.new
  6. redirect_pry_io(InputTester.new("def hello", "puts :bing", "amend-line puts :blah", "show-input", "exit-all"), str_output) do
  7. pry
  8. end
  9. str_output.string.should =~ /\A\d+: def hello\n\d+: puts :blah/
  10. end
  11. it 'should correctly amend the specified line of input when line number given ' do
  12. str_output = StringIO.new
  13. redirect_pry_io(InputTester.new("def hello", "puts :bing", "puts :bang", "amend-line 1 def goodbye", "show-input", "exit-all"), str_output) do
  14. pry
  15. end
  16. str_output.string.should =~ /\A\d+: def goodbye\n\d+: puts :bing\n\d+: puts :bang/
  17. end
  18. it 'should correctly amend the specified line of input when line number given, 0 should behave as 1 ' do
  19. str_output = StringIO.new
  20. redirect_pry_io(InputTester.new("def hello", "puts :bing", "puts :bang", "amend-line 0 def goodbye", "show-input", "exit-all"), str_output) do
  21. pry
  22. end
  23. str_output.string.should =~ /\A\d+: def goodbye\n\d+: puts :bing\n\d+: puts :bang/
  24. end
  25. it 'should correctly amend the specified line of input when line number given (negative number)' do
  26. str_output = StringIO.new
  27. redirect_pry_io(InputTester.new("def hello", "puts :bing", "puts :bang", "amend-line -1 puts :bink", "show-input", "exit-all"), str_output) do
  28. pry
  29. end
  30. str_output.string.should =~ /\A\d+: def hello\n\d+: puts :bing\n\d+: puts :bink/
  31. str_output = StringIO.new
  32. redirect_pry_io(InputTester.new("def hello", "puts :bing", "puts :bang", "amend-line -2 puts :bink", "show-input", "exit-all"), str_output) do
  33. pry
  34. end
  35. str_output.string.should =~ /\A\d+: def hello\n\d+: puts :bink\n\d+: puts :bang/
  36. end
  37. it 'should correctly amend the specified range of lines of input when range of negative numbers given (negative number)' do
  38. str_output = StringIO.new
  39. redirect_pry_io(InputTester.new("def hello", "puts :bing", "puts :bang", "puts :boat", "amend-line -3..-2 puts :bink", "show-input", "exit-all"), str_output) do
  40. pry
  41. end
  42. str_output.string.should =~ /\A\d+: def hello\n\d+: puts :bink\n\d+: puts :boat/
  43. end
  44. it 'should correctly amend the specified line with string interpolated text' do
  45. str_output = StringIO.new
  46. redirect_pry_io(InputTester.new("def hello", "puts :bing", "puts :bang", 'amend-line puts "#{goodbye}"', "show-input", "exit-all"), str_output) do
  47. pry
  48. end
  49. str_output.string.should =~ /\A\d+: def hello\n\d+: puts :bing\n\d+: puts \"\#\{goodbye\}\"/
  50. end
  51. it 'should display error if nothing to amend' do
  52. str_output = StringIO.new
  53. redirect_pry_io(InputTester.new("amend-line", "exit-all"), str_output) do
  54. pry
  55. end
  56. str_output.string.should =~ /No input to amend/
  57. end
  58. it 'should correctly amend the specified range of lines' do
  59. str_output = StringIO.new
  60. redirect_pry_io(InputTester.new("def hello", "puts :bing", "puts :bang", "puts :heart", "amend-line 2..3 puts :bong", "show-input", "exit-all"), str_output) do
  61. pry
  62. end
  63. str_output.string.should =~ /\A\d+: def hello\n\d+: puts :bong\n\d+: puts :heart/
  64. end
  65. it 'should correctly delete a specific line using the ! for content' do
  66. str_output = StringIO.new
  67. redirect_pry_io(InputTester.new("def hello", "puts :bing", "puts :bang", "puts :boast", "puts :heart", "amend-line 3 !", "show-input", "exit-all"), str_output) do
  68. pry
  69. end
  70. str_output.string.should =~ /\d+: def hello\n\d+: puts :bing\n\d+: puts :boast\n\d+: puts :heart/
  71. end
  72. it 'should correctly delete a range of lines using the ! for content' do
  73. str_output = StringIO.new
  74. redirect_pry_io(InputTester.new("def hello", "puts :bing", "puts :bang", "puts :boast", "puts :heart", "amend-line 2..4 !", "show-input", "exit-all"), str_output) do
  75. pry
  76. end
  77. str_output.string.should =~ /\d+: def hello\n\d+: puts :heart\n\Z/
  78. end
  79. it 'should correctly delete the previous line using the ! for content' do
  80. str_output = StringIO.new
  81. redirect_pry_io(InputTester.new("def hello", "puts :bing", "puts :bang", "puts :boast", "puts :heart", "amend-line !", "show-input", "exit-all"), str_output) do
  82. pry
  83. end
  84. str_output.string.should =~ /\d+: def hello\n\d+: puts :bing\n\d+: puts :bang\n\d+: puts :boast\n\Z/
  85. end
  86. it 'should correctly amend the specified range of lines, using negative numbers in range' do
  87. str_output = StringIO.new
  88. redirect_pry_io(InputTester.new("def hello", "puts :bing", "puts :bang", "puts :boast", "puts :heart", "amend-line 2..-2 puts :bong", "show-input", "exit-all"), str_output) do
  89. pry
  90. end
  91. str_output.string.should =~ /\d+: def hello\n\d+: puts :bong\n\d+: puts :heart/
  92. end
  93. it 'should correctly insert a new line of input before a specified line using the > syntax' do
  94. str_output = StringIO.new
  95. redirect_pry_io(InputTester.new("def hello", "puts :bing", "puts :bang", "amend-line 2 >puts :inserted", "show-input", "exit-all"), str_output) do
  96. pry
  97. end
  98. str_output.string.should =~ /\d+: def hello\n\d+: puts :inserted\n\d+: puts :bing\n\d+: puts :bang/
  99. end
  100. it 'should correctly insert a new line of input before a specified line using the > syntax (should ignore second value of range)' do
  101. str_output = StringIO.new
  102. redirect_pry_io(InputTester.new("def hello", "puts :bing", "puts :bang", "amend-line 2..21 >puts :inserted", "show-input", "exit-all"), str_output) do
  103. pry
  104. end
  105. str_output.string.should =~ /\d+: def hello\n\d+: puts :inserted\n\d+: puts :bing\n\d+: puts :bang/
  106. end
  107. end
  108. describe "show-input" do
  109. it 'should correctly show the current lines in the input buffer' do
  110. str_output = StringIO.new
  111. redirect_pry_io(InputTester.new("def hello", "puts :bing", "show-input", "exit-all"), str_output) do
  112. pry
  113. end
  114. str_output.string.should =~ /\A\d+: def hello\n\d+: puts :bing/
  115. end
  116. end
  117. describe "!" do
  118. it 'should correctly clear the input buffer ' do
  119. str_output = StringIO.new
  120. redirect_pry_io(InputTester.new("def hello", "puts :bing", "!", "show-input", "exit-all"), str_output) do
  121. pry
  122. end
  123. stripped_output = str_output.string.strip!
  124. stripped_output.each_line.count.should == 1
  125. stripped_output.should =~ /Input buffer cleared!/
  126. end
  127. end
  128. describe "play" do
  129. it 'should play a string variable (with no args)' do
  130. b = binding
  131. b.eval('x = "\"hello\""')
  132. redirect_pry_io(InputTester.new("play x", "exit-all"), str_output = StringIO.new) do
  133. Pry.start b, :hooks => Pry::Hooks.new
  134. end
  135. str_output.string.should =~ /hello/
  136. end
  137. it 'should play a string variable (with no args) using --lines to select what to play' do
  138. b = binding
  139. b.eval('x = "\"hello\"\n\"goodbye\"\n\"love\""')
  140. redirect_pry_io(InputTester.new("play x --lines 1", "exit-all"), str_output = StringIO.new) do
  141. Pry.start b, :hooks => Pry::Hooks.new
  142. end
  143. str_output.string.should =~ /hello/
  144. str_output.string.should.not =~ /love/
  145. str_output.string.should.not =~ /goodbye/
  146. end
  147. it 'should play documentation with the -d switch' do
  148. o = Object.new
  149. # @v = 10
  150. # @y = 20
  151. def o.test_method
  152. :test_method_content
  153. end
  154. redirect_pry_io(InputTester.new('play -d test_method', "exit-all"), str_output = StringIO.new) do
  155. o.pry
  156. end
  157. o.instance_variable_get(:@v).should == 10
  158. o.instance_variable_get(:@y).should == 20
  159. end
  160. it 'should play documentation with the -d switch (restricted by --lines)' do
  161. o = Object.new
  162. # @x = 0
  163. # @v = 10
  164. # @y = 20
  165. # @z = 30
  166. def o.test_method
  167. :test_method_content
  168. end
  169. redirect_pry_io(InputTester.new('play -d test_method --lines 2..3', "exit-all"), str_output = StringIO.new) do
  170. o.pry
  171. end
  172. o.instance_variable_get(:@x).should == nil
  173. o.instance_variable_get(:@z).should == nil
  174. o.instance_variable_get(:@v).should == 10
  175. o.instance_variable_get(:@y).should == 20
  176. end
  177. it 'should play a method with the -m switch (a single line)' do
  178. o = Object.new
  179. def o.test_method
  180. :test_method_content
  181. end
  182. redirect_pry_io(InputTester.new('play -m test_method --lines 2', "exit-all"), str_output = StringIO.new) do
  183. o.pry
  184. end
  185. str_output.string.should =~ /:test_method_content/
  186. end
  187. it 'should APPEND to the input buffer when playing a line with play -m, not replace it' do
  188. o = Object.new
  189. def o.test_method
  190. :test_method_content
  191. end
  192. redirect_pry_io(InputTester.new('def another_test_method', 'play -m test_method --lines 2', 'show-input', 'exit-all'), str_output = StringIO.new) do
  193. o.pry
  194. end
  195. str_output.string.should =~ /def another_test_method/
  196. str_output.string.should =~ /:test_method_content/
  197. end
  198. it 'should play a method with the -m switch (multiple line)' do
  199. o = Object.new
  200. def o.test_method
  201. @var0 = 10
  202. @var1 = 20
  203. @var2 = 30
  204. @var3 = 40
  205. end
  206. redirect_pry_io(InputTester.new('play -m test_method --lines 3..4', "exit-all"), str_output = StringIO.new) do
  207. o.pry
  208. end
  209. o.instance_variable_get(:@var0).should == nil
  210. o.instance_variable_get(:@var1).should == 20
  211. o.instance_variable_get(:@var2).should == 30
  212. o.instance_variable_get(:@var3).should == nil
  213. str_output.string.should =~ /30/
  214. str_output.string.should.not =~ /20/
  215. end
  216. end
  217. describe "hist" do
  218. before do
  219. Pry.history.clear
  220. @hist = Pry.history
  221. end
  222. it 'should display the correct history' do
  223. @hist.push "hello"
  224. @hist.push "world"
  225. str_output = StringIO.new
  226. redirect_pry_io(InputTester.new("hist", "exit-all"), str_output) do
  227. pry
  228. end
  229. str_output.string.should =~ /hello\n.*world/
  230. end
  231. it 'should replay history correctly (single item)' do
  232. o = Object.new
  233. @hist.push "@x = 10"
  234. @hist.push "@y = 20"
  235. @hist.push "@z = 30"
  236. str_output = StringIO.new
  237. redirect_pry_io(InputTester.new("hist --replay -1", "exit-all"), str_output) do
  238. o.pry
  239. end
  240. o.instance_variable_get(:@x).should == nil
  241. o.instance_variable_get(:@y).should == nil
  242. o.instance_variable_get(:@z).should == 30
  243. end
  244. it 'should replay a range of history correctly (range of items)' do
  245. o = Object.new
  246. @hist.push "@x = 10"
  247. @hist.push "@y = 20"
  248. str_output = StringIO.new
  249. redirect_pry_io(InputTester.new("hist --replay 0..2", "exit-all"), str_output) do
  250. o.pry
  251. end
  252. o.instance_variable_get(:@x).should == 10
  253. o.instance_variable_get(:@y).should == 20
  254. end
  255. it 'should grep for correct lines in history' do
  256. @hist.push "abby"
  257. @hist.push "box"
  258. @hist.push "button"
  259. @hist.push "pepper"
  260. @hist.push "orange"
  261. @hist.push "grape"
  262. @hist.push "def blah 1"
  263. @hist.push "def boink 2"
  264. @hist.push "place holder"
  265. str_output = StringIO.new
  266. redirect_pry_io(InputTester.new("hist --grep o", "exit-all"), str_output) do
  267. pry
  268. end
  269. str_output.string.should =~ /\d:.*?box\n\d:.*?button\n\d:.*?orange/
  270. # test more than one word in a regex match (def blah)
  271. str_output = StringIO.new
  272. redirect_pry_io(InputTester.new("hist --grep def blah", "exit-all"), str_output) do
  273. pry
  274. end
  275. str_output.string.should =~ /def blah 1/
  276. # test more than one word with leading white space in a regex match (def boink)
  277. str_output = StringIO.new
  278. redirect_pry_io(InputTester.new("hist --grep def boink", "exit-all"), str_output) do
  279. pry
  280. end
  281. str_output.string.should =~ /def boink 2/
  282. end
  283. it 'should return last N lines in history with --tail switch' do
  284. ("a".."z").each do |v|
  285. @hist.push v
  286. end
  287. str_output = StringIO.new
  288. redirect_pry_io(InputTester.new("hist --tail 3", "exit-all"), str_output) do
  289. pry
  290. end
  291. str_output.string.each_line.count.should == 3
  292. str_output.string.should =~ /x\n\d+:.*y\n\d+:.*z/
  293. end
  294. # strangeness in this test is due to bug in Readline::HISTORY not
  295. # always registering first line of input
  296. it 'should return first N lines in history with --head switch' do
  297. ("a".."z").each do |v|
  298. @hist.push v
  299. end
  300. str_output = StringIO.new
  301. redirect_pry_io(InputTester.new("hist --head 4", "exit-all"), str_output) do
  302. pry
  303. end
  304. str_output.string.each_line.count.should == 4
  305. str_output.string.should =~ /a\n\d+:.*b\n\d+:.*c/
  306. end
  307. # strangeness in this test is due to bug in Readline::HISTORY not
  308. # always registering first line of input
  309. it 'should show lines between lines A and B with the --show switch' do
  310. ("a".."z").each do |v|
  311. @hist.push v
  312. end
  313. str_output = StringIO.new
  314. redirect_pry_io(InputTester.new("hist --show 1..4", "exit-all"), str_output) do
  315. pry
  316. end
  317. str_output.string.each_line.count.should == 4
  318. str_output.string.should =~ /b\n\d+:.*c\n\d+:.*d/
  319. end
  320. it "should not contain duplicated lines" do
  321. str_output = StringIO.new
  322. redirect_pry_io(InputTester.new("3", "_ += 1", "_ += 1", "hist", "exit-all"), str_output) do
  323. pry
  324. end
  325. str_output.string.each_line.grep(/_ \+= 1/).count.should == 1
  326. end
  327. it "should not contain duplicated lines" do
  328. str_output = StringIO.new
  329. redirect_pry_io(InputTester.new(":place_holder", "2 + 2", "", "", "3 + 3", "hist", "exit-all"), str_output) do
  330. pry
  331. end
  332. a = str_output.string.each_line.to_a.index{|line| line.include?("2 + 2") }
  333. b = str_output.string.each_line.to_a.index{|line| line.include?("3 + 3") }
  334. (a + 1).should == b
  335. end
  336. end
  337. end