SlideShare a Scribd company logo
1 of 18
Download to read offline
Functional
Programming's
Relentless Bug Hunter:
Claire
Adam Baker
adam@dobetter.com
Property Based Testing
You may have heard of this from libraries such as
QuickCheck and ScalaCheck
Claire is a library by Quildreen Motta that helps you test
invariants in your javascript
Invariants!
We are used to writing tests like this:
eπi = -1
We should start writing tests like this:
∀x. exi = cos(x) + i sin(x)
Invariants!
An old idea in computer science
Preconditions and Postconditions
Loop Invariants
Usually expressed in a functional language
The Strategy
1. Define generators
2. Identify invariants
3. Convert to Claire property
4. Test property on generated data
5. ...
6. No more bugs!
Generators, Properties,
Tests
var claire = require('claire')
var _ = claire.data
//two generators, Int and Num
var commutativity = claire.forAll(_.Int, _.Num)
.satisfy(function(integer, real){ //the property being tested
return integer+real === real+integer
}).asTest() //returns a function
//use it in mocha
describe('addition', function(){
it('is commutative', commutativity)
})
//or invoke it to see the results
commutativity() //passes silently
Diagnosing Properties
var sorted_p = forAll( _.Array(_.Int) )
.satisfy(function(xs) {
xs = sorted(xs)
return xs.every(function(a, i) {
return i == 0 || a >= xs[i - 1]
})
}).classify(function(xs) {
return xs.length == 0 || xs.length == 1? 'trivial' : '> 1'
}).asTest({verbose: true, times: 1000})()
// + OK passed 1000 tests.
// > Collected test data:
// o 98% - > 1
// o 2% - trivial
A Simple Example
From a real app
To the command line!
Generators from a more
complex example
function modelObjGen(className, attrGens) {
return claire.label(className)(function() {
var attr, attrs, gen;
attrs = {};
for (attr in attrGens) {
gen = attrGens[attr];
attrs[attr] = gen.next().value;
}
return new App.Models[className](attrs);
});
};
Generators from a more
complex example
var SpecialItem = classObjGen('SpecialItem', {
type: claire.choice('Feedback', 'ContactForm', 'Document'),
info: gens.resize(5, ObjectGen(Any))
});
var Video = classObjGen('Video', {
youTubeId: claire.frequency([1, Nothing], [9, Id])
});
var Item = claire.label('Item', claire.choice(SpecialItem, Video));
var Unit = classObjGen('Unit', {
items: gens.resize(6, ArrayGen(Item))
});
Properties of Addition and
Multiplication
associativity: (a+b)+c = a+(b+c)
commutativity: a+b = b+a
identity: a+0 = a
closure: ∀ a,b ∈ ℤ. (a+b) ∈ ℤ
Test with Int, Num, and BigInt
for both addition and multiplication
Which ones fail?
Properties of Addition and
Multiplication
Two failures:
Num fails associativity of multiplication
BigInt fails associativity of addition
The Point: Claire makes it easy to test boundary conditions
and find corner cases.
A Look Inside
Some basic generators:
char = String.from-char-code
### -- Primitive data types --------------------------------------------
Null = as-generator null
Undefined = as-generator void
Bool = choice true, false
Num = label 'num' ((s) -> choose -s, s)
Byte = label 'byte' ((_) -> to-integer (choose 0, 255))
Char = label 'char' (transform char, Byte)
Str = label 'str' (transform join, (repeat Char))
Container Type Generators
### -- Container data types -------------------------------
List = (...as) -> repeat (choice ...as)
Map = (...as) -> transform to-object, (
repeat (sequence Id, (choice ...as))
)
#### λ to-object
# :internal:
# Folds a list of pairs into an object.
#
# :: [String, a] -> { String -> a }
to-object = (as) -> as.reduce ((r, [k,v]) -> r <<< { "#k": v }), {}
For the FP geeks
Claire's generators are monads:
claire.transformacts as fmap
.thenmethod on generators acts as monadic bind
What's in the Future?
1. Shrinking of counterexamples.
2. Generators based on generic lazy linked lists
3. Asynchronous tests
Why is this important?
Automated teting is important.
XUnit testing automates test running.
Propery based testing automates test generation.
Shrinking will help automate the first step in debugging.
Your confidence grows each time you run your test suite.
Random data (often) finds corners better than I do.
Thanks
install:
npm install claire
github:
a talk on QuickCheck by John Hughes.
https://github.com/killdream/claire
Functional Programming: A Secret Weapon for Software
Testing

