Skip to content

Commit

Permalink
start working on index-less json parsing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HLWeil committed Jun 27, 2024
1 parent 34af66f commit 09c017e
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 5 deletions.
102 changes: 100 additions & 2 deletions tests/FsSpreadsheet.Net.Tests/Json.Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ open FsSpreadsheet
open FsSpreadsheet.Net
open Fable.Pyxpecto

let getFilledTestWb() =
let wb = new FsWorkbook()
let ws = FsWorkbook.initWorksheet "MySheet" wb
let r1 = ws.Row(1)
r1.[1].Value <- "A1"
r1.[2].Value <- "B1"
let r2 = ws.Row(2)
r2.[1].Value <- "A2"
r2.[2].Value <- "B2"
wb

let rows =
testList "Rows" [
testCase "Read Standard" <| fun _ ->
Expand Down Expand Up @@ -53,7 +64,48 @@ let rows =
Expect.isTrue (row2.HasCellAt 1) "Row 7 cell"
Expect.equal (row2.[1].Value) "Line 7" "Row 7 cell value"

testCase "Read-Write DefaultTestObject" <| fun _ ->
testCase "NoNumber Filled Write" <| fun _ ->
let wb = getFilledTestWb()
let expectedString = """{
"sheets": [
{
"name": "MySheet",
"rows": [
{
"cells": [
{
"value": "A1"
},
{
"value": "B1"
}
]
},
{
"cells": [
{
"value": "A2"
},
{
"value": "B2"
}
]
}
]
}
]
}"""
let s = wb.ToRowsJsonString(noNumbering = true)
Expect.stringEqual s expectedString "NoNumber Filled Write-Read"

testCase "NoNumber Filled Write-Read" <| fun _ ->
let wb = getFilledTestWb()
let s = wb.ToRowsJsonString(noNumbering = true)
let wb2 = FsWorkbook.fromRowsJsonString(s)
Expect.workSheetEqual (wb.GetWorksheetAt(1)) (wb2.GetWorksheetAt(1)) "NoNumber Filled Write-Read"


testCase "Write-Read DefaultTestObject" <| fun _ ->
let dto = DefaultTestObject.defaultTestObject()
let s = dto.ToRowsJsonString()
System.IO.File.WriteAllText(DefaultTestObject.FsSpreadsheetJSON.asRelativePath,s)
Expand All @@ -64,7 +116,53 @@ let rows =
let columns =
testList "Columns" [

testCase "Read-Write DefaultTestObject" <| fun _ ->
testCase "NoNumber Filled Write" <| fun _ ->
let wb = getFilledTestWb()
let expectedString = """{
"sheets": [
{
"name": "MySheet",
"columns": [
{
"cells": [
{
"value": "A1"
},
{
"value": "A2"
}
]
},
{
"cells": [
{
"value": "B1"
},
{
"value": "B2"
}
]
}
]
}
]
}"""
let s = wb.ToColumnsJsonString(noNumbering = true)
Expect.stringEqual s expectedString "NoNumber Filled Write-Read"

testCase "NoNumber Filled Write-Read" <| fun _ ->
let wb = getFilledTestWb()
let s = wb.ToColumnsJsonString(noNumbering = true)
let wb2 = FsWorkbook.fromColumnsJsonString(s)
Expect.workSheetEqual (wb.GetWorksheetAt(1)) (wb2.GetWorksheetAt(1)) "NoNumber Filled Write-Read"

testCase "NoNumber Write-Read DefaultTestObject" <| fun _ ->
let dto = DefaultTestObject.defaultTestObject()
let s = dto.ToColumnsJsonString(noNumbering = true)
let dto2 = FsWorkbook.fromColumnsJsonString(s)
Expect.isDefaultTestObject dto2

testCase "Write-Read DefaultTestObject" <| fun _ ->
let dto = DefaultTestObject.defaultTestObject()
let s = dto.ToColumnsJsonString()
System.IO.File.WriteAllText(DefaultTestObject.FsSpreadsheetJSON.asRelativePath,s)
Expand Down
53 changes: 50 additions & 3 deletions tests/TestUtils/TestingUtils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,29 @@ open Fable.Core
open Fable.Pyxpecto


module Fable =

module JS =

[<Emit("process.stdout.write($0)")>]
let print (s:string) : unit = nativeOnly

module Py =

[<Emit("print($0, end = \"\")")>]
let print (s:string) : unit = nativeOnly

let fprint(s: string) =
#if FABLE_COMPILER_JAVASCRIPT
JS.print(s)
#endif
#if FABLE_COMPILER_PYTHON
Py.print(s)
#endif
#if !FABLE_COMPILER
printf "%s" s
#endif

[<RequireQualifiedAccess>]
module Utils =

Expand Down Expand Up @@ -45,6 +68,33 @@ type Stopwatch() =
/// Fable compatible Expecto/Mocha unification
module Expect =

let inline equal actual expected message = Expect.equal actual expected message
let notEqual actual expected message = Expect.notEqual actual expected message

/// <summary>
/// This function only verifies non-whitespace characters
/// </summary>
let stringEqual actual expected message =
let pattern = @"\s+"
let regex = System.Text.RegularExpressions.Regex(pattern, Text.RegularExpressions.RegexOptions.Singleline)
let actual = regex.Replace(actual, "")
let expected = regex.Replace(expected, "")
let mutable isSame = true
Seq.iter2
(fun s1 s2 ->
if isSame && s1 = s2 then
()
elif isSame && s1 <> s2 then
isSame <- false
Fable.fprint (sprintf "%s" (string s1))
else
Fable.fprint (sprintf "%s" (string s1))
)
actual
expected
equal actual expected message


/// Expects the `actual` sequence to equal the `expected` one.
let inline private _sequenceEqual message (comparison: int * 'a option * 'a option) =
match comparison with
Expand Down Expand Up @@ -133,9 +183,6 @@ module Expect =
for actualRow, expectedRow in Seq.zip actualRows expectedRows do
cellSequenceEquals actualRow expectedRow $"ExpectError: Worksheet rows are not equal for worksheet: {ws.Name}"

let inline equal actual expected message = Expect.equal actual expected message
let notEqual actual expected message = Expect.notEqual actual expected message

let isNull actual message = Expect.isNull actual message
let isNotNull actual message = Expect.isNotNull actual message

Expand Down

0 comments on commit 09c017e

Please sign in to comment.