Skip to content

Commit

Permalink
Merge pull request #441 from olscholz/main
Browse files Browse the repository at this point in the history
add tryGetColumnByHeaderBy member and static + tests
  • Loading branch information
HLWeil authored Sep 17, 2024
2 parents 8b4368a + 1a5c4d6 commit 32875d5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Core/Table/ArcTable.fs
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,16 @@ type ArcTable(name: string, headers: ResizeArray<CompositeHeader>, values: Syste
fun (table:ArcTable) ->
table.TryGetColumnByHeader(header)

// tryGetColumnByHeaderBy
member this.TryGetColumnByHeaderBy (headerPredicate:CompositeHeader -> bool) = //better name for header / action
this.Headers
|> Seq.tryFindIndex headerPredicate
|> Option.map (fun i -> this.GetColumn(i))

static member tryGetColumnByHeaderBy (headerPredicate:CompositeHeader -> bool) =
fun (table:ArcTable) ->
table.TryGetColumnByHeaderBy(headerPredicate)

member this.GetColumnByHeader (header:CompositeHeader) =
match this.TryGetColumnByHeader(header) with
| Some c -> c
Expand Down
39 changes: 39 additions & 0 deletions tests/Core/ArcTable.Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1863,6 +1863,44 @@ let private tests_RemoveColumns =
Expect.equal table.Values.[(table.ColumnCount-1,table.RowCount-1)] (CompositeCell.createTerm oa_SCIEXInstrumentModel) "table.ColumnCount-1,table.RowCount-1"
)
]

let private tests_TryGetColumnByHeaderBy =
testList "TryGetColumnByHeaderBy" [
testCase "Empty column" (fun () ->
let emptyTable = ArcTable.init("empty table")
let column_species = CompositeColumn.create(CompositeHeader.Characteristic oa_species)
emptyTable.AddColumns[|column_species|]
let colOption = emptyTable.TryGetColumnByHeaderBy (fun (header:CompositeHeader) ->
match header with
| CompositeHeader.Characteristic oa -> oa = oa_species
| _ -> false )
let col = Expect.wantSome colOption "should have found col but returned None"
// Expect.sequenceEqual col.Cells column_component.Cells "cells did not match"
Expect.hasLength col.Cells 0 "cells did not match"
)
testCase "Column with values" (fun () ->
let table = create_testTable()
let column_species = CompositeColumn.create(CompositeHeader.Characteristic oa_species, createCells_Term 8)
table.AddColumns[|column_species|]
let colOption = table.TryGetColumnByHeaderBy (fun (header:CompositeHeader) ->
match header with
| CompositeHeader.Characteristic oa -> oa = oa_species
| _ -> false )
let col = Expect.wantSome colOption "should have found col but returned None"
// Expect.hasLength col.Cells 8 "cells did not match"
Expect.sequenceEqual col.Cells column_species.Cells "cells did not match"
)
testCase "Non-existing column" (fun () ->
let table = create_testTable()
let colOption = table.TryGetColumnByHeaderBy (fun (header:CompositeHeader) ->
match header with
| CompositeHeader.Parameter oa -> oa = oa_temperature
| _ -> false )
Expect.isNone colOption "fails to find col therefore returns none"
)
]


let private tests_MoveColumn =
testList "MoveColumn" [
testCase "CheckBoundaries" (fun () ->
Expand Down Expand Up @@ -2541,6 +2579,7 @@ let main =
tests_AddColumnFill
tests_RemoveColumn
tests_RemoveColumns
tests_TryGetColumnByHeaderBy
tests_MoveColumn
tests_RemoveRow
tests_RemoveRows
Expand Down

0 comments on commit 32875d5

Please sign in to comment.