From 09c017e5f1c596574015b1bf2aca390ff77ffafc Mon Sep 17 00:00:00 2001 From: HLWeil Date: Thu, 27 Jun 2024 17:25:32 +0200 Subject: [PATCH] start working on index-less json parsing tests --- tests/FsSpreadsheet.Net.Tests/Json.Tests.fs | 102 +++++++++++++++++++- tests/TestUtils/TestingUtils.fs | 53 +++++++++- 2 files changed, 150 insertions(+), 5 deletions(-) diff --git a/tests/FsSpreadsheet.Net.Tests/Json.Tests.fs b/tests/FsSpreadsheet.Net.Tests/Json.Tests.fs index c797d9d..6612a9f 100644 --- a/tests/FsSpreadsheet.Net.Tests/Json.Tests.fs +++ b/tests/FsSpreadsheet.Net.Tests/Json.Tests.fs @@ -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 _ -> @@ -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) @@ -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) diff --git a/tests/TestUtils/TestingUtils.fs b/tests/TestUtils/TestingUtils.fs index f82457b..35aced5 100644 --- a/tests/TestUtils/TestingUtils.fs +++ b/tests/TestUtils/TestingUtils.fs @@ -6,6 +6,29 @@ open Fable.Core open Fable.Pyxpecto +module Fable = + + module JS = + + [] + let print (s:string) : unit = nativeOnly + + module Py = + + [] + 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 + [] module Utils = @@ -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 + + /// + /// This function only verifies non-whitespace characters + /// + 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 @@ -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