Skip to content

Commit

Permalink
finish porting to expecto
Browse files Browse the repository at this point in the history
  • Loading branch information
baronfel committed Aug 6, 2017
1 parent 9dee17b commit c9974f1
Show file tree
Hide file tree
Showing 19 changed files with 402 additions and 418 deletions.
29 changes: 14 additions & 15 deletions FSharpKoans/AboutArrays.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace FSharpKoans

open Expecto
open System.Collections.Generic

//---------------------------------------------------------------
Expand All @@ -9,47 +10,45 @@ open System.Collections.Generic
//---------------------------------------------------------------

module ``about arrays`` =

let CreatingArrays() =
let tests =
testList "teaching about arrays" [
testCase "creating arrays" <| fun () ->
let fruits = [| "apple"; "pear"; "peach"|]

AssertEquality fruits.[0] __
AssertEquality fruits.[1] __
AssertEquality fruits.[2] __


let ArraysAreDotNetArrays() =
testCase "arrays are .Net arrays" <| fun () ->
let fruits = [| "apple"; "pear" |]

let arrayType = fruits.GetType()
let systemArray = System.Array.CreateInstance(typeof<string>, 0).GetType()

(* Unlike List, Arrays in F# are the standard .NET arrays that
you're used to if you're coming from another .NET language *)
(* Unlike List, Arrays in F# are the standard .NET arrays that
you're used to if you're coming from another .NET language *)
AssertEquality arrayType systemArray


let ArraysAreMutable() =
testCase "arrays are mutable" <| fun () ->
let fruits = [| "apple"; "pear" |]
fruits.[1] <- "peach"

AssertEquality fruits __


let YouCanCreateArraysWithComprehensions() =
testCase "you can create arrays with comprehensions" <| fun () ->
let numbers =
[| for i in 0..10 do
if i % 2 = 0 then yield i |]
[| for i in 0..10 do
if i % 2 = 0 then yield i |]

AssertEquality numbers __


let ThereAreAlsoSomeOperationsYouCanPerformOnArrays() =
testCase "there are also some operations you can perform on arrays" <| fun () ->
let cube x =
x * x * x
x * x * x

let original = [| 0..5 |]
let result = Array.map cube original

AssertEquality original __
AssertEquality result __
]
74 changes: 35 additions & 39 deletions FSharpKoans/AboutBranching.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace FSharpKoans

open Expecto

//---------------------------------------------------------------
// About Branching
Expand All @@ -9,76 +10,71 @@
//---------------------------------------------------------------

module ``about branching`` =


