SlideShare a Scribd company logo
1 of 40
Download to read offline
Deep	into	Ruby	Code	Coverage
Code	Coverage
1.	 What	&	Why?
2.	 How?
3.	 Under	the	hood
What	&	Why
Covered:	ran	by	a	least	one	test.
Code	Coverage:	identifies	uncovered	code.
tests	missing
unused	code
overly	cautious	code
How	-	Method	Coverage
def foo(something: false)
if something
bar
end
end
# no test
Method	coverage:	0%
How	-	Line	Coverage
def foo(something: false)
if something
bar
end
end
# test:
expect(foo).to...
Method	coverage:	100%
Line	coverage:	75%
How	-	Node	Coverage
def foo(something: false)
bar if something
end
# test:
expect(foo).to...
Method	coverage:	100%
Line	coverage:	100%
Node	coverage:	80%
How	-	Branch	coverage
def foo(something: false)
bar if something
end
# test:
expect(foo(something: true)).to...
How	-	Branch	coverage
def foo(something: false)
bar if something
fail_unless_bar_was_run
end
# test:
expect(foo(something: true)).to...
Method	coverage:	100%
Line	coverage:	100%
Node	coverage:	100%
Branch	coverage:	50%
I	want	it
Sounds	awesome
I	want	it
I	want	it
I	want	it
Live	demo
Real	world	examples
Wiki	Education	Dashboard
100%	loc	=>	96.75%	loc
AsciiDoc	-	Kramdown
100%	coverage	=>	99%	of	nodes,	77%	of	branches
found	3	bugs	and	counting
Typical	examples
# Untested guard clauses
return unless Features.wiki_ed?
# Unused scopes
class Revision < ActiveRecord::Base
scope :after_date, ->(date) { where('date > ?', date) }
end
# Untested edge cases
siblings = (parent = opts[:parent]) && parent.children
DeepCover	vs	MRI
Feature MRI DeepCover
Line	coverage partial ✓
Node	coverage no ✓
Branch	coverage partial ✓
Method	coverage ✓ ~
Slowdown <	1% ~20%
Platform	support Ruby	2.5+Ruby	2.1+,	JRuby
DeepCover	vs	MRI
Branch	coverage MRIDeepCover
if	/	unless	/	?: ✓ ✓
case	/	when ✓ ✓
❘❘	/	&& no ✓
foo&.bar ✓ ✓
{❘foo = 42, bar:
43❘}
no ✓
while	/	until ✓ !
DeepCover	vs	MRI
require 'deep_cover/builtin_takeover'
Even	compatible	with	secret	2.5+	API
Under	the	Hood
How	to	implement?
Counters
Under	the	Hood	-	MRI
static int
iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *const ret, ...)
{
const int line = (int)nd_line(node);
const enum node_type type = nd_type(node);
if (ISEQ_COMPILE_DATA(iseq)->last_line == line) {
/* ignore */
}
else {
if (node->flags & NODE_FL_NEWLINE) {
ISEQ_COMPILE_DATA(iseq)->last_line = line;
ADD_TRACE_LINE_COVERAGE(ret, line);
ADD_TRACE(ret, RUBY_EVENT_LINE);
}
}
// ...
ADD_TRACE_LINE_COVERAGE:	same	API	as	TracePoint,	just	not	acccessible
from	Ruby.
Under	the	Hood	-	MRI
// ...
switch (type) {
case NODE_BLOCK:{
while (node && nd_type(node) == NODE_BLOCK) {
CHECK(COMPILE_(ret, "BLOCK body", node->nd_head,
(node->nd_next ? 1 : popped)));
node = node->nd_next;
}
if (node) {
CHECK(COMPILE_(ret, "BLOCK next", node->nd_next, popped));
}
break;
}
case NODE_IF:
case NODE_UNLESS:
CHECK(compile_if(iseq, ret, node, popped, type));
break;
…	1600	more	lines…
Under	the	Hood	-	MRI
; a = 1
b = 2; c = 3
d = b + c
puts d
Under	the	Hood	-	MRI
ADD_TRACE_LINE_COVERAGE; a = 1
ADD_TRACE_LINE_COVERAGE; b = 2; c = 3
ADD_TRACE_LINE_COVERAGE; d = b + c
ADD_TRACE_LINE_COVERAGE; puts d
Under	the	Hood	-	DeepCover
$counter[0]+=1; a = 1
$counter[1]+=1; b = 2; c = 3
$counter[2]+=1; d = b + c
$counter[3]+=1; puts d
Under	the	Hood	-	DeepCover
$counter[0]+=1; a = 1
$counter[1]+=1; b = 2; $counter[2]+=1; c = 3
$counter[3]+=1; d = b + c
$counter[4]+=1; puts d
Under	the	Hood	-	DeepCover
$counter[0]+=1; a = 1
b = 2; c = 3
d = b + c; $counter[1]+=1;
puts d;
Under	the	Hood	-	DeepCover
use	whitequark/parser	gem
rewrite	using	rules	for	each	type	of	node
only	code	insertions
no	insertion	of	new	lines
deduce	execution	from	trackers
Either:
monkeypatch	require,	load	and	autoload
define	RubyVM::InstructionSequence.load_iseq
clone	project	&	rewrite
Rewriting	rules
puts 'Hello'
# becomes
temp = puts 'hello'; tracker += 1 ; temp
Rewriting	rules	-	most	complex	case
foo&.bar
# becomes
temp = foo
if nil != temp
branch_tracker += 1
temp = temp&.bar
tracker += 1
temp
end
Rewriting	rules	-	actually	most	complex	case
obj.a, b, obj.c = [...]
# becomes
# ???
Rewriting	rules	-	actually	most	complex	case
obj.a, b, obj.c = [...]
# becomes
(tracker += 1; obj).a, b, 
(tracker2 += 1; obj).c = [...]; tracker3 += 1
Where	can	control	flow	be	interrupted?
foo = 1
bar = 2
puts(foo + bar)
Where	can	control	flow	be	interrupted?
require "active_support/core_ext/object/try"
module DateAndTime
module Calculations
WEEKEND_DAYS = [ 6, 0 ]
def on_weekend?
WEEKEND_DAYS.include?(wday)
end
def all_day
beginning_of_day..end_of_day
end
def end_of_week(start_day = Date.beginning_of_week)
# ...
end
alias :at_end_of_week :end_of_week
end
end
Future
Reporting	for	Coveralls/CodeCov
Better	handling	for	complex	setups	(multi-version	/	CI	/	…)
100%	coverage	for	DeepCover	(!)
Thanks
Coauthor	and	original	instigator
Maxime	Lapointe	(maxlap)
MRI	branch	coverage
Yusuke	Endoh	(mame)
Yuichiro	Kaneko	(yui-knk)
Early	adopters
Sage	Ross	(ragesoss),	Dan	Allen	(mojavelinux),	Cedric	(stripedpumpkin),	Sylvain
Joyeux
Deep into Ruby Code Coverage

More Related Content

Similar to Deep into Ruby Code Coverage

Functional Pe(a)rls version 2
Functional Pe(a)rls version 2Functional Pe(a)rls version 2
Functional Pe(a)rls version 2osfameron
 
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyC++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyGrejoJoby1
 
COG Back to the Future, Part II
COG Back to the Future, Part IICOG Back to the Future, Part II
COG Back to the Future, Part IIESUG
 
Scaling Rails with Ruby-prof -- Ruby Conf Kenya 2017 by Ben Hughes
Scaling Rails with Ruby-prof -- Ruby Conf Kenya 2017 by Ben Hughes Scaling Rails with Ruby-prof -- Ruby Conf Kenya 2017 by Ben Hughes
Scaling Rails with Ruby-prof -- Ruby Conf Kenya 2017 by Ben Hughes Michael Kimathi
 
Hack in the Box Keynote 2006
Hack in the Box Keynote 2006Hack in the Box Keynote 2006
Hack in the Box Keynote 2006Mark Curphey
 
Source Filters in Perl 2010
Source Filters in Perl 2010Source Filters in Perl 2010
Source Filters in Perl 2010hendrikvb
 
深入淺出C語言
深入淺出C語言深入淺出C語言
深入淺出C語言Simen Li
 
Kamil witecki asynchronous, yet readable, code
Kamil witecki asynchronous, yet readable, codeKamil witecki asynchronous, yet readable, code
Kamil witecki asynchronous, yet readable, codeKamil Witecki
 
Practical File of C Language
Practical File of C LanguagePractical File of C Language
Practical File of C LanguageRAJWANT KAUR
 
Lazy, Lazy, Lazy all the things !
Lazy, Lazy, Lazy all the things !Lazy, Lazy, Lazy all the things !
Lazy, Lazy, Lazy all the things !Shaunak Pagnis
 
The Perl API for the Mortally Terrified (beta)
The Perl API for the Mortally Terrified (beta)The Perl API for the Mortally Terrified (beta)
The Perl API for the Mortally Terrified (beta)Mike Friedman
 
Lessons from a coding veteran - Web Directions @Media
Lessons from a coding veteran - Web Directions @MediaLessons from a coding veteran - Web Directions @Media
Lessons from a coding veteran - Web Directions @MediaTom Croucher
 
Beijing Perl Workshop 2008 Hiveminder Secret Sauce
Beijing Perl Workshop 2008 Hiveminder Secret SauceBeijing Perl Workshop 2008 Hiveminder Secret Sauce
Beijing Perl Workshop 2008 Hiveminder Secret SauceJesse Vincent
 

Similar to Deep into Ruby Code Coverage (20)

Functional Pe(a)rls version 2
Functional Pe(a)rls version 2Functional Pe(a)rls version 2
Functional Pe(a)rls version 2
 
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyC++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
 
lda2vec Text by the Bay 2016
lda2vec Text by the Bay 2016lda2vec Text by the Bay 2016
lda2vec Text by the Bay 2016
 
COG Back to the Future, Part II
COG Back to the Future, Part IICOG Back to the Future, Part II
COG Back to the Future, Part II
 
Scaling Rails with Ruby-prof -- Ruby Conf Kenya 2017 by Ben Hughes
Scaling Rails with Ruby-prof -- Ruby Conf Kenya 2017 by Ben Hughes Scaling Rails with Ruby-prof -- Ruby Conf Kenya 2017 by Ben Hughes
Scaling Rails with Ruby-prof -- Ruby Conf Kenya 2017 by Ben Hughes
 
Format String Exploitation
Format String ExploitationFormat String Exploitation
Format String Exploitation
 
Hack in the Box Keynote 2006
Hack in the Box Keynote 2006Hack in the Box Keynote 2006
Hack in the Box Keynote 2006
 
Source Filters in Perl 2010
Source Filters in Perl 2010Source Filters in Perl 2010
Source Filters in Perl 2010
 
深入淺出C語言
深入淺出C語言深入淺出C語言
深入淺出C語言
 
Talk About Performance
Talk About PerformanceTalk About Performance
Talk About Performance
 
Kamil witecki asynchronous, yet readable, code
Kamil witecki asynchronous, yet readable, codeKamil witecki asynchronous, yet readable, code
Kamil witecki asynchronous, yet readable, code
 
Practical File of C Language
Practical File of C LanguagePractical File of C Language
Practical File of C Language
 
Lazy, Lazy, Lazy all the things !
Lazy, Lazy, Lazy all the things !Lazy, Lazy, Lazy all the things !
Lazy, Lazy, Lazy all the things !
 
C++ in 10 Hours.pdf.pdf
C++ in 10 Hours.pdf.pdfC++ in 10 Hours.pdf.pdf
C++ in 10 Hours.pdf.pdf
 
Presentation1
Presentation1Presentation1
Presentation1
 
Mips1
Mips1Mips1
Mips1
 
The Perl API for the Mortally Terrified (beta)
The Perl API for the Mortally Terrified (beta)The Perl API for the Mortally Terrified (beta)
The Perl API for the Mortally Terrified (beta)
 
Data Binding in qooxdoo
Data Binding in qooxdooData Binding in qooxdoo
Data Binding in qooxdoo
 
Lessons from a coding veteran - Web Directions @Media
Lessons from a coding veteran - Web Directions @MediaLessons from a coding veteran - Web Directions @Media
Lessons from a coding veteran - Web Directions @Media
 
Beijing Perl Workshop 2008 Hiveminder Secret Sauce
Beijing Perl Workshop 2008 Hiveminder Secret SauceBeijing Perl Workshop 2008 Hiveminder Secret Sauce
Beijing Perl Workshop 2008 Hiveminder Secret Sauce
 

Recently uploaded

React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...Karmanjay Verma
 
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...JET Technology Labs White Paper for Virtualized Security and Encryption Techn...
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...amber724300
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesBernd Ruecker
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesManik S Magar
 
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxGenerative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxfnnc6jmgwh
 
A Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxA Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxAna-Maria Mihalceanu
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Strongerpanagenda
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integrationmarketing932765
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructureitnewsafrica
 
QMMS Lesson 2 - Using MS Excel Formula.pdf
QMMS Lesson 2 - Using MS Excel Formula.pdfQMMS Lesson 2 - Using MS Excel Formula.pdf
QMMS Lesson 2 - Using MS Excel Formula.pdfROWELL MARQUINA
 
Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Karmanjay Verma
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 

Recently uploaded (20)

React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
 
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...JET Technology Labs White Paper for Virtualized Security and Encryption Techn...
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architectures
 
How Tech Giants Cut Corners to Harvest Data for A.I.
How Tech Giants Cut Corners to Harvest Data for A.I.How Tech Giants Cut Corners to Harvest Data for A.I.
How Tech Giants Cut Corners to Harvest Data for A.I.
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
 
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxGenerative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
 
A Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxA Glance At The Java Performance Toolbox
A Glance At The Java Performance Toolbox
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
 
QMMS Lesson 2 - Using MS Excel Formula.pdf
QMMS Lesson 2 - Using MS Excel Formula.pdfQMMS Lesson 2 - Using MS Excel Formula.pdf
QMMS Lesson 2 - Using MS Excel Formula.pdf
 
Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 

Deep into Ruby Code Coverage