Skip to content

Commit

Permalink
Add methods to get FsWorksheet by name + unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
omaus committed Mar 14, 2023
1 parent c08383b commit c6c064f
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/FsSpreadsheet/FsWorkbook.fs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,25 @@ type FsWorkbook() =
static member getWorksheets (workbook : FsWorkbook) =
workbook.GetWorksheets()

/// <summary>Returns the FsWorksheet with the given name if it exists in the FsWorkbook. Else returns None.</summary>
member self.TryGetWorksheetByName(sheetName) =
_worksheets |> List.tryFind (fun w -> w.Name = sheetName)

/// <summary>Returns the FsWorksheet with the given name if it exists in a given FsWorkbook. Else returns None.</summary>
static member tryGetWorksheetByName sheetName (workbook : FsWorkbook) =
workbook.TryGetWorksheetByName sheetName

/// <summary>Returns the FsWorksheet with the given name.</summary>
/// <exception cref="System.Exception">if FsWorksheet with given name is not present in the FsWorkkbook.</exception>
member self.GetWorksheetByName(sheetName) =
try (self.TryGetWorksheetByName sheetName).Value
with _ -> failwith $"FsWorksheet with name {sheetName} is not present in the FsWorkbook."

/// <summary>Returns the FsWorksheet with the given name from an FsWorkbook.</summary>
/// <exception cref="System.Exception">if FsWorksheet with given name is not present in the FsWorkkbook.</exception>
static member getWorksheetByName sheetName (workbook : FsWorkbook) =
workbook.GetWorksheetByName sheetName

/// <summary>Removes an FsWorksheet with given name.</summary>
/// <exception cref="System.Exception">if FsWorksheet with given name is not present in the FsWorkkbook.</exception>
member self.RemoveWorksheet(name : string) =
Expand Down
3 changes: 2 additions & 1 deletion tests/FsSpreadsheet.Tests/FsSpreadsheet.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
</PropertyGroup>

<ItemGroup>
<Compile Include="FsWorkbook.fs" />
<Compile Include="FsWorksheet.fs" />
<Compile Include="FsAddress.fs" />
<Compile Include="FsCell.fs" />
<Compile Include="FsAddress.fs" />
<Compile Include="Main.fs" />
</ItemGroup>

Expand Down
27 changes: 27 additions & 0 deletions tests/FsSpreadsheet.Tests/FsWorkbook.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module FsWorkbook

open Expecto
open FsSpreadsheet


let dummyWorkbook = new FsWorkbook()
let dummyWorksheet = FsWorksheet("dummyWorksheet")
dummyWorkbook.AddWorksheet dummyWorksheet |> ignore


[<Tests>]
let fsWorkbookTests =
testList "FsWorkbook" [
testList "TryGetWorksheetByName" [
let testWorksheet = dummyWorkbook.TryGetWorksheetByName "dummyWorksheet"
testCase "is Some" <| fun _ ->
Expect.isSome testWorksheet "is None"
// TO DO: add more cases
]
testList "GetWorksheetByName" [
testCase "does not throw exception when present" <| fun _ ->
Expect.isOk (dummyWorkbook.GetWorksheetByName "dummyWorksheet" |> Result.Ok) "did throw exception"
testCase "does throw exception when not present" <| fun _ ->
Expect.throws (fun () -> dummyWorkbook.GetWorksheetByName "bla" |> ignore) "did not throw exception"
]
]
1 change: 1 addition & 0 deletions tests/FsSpreadsheet.Tests/FsWorksheet.fs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module FsWorkSheet

open Expecto
open FsSpreadsheet

Expand Down

0 comments on commit c6c064f

Please sign in to comment.