Upgrade to Pro — share decks privately, control downloads, hide ads and more …

RubyHiroba 2014

yui-knk
September 21, 2014

RubyHiroba 2014

Power Assert with Pry

yui-knk

September 21, 2014
Tweet

More Decks by yui-knk

Other Decks in Programming

Transcript

  1. I am … • @yui-knk (github) / @spikeolaf (twitter) •

    pry commit (ghosting / ͓͞΅Γ) • Interested in “Binding” (Don’t throw “Masakari”)
  2. Pry ?? • An IRB alternative and runtime developer console

    $ pry ! [1] pry(main)> "ruby".capitalize => "Ruby" [2] pry(main)> ls 10 Comparable#methods: between? Numeric#methods: +@ coerce i phase quo rectangular to_c ... Integer#methods: ceil denominator floor vcdlcm ... Fixnum#methods: % * + -@ < ...
  3. Power Assert ?? • Power Assert shows each value of

    variables and method calls in the expression. It is useful for testing, providing which value wasn't correct when the condition is not satisfied. (test-unit) ! assert do "0".class == "3".to_i.times.map {|i| i + 1 }.class end
  4. assert do "0".class == "3".to_i.times.map {|i| i + 1 }.class

    end ! ! Failure: ! "0".class == "3".to_i.times.map {|i| i + 1 }.class | | | | | | | | | | | Array | | | | [1, 2, 3] | | | #<Enumerator: 3:times> | | 3 | false String test_failed(MyTest)
  5. I think • Power Assert is Cool !! • Power

    Assert is Magical !! • Want to Power Assert Easily !! (not only Failure case in test)
  6. How to install and use it • Type “pa RUBY_CODE”

    in pry $ gem install pry-power_assert $ pry ! [1] pry(main)> pa "0".class == "3".to_i.times.map {|i| i + 1 }.class "0".class == "3".to_i.times.map {|i| i + 1 }.class | | | | | | | | | | | Array | | | | [1, 2, 3] | | | #<Enumerator: 3:times> | | 3 | false String [2] pry(main)>
  7. Edge case 1 ( [], {}, ->) • (Maybe) these

    are not treated as method calls (TracePoint) [1] pry(main)> prc = -> x { -> y {x * y}} => #<Proc:0x007fd1360c9130@(pry):59 (lambda)> [2] pry(main)> pa prc[10][2] prc[10][2] | | | | | 20 | #<Proc:0x007fd13609a9c0@(pry):59 (lambda)> #<Proc:0x007fd1360c9130@(pry):59 (lambda)> ! [3] pry(main)> pa -> x { -> y {x * y}}[10][2] -> x { -> y {x * y}}[10][2]
  8. Edge case 2 ( A ? B : C /

    if ) • Why? [1] pry(main)> pry(main)> a = 10 => 10 [2] pry(main)> pa (a.to_s == 12) ? 1 : 0 (a.to_s == 12) ? 1 : 0 ! [3] pry(main)> pa 10 if a.to_s == 12 10 if a.to_s == 12
  9. OFF TOPIC (What P.A. needs) • Proc (yield, variables(proc.binding)) •

    String (Ripper.sexp(@line) for idents) > Proc.new {"0".class == "3".to_i.times.map {|i| i + 1 }.class} => #<Proc:0x007fd136068b00@(pry):79> ! > %Q["0".class == "3".to_i.times.map {|i| i + 1 }.class] => "\"0\".class == \"3\".to_i.times.map {|i| i + 1 }.class"
  10. OFF TOPIC (What P.A. needs) 1SPD 4USJOH UFTUVOJU BSH PQFO

    QBUI NJOJUFTU BSH PQFO QBUI QSZQ@B FWBM1SPDOFX \\TUS^^ BSH STQFD PQFO QBUI • RSpec is not supported
  11. OFF TOPIC (What P.A. needs) • How to get Proc

    in RSpec ? (rspec) ! describe “class of string should be String” do “0”.class.should == String end