diff --git a/tests/FSharp.Data.Core.Tests/BaseTypesHtmlGenerics.fs b/tests/FSharp.Data.Core.Tests/BaseTypesHtmlGenerics.fs new file mode 100644 index 000000000..737c6d3ce --- /dev/null +++ b/tests/FSharp.Data.Core.Tests/BaseTypesHtmlGenerics.fs @@ -0,0 +1,161 @@ +module FSharp.Data.Tests.BaseTypesHtmlGenerics + +open NUnit.Framework +open FsUnit +open System +open System.Reflection +open System.IO +open FSharp.Data +open FSharp.Data.Runtime.BaseTypes + +// ============================================ +// BaseTypes.HtmlDocument Coverage Tests +// ============================================ + +// Use reflection to create HtmlDocument since Create is for generated code only +let private createHtmlDocumentFromString (html: string) = + use reader = new StringReader(html) + let createMethod = typeof.GetMethod("Create", [| typeof; typeof |]) + createMethod.Invoke(null, [| false; reader |]) :?> HtmlDocument + +[] +let ``HtmlDocument Create should work using reflection`` () = + let html = """
Test
""" + let doc = createHtmlDocumentFromString html + + doc |> should not' (be null) + doc.Html |> should not' (be null) + +[] +let ``HtmlDocument GetTable should handle reflection calls`` () = + let html = """
Test
""" + let doc = createHtmlDocumentFromString html + + let getTableMethod = doc.GetType().GetMethod("GetTable") + // Even if the table is not found, the method should exist and be callable + getTableMethod |> should not' (be null) + +[] +let ``HtmlDocument GetList should handle reflection calls`` () = + let html = """
  • Item
""" + let doc = createHtmlDocumentFromString html + + let getListMethod = doc.GetType().GetMethod("GetList") + // Even if the list is not found, the method should exist and be callable + getListMethod |> should not' (be null) + +[] +let ``HtmlDocument GetDefinitionList should handle reflection calls`` () = + let html = """
Term
Definition
""" + let doc = createHtmlDocumentFromString html + + let getDefListMethod = doc.GetType().GetMethod("GetDefinitionList") + // Even if the definition list is not found, the method should exist and be callable + getDefListMethod |> should not' (be null) + +// ============================================ +// BaseTypes.HtmlTable Coverage Tests +// ============================================ + +[] +let ``HtmlTable Create with headers should work using reflection`` () = + let html = """
NameAge
John30
""" + let doc = createHtmlDocumentFromString html + let converter = Func(fun row -> String.Join(",", row)) + let createMethod = typeof>.GetMethod("Create", [| typeof>; typeof; typeof; typeof |]) + + let table = createMethod.Invoke(null, [| converter; doc; "testTable"; true |]) :?> HtmlTable + + table.Name |> should equal "testTable" + table.Headers |> should not' (be null) + table.Rows.Length |> should be (greaterThan 0) + table.Html |> should not' (be null) + +[] +let ``HtmlTable Create without headers should work using reflection`` () = + let html = """
John30
Jane25
""" + let doc = createHtmlDocumentFromString html + let converter = Func(fun row -> String.Join("|", row)) + let createMethod = typeof>.GetMethod("Create", [| typeof>; typeof; typeof; typeof |]) + + let table = createMethod.Invoke(null, [| converter; doc; "testTable"; false |]) :?> HtmlTable + + table.Name |> should equal "testTable" + table.Headers |> should be null + table.Rows.Length |> should be (greaterThan 0) + table.Html |> should not' (be null) + +[] +let ``HtmlTable properties should be accessible`` () = + let html = """
Col1Col2
AB
""" + let doc = createHtmlDocumentFromString html + let converter = Func(fun row -> row.Length) + let createMethod = typeof>.GetMethod("Create", [| typeof>; typeof; typeof; typeof |]) + + let table = createMethod.Invoke(null, [| converter; doc; "testTable"; true |]) :?> HtmlTable + + table.Name |> should equal "testTable" + table.Headers.Value |> should haveLength 2 + table.Rows |> should haveLength 1 + table.Rows.[0] |> should equal 2 + table.Html |> should not' (be null) + +// ============================================ +// BaseTypes.HtmlList Coverage Tests +// ============================================ + +[] +let ``HtmlList Create should work using reflection`` () = + let html = """
  • Item 1
  • Item 2
  • Item 3
""" + let doc = createHtmlDocumentFromString html + let converter = Func(fun item -> item.Length) + let createMethod = typeof>.GetMethod("Create", [| typeof>; typeof; typeof |]) + + let list = createMethod.Invoke(null, [| converter; doc; "testList" |]) :?> HtmlList + + list.Name |> should equal "testList" + list.Values.Length |> should be (greaterThan 0) + list.Html |> should not' (be null) + +[] +let ``HtmlList CreateNested should work using reflection`` () = + let html = """
Term 1
Definition 1
Another definition
Term 2
Definition 2
""" + let doc = createHtmlDocumentFromString html + let converter = Func(fun item -> item.ToUpper()) + let createMethod = typeof>.GetMethod("CreateNested", [| typeof>; typeof; typeof; typeof |]) + + let list = createMethod.Invoke(null, [| converter; doc; "testDefList"; 0 |]) :?> HtmlList + + list.Name |> should not' (be null) + list.Values.Length |> should be (greaterThan 0) + list.Html |> should not' (be null) + +[] +let ``HtmlList properties should be accessible`` () = + let html = """
  • Item 1
  • Item 2
""" + let doc = createHtmlDocumentFromString html + let converter = Func(fun item -> item.Replace("Item", "Element")) + let createMethod = typeof>.GetMethod("Create", [| typeof>; typeof; typeof |]) + + let list = createMethod.Invoke(null, [| converter; doc; "testList" |]) :?> HtmlList + + list.Name |> should equal "testList" + list.Values.Length |> should be (greaterThan 0) + list.Html |> should not' (be null) + +[] +let ``HtmlList with complex converter should work using reflection`` () = + let html = """
  • Item 1
  • Item 2
""" + let doc = createHtmlDocumentFromString html + + // Complex converter that creates a tuple + let converter = Func(fun item -> (item.Trim(), item.Length)) + let createMethod = typeof>.GetMethod("Create", [| typeof>; typeof; typeof |]) + + let list = createMethod.Invoke(null, [| converter; doc; "testList" |]) :?> HtmlList + + list.Name |> should equal "testList" + list.Values.Length |> should be (greaterThan 0) + let (text, length) = list.Values.[0] + text |> should not' (be null) + length |> should be (greaterThan 0) \ No newline at end of file diff --git a/tests/FSharp.Data.Core.Tests/FSharp.Data.Core.Tests.fsproj b/tests/FSharp.Data.Core.Tests/FSharp.Data.Core.Tests.fsproj index e2aa5f628..090a43f82 100644 --- a/tests/FSharp.Data.Core.Tests/FSharp.Data.Core.Tests.fsproj +++ b/tests/FSharp.Data.Core.Tests/FSharp.Data.Core.Tests.fsproj @@ -44,6 +44,7 @@ +