Skip to content

Commit

Permalink
add code examples to property into
Browse files Browse the repository at this point in the history
  • Loading branch information
ri-boris committed Apr 25, 2018
1 parent 28a265d commit 3272223
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
4 changes: 2 additions & 2 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
.reveal pre code {
padding: 5px;
overflow: auto;
max-height: 800px;
max-height: 700px;
word-wrap: normal;
}
</style>
Expand Down Expand Up @@ -136,7 +136,7 @@ <h3>Example #2</h3>
Nicolas Seriot did a research and tests bunch of json parser implementations
</aside>
</section>
<section data-background-image="img/json-parsing-tests.png" data-background-size="1024px">
<section data-background-image="img/json-parsing-tests.png" data-background-size="1536px">
<aside class="notes">
No two parsers behave the same way!
It is hard to reason about corner cases.
Expand Down
39 changes: 34 additions & 5 deletions docs/slides/intro.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,52 @@
### Introduction to properties
based on ["Choosing properties for property-based testing"](https://fsharpforfunandprofit.com/posts/property-based-testing-2/) from Scott Wlaschin's blog

```scala
Prop.forAll { s: String =>
val len = s.length
(s + s).length == len + len
}
```
Note: if you generate the data, how could you check the results? Properties for the rescue.
You think about your code as a set of laws, or algebra
You think about your code as a set of laws, or algebra.
Here is a list of patterns we could find


### "Different paths, same destination"
aka commutative property

![](./img/property_commutative.png "commutative property")

```scala
Prop.forAll { (a: Int, b: Int) =>
a + b == b + a
}
```
Note:


### "There and back again"
aka symmetry

![](./img/property_inverse.png "symmetry")

```scala
Prop.forAll { xs: List[Int] =>
xs.reverse.reverse == xs
}
```
Note: What is a typical example of it? Serialization/Desirialization


### "Some things never change"
aka invariants

![](./img/property_invariant.png "invariants")
```scala
Prop.forAll { l: List[Int] =>
l.sortWith(mySortFun).size == l.size
}
```
Note: e.g. size of a collection, max/min element etc.


Expand All @@ -31,10 +55,10 @@ aka idempotence

![](./img/property_idempotence.png "idempotence")
```scala
"List.sortWith" should "produce the same list while sort twice" in forAll(minSuccessful(500)) {
l: List[Int] =>
l.sortWith(mySortFun) should be(l.sortWith(mySortFun).sortWith(mySortFun))
}
"List.sortWith" should "produce the same list while sort twice" in forAll(minSuccessful(500)) {
l: List[Int] =>
l.sortWith(mySortFun) should be(l.sortWith(mySortFun).sortWith(mySortFun))
}
```
Note:

Expand All @@ -43,6 +67,11 @@ Note:
aka induction

![](./img/property_induction.png "property_induction.png")
```scala
"induction" should "be applicable to size" in forAll { (i: Int, l: List[Int]) =>
(i :: l).size should be (l.size + 1)
}
```
Note: some property is true for these smaller parts, then you can often prove that the property is true for a large thing as well


Expand Down
4 changes: 4 additions & 0 deletions src/test/scala/de/riskident/meetup/SortWithTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class SortWithTest extends FlatSpec with GeneratorDrivenPropertyChecks with Matc
"List.sortWith" should "produce the same list while sort twice" in forAll(minSuccessful(500)) { l: List[Int] =>
l.sortWith(mySortFun) should be(l.sortWith(mySortFun).sortWith(mySortFun))
}

"induction" should "be applicable to size" in forAll { (i: Int, l: List[Int]) =>
(i :: l).size should be (l.size + 1)
}
}

import org.scalacheck.Arbitrary._
Expand Down

0 comments on commit 3272223

Please sign in to comment.