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

cookpad-17day-tech-internship-2017-tdd

 cookpad-17day-tech-internship-2017-tdd

クックパッド17day技術インターンシップの前半、TDDへんの講義資料です

MOROHASHI Kyosuke

August 31, 2017
Tweet

More Decks by MOROHASHI Kyosuke

Other Decks in Programming

Transcript

  1. $ vim spec/tokei_spec.rb ``` describe '時計' do specify { expect(true).to

    be true } end ``` ``` $ bundle exec rspec . Finished in 0.00097 seconds (files took 0.09778 seconds to load) 1 example, 0 failures ``` ࠷ॳͷҰา 
  2. ``` describe Clock do specify { expect(Clock.new).to be_an_instance_of(Clock) } end

    ``` $ bundle exec rspec spec bundler: failed to load command: rspec 
 (/opt/brew/opt/rbenv/versions/2.3.0/bin/rspec) NameError: uninitialized constant Clock ClockΫϥεΛ࡞Γ͍ͨ → ·ͩͳ͍͔ΒΤϥʔʹͳΔ͸ͣ 
  3. $ mkdir lib $ vim lib/clock.rb ``` class Clock end

    ``` $ bundle exec rspec spec bundler: failed to load command: rspec 
 (/opt/brew/opt/rbenv/versions/2.3.0/bin/rspec) NameError: uninitialized constant Clock ClockΫϥεΛ࡞Δ → ಡΈࠐ·ͳ͍ͱࢀরͰ͖ͳ͍ͷ͸มΘΒͳ͍͸ͣ 
  4. ``` require 'clock' describe Clock do specify { expect(Clock.new).to be_an_instance_of(Clock)

    } end ``` $ bundle exec rspec spec . Finished in 0.00462 seconds (files took 0.09435 seconds to load) 1 example, 0 failures require΋ͨ͠ˠ͜ΜͲͦ͜௨Δ͸ͣ 
  5. --- a/spec/clock_spec.rb +++ b/spec/clock_spec.rb @@ -2,4 +2,10 @@ require 'clock'

    describe Clock do specify { expect(Clock).to be } + + context 'new(10, 20, 00)' do + let(:clock) { Clock.new(10, 20, 0) } + + specify { expect(clock.hour).to eq(10) } + end end ΠϯελϯεΛੜ੒͢Δςετ 
  6. $ bundle exec rspec .F Failures: 1) Clock new(10, 20,

    00) Failure/Error: let(:clock) { Clock.new(10, 20, 0) } ArgumentError: wrong number of arguments (given 3, expected 0) # ./spec/clock_spec.rb:7:in `initialize' # ./spec/clock_spec.rb:7:in `new' # ./spec/clock_spec.rb:7:in `block (3 levels) in <top (required)>' # ./spec/clock_spec.rb:9:in `block (3 levels) in <top (required)>' Finished in 0.00186 seconds (files took 0.08364 seconds to load) 2 examples, 1 failure Failed examples: rspec ./spec/clock_spec.rb:9 # Clock new(10, 20, 00) ੜ੒͢ΔςετˠίϯετϥΫλҾ਺͕ҧࣦͬͯഊ͢Δ͸ͣ 
  7. ``` --- a/lib/clock.rb +++ b/lib/clock.rb @@ -1,2 +1,8 @@ class

    Clock + def initialize(*args) + end + + def hour + 10 + end end ``` Ծ࣮૷ (Fake it) 
  8. --- a/spec/clock_spec.rb +++ b/spec/clock_spec.rb @@ -8,4 +8,10 @@ describe Clock

    do specify { expect(clock.hour).to eq(10) } end + + context 'new(12, 30, 00)' do + let(:clock) { Clock.new(12, 30, 0) } + + specify { expect(clock.hour).to eq(12) } + end end ࡾ֯ଌྔ (Triangulation) 
  9. --- a/lib/clock.rb +++ b/lib/clock.rb @@ -1,8 +1,9 @@ class Clock

    - def initialize(*args) + def initialize(hour, *) + @hour = hour end def hour - 10 + @hour end end 12:30ͷςετΛ௨͢Α͏ͳɺ࠷௿ݶͷ࣮૷ 
  10. --- a/lib/clock.rb +++ b/lib/clock.rb @@ -1,9 +1,8 @@ class Clock

    + attr_reader :hour + def initialize(hour, *) @hour = hour end - def hour - @hour - end end {ಈ࡞͢Δ && ͖Ε͍ͳ}ίʔυΛ໨ࢦͯ͠ 
  11. --- a/spec/clock_spec.rb +++ b/spec/clock_spec.rb @@ -1,8 +1,6 @@ require 'clock'

    describe Clock do - specify { expect(Clock.new).to be_an_instance_of(Clock) } - context 'new(10, 20, 00)' do let(:clock) { Clock.new(10, 20, 0) } ςετίʔυ΋ϦϑΝΫλϦϯά͢Δ 
  12. --- a/spec/clock_spec.rb +++ b/spec/clock_spec.rb @@ -5,6 +5,8 @@ describe Clock

    do let(:clock) { Clock.new(10, 20, 0) } specify { expect(clock.hour).to eq(10) } + specify { expect(clock.min).to eq(20) } + specify { expect(clock.sec).to eq(0) } end context 'new(12, 30, 00)' do ෼ͱඵʹର͢Δςετˠ#hour, #minϝιου͕ͳ͍ͷͰࣦഊ͢Δ͸ͣ 
  13. --- a/lib/clock.rb +++ b/lib/clock.rb @@ -1,8 +1,10 @@ class Clock

    - attr_reader :hour + attr_reader :hour, :min, :sec - def initialize(hour, *) + def initialize(hour, min, sec) @hour = hour + @min = min + @sec = sec end end ໌നͳ࣮૷ (Obvious Implementation) 
  14. --- a/spec/clock_spec.rb +++ b/spec/clock_spec.rb @@ -22,4 +22,11 @@ describe Clock

    do + + describe '#to_time' do + let(:clock) { Clock.new(10, 20, 0) } + let(:time) { clock.to_time(Date.today) } + + specify { + expect(time.iso8601).to eq ‘2017-08-08T10:20:00+09:00' + } + end end APIΛߟ͑Δ #to_time(<#Date>)͕ૉ௚ͦ͏ˠ#to_timeͷ࣮૷ͳ͍ 
  15. --- a/lib/clock.rb +++ b/lib/clock.rb @@ -7,6 +7,10 @@ class Clock

    @sec = varify_valid_clock_input(sec, 60) end + def to_time(date) + Time.local(2017, 8, 8, 10, 20) + end + private def varify_valid_clock_input(val, max) ճ౴ྫ: Ծ࣮૷ 
  16. ‣ ࣍ͷ೔࣌ɺલͷ೔࣌Λಋग़Ͱ͖Δ ‣ ͖ΐ͏ͷ 10:20 ͸ 2017-08-08T10:20:00 JST ‣ 8/1ͷ

    10:20 ͸ 2017-08-01T10:20:00 JST ࡾ֯ଌྔͷexampleΛ۩ମԽ 
  17. --- a/spec/clock_spec.rb +++ b/spec/clock_spec.rb @@ -25,8 +25,16 @@ describe Clock

    do describe '#to_time' do let(:clock) { Clock.new(10, 20, 0) } - let(:time) { clock.to_time(Date.today) } + context '(Date.today)' do + let(:time) { clock.to_time(Date.today) } - specify { expect(time.iso8601).to eq '2017-08-08T10:20:00+09:00' } + specify { expect(time.iso8601).to eq '2017-08-08T10:20:00+09:00' } + end + + context '(2017-08-01)' do + let(:time) { clock.to_time(Date.new(2017, 8, 1)) } + + specify { expect(time.iso8601).to eq '2017-08-08T10:20:00+09:00' } + end end end ࡾ֯ଌྔ 
  18. --- a/lib/clock.rb +++ b/lib/clock.rb @@ -8,7 +8,7 @@ class Clock

    end def to_time(date) - Time.local(2017, 8, 8, 10, 20) + Time.local(date.year, date.month, date.day, @hour, @min, @sec) end private ࡾ֯ଌྔ 
  19. --- a/spec/clock_spec.rb +++ b/spec/clock_spec.rb @@ -26,7 +26,7 @@ describe Clock

    do describe '#to_time' do let(:clock) { Clock.new(10, 20, 0) } context '(Date.today)' do - let(:time) { clock.to_time(Date.today) } + let(:time) { clock.to_time(Date.new(2017, 8, 8)) } specify { expect(time.iso8601).to eq '2017-08-08T10:20:00+09:0 end ςετίʔυͷվળ: ֎෦ґଘ஋Λແ͘͢ 
  20. ‣ Q3. `hello :innocent:` Λ `hello `ʹม׵͢Δ
 ϓϩάϥϜΛTDDͰ࣮૷͠ͳ͍͞ɻ ‣ 3-1.

    ΠϯλʔϑΣʔεΛఆٛ͢ΔͷΈͷࣦഊ͢ΔςετΛ࡞੒͠ɺίϛοτ͠ͳ͍͞ ‣ 3-2. 3-1ͷςετ͕௨ΔΑ͏ͳԾ࣮૷Λ௥Ճ͠ɺίϛοτ͠ͳ͍͞ ‣ 3-3. `hello :sunglasses:` Λ `hello ` ʹ͢Δࡾ֯ଌྔͷςετΛ௥Ճ͠ɺίϛοτ͠ͳ͍͞ ‣ 3-4. 3-1, 3-3྆ํͷςετ͕௨ΔΑ͏ʹ࣮૷͠ίϛοτ͠ͳ͍͞ ‣ ώϯτ: ͸ "\u{1f607}" ͸ "\u{1f60e}" ͰදࣔͰ͖·͢ ‣ ίϛοτϝοηʔδʹ͸ʮ3-xʯͷΑ͏ͳ൪߸Λ;͘Ί͍ͯͩ͘͞ɻ ࣮श 
  21. ‣ Q4. `hello :innocent: :sunglasses:` Λ `hello `ʹ
 ม׵͢ΔϓϩάϥϜΛTDDͰ࣮૷͠ͳ͍͞ɻ ‣

    4-1a. ࣗ৴͕͋Δ৔߹͸ɺҰؾʹ໌֬ͳ࣮૷Λ͠ɺίϛοτ͠ͳ͍͞ ‣ 4-1b. ࣗ৴͕ͳ͍৔߹ɺࣦഊ͢ΔςετɺԾ࣮૷ɺࡾ֯ଌྔͷςετΛ௥Ճ͠ɺίϛοτ͠ͳ͍͞ ࣮श 
  22. ‣ Q5. gemoji gem Λ࢖͍ɺม׵ίʔυΛϦϑΝΫλϦϯά ͠ͳ͍͞ ‣ https://github.com/github/gemoji ‣ 5-1.

    ςετίʔυͷมߋ͸ͤͣɺϓϩμΫτίʔυͷΈमਖ਼͠ͳ͍͞ɻ ࣮श 
  23. ‣ Q6. ม׵ίʔυΛ֦ு͠ɺ֦ுUnicodeͰ͸ͳ͘
 <img>λάΛग़ྗ͢Δ͜ͱʮ΋ʯՄೳʹ͠ͳ͍͞ɻ ‣ 期待する出力 "hello <img src=unicode/1f607.png />"

    ‣ 6-1. ग़ྗΛ੾Γସ͑ΔΠϯλʔϑΣʔεΛઃܭ͠ɺࣦഊ͢ΔςετΛ௥Ճ͠ɺίϛοτ͠ͳ͍͞ɻ ‣ 6-2. ϓϩμΫτίʔυΛ࣮૷͠ɺίϛοτ͠ͳ͍͞ɻ ࣮श