Skip to content
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

add tryGetColumnByHeaderBy member and static + tests #441

Merged
merged 2 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading