Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
君はPower Assertを知っているか #potatotips
Search
FUJI Goro
May 15, 2014
Technology
5
4k
君はPower Assertを知っているか #potatotips
potatotips #7 2015/5/15 at DeNA
FUJI Goro
May 15, 2014
Tweet
Share
More Decks by FUJI Goro
See All by FUJI Goro
How to Boost Your Code with WebAssembly
gfx
2
2.9k
AssemblyScriptでライブラリコードの高速化をしてみる
gfx
5
3.1k
実践TypeScriptトークバトル
gfx
1
1.1k
歴史的経緯の説明 as code
gfx
7
2.8k
Elasticsearchによる 全文検索の実装 in Rails
gfx
6
9.4k
すばらしきGraphQLのSEKAIへようこそ
gfx
20
9.3k
マルチテナント・ウェブアプリケーションの実践
gfx
14
9.5k
How to choose the ORM on Android
gfx
1
4.2k
How Do We Get Along With Static Types
gfx
5
3.3k
Other Decks in Technology
See All in Technology
いまからでも遅くない!コンテナでWebアプリを動かしてみよう!コンテナハンズオン編
nomu
0
150
OPENLOGI Company Profile for engineer
hr01
1
20k
Share my, our lessons from the road to re:Invent
naospon
0
140
システム・ML活用を広げるdbtのデータモデリング / Expanding System & ML Use with dbt Modeling
i125
1
320
Pwned Labsのすゝめ
ken5scal
1
350
php-conference-nagoya-2025
fuwasegu
0
150
PHPで印刷所に入稿できる名札データを作る / Generating Print-Ready Name Tag Data with PHP
tomzoh
0
180
クラウド食堂とは?
hiyanger
0
110
データエンジニアリング領域におけるDuckDBのユースケース
chanyou0311
9
2.1k
AWSアカウントのセキュリティ自動化、どこまで進める? 最適な設計と実践ポイント
yuobayashi
7
540
Apache Iceberg Case Study in LY Corporation
lycorptech_jp
PRO
0
300
2/18 Making Security Scale: メルカリが考えるセキュリティ戦略 - Coincheck x LayerX x Mercari
jsonf
0
170
Featured
See All Featured
What's in a price? How to price your products and services
michaelherold
244
12k
Being A Developer After 40
akosma
89
590k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
45
9.4k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
193
16k
Building an army of robots
kneath
303
45k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
21
2.5k
Measuring & Analyzing Core Web Vitals
bluesmoon
6
250
The Illustrated Children's Guide to Kubernetes
chrisshort
48
49k
Fashionably flexible responsive web design (full day workshop)
malarkey
406
66k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
27
1.6k
Testing 201, or: Great Expectations
jmmastey
42
7.2k
The Cult of Friendly URLs
andyhume
78
6.2k
Transcript
܅ Power Assert Λ͍ͬͯΔ͔ FUJI Goro (@__gfx__), a software engineer
in COOKPAD #potatotips 2014/5/15 at DeNA
FUJI Goro (@__gfx__) • software engineer @ COOKPAD Inc. •
Android / iOS ͚ڞ௨ϥΠϒϥϦͷઃܭɾ։ൃ
Assertions in Java // EXPR͕ࣦഊ͢Δͱ throw new AssertionError() Λ࣮ߦ͢Δ assert
foo == 42; ! // commentAssertionErrorͷҾͱͯ͠͞ΕΔ assert foo == 42 : “with message”; ! // assertແޮ͕࣌ࣜධՁ͞Εͳ͍ͷͰॏΊͷલఏ݅νΣοΫՄ assert checkParamsButSlow(foo);
Assertions in Android • AndroidͰJavaͷassertจ͕͑Δ • `assert getActivity() != null;`
• ͨͩ͠assertσϑΥϧτͰແޮ • `adb shell setprop debug.assert 1` Ͱ༗ޮԽ • …Ͱ͖Δ͜ͱ͋Δʢ࣮ػͩͱͰ͖ͳ͍ʁʣ
Assertionsͷ • εΠον͕ΞϓϦ͝ͱͰͳ͘͝ͱ • ͦͦޮ͔ͳ͔ͬͨΓ͢Δ͠ • ։ൃऀશһͰৗʹ༗ޮʹ͢Δͱ͍͏ͷ͍͠ • ࣦഊ࣌ͷใʹ͍͠ •
ৄࡉΛ͏ͨΊʹϒϨʔΫϙΠϯτΛࠐΜͰ σόοά͢Δඞཁ͕͋Δ
ͭΒ͍
ͱ͜ΖͰ Power Assert Λ͍ͬͯ·͔͢
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)
࣮ͷͨΊͷπʔϧνΣΠϯ • ࣮ߦલʹίʔυΛॻ͖͑Δඞཁ͕͋Δ • JavaScript൛ιʔείʔυΛॻ͖͑Δ • SpockίϯύΠϧޙͷASTΛॻ͖͑Δ • ASTʹରͯ͠ϝλϓϩάϥϛϯάͰ͖Δڥ ͕͋ΕOK
AndroidͰPower Assert͕ ͍͍ͨੈքઢʹߦ͖͍ͨ…
– OSSքͷ֨ݴ “ͩͬͨΒ͓લ͕Ε”
Γ·ͨ͠
Android Power Assert Plugin • assertจΛڧ੍తʹ༗ޮԽ • assertࣦഊ࣌ʹใΛՃ • ϦϦʔεϏϧυͰԿ͠ͳ͍
• ࣮ߦʹӨڹ΄ͱΜͲͳ͠ • APKͷαΠζ૿͑ͳ͍
ग़ྗྫ assert findViewById(R.id.never).isFocused();
σϑΥϧτͷྫ֎
assert findViewById(R.id.never).isFocused(); with default mode java.lang.NullPointerException: at MainActivity.onButton2Click(MainActivity.java:28) (…stacktrace͕ଓ͘…) !
// NOTE: // KitKat͔Β͏গ͠ใ͕૿͑ͯɺͳʹ͕nullͩͬͨͷ͔Θ͔Γ͘͢ͳ͍ͬͯΔ
Power Assert༗ޮͷͱ͖
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͕ଓ͘…)
͍ํ // 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’
͋ͱassertจΛ͏͚ͩ
Power Assert ೋͭͷଆ໘
ΞϓϦέʔγϣϯίʔυͰ͏ • ڥ͕ಛఆͷঢ়ଶͰ͋Δ͜ͱΛอূ͢Δ • ҾͷνΣοΫnullνΣοΫͳͲ • Android StudioͰNPEܯࠂΛ੩ΊΔͨΊ • ϦϦʔε൛Ͱassertจফ͑ΔͷͰɺ͋͘·Ͱ
ʮ։ൃऀ͕ਖ਼͍͠ίʔυΛॻ͍͍ͯΔ͜ͱΛอ ূ͢ΔʯͨΊʹ͏
ςετͰ͏ • ൚༻ςετϚονϟʔͱͯ͠͏ • JUnitͷassertEquals()ͷҾͷॱ൪Λߟ͑ͳ͘ ͯΑ͘ͳΔ • ࣜͷ్தܦաΛදࣔ͢ΔͷͰɺෳͷ assertXXX()Λ·ͱΊΒΕΔ •
assert obj.foo().bar().equals(“baz”)
How it works
JavassistʹΑΔόΠτίʔυૢ࡞ • $assertionsDisabledΛৗʹfalseʹ͢Δ • assertจʢ͕ίϯύΠϧ͞ΕͨόΠτίʔυʣ Λݟ͚ͭͨΒपลใΛूΊͯจࣈྻΛੜ ͢ΔίʔυΛૠೖ͢Δ • field access
/ method calltry-catchͰғΜͰ NPEΛΩϟονˍपลใΛ༩ͯ͠re-throw
՝ • Ճใ͕ଟ͗͢Δ • ͍·ϩʔΧϧมΛͯ͢dump͍ͯ͠Δ • SpockͷΑ͏ͳࠩදࣔΛ͍ͨ͠ • ͍·commonsͷToStringBuilderΛ͍ͬͯΔ •
JavassistͰେྔͷίʔυΛॻ͘ͷਏ͍ͷͰɺग़ྗ ϑΥʔϚολΛผʹ࣮͢Δඞཁ͕͋Δ
We are hiring! https://info.cookpad.com/jobs/
͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ https://github.com/gfx/android-power-assert-plugin ਓபืूத