Implement

This commit is contained in:
Smaug123
2025-09-17 08:37:41 +01:00
parent 2c539c13a3
commit 07e9eaff38
9 changed files with 142 additions and 128 deletions

View File

@@ -5,7 +5,7 @@ open WoofWare.Myriad.Plugins
[<GenerateCapturingMock>] [<GenerateCapturingMock>]
type IPublicType = type IPublicType =
abstract Mem1 : string * int -> string list abstract Mem1 : foo : string * int -> string list
abstract Mem2 : string -> int abstract Mem2 : string -> int
abstract Mem3 : x : int * ?ct : System.Threading.CancellationToken -> string abstract Mem3 : x : int * ?ct : System.Threading.CancellationToken -> string
@@ -36,9 +36,9 @@ type VeryPublicType<'a, 'b> =
[<GenerateCapturingMock>] [<GenerateCapturingMock>]
type Curried<'a> = type Curried<'a> =
abstract Mem1 : int -> 'a -> string abstract Mem1 : bar : int -> 'a -> string
abstract Mem2 : int * string -> 'a -> string abstract Mem2 : int * string -> baz : 'a -> string
abstract Mem3 : (int * string) -> 'a -> string abstract Mem3 : quux : (int * string) -> flurb : 'a -> string
abstract Mem4 : (int * string) -> ('a * int) -> string abstract Mem4 : (int * string) -> ('a * int) -> string
abstract Mem5 : x : int * string -> ('a * int) -> string abstract Mem5 : x : int * string -> ('a * int) -> string
abstract Mem6 : int * string -> y : 'a * int -> string abstract Mem6 : int * string -> y : 'a * int -> string

View File