More Related Content

What's hot

The Vanishing Pattern: from iterators to generators in Python
The Vanishing Pattern: from iterators to generators in PythonThe Vanishing Pattern: from iterators to generators in Python
The Vanishing Pattern: from iterators to generators in PythonOSCON Byrum
 
Types of Constructor in C++
Types of Constructor in C++Types of Constructor in C++
Types of Constructor in C++Bhavik Vashi
 
Swift で JavaScript 始めませんか? #iOSDC
Swift で JavaScript 始めませんか? #iOSDCSwift で JavaScript 始めませんか? #iOSDC
Swift で JavaScript 始めませんか? #iOSDCTomohiro Kumagai
 
What\'s New in C# 4.0
What\'s New in C# 4.0What\'s New in C# 4.0
What\'s New in C# 4.0Eyal Vardi
 
Lambda Expressions in C++
Lambda Expressions in C++Lambda Expressions in C++
Lambda Expressions in C++Patrick Viafore
 
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Abu Saleh
 
Oops practical file
Oops practical fileOops practical file
Oops practical fileAnkit Dixit
 
Giorgio zoppi cpp11concurrency
Giorgio zoppi cpp11concurrencyGiorgio zoppi cpp11concurrency
Giorgio zoppi cpp11concurrencyGiorgio Zoppi
 
Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)Scott Wlaschin
 
Functional Programming 101 with Scala and ZIO @FunctionalWorld
Functional Programming 101 with Scala and ZIO @FunctionalWorldFunctional Programming 101 with Scala and ZIO @FunctionalWorld
Functional Programming 101 with Scala and ZIO @FunctionalWorldJorge Vásquez
 
Object Oriented Programming Using C++ Practical File
Object Oriented Programming Using C++ Practical FileObject Oriented Programming Using C++ Practical File
Object Oriented Programming Using C++ Practical FileHarjinder Singh
 
Functional Programming in C#
Functional Programming in C#Functional Programming in C#
Functional Programming in C#Giorgio Zoppi
 
AnyObject – 自分が見落としていた、基本の話
AnyObject – 自分が見落としていた、基本の話AnyObject – 自分が見落としていた、基本の話
AnyObject – 自分が見落としていた、基本の話Tomohiro Kumagai
 
API design: using type classes and dependent types
API design: using type classes and dependent typesAPI design: using type classes and dependent types
API design: using type classes and dependent typesbmlever
 
Intro to Functional Programming
Intro to Functional ProgrammingIntro to Functional Programming
Intro to Functional ProgrammingHugo Firth
 
An Introduction to Property Based Testing
An Introduction to Property Based TestingAn Introduction to Property Based Testing
An Introduction to Property Based TestingC4Media
 
Applying Compiler Techniques to Iterate At Blazing Speed
Applying Compiler Techniques to Iterate At Blazing SpeedApplying Compiler Techniques to Iterate At Blazing Speed
Applying Compiler Techniques to Iterate At Blazing SpeedPascal-Louis Perez
 

What's hot (20)

The Vanishing Pattern: from iterators to generators in Python
The Vanishing Pattern: from iterators to generators in PythonThe Vanishing Pattern: from iterators to generators in Python
The Vanishing Pattern: from iterators to generators in Python
 
Types of Constructor in C++
Types of Constructor in C++Types of Constructor in C++
Types of Constructor in C++
 
Swift で JavaScript 始めませんか? #iOSDC
Swift で JavaScript 始めませんか? #iOSDCSwift で JavaScript 始めませんか? #iOSDC
Swift で JavaScript 始めませんか? #iOSDC
 
What\'s New in C# 4.0
What\'s New in C# 4.0What\'s New in C# 4.0
What\'s New in C# 4.0
 
Lambda Expressions in C++
Lambda Expressions in C++Lambda Expressions in C++
Lambda Expressions in C++
 
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
 
A taste of Functional Programming
A taste of Functional ProgrammingA taste of Functional Programming
A taste of Functional Programming
 
