|
| 1 | +/// Cross-platform test utilities for writing tests that run on both .NET (with XUnit) and Python (with pytest). |
| 2 | +/// |
| 3 | +/// Example usage: |
| 4 | +/// ```fsharp |
| 5 | +/// open Fable.Python.Testing |
| 6 | +/// |
| 7 | +/// [<Fact>] |
| 8 | +/// let ``test addition works`` () = |
| 9 | +/// let result = 2 + 2 |
| 10 | +/// result |> equal 4 |
| 11 | +/// |
| 12 | +/// [<Fact>] |
| 13 | +/// let ``test throws on invalid input`` () = |
| 14 | +/// throwsAnyError (fun () -> failwith "boom") |
| 15 | +/// ``` |
| 16 | +module Fable.Python.Testing |
| 17 | + |
| 18 | +open System |
| 19 | + |
| 20 | +#if FABLE_COMPILER |
| 21 | +open Fable.Core.Testing |
| 22 | + |
| 23 | +/// Assert equality (expected first, then actual - F# style) |
| 24 | +let equal expected actual : unit = Assert.AreEqual(actual, expected) |
| 25 | + |
| 26 | +/// Assert inequality |
| 27 | +let notEqual expected actual : unit = Assert.NotEqual(actual, expected) |
| 28 | + |
| 29 | +/// Attribute to mark test functions (compiles to test_ prefix for pytest) |
| 30 | +type FactAttribute() = |
| 31 | + inherit System.Attribute() |
| 32 | + |
| 33 | +#else |
| 34 | +open Xunit |
| 35 | + |
| 36 | +/// Assert equality (expected first, then actual - F# style) |
| 37 | +let equal<'T> (expected: 'T) (actual: 'T) : unit = Assert.Equal(expected, actual) |
| 38 | + |
| 39 | +/// Assert inequality |
| 40 | +let notEqual<'T> (expected: 'T) (actual: 'T) : unit = Assert.NotEqual(expected, actual) |
| 41 | + |
| 42 | +/// FactAttribute is already provided by XUnit |
| 43 | +type FactAttribute = Xunit.FactAttribute |
| 44 | +#endif |
| 45 | + |
| 46 | +// Exception testing helpers (work on both platforms) |
| 47 | + |
| 48 | +let private run (f: unit -> 'a) = |
| 49 | + try |
| 50 | + f () |> Ok |
| 51 | + with e -> |
| 52 | + Error e.Message |
| 53 | + |
| 54 | +/// Assert that a function throws an error with the exact message |
| 55 | +let throwsError (expected: string) (f: unit -> 'a) : unit = |
| 56 | + match run f with |
| 57 | + | Error actual when actual = expected -> () |
| 58 | + | Error actual -> equal expected actual |
| 59 | + | Ok _ -> equal expected "No error was thrown" |
| 60 | + |
| 61 | +/// Assert that a function throws an error containing the expected substring |
| 62 | +let throwsErrorContaining (expected: string) (f: unit -> 'a) : unit = |
| 63 | + match run f with |
| 64 | + | Error _ when String.IsNullOrEmpty expected -> () |
| 65 | + | Error (actual: string) when actual.Contains expected -> () |
| 66 | + | Error actual -> equal (sprintf "Error containing '%s'" expected) actual |
| 67 | + | Ok _ -> equal (sprintf "Error containing '%s'" expected) "No error was thrown" |
| 68 | + |
| 69 | +/// Assert that a function throws any error |
| 70 | +let throwsAnyError (f: unit -> 'a) : unit = |
| 71 | + match run f with |
| 72 | + | Error _ -> () |
| 73 | + | Ok _ -> equal "An error" "No error was thrown" |
| 74 | + |
| 75 | +/// Assert that a function does not throw |
| 76 | +let doesntThrow (f: unit -> 'a) : unit = |
| 77 | + match run f with |
| 78 | + | Ok _ -> () |
| 79 | + | Error msg -> equal "No error" (sprintf "Error: %s" msg) |
0 commit comments