@@ -170,63 +170,64 @@ namespace SomeNamespace.CapturingMock
open System open System
open WoofWare.Myriad.Plugins open WoofWare.Myriad.Plugins
/// A single call to the Mem1 method module internal CurriedMockCalls =
type internal Mem1Call<'a> = /// A single call to the Mem1 method
{ type internal Mem1Call<'a> =
arg0 : int {
arg1 : 'a bar : int
} Arg1 : 'a
}
/// A single call to the Mem2 method /// A single call to the Mem2 method
type internal Mem2Call<'a> = type internal Mem2Call<'a> =
{ {
arg0 : int * string Arg0 : int * string
arg1 : 'a baz : 'a
} }
/// A single call to the Mem3 method /// A single call to the Mem3 method
type internal Mem3Call<'a> = type internal Mem3Call<'a> =
{ {
arg0 : int * string quux : (int * string)
arg1 : 'a flurb : 'a
} }
/// A single call to the Mem4 method /// A single call to the Mem4 method
type internal Mem4Call<'a> = type internal Mem4Call<'a> =
{ {
arg0 : int * string Arg0 : int * string
arg1 : 'a * int Arg1 : 'a * int
} }
/// A single call to the Mem5 method /// A single call to the Mem5 method
type internal Mem5Call<'a> = type internal Mem5Call<'a> =
{ {
arg0 : int * string Arg0 : int * string
arg1 : 'a * int Arg1 : 'a * int
} }
/// A single call to the Mem6 method /// A single call to the Mem6 method
type internal Mem6Call<'a> = type internal Mem6Call<'a> =
{ {
arg0 : int * string Arg0 : int * string
arg1 : 'a * int Arg1 : 'a * int
} }
/// Mock record type for an interface /// Mock record type for an interface
type internal CurriedMock<'a> = type internal CurriedMock<'a> =
{ {
Mem1 : int -> 'a -> string Mem1 : int -> 'a -> string
Mem1_Calls : ResizeArray<Mem1Call<'a>> Mem1_Calls : ResizeArray<CurriedMockCalls.Mem1Call<'a>>
Mem2 : int * string -> 'a -> string Mem2 : int * string -> 'a -> string
Mem2_Calls : ResizeArray<Mem2Call<'a>> Mem2_Calls : ResizeArray<CurriedMockCalls.Mem2Call<'a>>
Mem3 : (int * string) -> 'a -> string Mem3 : (int * string) -> 'a -> string
Mem3_Calls : ResizeArray<Mem3Call<'a>> Mem3_Calls : ResizeArray<CurriedMockCalls.Mem3Call<'a>>
Mem4 : (int * string) -> ('a * int) -> string Mem4 : (int * string) -> ('a * int) -> string
Mem4_Calls : ResizeArray<Mem4Call<'a>> Mem4_Calls : ResizeArray<CurriedMockCalls.Mem4Call<'a>>
Mem5 : int * string -> ('a * int) -> string Mem5 : int * string -> ('a * int) -> string
Mem5_Calls : ResizeArray<Mem5Call<'a>> Mem5_Calls : ResizeArray<CurriedMockCalls.Mem5Call<'a>>
Mem6 : int * string -> 'a * int -> string Mem6 : int * string -> 'a * int -> string
Mem6_Calls : ResizeArray<Mem6Call<'a>> Mem6_Calls : ResizeArray<CurriedMockCalls.Mem6Call<'a>>
} }
/// An implementation where every non-unit method throws. /// An implementation where every non-unit method throws.
@@ -249,7 +250,7 @@ type internal CurriedMock<'a> =
interface Curried<'a> with interface Curried<'a> with
member this.Mem1 arg_0_0 arg_1_0 = this.Mem1 (arg_0_0) (arg_1_0) member this.Mem1 arg_0_0 arg_1_0 = this.Mem1 (arg_0_0) (arg_1_0)
member this.Mem2 (arg_0_0, arg_0_1) arg_1_0 = this.Mem2 (arg_0_0, arg_0_1) (arg_1_0) member this.Mem2 (arg_0_0, arg_0_1) arg_1_0 = this.Mem2 (arg_0_0, arg_0_1) (arg_1_0)
member this.Mem3 ((arg_0_0, arg_0_1)) arg_1_0 = this.Mem3 (arg_0_0, arg_0_1) (arg_1_0) member this.Mem3 arg_0_0 arg_1_0 = this.Mem3 (arg_0_0) (arg_1_0)
member this.Mem4 ((arg_0_0, arg_0_1)) ((arg_1_0, arg_1_1)) = member this.Mem4 ((arg_0_0, arg_0_1)) ((arg_1_0, arg_1_1)) =
this.Mem4 (arg_0_0, arg_0_1) (arg_1_0, arg_1_1) this.Mem4 (arg_0_0, arg_0_1) (arg_1_0, arg_1_1)

View File

