Skip to content

Commit

Permalink
make tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
Freymaurer committed Nov 30, 2023
1 parent d404861 commit 3f4622b
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 29 deletions.
12 changes: 11 additions & 1 deletion build/Helpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,17 @@ let python =
match ProcessUtils.tryFindFileOnPath "python" with
| Some path -> path
| None ->
"py was not found in path. Please install it and make sure it's available from your path."
"python was not found in path. Please install it and make sure it's available from your path."
|> failwith

createProcess path

let node =
let path =
match ProcessUtils.tryFindFileOnPath "node" with
| Some path -> path
| None ->
"node was not found in path. Please install it and make sure it's available from your path."
|> failwith

createProcess path
Expand Down
29 changes: 29 additions & 0 deletions build/TestTasks.fs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,35 @@ let runTestsJs = BuildTask.create "RunTestsJs" [clean; build;] {
run npx $"mocha {test}/js --timeout 20000" ""
}

module RunMt =
let rootPath = @"./tests/Multitarget.Tests"

let py = BuildTask.create "runMtPy" [clean; build] {
let py_folder_name = "py"
run dotnet $"fable {rootPath} --lang py -o {rootPath}/{py_folder_name}" ""
run python $"{rootPath}/{py_folder_name}/main.py" ""
}

let js = BuildTask.create "runMtJs" [clean; build] {
let js_folder_name = "js"
run dotnet $"fable {rootPath} -o {rootPath}/{js_folder_name}" ""
run node $"{rootPath}/{js_folder_name}/Main.js" ""
}

let ts = BuildTask.create "runMtTs" [clean; build] {
let ts_folder_name = "ts"
run dotnet $"fable {rootPath} --lang ts -o {rootPath}/{ts_folder_name}" ""
run npx $"ts-node {rootPath}/{ts_folder_name}/Main.ts" ""
}

let net = BuildTask.create "runMtNet" [clean; build] {
run dotnet "run" rootPath
}

let runMultiTargetTests = BuildTask.create "runMt" [clean; build; RunMt.js; RunMt.net; RunMt.ts; RunMt.py] {
()
}

let runTests = BuildTask.create "RunTests" [clean; build; runTestsDotNet; runTestsPy; runTestsJs] {
()
}
59 changes: 40 additions & 19 deletions src/Pyxpecto.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,23 @@
open System
open Fable.Core

#if FABLE_COMPILER_JAVASCRIPT || FABLE_COMPILER_TYPESCRIPT
open Fable.Core.JsInterop
#endif

module TranspilerHelper =

// This is possibly the most magic
#if !FABLE_COMPILER_JAVASCRIPT && !FABLE_COMPILER_TYPESCRIPT
let (!!) (any: 'a) = any
#endif

let private x = 0 // placeholder to make it not error in not js/ts

open TranspilerHelper

module Assert =

open Fable.Core.Testing

let AreEqual(actual, expected, msg) =
Expand Down Expand Up @@ -223,7 +239,9 @@ module Test =
module Expect =

let inline equal (actual: 'a) (expected: 'a) msg : unit =
if actual = expected then
let actual' = !! actual
let expected' = !! expected
if actual' = expected' then
Assert.AreEqual(actual, expected, msg)
else
let errorMsg =
Expand Down Expand Up @@ -531,26 +549,29 @@ module Pyxpecto =
sb.AppendLine() |> ignore
let msg = sb.AppendLine(sep).AppendLine(innerMsgString).AppendLine(sep).ToString()
printfn "%s" msg
match runner.ErrorTests.Value, runner.FailedTests.Value with
| errors,_ when errors > 0 ->
Exception($"{BColors.FAIL}❌ Exited with error code 2{BColors.ENDC}") |> printfn "%A"
return 2
| _,failed when failed > 0 ->
Exception($"{BColors.FAIL}❌ Exited with error code 1{BColors.ENDC}") |> printfn "%A"
return 1
| _ ->
printfn $"{BColors.OKGREEN}Success!{BColors.ENDC}"
return 0
// This might not be necessary if the exitcode issue with python is fixed
let raiseInJs =
#if FABLE_COMPILER_JAVASCRIPT || FABLE_COMPILER_TYPESCRIPT
failwith
#else
fun _ -> ()
#endif
let exitCode : int =
match runner.ErrorTests.Value, runner.FailedTests.Value with
| errors,_ when errors > 0 ->
Exception($"{BColors.FAIL}❌ Exited with error code 2{BColors.ENDC}") |> printfn "%A"
raiseInJs "ExitCode: 2"
2
| _,failed when failed > 0 ->
Exception($"{BColors.FAIL}❌ Exited with error code 1{BColors.ENDC}") |> printfn "%A"
raiseInJs "ExitCode: 1"
1
| _ ->
printfn $"{BColors.OKGREEN}Success!{BColors.ENDC}"
0
return exitCode
}

