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

君はPower Assertを知っているか #potatotips

君はPower Assertを知っているか #potatotips

potatotips #7 2015/5/15 at DeNA

9278c3a06b8d8752fb913dea93f959c1?s=128

FUJI Goro

May 15, 2014
Tweet

Transcript

  1. ܅͸ Power Assert Λ஌͍ͬͯΔ͔ FUJI Goro (@__gfx__), a software engineer

    in COOKPAD #potatotips 2014/5/15 at DeNA
  2. FUJI Goro (@__gfx__) • software engineer @ COOKPAD Inc. •

    Android / iOS ޲͚ڞ௨ϥΠϒϥϦͷઃܭɾ։ൃ
  3. Assertions in Java // EXPR͕ࣦഊ͢Δͱ throw new AssertionError() Λ࣮ߦ͢Δ assert

    foo == 42; ! // comment͸AssertionErrorͷҾ਺ͱͯ͠౉͞ΕΔ assert foo == 42 : “with message”; ! // assertແޮ࣌͸͕ࣜධՁ͞Εͳ͍ͷͰॏΊͷલఏ৚݅νΣοΫ΋Մ assert checkParamsButSlow(foo);
  4. Assertions in Android • AndroidͰ͸Javaͷassertจ͕࢖͑Δ • `assert getActivity() != null;`

    • ͨͩ͠assert͸σϑΥϧτͰແޮ • `adb shell setprop debug.assert 1` Ͱ༗ޮԽ • …Ͱ͖Δ͜ͱ΋͋Δʢ࣮ػͩͱͰ͖ͳ͍ʁʣ
  5. Assertionsͷ໰୊఺ • εΠον͕ΞϓϦ͝ͱͰ͸ͳ͘୺຤͝ͱ • ͦ΋ͦ΋ޮ͔ͳ͔ͬͨΓ͢Δ͠ • ։ൃऀશһͰৗʹ༗ޮʹ͢Δͱ͍͏ͷ͸೉͍͠ • ࣦഊ࣌ͷ৘ใʹ๡͍͠ •

    ৄࡉΛ௥͏ͨΊʹϒϨʔΫϙΠϯτΛ࢓ࠐΜͰ σόοά͢Δඞཁ͕͋Δ
  6. ͭΒ͍

  7. ͱ͜ΖͰ Power Assert Λ஌͍ͬͯ·͔͢

  8. Power Assert ͷػೳ • assertࣦഊ࣌ʹؔ܎͢Δࣜͷ஋Λදࣔ͢Δ def x = new File(“foo.groovy”)

    assert x.absolutePath.contains("bar") // shows: Assertion failed: ! assert x.absolutePath.contains("bar") | | | | | false | /path/to/foo.groovy foo.groovy at foo.run(foo.groovy:4)
  9. ࣮૷ͷͨΊͷπʔϧνΣΠϯ • ࣮ߦલʹίʔυΛॻ͖׵͑Δඞཁ͕͋Δ • JavaScript൛͸ιʔείʔυΛॻ͖׵͑Δ • Spock͸ίϯύΠϧޙͷASTΛॻ͖׵͑Δ • ASTʹରͯ͠ϝλϓϩάϥϛϯάͰ͖Δ؀ڥ ͕͋Ε͹OK

  10. AndroidͰ΋Power Assert͕ ࢖͍͍ͨੈքઢʹߦ͖͍ͨ…

  11. – OSSքͷ֨ݴ “ͩͬͨΒ͓લ͕΍Ε”

  12. ΍Γ·ͨ͠

  13. Android Power Assert Plugin • assertจΛڧ੍తʹ༗ޮԽ • assertࣦഊ࣌ʹ৘ใΛ௥Ճ • ϦϦʔεϏϧυͰ͸Կ΋͠ͳ͍

    • ࣮ߦ଎౓ʹӨڹ͸΄ͱΜͲͳ͠ • APKͷαΠζ΋૿͑ͳ͍
  14. ग़ྗྫ assert findViewById(R.id.never).isFocused();

  15. σϑΥϧτͷྫ֎

  16. assert findViewById(R.id.never).isFocused(); with default mode java.lang.NullPointerException: at MainActivity.onButton2Click(MainActivity.java:28) (…stacktrace͕ଓ͘…) !

    // NOTE: // KitKat͔Β͸΋͏গ͠৘ใ͕૿͑ͯɺͳʹ͕nullͩͬͨͷ͔Θ͔Γ΍͘͢ͳ͍ͬͯΔ
  17. Power Assert༗ޮͷͱ͖

  18. assert findViewById(R.id.never).isFocused(); with power assert java.lang.NullPointerException: 26: void onButton2Click() {

    27> assert findViewById(R.id.never).isFocused(); 27: } MainActivity.findViewById()=<null> at MainActivity.onButton2Click(MainActivity.java:28) (…stacktrace͕ଓ͘…)
  19. ࢖͍ํ // The root build.gradle buildscript { dependencies { classpath

    ‘com.github.gfx:android-power-assert-plugin:0.10.+’ } } ! // in build.gradle of a module apply plugin: ‘android-power-assert’
  20. ͋ͱ͸assertจΛ࢖͏͚ͩ

  21. Power Assert ೋͭͷଆ໘

  22. ΞϓϦέʔγϣϯίʔυͰ࢖͏ • ؀ڥ͕ಛఆͷঢ়ଶͰ͋Δ͜ͱΛอূ͢Δ • Ҿ਺ͷνΣοΫ΍nullνΣοΫͳͲ • Android StudioͰNPEܯࠂΛ੩ΊΔͨΊ • ϦϦʔε൛Ͱassertจ͸ফ͑ΔͷͰɺ͋͘·Ͱ΋

    ʮ։ൃऀ͕ਖ਼͍͠ίʔυΛॻ͍͍ͯΔ͜ͱΛอ ূ͢ΔʯͨΊʹ࢖͏
  23. ςετͰ࢖͏ • ൚༻ςετϚονϟʔͱͯ͠࢖͏ • JUnitͷassertEquals()ͷҾ਺ͷॱ൪Λߟ͑ͳ͘ ͯΑ͘ͳΔ • ࣜͷ్தܦաΛදࣔ͢ΔͷͰɺෳ਺ͷ assertXXX()Λ·ͱΊΒΕΔ •

    assert obj.foo().bar().equals(“baz”)
  24. How it works

  25. JavassistʹΑΔόΠτίʔυૢ࡞ • $assertionsDisabledΛৗʹfalseʹ͢Δ • assertจʢ͕ίϯύΠϧ͞ΕͨόΠτίʔυʣ Λݟ͚ͭͨΒपล৘ใΛूΊͯจࣈྻΛੜ੒ ͢ΔίʔυΛૠೖ͢Δ • field access

    / method call͸try-catchͰғΜͰ NPEΛΩϟονˍपล৘ใΛ෇༩ͯ͠re-throw
  26. ՝୊ • ෇Ճ৘ใ͕ଟ͗͢Δ • ͍·͸ϩʔΧϧม਺Λ͢΂ͯdump͍ͯ͠Δ • SpockͷΑ͏ͳࠩ෼දࣔΛ͍ͨ͠ • ͍·͸commonsͷToStringBuilderΛ࢖͍ͬͯΔ •

    JavassistͰେྔͷίʔυΛॻ͘ͷ͸ਏ͍ͷͰɺग़ྗ ϑΥʔϚολΛผʹ࣮૷͢Δඞཁ͕͋Δ
  27. We are hiring! https://info.cookpad.com/jobs/

  28. ͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ https://github.com/gfx/android-power-assert-plugin ਓபืूத