Skip to content

Commit

Permalink
Make tests pass ✔️
Browse files Browse the repository at this point in the history
  • Loading branch information
Freymaurer committed May 11, 2023
1 parent d158414 commit 58dc08c
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 30 deletions.
3 changes: 2 additions & 1 deletion src/FsSpreadsheet/Cells/FsCell.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
15 changes: 9 additions & 6 deletions src/FsSpreadsheet/Cells/FsCellsCollection.fs
Original file line number Diff line number Diff line change
Expand Up @@ -694,12 +694,15 @@ type FsCellsCollection() =
/// Returns the upper left corner of the FsCellsCollection.
/// </summary>
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)

/// <summary>
/// Returns the upper left corner of a given FsCellsCollection.
Expand Down
18 changes: 12 additions & 6 deletions tests/FsSpreadsheet.Tests/FsCell.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,25 @@ open Expecto
#endif
open FsSpreadsheet

let main =
let dataType =
testList "DataType" [
testList "InferCellValue bool = true" [
let boolValTrue = true
let resultDtTrue, resultStrTrue = DataType.InferCellValue boolValTrue
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
let resultDtFalse, resultStrFalse = DataType.InferCellValue boolValFalse
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"
Expand Down Expand Up @@ -75,8 +77,7 @@ let main =
]
]

[<Tests>]
let fsCellTest =
let fsCellData =
testList "FsCell data" [
testList "Data | DataType | Adress" [
let fscellA1_string = FsCell.create 1 1 "A1"
Expand Down Expand Up @@ -113,4 +114,9 @@ let fsCellTest =
Expect.equal (fscellB1_num.ColumnNumber) 2 "resulting value is not 1 as integer"

]
]
]

let main = testList "FsCell" [
dataType
fsCellData
]
2 changes: 1 addition & 1 deletion tests/FsSpreadsheet.Tests/FsColumn.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>) expectedValues "Values are not correct"
Expect.mySequenceEqual (Seq.item 0 columns |> Seq.map FsCell.getValueAs<string>) expectedValues "Values are not correct"
]
]
1 change: 1 addition & 0 deletions tests/FsSpreadsheet.Tests/FsSpreadsheet.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<Compile Include="Utils.fs" />
<Compile Include="Resources.fs" />
<Compile Include="FsWorkbook.fs" />
<Compile Include="FsWorksheet.fs" />
Expand Down
6 changes: 3 additions & 3 deletions tests/FsSpreadsheet.Tests/FsTable.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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" [
Expand Down Expand Up @@ -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"
]
]
]
2 changes: 1 addition & 1 deletion tests/FsSpreadsheet.Tests/FsTableField.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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!"
Expand Down
21 changes: 9 additions & 12 deletions tests/FsSpreadsheet.Tests/FsWorksheet.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,23 @@ open Expecto
#endif
open FsSpreadsheet


let dummyCellsColl = FsCellsCollection()
let dummyTable1 = FsTable("dummyTable1", FsRangeAddress("A1:B2"))
let dummyTable2 = FsTable("dummyTable2", FsRangeAddress("D1:F3"))
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" [
Expand All @@ -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]
Expand Down
42 changes: 42 additions & 0 deletions tests/FsSpreadsheet.Tests/Utils.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[<AutoOpen>]
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

0 comments on commit 58dc08c

Please sign in to comment.