@@ -163,63 +163,64 @@ namespace SomeNamespace.CapturingMock
open System open System
/// A single call to the Mem1 method module internal CurriedNoAttrMockCalls =
type internal Mem1Call<'a> = /// A single call to the Mem1 method
{ type internal Mem1Call<'a> =
arg0 : int {
arg1 : 'a Arg0 : int
} Arg1 : 'a
}
/// A single call to the Mem2 method /// A single call to the Mem2 method
type internal Mem2Call<'a> = type internal Mem2Call<'a> =
{ {
arg0 : int * string Arg0 : int * string
arg1 : 'a Arg1 : 'a
} }
/// A single call to the Mem3 method /// A single call to the Mem3 method
type internal Mem3Call<'a> = type internal Mem3Call<'a> =
{ {
arg0 : int * string Arg0 : int * string
arg1 : 'a Arg1 : 'a
} }
/// A single call to the Mem4 method /// A single call to the Mem4 method
type internal Mem4Call<'a> = type internal Mem4Call<'a> =
{ {
arg0 : int * string Arg0 : int * string
arg1 : 'a * int Arg1 : 'a * int
} }
/// A single call to the Mem5 method /// A single call to the Mem5 method
type internal Mem5Call<'a> = type internal Mem5Call<'a> =
{ {
arg0 : int * string Arg0 : int * string
arg1 : 'a * int Arg1 : 'a * int
} }
/// A single call to the Mem6 method /// A single call to the Mem6 method
type internal Mem6Call<'a> = type internal Mem6Call<'a> =
{ {
arg0 : int * string Arg0 : int * string
arg1 : 'a * int Arg1 : 'a * int
} }
/// Mock record type for an interface /// Mock record type for an interface
type internal CurriedNoAttrMock<'a> = type internal CurriedNoAttrMock<'a> =
{ {
Mem1 : int -> 'a -> string Mem1 : int -> 'a -> string
Mem1_Calls : ResizeArray<Mem1Call<'a>> Mem1_Calls : ResizeArray<CurriedNoAttrMockCalls.Mem1Call<'a>>
Mem2 : int * string -> 'a -> string Mem2 : int * string -> 'a -> string
Mem2_Calls : ResizeArray<Mem2Call<'a>> Mem2_Calls : ResizeArray<CurriedNoAttrMockCalls.Mem2Call<'a>>
Mem3 : (int * string) -> 'a -> string Mem3 : (int * string) -> 'a -> string
Mem3_Calls : ResizeArray<Mem3Call<'a>> Mem3_Calls : ResizeArray<CurriedNoAttrMockCalls.Mem3Call<'a>>
Mem4 : (int * string) -> ('a * int) -> string Mem4 : (int * string) -> ('a * int) -> string
Mem4_Calls : ResizeArray<Mem4Call<'a>> Mem4_Calls : ResizeArray<CurriedNoAttrMockCalls.Mem4Call<'a>>
Mem5 : int * string -> ('a * int) -> string Mem5 : int * string -> ('a * int) -> string
Mem5_Calls : ResizeArray<Mem5Call<'a>> Mem5_Calls : ResizeArray<CurriedNoAttrMockCalls.Mem5Call<'a>>
Mem6 : int * string -> 'a * int -> string Mem6 : int * string -> 'a * int -> string
Mem6_Calls : ResizeArray<Mem6Call<'a>> Mem6_Calls : ResizeArray<CurriedNoAttrMockCalls.Mem6Call<'a>>
} }
/// An implementation where every non-unit method throws. /// An implementation where every non-unit method throws.

View File

@@ -6,7 +6,7 @@ open NUnit.Framework
open FsUnitTyped open FsUnitTyped
[<TestFixture>] [<TestFixture>]
module TestMockGenerator = module TestCapturingMockGenerator =
[<Test>] [<Test>]
let ``Example of use: IPublicType`` () = let ``Example of use: IPublicType`` () =

View File

@@ -6,7 +6,7 @@ open NUnit.Framework
open FsUnitTyped open FsUnitTyped
[<TestFixture>] [<TestFixture>]
module TestCapturingMockGeneratorNoAttr = module TestMockGeneratorNoAttr =
[<Test>] [<Test>]
let ``Example of use: IPublicType`` () = let ``Example of use: IPublicType`` () =

View File

@@ -28,7 +28,9 @@
<Compile Include="TestHttpClient\TestVaultClient.fs" /> <Compile Include="TestHttpClient\TestVaultClient.fs" />
<Compile Include="TestHttpClient\TestVariableHeader.fs" /> <Compile Include="TestHttpClient\TestVariableHeader.fs" />
<Compile Include="TestMockGenerator\TestMockGenerator.fs" /> <Compile Include="TestMockGenerator\TestMockGenerator.fs" />
<Compile Include="TestMockGenerator\TestCapturingMockGeneratorNoAttr.fs" /> <Compile Include="TestMockGenerator\TestMockGeneratorNoAttr.fs" />
<Compile Include="TestCapturingMockGenerator\TestCapturingMockGenerator.fs" />
<Compile Include="TestCapturingMockGenerator\TestCapturingMockGeneratorNoAttr.fs" />
<Compile Include="TestJsonSerialize\TestJsonSerde.fs" /> <Compile Include="TestJsonSerialize\TestJsonSerde.fs" />
<Compile Include="TestCataGenerator\TestCataGenerator.fs" /> <Compile Include="TestCataGenerator\TestCataGenerator.fs" />
<Compile Include="TestCataGenerator\TestDirectory.fs" /> <Compile Include="TestCataGenerator\TestDirectory.fs" />
@@ -67,9 +69,4 @@
<ProjectReference Include="..\ConsumePlugin\ConsumePlugin.fsproj" /> <ProjectReference Include="..\ConsumePlugin\ConsumePlugin.fsproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Compile Include="TestCapturingMockGenerator\TestCapturingMockGenerator.fs" />
<Compile Include="TestCapturingMockGenerator\TestCapturingMockGeneratorNoAttr.fs" />
</ItemGroup>
</Project> </Project>