Oops practical file
Oops practical fileOops practical file
Oops practical file
 
Giorgio zoppi cpp11concurrency
Giorgio zoppi cpp11concurrencyGiorgio zoppi cpp11concurrency
Giorgio zoppi cpp11concurrency
 
Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)
 
Functional Programming 101 with Scala and ZIO @FunctionalWorld
Functional Programming 101 with Scala and ZIO @FunctionalWorldFunctional Programming 101 with Scala and ZIO @FunctionalWorld
Functional Programming 101 with Scala and ZIO @FunctionalWorld
 
Object Oriented Programming Using C++ Practical File
Object Oriented Programming Using C++ Practical FileObject Oriented Programming Using C++ Practical File
Object Oriented Programming Using C++ Practical File
 
Functional Programming in C#
Functional Programming in C#Functional Programming in C#
Functional Programming in C#
 
AnyObject – 自分が見落としていた、基本の話
AnyObject – 自分が見落としていた、基本の話AnyObject – 自分が見落としていた、基本の話
AnyObject – 自分が見落としていた、基本の話
 
C++11
C++11C++11
C++11
 
C++11
C++11C++11
C++11
 
API design: using type classes and dependent types
API design: using type classes and dependent typesAPI design: using type classes and dependent types
API design: using type classes and dependent types
 
Intro to Functional Programming
Intro to Functional ProgrammingIntro to Functional Programming
Intro to Functional Programming
 
An Introduction to Property Based Testing
An Introduction to Property Based TestingAn Introduction to Property Based Testing
An Introduction to Property Based Testing
 
Applying Compiler Techniques to Iterate At Blazing Speed
Applying Compiler Techniques to Iterate At Blazing SpeedApplying Compiler Techniques to Iterate At Blazing Speed
Applying Compiler Techniques to Iterate At Blazing Speed
 

Similar to Functional programming's relentless bug hunter claire

Introduction to web programming for java and c# programmers by @drpicox
Introduction to web programming for java and c# programmers by @drpicoxIntroduction to web programming for java and c# programmers by @drpicox
Introduction to web programming for java and c# programmers by @drpicoxDavid Rodenas
 
Aaron Bedra - Effective Software Security Teams
Aaron Bedra - Effective Software Security TeamsAaron Bedra - Effective Software Security Teams
Aaron Bedra - Effective Software Security Teamscentralohioissa
 
Bringing nullability into existing code - dammit is not the answer.pptx
Bringing nullability into existing code - dammit is not the answer.pptxBringing nullability into existing code - dammit is not the answer.pptx
Bringing nullability into existing code - dammit is not the answer.pptxMaarten Balliauw
 
PVS-Studio vs Chromium
PVS-Studio vs ChromiumPVS-Studio vs Chromium
PVS-Studio vs ChromiumPVS-Studio
 
Tdd for BT E2E test community
Tdd for BT E2E test communityTdd for BT E2E test community
Tdd for BT E2E test communityKerry Buckley
 
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 1
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 1Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 1
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 1PVS-Studio
 
ADG Poznań - Kotlin for Android developers
ADG Poznań - Kotlin for Android developersADG Poznań - Kotlin for Android developers
ADG Poznań - Kotlin for Android developersBartosz Kosarzycki
 
Devnology Workshop Genpro 2 feb 2011
Devnology Workshop Genpro 2 feb 2011Devnology Workshop Genpro 2 feb 2011
Devnology Workshop Genpro 2 feb 2011Devnology
 
Test and refactoring
Test and refactoringTest and refactoring
Test and refactoringKenneth Ceyer
 
JS Responsibilities
JS ResponsibilitiesJS Responsibilities
JS ResponsibilitiesBrendan Eich
 
Optimization in the world of 64-bit errors
Optimization  in the world of 64-bit errorsOptimization  in the world of 64-bit errors
Optimization in the world of 64-bit errorsPVS-Studio
 
2CPP06 - Arrays and Pointers
2CPP06 - Arrays and Pointers2CPP06 - Arrays and Pointers
2CPP06 - Arrays and PointersMichael Heron
 
