Skip to content

Commit

Permalink
(minor) formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
smoothdeveloper committed Oct 21, 2023
1 parent 2522b16 commit 51bb312
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 121 deletions.
95 changes: 44 additions & 51 deletions src/Fable.Transforms/Python/Fable2Python.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3689,14 +3689,14 @@ module Util =
let fieldIds = getUnionFieldsAsIdents com ctx ent

let genTypeArgument =
let gen =
getGenericTypeParams [ fieldIds[1].Type ]
|> Set.toList
|> List.tryHead
let gen =
getGenericTypeParams [ fieldIds[1].Type ]
|> Set.toList
|> List.tryHead

let ta = Expression.name (gen |> Option.defaultValue "Any")
let id = ident com ctx fieldIds[1]
Arg.arg (id, annotation = ta)
let ta = Expression.name (gen |> Option.defaultValue "Any")
let id = ident com ctx fieldIds[1]
Arg.arg (id, annotation = ta)

let args, isOptional =
let args =
Expand Down Expand Up @@ -3750,53 +3750,46 @@ module Util =
Statement.functionDef (name, Arguments.arguments (), body = body, returns = returnType, decoratorList = decorators)

let constructors =
[

for tag, case in ent.UnionCases |> Seq.indexed do

let name = Identifier case.Name
let args =
Arguments.arguments
[
for field in case.UnionCaseFields do
let ta, _ = typeAnnotation com ctx None field.FieldType
Arg.arg(com.GetIdentifier(ctx, field.Name), ta)
]
let decorators = [Expression.name "staticmethod"]

let values =
[
for field in case.UnionCaseFields do
let identifier : Fable.Ident =
{ Name = field.Name
Type = field.FieldType
IsMutable = false
IsThisArgument = true
IsCompilerGenerated = false
Range = None }
Fable.Expr.IdentExpr identifier
]

let unionExpr,_ =
Fable.Value(Fable.ValueKind.NewUnion(values, tag, ent.Ref, []),None)
|> transformAsExpr com ctx

let body =
[Statement.return' unionExpr]

let returnType =
match args.VarArg with
| None -> Expression.name entName
| Some _ ->
Expression.subscript(Expression.name entName, Expression.name genTypeArgument.Arg)

Statement.functionDef(name, args, body = body, returns = returnType, decoratorList = decorators)
[
for tag, case in ent.UnionCases |> Seq.indexed do
let name = Identifier case.Name
let args =
Arguments.arguments
[
for field in case.UnionCaseFields do
let ta, _ = typeAnnotation com ctx None field.FieldType
Arg.arg(com.GetIdentifier(ctx, field.Name), ta)
]
let decorators = [Expression.name "staticmethod"]
let values =
[
for field in case.UnionCaseFields do
let identifier : Fable.Ident =
{ Name = field.Name
Type = field.FieldType
IsMutable = false
IsThisArgument = true
IsCompilerGenerated = false
Range = None }
Fable.Expr.IdentExpr identifier
]
let unionExpr,_ =
Fable.Value(Fable.ValueKind.NewUnion(values, tag, ent.Ref, []),None)
|> transformAsExpr com ctx
let body =
[Statement.return' unionExpr]
let returnType =
match args.VarArg with
| None -> Expression.name entName
| Some _ ->
Expression.subscript(Expression.name entName, Expression.name genTypeArgument.Arg)
Statement.functionDef(name, args, body = body, returns = returnType, decoratorList = decorators)
]
let baseExpr = libValue com ctx "types" "Union" |> Some
let classMembers = [
cases
yield! constructors
yield! classMembers
cases
yield! constructors
yield! classMembers
]
declareType com ctx ent entName args isOptional body baseExpr classMembers

Expand Down
77 changes: 7 additions & 70 deletions src/quicktest/QuickTest.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,76 +5,13 @@ module QuickTest
// When everything works, move the tests to the appropriate file in tests/Main.
// Please don't add this file to your commits.

open System
open System.Collections.Generic
open Fable.Core
open Fable.Core.JsInterop
open Fable.Core.Testing
type U = Z of value: int | Y of name: string

let log (o: obj) =
JS.console.log(o)
// printfn "%A" o
let u1 = Z 1
let u2 = Y "abc"

let equal expected actual =
let areEqual = expected = actual
printfn "%A = %A > %b" expected actual areEqual
if not areEqual then
failwithf "[ASSERT ERROR] Expected %A but got %A" expected actual
let a : U = Fable.Core.PyInterop.emitPyExpr 1 "U.Z($0)"
let b : U = Fable.Core.PyInterop.emitPyExpr "abc" "U.Y($0)"
Fable.Core.Testing.Assert.AreEqual(a, u1)
Fable.Core.Testing.Assert.AreEqual(b, u2)

let throwsError (expected: string) (f: unit -> 'a): unit =
let success =
try
f () |> ignore
true
with e ->
if not <| String.IsNullOrEmpty(expected) then
equal e.Message expected
false
// TODO better error messages
equal false success

let testCase (msg: string) f: unit =
try
printfn "%s" msg
f ()
with ex ->
printfn "%s" ex.Message
if ex.Message <> null && ex.Message.StartsWith("[ASSERT ERROR]") |> not then
printfn "%s" (ex.StackTrace ??= "")
printfn ""

let testCaseAsync msg f =
testCase msg (fun () ->
async {
try
do! f ()
with ex ->
printfn "%s" ex.Message
if ex.Message <> null && ex.Message.StartsWith("[ASSERT ERROR]") |> not then
printfn "%s" (ex.StackTrace ??= "")
} |> Async.StartImmediate)

let throwsAnyError (f: unit -> 'a): unit =
let success =
try
f() |> ignore
true
with e ->
printfn "Got expected error: %s" e.Message
false
if success then
printfn "[ERROR EXPECTED]"

let measureTime (f: unit -> unit): unit = emitJsStatement () """
//js
const startTime = process.hrtime();
f();
const elapsed = process.hrtime(startTime);
console.log("Ms:", elapsed[0] * 1e3 + elapsed[1] / 1e6);
//!js
"""

// Write here your unit test, you can later move it
// to Fable.Tests project. For example:
// testCase "Addition works" <| fun () ->
// 2 + 2 |> equal 4

0 comments on commit 51bb312

Please sign in to comment.