// This is possibly the most magic
#if !FABLE_COMPILER_JAVASCRIPT && !FABLE_COMPILER_TYPESCRIPT
let (!!) (any: 'a) = any
#endif
#if FABLE_COMPILER_JAVASCRIPT || FABLE_COMPILER_TYPESCRIPT
open Fable.Core.JsInterop
#endif

let rec runTests (test: TestCase) =
let r = runViaPy test |> start
!!r
21 changes: 12 additions & 9 deletions tests/Mocha.Tests/Main.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,27 @@ open Expecto
#endif

let tests_sequential = testSequenced <| testList "Sequential" [
let mutable TESTMUTABLE = 0
let TESTMUTABLE = ref 0

testSequenced <| testList "Nested Sequential" [
testCase "sync 1" <| fun _ ->
Expect.equal TESTMUTABLE 0 "Should be init value"
Expect.equal TESTMUTABLE.Value 0 "Should be init value"
testCaseAsync "async 1" <| async {
do! Async.Sleep 1000
TESTMUTABLE <- 1
Expect.equal TESTMUTABLE 1 "Should be 1"
TESTMUTABLE.Value <- 1
Expect.equal TESTMUTABLE.Value 1 "Should be 1"
}
testCase "sync 2" <| fun _ ->
Expect.equal TESTMUTABLE 1 "Should be 1"
Expect.equal TESTMUTABLE.Value 1 "Should be 1"
]
testCaseAsync "one" <| async {
do! Async.Sleep 1000
TESTMUTABLE <- 2
Expect.equal TESTMUTABLE 2 "Should be 2"
TESTMUTABLE.Value <- 2
Expect.equal TESTMUTABLE.Value 2 "Should be 2"
}

testCase "sync one" <| fun _ ->
Expect.equal TESTMUTABLE 2 "Should be 2"
Expect.equal TESTMUTABLE.Value 2 "Should be 2"

testCaseAsync "two" <| async {
do! Async.Sleep 1000
Expand All @@ -51,6 +51,9 @@ let tests_sequential = testSequenced <| testList "Sequential" [
]

let tests_basic = testList "Basic" [
testCase "fails in python" <| fun () ->
Expect.equal (1 + 1) 2 "Should be equal"

testCase "testCase works with numbers" <| fun () ->
Expect.equal (1 + 1) 2 "Should be equal"

Expand All @@ -70,7 +73,7 @@ let tests_basic = testList "Basic" [
Expect.isOk actual "Should fail"
Expect.equal true false "Should not be tested"
let catch (exn: System.Exception) =
Expect.equal exn.Message "Should fail. Expected Ok, was Error(\"fails\")." "Error messages should be the same"
Expect.equal exn.Message "Should fail. Expected Ok, was Error('fails')." "Error messages should be the same"
Expect.throwsC case catch

testCase "isEmpty works correctly" <| fun _ ->
Expand Down
9 changes: 9 additions & 0 deletions tests/Multitarget.Tests/Main.fs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ let tests_sequential = testSequenced <| testList "Sequential" [
]

let tests_basic = testList "Basic" [
testCase "check fail" <| fun _ ->
#if FABLE_COMPILER_PYTHON
Expect.equal (1 + 1) 3 "Should be equal"
#endif
//#if !FABLE_COMPILER
//Expect.equal (1 + 1) 3 "Should be equal"
//#endif
Expect.equal (1 + 1) 2 "Should be equal"

testCase "testCase works with numbers" <| fun () ->
Expect.equal (1 + 1) 2 "Should be equal"

Expand Down

0 comments on commit 3f4622b

Please sign in to comment.