The Kotlin Programming Language, Svetlana Isakova
The Kotlin Programming Language, Svetlana IsakovaThe Kotlin Programming Language, Svetlana Isakova
The Kotlin Programming Language, Svetlana IsakovaVasil Remeniuk
 
Светлана Исакова «Язык Kotlin»
Светлана Исакова «Язык Kotlin»Светлана Исакова «Язык Kotlin»
Светлана Исакова «Язык Kotlin»e-Legion
 
關於測試,我說的其實是......
關於測試,我說的其實是......關於測試,我說的其實是......
關於測試,我說的其實是......hugo lu
 
How to not write a boring test in Golang
How to not write a boring test in GolangHow to not write a boring test in Golang
How to not write a boring test in GolangDan Tran
 
C++20 the small things - Timur Doumler
C++20 the small things - Timur DoumlerC++20 the small things - Timur Doumler
C++20 the small things - Timur Doumlercorehard_by
 

Similar to Functional programming's relentless bug hunter claire (20)

Compose Code Camp (1).pptx
Compose Code Camp (1).pptxCompose Code Camp (1).pptx
Compose Code Camp (1).pptx
 
Introduction to web programming for java and c# programmers by @drpicox
Introduction to web programming for java and c# programmers by @drpicoxIntroduction to web programming for java and c# programmers by @drpicox
Introduction to web programming for java and c# programmers by @drpicox
 
Price of an Error
Price of an ErrorPrice of an Error
Price of an Error
 
Aaron Bedra - Effective Software Security Teams
Aaron Bedra - Effective Software Security TeamsAaron Bedra - Effective Software Security Teams
Aaron Bedra - Effective Software Security Teams
 
Bringing nullability into existing code - dammit is not the answer.pptx
Bringing nullability into existing code - dammit is not the answer.pptxBringing nullability into existing code - dammit is not the answer.pptx
Bringing nullability into existing code - dammit is not the answer.pptx
 
PVS-Studio vs Chromium
PVS-Studio vs ChromiumPVS-Studio vs Chromium
PVS-Studio vs Chromium
 
Tdd for BT E2E test community
Tdd for BT E2E test communityTdd for BT E2E test community
Tdd for BT E2E test community
 
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 1
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 1Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 1
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 1
 
ADG Poznań - Kotlin for Android developers
ADG Poznań - Kotlin for Android developersADG Poznań - Kotlin for Android developers
ADG Poznań - Kotlin for Android developers
 
Devnology Workshop Genpro 2 feb 2011
Devnology Workshop Genpro 2 feb 2011Devnology Workshop Genpro 2 feb 2011
Devnology Workshop Genpro 2 feb 2011
 
Test and refactoring
Test and refactoringTest and refactoring
Test and refactoring
 
Cocoa heads 09112017
Cocoa heads 09112017Cocoa heads 09112017
Cocoa heads 09112017
 
JS Responsibilities
JS ResponsibilitiesJS Responsibilities
JS Responsibilities
 
Optimization in the world of 64-bit errors
Optimization  in the world of 64-bit errorsOptimization  in the world of 64-bit errors
Optimization in the world of 64-bit errors
 
2CPP06 - Arrays and Pointers
2CPP06 - Arrays and Pointers2CPP06 - Arrays and Pointers
2CPP06 - Arrays and Pointers
 
The Kotlin Programming Language, Svetlana Isakova
The Kotlin Programming Language, Svetlana IsakovaThe Kotlin Programming Language, Svetlana Isakova
The Kotlin Programming Language, Svetlana Isakova
 
Светлана Исакова «Язык Kotlin»
Светлана Исакова «Язык Kotlin»Светлана Исакова «Язык Kotlin»
Светлана Исакова «Язык Kotlin»
 
關於測試,我說的其實是......
關於測試,我說的其實是......關於測試,我說的其實是......
關於測試,我說的其實是......
 
How to not write a boring test in Golang
How to not write a boring test in GolangHow to not write a boring test in Golang
How to not write a boring test in Golang
 
C++20 the small things - Timur Doumler
C++20 the small things - Timur DoumlerC++20 the small things - Timur Doumler
C++20 the small things - Timur Doumler
 