View File

@@ -41,7 +41,12 @@ module internal CapturingInterfaceMockGenerator =
args args
|> List.mapi (fun i tupledArg -> |> List.mapi (fun i tupledArg ->
{ {
SynFieldData.Ident = $"arg%i{i}" |> Ident.create |> Some SynFieldData.Ident =
match tupledArg.Args with
| [ arg ] -> arg.Id
| _ -> None
|> Option.defaultValue (Ident.create $"Arg%i{i}")
|> Some
Attrs = [] Attrs = []
Type = Type =
tupledArg.Args tupledArg.Args
@@ -115,12 +120,7 @@ module internal CapturingInterfaceMockGenerator =
/// Builds the record field for the mock object, and also if applicable a type representing a single call to /// Builds the record field for the mock object, and also if applicable a type representing a single call to
/// that object (packaging up the args of the call). /// that object (packaging up the args of the call).
let private constructMember let private constructMember (spec : CapturingInterfaceMockOutputSpec) (mem : MemberInfo) : SynField * CallField =
(spec : CapturingInterfaceMockOutputSpec)
(generics : SynTyparDecls option)
(mem : MemberInfo)
: SynField * CallField
=
let inputType = mem.Args |> List.map constructMemberSinglePlace let inputType = mem.Args |> List.map constructMemberSinglePlace
let funcType = SynType.toFun inputType mem.ReturnType let funcType = SynType.toFun inputType mem.ReturnType
@@ -179,11 +179,11 @@ module internal CapturingInterfaceMockGenerator =
(name : string) (name : string)
(interfaceType : InterfaceType) (interfaceType : InterfaceType)
(xmlDoc : PreXmlDoc) (xmlDoc : PreXmlDoc)
: SynModuleDecl : SynModuleDecl option * SynModuleDecl
= =
let fields = let fields =
interfaceType.Members interfaceType.Members
|> List.map (constructMember spec interfaceType.Generics) |> List.map (constructMember spec)
|> List.append ( |> List.append (
interfaceType.Properties interfaceType.Properties
|> List.map constructProperty |> List.map constructProperty
@@ -252,15 +252,11 @@ module internal CapturingInterfaceMockGenerator =
let callsArrays = let callsArrays =
fields fields
|> List.map (fun (_field, extraType, fieldName) -> |> List.map (fun (_field, _, fieldName) ->
let name = SynLongIdent.createS $"{fieldName}_Calls" let name = SynLongIdent.createS $"{fieldName}_Calls"
let init = let init =
match extraType with SynExpr.createIdent "ResizeArray" |> SynExpr.applyTo (SynExpr.CreateConst ())
| CallField.Original _ ->
SynExpr.createIdent "ResizeArray" |> SynExpr.applyTo (SynExpr.CreateConst ())
| CallField.ArgsObject _ ->
SynExpr.createIdent "ResizeArray" |> SynExpr.applyTo (SynExpr.CreateConst ())
name, init name, init
) )
@@ -305,17 +301,19 @@ module internal CapturingInterfaceMockGenerator =
Type = SynType.app "ResizeArray" [ ty ] Type = SynType.app "ResizeArray" [ ty ]
} }
|> SynField.make |> SynField.make
| CallField.ArgsObject (name, _, generics) -> | CallField.ArgsObject (argsObjectName, _, generics) ->
{ {
Attrs = [] Attrs = []
Ident = Some (fieldName + "_Calls" |> Ident.create) Ident = Some (fieldName + "_Calls" |> Ident.create)
Type = Type =
match generics with match generics with
| None -> SynType.named name.idText | None -> SynType.named argsObjectName.idText
| Some generics -> | Some generics ->
generics.TyparDecls generics.TyparDecls
|> List.map (fun (SynTyparDecl.SynTyparDecl (_, typar)) -> SynType.var typar) |> List.map (fun (SynTyparDecl.SynTyparDecl (_, typar)) -> SynType.var typar)
|> SynType.app name.idText |> SynType.app' (
SynType.createLongIdent' [ $"%s{name}Calls" ; argsObjectName.idText ]
)
|> List.singleton |> List.singleton
|> SynType.app "ResizeArray" |> SynType.app "ResizeArray"
} }
@@ -445,16 +443,25 @@ module internal CapturingInterfaceMockGenerator =
let typeDecl = AstHelper.defineRecordType record let typeDecl = AstHelper.defineRecordType record
SynModuleDecl.Types ( let callsModule =
[ fields
for _, field, _ in fields do |> List.choose (fun (_, field, _) ->
match field with match field with
| CallField.Original _ -> () | CallField.Original _ -> None
| CallField.ArgsObject (_, callType, _) -> yield callType | CallField.ArgsObject (_, callType, _) -> Some callType
yield typeDecl )
], |> function
range0 | [] -> None
) | l ->
SynModuleDecl.Types (l, range0)
|> List.singleton
|> SynModuleDecl.nestedModule (
SynComponentInfo.create (Ident.create $"%s{name}Calls")
|> SynComponentInfo.withAccessibility access
)
|> Some
(callsModule, SynModuleDecl.Types ([ typeDecl ], range0))
let createRecord let createRecord
(namespaceId : LongIdent) (namespaceId : LongIdent)
@@ -476,9 +483,15 @@ module internal CapturingInterfaceMockGenerator =
s s
|> fun s -> s + "Mock" |> fun s -> s + "Mock"
let typeDecl = createType spec name interfaceType docString let callsTypes, typeDecl = createType spec name interfaceType docString
[ yield! opens |> List.map SynModuleDecl.openAny ; yield typeDecl ] [
yield! opens |> List.map SynModuleDecl.openAny
match callsTypes with
| None -> ()
| Some c -> yield c
yield typeDecl
]
|> SynModuleOrNamespace.createNamespace namespaceId |> SynModuleOrNamespace.createNamespace namespaceId
open Myriad.Core open Myriad.Core

View File

@@ -1,5 +1,7 @@
WoofWare.Myriad.Plugins.ArgParserGenerator inherit obj, implements Myriad.Core.IMyriadGenerator WoofWare.Myriad.Plugins.ArgParserGenerator inherit obj, implements Myriad.Core.IMyriadGenerator
WoofWare.Myriad.Plugins.ArgParserGenerator..ctor [constructor]: unit WoofWare.Myriad.Plugins.ArgParserGenerator..ctor [constructor]: unit
WoofWare.Myriad.Plugins.CapturingInterfaceMockGenerator inherit obj, implements Myriad.Core.IMyriadGenerator
WoofWare.Myriad.Plugins.CapturingInterfaceMockGenerator..ctor [constructor]: unit
WoofWare.Myriad.Plugins.CreateCatamorphismGenerator inherit obj, implements Myriad.Core.IMyriadGenerator WoofWare.Myriad.Plugins.CreateCatamorphismGenerator inherit obj, implements Myriad.Core.IMyriadGenerator
WoofWare.Myriad.Plugins.CreateCatamorphismGenerator..ctor [constructor]: unit WoofWare.Myriad.Plugins.CreateCatamorphismGenerator..ctor [constructor]: unit
WoofWare.Myriad.Plugins.HttpClientGenerator inherit obj, implements Myriad.Core.IMyriadGenerator WoofWare.Myriad.Plugins.HttpClientGenerator inherit obj, implements Myriad.Core.IMyriadGenerator

View File

@@ -1,5 +1,5 @@
{ {
"version": "8.0", "version": "8.1",
"publicReleaseRefSpec": [ "publicReleaseRefSpec": [
"^refs/heads/main$" "^refs/heads/main$"
], ],