let BasicBranching() =
let tests =
testList "teaching about branching" [
testCase "basic branching" <| fun () ->
let isEven x =
if x % 2 = 0 then
"it's even!"
else
"it's odd!"
if x % 2 = 0 then
"it's even!"
else
"it's odd!"

let result = isEven 2
AssertEquality result __


let IfStatementsReturnValues() =

(* In languages like C#, if statements do not yield results; they can
only cause side effects. If statements in F# return values due to
F#'s functional programming roots. *)
testCase "'if' statments return values" <| fun () ->
(* In languages like C#, if statements do not yield results; they can
only cause side effects. If statements in F# return values due to
F#'s functional programming roots. *)

let result =
if 2 = 3 then
"something is REALLY wrong"
else
"no problem here"
if 2 = 3 then
"something is REALLY wrong"
else
"no problem here"

AssertEquality result __


let BranchingWithAPatternMatch() =
testCase "branching with a pattern match" <| fun () ->
let isApple x =
match x with
| "apple" -> true
| _ -> false
match x with
| "apple" -> true
| _ -> false

let result1 = isApple "apple"
let result2 = isApple ""

AssertEquality result1 __
AssertEquality result2 __


let UsingTuplesWithIfStatementsQuicklyBecomesClumsy() =

testCase "using tuples with if statements quickly becomes clumsy" <| fun () ->
let getDinner x =
let name, foodChoice = x
let name, foodChoice = x

if foodChoice = "veggies" || foodChoice ="fish" ||
foodChoice = "chicken" then
sprintf "%s doesn't want red meat" name
else
sprintf "%s wants 'em some %s" name foodChoice
if foodChoice = "veggies" ||
foodChoice ="fish" ||
foodChoice = "chicken" then
sprintf "%s doesn't want red meat" name
else
sprintf "%s wants 'em some %s" name foodChoice

let person1 = ("Chris", "steak")
let person2 = ("Dave", "veggies")

AssertEquality (getDinner person1) __
AssertEquality (getDinner person2) __


let PatternMatchingIsNicer() =

testCase "pattern matching is nicer" <| fun () ->
let getDinner x =
match x with
| (name, "veggies")
| (name, "fish")
| (name, "chicken") -> sprintf "%s doesn't want red meat" name
| (name, foodChoice) -> sprintf "%s wants 'em some %s" name foodChoice
match x with
| (name, "veggies")
| (name, "fish")
| (name, "chicken") -> sprintf "%s doesn't want red meat" name
| (name, foodChoice) -> sprintf "%s wants 'em some %s" name foodChoice

let person1 = ("Bob", "fish")
let person2 = ("Sally", "Burger")

AssertEquality (getDinner person1) __
AssertEquality (getDinner person2) __
]
25 changes: 11 additions & 14 deletions FSharpKoans/AboutClasses.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace FSharpKoans

open Expecto

//---------------------------------------------------------------
// About Classes
Expand Down Expand Up @@ -38,40 +39,35 @@ type Person2(name:string) =


module ``about classes`` =


let ClassesCanHaveProperties() =
let tests =
testList "teaching about classes" [
testCase "classes can have properties" <| fun () ->
let zombie = new Zombie()

AssertEquality zombie.FavoriteFood __


let ClassesCanHaveMethods() =
testCase "classes can have methods" <| fun () ->
let zombie = new Zombie()

let result = zombie.Eat "brains"
AssertEquality result __


let ClassesCanHaveConstructors() =

testCase "classes can have constructors" <| fun () ->
let person = new Person("Shaun")

let result = person.Speak()
AssertEquality result __


let ClassesCanHaveLetBindingsInsideThem() =
testCase "classes can have let bindings inside them" <| fun () ->
let zombie = new Zombie2()

let result = zombie.Eat "chicken"
AssertEquality result __

(* TRY IT: Can you access the let bound value Zombie2.favoriteFood
outside of the class definition? *)

(* TRY IT: Can you access the let bound value Zombie2.favoriteFood
outside of the class definition? *)

let ClassesCanHaveReadWriteProperties() =
testCase "classes can have read/write properties" <| fun () ->
let person = new Person2("Shaun")

let firstPhrase = person.Speak()
Expand All @@ -80,3 +76,4 @@ module ``about classes`` =
person.Name <- "Shaun of the Dead"
let secondPhrase = person.Speak()
AssertEquality secondPhrase __
]
37 changes: 19 additions & 18 deletions FSharpKoans/AboutDiscriminatedUnions.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace FSharpKoans

open Expecto

type Condiment =
| Mustard
Expand All @@ -21,35 +22,35 @@ type Favorite =
//---------------------------------------------------------------

module ``about discriminated unions`` =

let DiscriminatedUnionsCaptureASetOfOptions() =

let tests =
testList "teaching about discriminated unions" [
testCase "discriminated unions capture a set of options" <| fun () ->
let toColor condiment =
match condiment with
| Mustard -> "yellow"
| Ketchup -> "red"
| Relish -> "green"
| Vinegar -> "brownish?"
match condiment with
| Mustard -> "yellow"
| Ketchup -> "red"
| Relish -> "green"
| Vinegar -> "brownish?"

let choice = Mustard

AssertEquality (toColor choice) __

(* TRY IT: What happens if you remove a case from the above pattern
match? *)


let DiscriminatedUnionCasesCanHaveTypes() =
(* TRY IT: What happens if you remove a case from the above pattern
match? *)

testCase "discriminated union cases can have types" <| fun () ->
let saySomethingAboutYourFavorite favorite =
match favorite with
| Number 7 -> "me too!"
| Bourbon "Bookers" -> "me too!"
| Bourbon b -> "I prefer Bookers to " + b
| Number _ -> "I'm partial to 7"
match favorite with
| Number 7 -> "me too!"
| Bourbon "Bookers" -> "me too!"
| Bourbon b -> "I prefer Bookers to " + b
| Number _ -> "I'm partial to 7"

let bourbonResult = saySomethingAboutYourFavorite <| Bourbon "Maker's Mark"
let numberResult = saySomethingAboutYourFavorite <| Number 7

AssertEquality bourbonResult __
AssertEquality numberResult __
]

35 changes: 17 additions & 18 deletions FSharpKoans/AboutDotNetCollections.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace FSharpKoans

open Expecto
open System.Collections.Generic

//---------------------------------------------------------------
Expand All @@ -11,9 +12,11 @@ open System.Collections.Generic
//---------------------------------------------------------------

module ``about dot net collections`` =


let CreatingDotNetLists() =
let tests =
testList "teaching about .Net collections" [
testCase "creating .Net lists(Resizeable Arrays)" <| fun () ->
(* NOTE: this is not the F# List, because we've overriden the type List by opening
the `System.Collections.Generic` namespace above *)
let fruits = new List<string>()

fruits.Add("apple")
Expand All @@ -22,8 +25,7 @@ module ``about dot net collections`` =
AssertEquality fruits.[0] __
AssertEquality fruits.[1] __


let CreatingDotNetDictionaries() =
testCase "creating .Net dictionaries" <| fun () ->
let addressBook = new Dictionary<string, string>()

addressBook.["Chris"] <- "Ann Arbor"
Expand All @@ -32,8 +34,7 @@ module ``about dot net collections`` =
AssertEquality addressBook.["Chris"] __
AssertEquality addressBook.["SkillsMatter"] __


let YouUseCombinatorsWithDotNetTypes() =
testCase "you use combinators with .Net types" <| fun () ->
let addressBook = new Dictionary<string, string>()

addressBook.["Chris"] <- "Ann Arbor"
Expand All @@ -44,23 +45,21 @@ module ``about dot net collections`` =
|> Seq.map (fun kvp -> sprintf "Name: %s - City: %s" kvp.Key kvp.Value)
|> Seq.toArray

//NOTE: The seq type in F# is an alias for .NET's IEnumerable interface
// Like the List and Array module, the Seq module contains functions
// that you can combine to perform operations on types implementing
// seq/IEnumerable.
(* NOTE: The seq type in F# is an alias for .NET's IEnumerable interface.
Like the List and Array module, the Seq module contains functions
that you can combine to perform operations on types implementing
seq/IEnumerable. *)

AssertEquality verboseBook.[0] __
AssertEquality verboseBook.[1] __


let SkippingElements() =
testCase "skipping elements" <| fun () ->
let original = [0..5]
let result = Seq.skip 2 original

AssertEquality result __


let FindingTheMax() =
testCase "finding the max" <| fun () ->
let values = new List<int>()

values.Add(11)
Expand All @@ -73,12 +72,12 @@ module ``about dot net collections`` =

AssertEquality result __


let FindingTheMaxUsingACondition() =
testCase "finding the max using a condition" <| fun () ->
let getNameLength (name:string) =
name.Length
name.Length

let names = [| "Harry"; "Lloyd"; "Nicholas"; "Mary"; "Joe"; |]
let result = Seq.maxBy getNameLength names

AssertEquality result __
]
Loading

0 comments on commit c9974f1

Please sign in to comment.