Functional programming's relentless bug hunter claire

  • 2. Property Based Testing You may have heard of this from libraries such as QuickCheck and ScalaCheck Claire is a library by Quildreen Motta that helps you test invariants in your javascript
  • 3. Invariants! We are used to writing tests like this: eπi = -1 We should start writing tests like this: ∀x. exi = cos(x) + i sin(x)
  • 4. Invariants! An old idea in computer science Preconditions and Postconditions Loop Invariants Usually expressed in a functional language
  • 5. The Strategy 1. Define generators 2. Identify invariants 3. Convert to Claire property 4. Test property on generated data 5. ... 6. No more bugs!
  • 6. Generators, Properties, Tests var claire = require('claire') var _ = claire.data //two generators, Int and Num var commutativity = claire.forAll(_.Int, _.Num) .satisfy(function(integer, real){ //the property being tested return integer+real === real+integer }).asTest() //returns a function //use it in mocha describe('addition', function(){ it('is commutative', commutativity) }) //or invoke it to see the results commutativity() //passes silently
  • 7. Diagnosing Properties var sorted_p = forAll( _.Array(_.Int) ) .satisfy(function(xs) { xs = sorted(xs) return xs.every(function(a, i) { return i == 0 || a >= xs[i - 1] }) }).classify(function(xs) { return xs.length == 0 || xs.length == 1? 'trivial' : '> 1' }).asTest({verbose: true, times: 1000})() // + OK passed 1000 tests. // > Collected test data: // o 98% - > 1 // o 2% - trivial
  • 8. A Simple Example From a real app To the command line!
  • 9. Generators from a more complex example function modelObjGen(className, attrGens) { return claire.label(className)(function() { var attr, attrs, gen; attrs = {}; for (attr in attrGens) { gen = attrGens[attr]; attrs[attr] = gen.next().value; } return new App.Models[className](attrs); }); };
  • 10. Generators from a more complex example var SpecialItem = classObjGen('SpecialItem', { type: claire.choice('Feedback', 'ContactForm', 'Document'), info: gens.resize(5, ObjectGen(Any)) }); var Video = classObjGen('Video', { youTubeId: claire.frequency([1, Nothing], [9, Id]) }); var Item = claire.label('Item', claire.choice(SpecialItem, Video)); var Unit = classObjGen('Unit', { items: gens.resize(6, ArrayGen(Item)) });
  • 11. Properties of Addition and Multiplication associativity: (a+b)+c = a+(b+c) commutativity: a+b = b+a identity: a+0 = a closure: ∀ a,b ∈ ℤ. (a+b) ∈ ℤ Test with Int, Num, and BigInt for both addition and multiplication Which ones fail?
  • 12. Properties of Addition and Multiplication Two failures: Num fails associativity of multiplication BigInt fails associativity of addition The Point: Claire makes it easy to test boundary conditions and find corner cases.
  • 13. A Look Inside Some basic generators: char = String.from-char-code ### -- Primitive data types -------------------------------------------- Null = as-generator null Undefined = as-generator void Bool = choice true, false Num = label 'num' ((s) -> choose -s, s) Byte = label 'byte' ((_) -> to-integer (choose 0, 255)) Char = label 'char' (transform char, Byte) Str = label 'str' (transform join, (repeat Char))
  • 14. Container Type Generators ### -- Container data types ------------------------------- List = (...as) -> repeat (choice ...as) Map = (...as) -> transform to-object, ( repeat (sequence Id, (choice ...as)) ) #### λ to-object # :internal: # Folds a list of pairs into an object. # # :: [String, a] -> { String -> a } to-object = (as) -> as.reduce ((r, [k,v]) -> r <<< { "#k": v }), {}
  • 15. For the FP geeks Claire's generators are monads: claire.transformacts as fmap .thenmethod on generators acts as monadic bind
  • 16. What's in the Future? 1. Shrinking of counterexamples. 2. Generators based on generic lazy linked lists 3. Asynchronous tests
  • 17. Why is this important? Automated teting is important. XUnit testing automates test running. Propery based testing automates test generation. Shrinking will help automate the first step in debugging. Your confidence grows each time you run your test suite. Random data (often) finds corners better than I do.
  • 18. Thanks install: npm install claire github: a talk on QuickCheck by John Hughes. https://github.com/killdream/claire Functional Programming: A Secret Weapon for Software Testing