From 58dc08c0cbbd412305e15bb1ea412e57ffae6e45 Mon Sep 17 00:00:00 2001 From: Kevin F Date: Thu, 11 May 2023 14:39:54 +0200 Subject: [PATCH] Make tests pass :heavy_check_mark: --- src/FsSpreadsheet/Cells/FsCell.fs | 3 +- src/FsSpreadsheet/Cells/FsCellsCollection.fs | 15 ++++--- tests/FsSpreadsheet.Tests/FsCell.fs | 18 +++++--- tests/FsSpreadsheet.Tests/FsColumn.fs | 2 +- .../FsSpreadsheet.Tests.fsproj | 1 + tests/FsSpreadsheet.Tests/FsTable.fs | 6 +-- tests/FsSpreadsheet.Tests/FsTableField.fs | 2 +- tests/FsSpreadsheet.Tests/FsWorksheet.fs | 21 ++++------ tests/FsSpreadsheet.Tests/Utils.fs | 42 +++++++++++++++++++ 9 files changed, 80 insertions(+), 30 deletions(-) create mode 100644 tests/FsSpreadsheet.Tests/Utils.fs diff --git a/src/FsSpreadsheet/Cells/FsCell.fs b/src/FsSpreadsheet/Cells/FsCell.fs index 59d5016f..a2e4a0c8 100644 --- a/src/FsSpreadsheet/Cells/FsCell.fs +++ b/src/FsSpreadsheet/Cells/FsCell.fs @@ -20,7 +20,8 @@ type DataType = match value with | :? char as c -> DataType.String,c.ToString() | :? string as s -> DataType.String,s.ToString() - | :? bool as c -> DataType.Boolean,c.ToString() + | :? bool as true -> DataType.Boolean, "True" + | :? bool as false -> DataType.Boolean, "False" | :? byte as i -> DataType.Number,i.ToString() | :? sbyte as i -> DataType.Number,i.ToString() | :? int as i -> DataType.Number,i.ToString() diff --git a/src/FsSpreadsheet/Cells/FsCellsCollection.fs b/src/FsSpreadsheet/Cells/FsCellsCollection.fs index fee602dc..64a53d35 100644 --- a/src/FsSpreadsheet/Cells/FsCellsCollection.fs +++ b/src/FsSpreadsheet/Cells/FsCellsCollection.fs @@ -694,12 +694,15 @@ type FsCellsCollection() = /// Returns the upper left corner of the FsCellsCollection. /// member this.GetFirstAddress() = - let minRow = _rowsCollection.Keys |> Seq.min - let minCol = - _rowsCollection.Values - |> Seq.minBy (fun d -> Seq.min d.Keys) - |> fun d -> Seq.min d.Keys - FsAddress(minRow, minCol) + if Seq.isEmpty _rowsCollection || Seq.isEmpty _rowsCollection.Keys then + FsAddress(0,0) + else + let minRow = _rowsCollection.Keys |> Seq.min + let minCol = + _rowsCollection.Values + |> Seq.minBy (fun d -> Seq.min d.Keys) + |> fun d -> Seq.min d.Keys + FsAddress(minRow, minCol) /// /// Returns the upper left corner of a given FsCellsCollection. diff --git a/tests/FsSpreadsheet.Tests/FsCell.fs b/tests/FsSpreadsheet.Tests/FsCell.fs index 642405d3..ead88941 100644 --- a/tests/FsSpreadsheet.Tests/FsCell.fs +++ b/tests/FsSpreadsheet.Tests/FsCell.fs @@ -7,7 +7,7 @@ open Expecto #endif open FsSpreadsheet -let main = +let dataType = testList "DataType" [ testList "InferCellValue bool = true" [ let boolValTrue = true @@ -15,7 +15,8 @@ let main = testCase "Correct DataType" <| fun _ -> Expect.isTrue (resultDtTrue = DataType.Boolean) "is not the expected DataType.Boolean" testCase "Correct string" <| fun _ -> - Expect.isTrue (resultStrTrue = "True") "resulting string is not correct" + let expected = "True" + Expect.equal resultStrTrue expected $"resulting string is not correct: {resultStrTrue}" ] testList "InferCellValue bool = false" [ let boolValFalse = false @@ -23,7 +24,8 @@ let main = testCase "Correct DataType" <| fun _ -> Expect.isTrue (resultDtFalse = DataType.Boolean) "is not the expected DataType.Boolean" testCase "Correct string" <| fun _ -> - Expect.isTrue (resultStrFalse = "False") "resulting string is not correct" + let expected = "False" + Expect.equal resultStrFalse expected "resulting string is not correct" ] testList "InferCellValue string = \"test\"" [ let stringValTest = "test" @@ -75,8 +77,7 @@ let main = ] ] -[] -let fsCellTest = +let fsCellData = testList "FsCell data" [ testList "Data | DataType | Adress" [ let fscellA1_string = FsCell.create 1 1 "A1" @@ -113,4 +114,9 @@ let fsCellTest = Expect.equal (fscellB1_num.ColumnNumber) 2 "resulting value is not 1 as integer" ] - ] \ No newline at end of file + ] + +let main = testList "FsCell" [ + dataType + fsCellData +] \ No newline at end of file diff --git a/tests/FsSpreadsheet.Tests/FsColumn.fs b/tests/FsSpreadsheet.Tests/FsColumn.fs index b2f1b72e..24e86bae 100644 --- a/tests/FsSpreadsheet.Tests/FsColumn.fs +++ b/tests/FsSpreadsheet.Tests/FsColumn.fs @@ -67,6 +67,6 @@ let main = testCase "Correct values" <| fun _ -> let columns = FsTable.dummyFsTable.Columns(FsTable.dummyFsCellsCollection) let expectedValues = ["Name";"John Doe";"Jane Doe";"Jack Doe"] - Expect.sequenceEqual (Seq.item 0 columns |> Seq.map FsCell.getValueAs) expectedValues "Values are not correct" + Expect.mySequenceEqual (Seq.item 0 columns |> Seq.map FsCell.getValueAs) expectedValues "Values are not correct" ] ] diff --git a/tests/FsSpreadsheet.Tests/FsSpreadsheet.Tests.fsproj b/tests/FsSpreadsheet.Tests/FsSpreadsheet.Tests.fsproj index 193d4c14..73a2c4a0 100644 --- a/tests/FsSpreadsheet.Tests/FsSpreadsheet.Tests.fsproj +++ b/tests/FsSpreadsheet.Tests/FsSpreadsheet.Tests.fsproj @@ -7,6 +7,7 @@ + diff --git a/tests/FsSpreadsheet.Tests/FsTable.fs b/tests/FsSpreadsheet.Tests/FsTable.fs index 05e7c8ce..d80be497 100644 --- a/tests/FsSpreadsheet.Tests/FsTable.fs +++ b/tests/FsSpreadsheet.Tests/FsTable.fs @@ -95,9 +95,9 @@ let main = |> Seq.toArray |> Array.unzip testCase "Names are there" <| fun _ -> - Expect.sequenceEqual testNames dummyNames "Names are not equal" + Expect.mySequenceEqual testNames dummyNames "Names are not equal" testCase "Indeces are there" <| fun _ -> - Expect.sequenceEqual testIndeces dummyIndeces "Indeces are not equal" + Expect.mySequenceEqual testIndeces dummyIndeces "Indeces are not equal" ] ] testList "TryGetHeaderCellOfColumn" [ @@ -162,7 +162,7 @@ let main = testCase "Has correct values" <| fun _ -> let minRowNo = dummyFsCells |> Seq.map (fun c -> c.RowNumber) |> Seq.min let actualCells = dummyFsCells |> Seq.filter (fun c -> c.ColumnNumber = 2 && c.RowNumber > minRowNo) - Expect.sequenceEqual (testDataCells |> Seq.map (fun c -> c.Value)) (actualCells |> Seq.map (fun c -> c.Value)) "FsCells are incorrect in value" + Expect.mySequenceEqual (testDataCells |> Seq.map (fun c -> c.Value)) (actualCells |> Seq.map (fun c -> c.Value)) "FsCells are incorrect in value" ] ] ] \ No newline at end of file diff --git a/tests/FsSpreadsheet.Tests/FsTableField.fs b/tests/FsSpreadsheet.Tests/FsTableField.fs index d0bd3cd9..93bf0056 100644 --- a/tests/FsSpreadsheet.Tests/FsTableField.fs +++ b/tests/FsSpreadsheet.Tests/FsTableField.fs @@ -152,7 +152,7 @@ let main = let col3Cells = testFsCellsCollection.GetCellsInColumn 3 let col3CellsNoHeader = col3Cells |> Seq.skip 1 let col3CellsNoHeaderVals = col3CellsNoHeader |> Seq.map (fun c -> c.Value) - Expect.sequenceEqual dataCellsVals col3CellsNoHeaderVals "Values of data cells are not equal to values of expected cells" + Expect.mySequenceEqual dataCellsVals col3CellsNoHeaderVals "Values of data cells are not equal to values of expected cells" ] //testCase "Gets correct header cell" <| fun _ -> // Expect.equal headerCell.Value "I am the Header!" "Value is not I am the Header!" diff --git a/tests/FsSpreadsheet.Tests/FsWorksheet.fs b/tests/FsSpreadsheet.Tests/FsWorksheet.fs index ef9eb34f..30903c3d 100644 --- a/tests/FsSpreadsheet.Tests/FsWorksheet.fs +++ b/tests/FsSpreadsheet.Tests/FsWorksheet.fs @@ -7,7 +7,6 @@ open Expecto #endif open FsSpreadsheet - let dummyCellsColl = FsCellsCollection() let dummyTable1 = FsTable("dummyTable1", FsRangeAddress("A1:B2")) let dummyTable2 = FsTable("dummyTable2", FsRangeAddress("D1:F3")) @@ -15,20 +14,16 @@ let dummySheet1 = FsWorksheet("dummySheet1", [], [], dummyCellsColl) let dummySheet2 = FsWorksheet("dummySheet2", [], [dummyTable1; dummyTable2], dummyCellsColl) let main = - testList "FsWorksheet" [ + testSequenced <| testList "FsWorksheet" [ testList "FsCell data" [ // TO DO: Ask TM: useful? or was that a mistake? (since the same test is seen in FsCell.fs) testList "Data | DataType | Adress" [ let fscellA1_string = FsCell.create 1 1 "A1" let fscellB1_num = FsCell.create 1 2 1 let fscellA2_bool = FsCell.create 1 2 true - //let worksheet = FsWorksheet. - testCase "DataType string" <| fun _ -> Expect.equal fscellA1_string.DataType DataType.String "is not the expected DataType.String" - - ] ] testList "FsTable methods" [ @@ -45,20 +40,22 @@ let main = // TO DO: add more testCases ] testList "AddTable" [ - let testSheet = FsWorksheet("testSheet", [], [], FsCellsCollection()) - testSheet.AddTable dummyTable1 |> ignore testCase "dummyTable1 is present" <| fun _ -> - Expect.contains testSheet.Tables dummyTable1 "does not contain dummyTable1" + let ws = new FsWorksheet("testSheet", [], [], FsCellsCollection()) + ws.AddTable dummyTable1 |> ignore + Expect.myContains ws.Tables dummyTable1 "does not contain dummyTable1" testCase "dummyTable1 is not present twice" <| fun _ -> - testSheet.AddTable dummyTable1 |> ignore - Expect.hasCountOf testSheet.Tables 1u (fun t -> t.Name = dummyTable1.Name) "has dummyTable1 twice (or more)" + let ws = new FsWorksheet("testSheet", [], [], FsCellsCollection()) + ws.AddTable dummyTable1 |> ignore + ws.AddTable dummyTable1 |> ignore + Expect.myHasCountOf ws.Tables 1 (fun t -> t.Name = dummyTable1.Name) "has dummyTable1 twice (or more)" // DO DO: add more testCases ] testList "AddTables" [ testCase "dummyTable1 is present" <| fun _ -> let testSheet = FsWorksheet("testSheet", [], [], FsCellsCollection()) testSheet.AddTables [dummyTable1] |> ignore - Expect.contains testSheet.Tables dummyTable1 "does not contain dummyTable1" + Expect.myContains testSheet.Tables dummyTable1 "does not contain dummyTable1" testCase "dummyTable1 & dummyTable2 are present" <| fun _ -> let testSheet = FsWorksheet("testSheet", [], [], FsCellsCollection()) let dummyTablesList = [dummyTable1; dummyTable2] diff --git a/tests/FsSpreadsheet.Tests/Utils.fs b/tests/FsSpreadsheet.Tests/Utils.fs new file mode 100644 index 00000000..69e8d4c4 --- /dev/null +++ b/tests/FsSpreadsheet.Tests/Utils.fs @@ -0,0 +1,42 @@ +[] +module Utils + +module Helpers = + + let firstDiff s1 s2 = + let s1 = Seq.append (Seq.map Some s1) (Seq.initInfinite (fun _ -> None)) + let s2 = Seq.append (Seq.map Some s2) (Seq.initInfinite (fun _ -> None)) + Seq.mapi2 (fun i s p -> i,s,p) s1 s2 + |> Seq.find (function |_,Some s,Some p when s=p -> false |_-> true) + +#if FABLE_COMPILER +open Fable.Mocha +#else +open Expecto +#endif + +module Expect = + + let myContains (seq:seq<'T>) (ele:'T) msg = + let contains = Seq.contains ele seq + Expect.isTrue contains msg + + let myHasCountOf (seq: seq<'T>) (expectedCount: int) (selector: 'T -> bool) msg = + let itemsFiltered = Seq.filter selector seq + let actualCount = itemsFiltered |> Seq.length + let isSameCount = expectedCount = actualCount + Expect.isTrue isSameCount msg + + /// Expects the `actual` sequence to equal the `expected` one. + let inline mySequenceEqual actual expected message = + match Helpers.firstDiff actual expected with + | _,None,None -> () + | i,Some a, Some e -> + failwithf "%s. Sequence does not match at position %i. Expected item: %A, but got %A." + message i e a + | i,None,Some e -> + failwithf "%s. Sequence actual shorter than expected, at pos %i for expected item %A." + message i e + | i,Some a,None -> + failwithf "%s. Sequence actual longer than expected, at pos %i found item %A." + message i a \ No newline at end of file