openai/openai-dotnet

Public

mirrored from https://github.com/openai/openai-dotnetAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
copilot/sub-pr-1026-another-one

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

codegen/generator/test/TestHelpers/MockHelpers.cs

103lines · modecode

1using Microsoft.TypeSpec.Generator;
2using Microsoft.TypeSpec.Generator.ClientModel;
3using Microsoft.TypeSpec.Generator.ClientModel.Providers;
4using Microsoft.TypeSpec.Generator.Input;
5using Microsoft.TypeSpec.Generator.Primitives;
6using Microsoft.TypeSpec.Generator.Providers;
7using Microsoft.TypeSpec.Generator.SourceInput;
8using Moq;
9using Moq.Protected;
10using System;
11using System.Collections.Generic;
12using System.IO;
13using System.Reflection;
14
15namespace OpenAILibraryPlugin.Tests.TestHelpers
16{
17 internal class MockHelpers
18 {
19 private static readonly string _configFilePath = Path.Combine(AppContext.BaseDirectory, TestHelpersFolder);
20 private const string TestHelpersFolder = "TestHelpers";
21
22 public static Mock<ScmCodeModelGenerator> LoadMockGenerator(
23 Func<InputType, TypeProvider, IReadOnlyList<TypeProvider>>? createSerializationsCore = null,
24 Func<InputType, CSharpType>? createCSharpTypeCore = null,
25 Func<InputApiKeyAuth>? apiKeyAuth = null,
26 Func<InputOAuth2Auth>? oauth2Auth = null,
27 Func<IReadOnlyList<string>>? apiVersions = null,
28 Func<IReadOnlyList<InputLiteralType>>? inputLiterals = null,
29 Func<IReadOnlyList<InputEnumType>>? inputEnums = null,
30 Func<IReadOnlyList<InputModelType>>? inputModels = null,
31 Func<IReadOnlyList<InputClient>>? clients = null,
32 Func<InputClient, ClientProvider?>? createClientCore = null,
33 ClientResponseApi? clientResponseApi = null,
34 ClientPipelineApi? clientPipelineApi = null,
35 HttpMessageApi? httpMessageApi = null,
36 string? configurationJson = null,
37 string? inputNamespace = null)
38 {
39 IReadOnlyList<string> inputNsApiVersions = apiVersions?.Invoke() ?? [];
40 IReadOnlyList<InputLiteralType> inputNsLiterals = inputLiterals?.Invoke() ?? [];
41 IReadOnlyList<InputEnumType> inputNsEnums = inputEnums?.Invoke() ?? [];
42 IReadOnlyList<InputClient> inputNsClients = clients?.Invoke() ?? [];
43 IReadOnlyList<InputModelType> inputNsModels = inputModels?.Invoke() ?? [];
44 InputAuth inputNsAuth = new InputAuth(apiKeyAuth?.Invoke(), oauth2Auth?.Invoke());
45 var mockInputNs = new Mock<InputNamespace>(
46 inputNamespace ?? "Samples",
47 inputNsApiVersions,
48 inputNsLiterals,
49 inputNsEnums,
50 inputNsModels,
51 inputNsClients,
52 inputNsAuth);
53 var mockInputLibrary = new Mock<InputLibrary>(_configFilePath);
54 mockInputLibrary.Setup(p => p.InputNamespace).Returns(mockInputNs.Object);
55
56 Mock<ScmTypeFactory>? mockTypeFactory = null;
57 if (createCSharpTypeCore is not null)
58 {
59 mockTypeFactory = new Mock<ScmTypeFactory>() { CallBase = true };
60 mockTypeFactory.Protected().Setup<CSharpType>("CreateCSharpTypeCore", ItExpr.IsAny<InputType>()).Returns(createCSharpTypeCore);
61 }
62
63 if (createClientCore is not null)
64 {
65 mockTypeFactory ??= new Mock<ScmTypeFactory>() { CallBase = true };
66 mockTypeFactory.Protected().Setup<ClientProvider?>("CreateClientCore", ItExpr.IsAny<InputClient>()).Returns(createClientCore);
67 }
68
69 // initialize the mock singleton instance of the plugin
70 var codeModelInstance = typeof(CodeModelGenerator).GetField("_instance", BindingFlags.Static | BindingFlags.NonPublic);
71 // invoke the load method with the config file path
72 var loadMethod = typeof(Configuration).GetMethod("Load", BindingFlags.Static | BindingFlags.NonPublic);
73 object?[] parameters = [_configFilePath, configurationJson];
74 var config = loadMethod?.Invoke(null, parameters);
75 var mockGeneratorContext = new Mock<GeneratorContext>(config!);
76 var mockGeneratorInstance = new Mock<ScmCodeModelGenerator>(mockGeneratorContext.Object) { CallBase = true };
77 codeModelInstance!.SetValue(null, mockGeneratorInstance.Object);
78 mockGeneratorInstance.SetupGet(p => p.InputLibrary).Returns(mockInputLibrary.Object);
79
80 if (mockTypeFactory is not null)
81 {
82 mockGeneratorInstance.SetupGet(p => p.TypeFactory).Returns(mockTypeFactory.Object);
83 }
84
85 var sourceInputModel = new Mock<SourceInputModel>(() => new SourceInputModel(null, null)) { CallBase = true };
86 mockGeneratorInstance.Setup(p => p.SourceInputModel).Returns(sourceInputModel.Object);
87 var configureMethod = typeof(CodeModelGenerator).GetMethod(
88 "Configure",
89 BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.InvokeMethod
90 );
91 configureMethod!.Invoke(mockGeneratorInstance.Object, null);
92 return mockGeneratorInstance;
93 }
94
95 public static void SetCustomCodeView(ModelProvider modelProvider, TypeProvider customCodeTypeProvider)
96 {
97 modelProvider.GetType().BaseType!.GetField(
98 "_customCodeView",
99 BindingFlags.NonPublic | BindingFlags.Instance)?
100 .SetValue(modelProvider, new Lazy<TypeProvider>(() => customCodeTypeProvider));
101 }
102 }
103}
104