-
-
Notifications
You must be signed in to change notification settings - Fork 110
Result concept exercise (initial draft) #1364
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
abe0b6d
b48a70b
c2ea535
3c08d02
5e9a104
98dabd9
b8751b8
448c06d
bf00b5c
e299424
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| module FamilyRecipes | ||
|
|
||
| type ValidationError = | ||
| | EmptyList | ||
| | InvalidIngredientQuantity | ||
| | MissingIngredientItem | ||
|
|
||
| let parseInt (text: string) : Result<int, unit> = | ||
| let success, value = System.Int32.TryParse text | ||
| if success then Ok value else Error () | ||
|
|
||
| let splitOnce (text: string) (separator: char) : (string * string) option = | ||
| match text.IndexOf(separator) with | ||
| | -1 -> None | ||
| | index -> Some (text.Substring(0, index), text.Substring(index + 1)) | ||
|
|
||
| let validateIngredient (text: string): Result<unit, ValidationError> = | ||
| match splitOnce text ' ' with | ||
| | Some (quantity, item) -> | ||
| match parseInt quantity with | ||
| | Ok _ -> | ||
| if item.Length = 0 then | ||
| Error MissingIngredientItem | ||
| else | ||
| Ok () | ||
| | Error _ -> Error InvalidIngredientQuantity | ||
| | _ -> Error MissingIngredientItem | ||
|
|
||
| let validate (input: string) : Result<string, ValidationError> = | ||
| let nonblankLines = | ||
| input.Split('\n') | ||
| |> Array.filter (fun l -> l.Length > 0) | ||
| if nonblankLines.Length = 0 then Error EmptyList | ||
| else | ||
| let firstError = | ||
| nonblankLines | ||
| |> Array.map validateIngredient | ||
| |> Array.tryFind (fun r -> r.IsError) | ||
| match firstError with | ||
| | Some (Error error) -> Error error | ||
| | _ -> Ok input |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| module FamilyRecipes | ||
|
|
||
| type ValidationError = | ||
| | EmptyList | ||
| | InvalidIngredientQuantity | ||
| | MissingIngredientItem | ||
|
|
||
| let parseInt (text: string) : Result<int, unit> = | ||
| let success, value = System.Int32.TryParse text | ||
| if success then Ok value else Error () | ||
|
Comment on lines
+8
to
+10
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The purpose of this pre-written function is to provide the student an opportunity to practice consuming a Another idea is to introduce a mock Other thoughts?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ehm, I'm not entirely. I'll try and find some time this week to do a proposal for some further changes, okay? |
||
|
|
||
| let validate input = | ||
| failwith "Please implement this function" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net9.0</TargetFramework> | ||
|
|
||
| <IsPackable>false</IsPackable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Compile Include="FamilyRecipes.fs" /> | ||
| <Compile Include="FamilyRecipesTests.fs" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" /> | ||
| <PackageReference Include="xunit" Version="2.4.1" /> | ||
| <PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" /> | ||
| <PackageReference Include="FsUnit.xUnit" Version="4.0.4" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| module Tests | ||
|
|
||
| open FsUnit.Xunit | ||
| open Xunit | ||
|
|
||
| open FamilyRecipes | ||
|
|
||
| [<Fact>] | ||
| let ``Error on blank list`` () = | ||
| let expected: Result<string, ValidationError> = Error EmptyList | ||
| validate "" |> should equal expected | ||
|
|
||
| [<Fact>] | ||
| let ``Error on blank line`` () = | ||
| let expected: Result<string, ValidationError> = Error EmptyList | ||
| validate "\n" |> should equal expected | ||
|
|
||
| [<Fact>] | ||
| let ``Error on non-numeric ingredient quantity`` () = | ||
| let expected: Result<string, ValidationError> = Error InvalidIngredientQuantity | ||
| validate "foo bar" |> should equal expected | ||
|
|
||
| [<Fact>] | ||
| let ``Error on missing ingredient item`` () = | ||
| let expected: Result<string, ValidationError> = Error MissingIngredientItem | ||
| validate "24 " |> should equal expected | ||
|
|
||
| [<Fact>] | ||
| let ``Minimal valid list`` () = | ||
| let input = """1 cup rice""" | ||
| let expected: Result<string, ValidationError> = Ok input | ||
| validate input |> should equal expected | ||
|
|
||
| [<Fact>] | ||
| let ``Valid list with multiple ingredients`` () = | ||
| let input = """4 ounces of oatmeal | ||
| 12 ounces of water | ||
| 1 tablespoon of honey""" | ||
| let expected: Result<string, ValidationError> = Ok input | ||
| validate input |> should equal expected | ||
|
|
||
| [<Fact>] | ||
| let ``Blank lines within valid list are OK`` () = | ||
| let input = "1 foo\n2 bar" | ||
| let expected: Result<string, ValidationError> = Ok input | ||
| validate input |> should equal expected |
Uh oh!
There was an error while loading. Please reload this page.