sdkloom 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +239 -2
- package/kits/allowed-sources.json +24 -0
- package/kits/csharp/kit.json +6 -0
- package/kits/csharp/src/Runtime/Client.cs +295 -0
- package/kits/csharp/src/Runtime/Context.cs +216 -0
- package/kits/csharp/src/Runtime/Errors.cs +121 -0
- package/kits/csharp/src/Runtime/Http.cs +218 -0
- package/kits/csharp/src/Runtime/Json.cs +650 -0
- package/kits/csharp/src/Runtime/Lang.cs +212 -0
- package/kits/csharp/src/Runtime/Models.cs +206 -0
- package/kits/csharp/src/Runtime/Multipart.cs +108 -0
- package/kits/csharp/src/Runtime/Pagination.cs +122 -0
- package/kits/csharp/src/Runtime/Sse.cs +197 -0
- package/kits/csharp/src/Runtime/Testing.cs +375 -0
- package/kits/csharp/src/Runtime/Transport.cs +212 -0
- package/kits/csharp/src/Runtime/Wait.cs +57 -0
- package/kits/csharp/src/Runtime/Webhooks.cs +148 -0
- package/kits/csharp/tests/ClientTests.cs +128 -0
- package/kits/csharp/tests/HttpTests.cs +44 -0
- package/kits/csharp/tests/JsonTests.cs +132 -0
- package/kits/csharp/tests/LangTests.cs +57 -0
- package/kits/csharp/tests/MultipartTests.cs +31 -0
- package/kits/csharp/tests/PaginationTests.cs +93 -0
- package/kits/csharp/tests/SseTests.cs +122 -0
- package/kits/csharp/tests/TestRunner.cs +93 -0
- package/kits/csharp/tests/TestingTests.cs +80 -0
- package/kits/csharp/tests/TransportTests.cs +145 -0
- package/kits/csharp/tests/WaitTests.cs +39 -0
- package/kits/csharp/tests/WebhooksTests.cs +46 -0
- package/kits/csharp/versions.json +12 -0
- package/kits/go/kit.json +6 -0
- package/kits/go/src/sdkruntime/client.go +322 -0
- package/kits/go/src/sdkruntime/context.go +207 -0
- package/kits/go/src/sdkruntime/errors.go +74 -0
- package/kits/go/src/sdkruntime/http.go +261 -0
- package/kits/go/src/sdkruntime/json.go +527 -0
- package/kits/go/src/sdkruntime/lang.go +462 -0
- package/kits/go/src/sdkruntime/multipart.go +75 -0
- package/kits/go/src/sdkruntime/pagination.go +150 -0
- package/kits/go/src/sdkruntime/sse.go +267 -0
- package/kits/go/src/sdkruntime/testing.go +343 -0
- package/kits/go/src/sdkruntime/transport.go +168 -0
- package/kits/go/src/sdkruntime/wait.go +49 -0
- package/kits/go/src/sdkruntime/webhooks.go +129 -0
- package/kits/go/tests/client_test.go +185 -0
- package/kits/go/tests/http_test.go +78 -0
- package/kits/go/tests/json_test.go +127 -0
- package/kits/go/tests/lang_test.go +98 -0
- package/kits/go/tests/multipart_test.go +29 -0
- package/kits/go/tests/pagination_test.go +69 -0
- package/kits/go/tests/sse_test.go +129 -0
- package/kits/go/tests/testing_test.go +121 -0
- package/kits/go/tests/transport_test.go +133 -0
- package/kits/go/tests/wait_test.go +46 -0
- package/kits/go/tests/webhooks_test.go +62 -0
- package/kits/go/versions.json +12 -0
- package/kits/java/kit.json +6 -0
- package/kits/java/src/dev/sdkloom/runtime/ApiException.java +63 -0
- package/kits/java/src/dev/sdkloom/runtime/ArgumentException.java +24 -0
- package/kits/java/src/dev/sdkloom/runtime/Assert.java +72 -0
- package/kits/java/src/dev/sdkloom/runtime/ClientRuntime.java +220 -0
- package/kits/java/src/dev/sdkloom/runtime/Clock.java +39 -0
- package/kits/java/src/dev/sdkloom/runtime/Context.java +79 -0
- package/kits/java/src/dev/sdkloom/runtime/Decode.java +200 -0
- package/kits/java/src/dev/sdkloom/runtime/DecodeException.java +26 -0
- package/kits/java/src/dev/sdkloom/runtime/Decoder.java +16 -0
- package/kits/java/src/dev/sdkloom/runtime/Emitter.java +56 -0
- package/kits/java/src/dev/sdkloom/runtime/ErrorFactory.java +18 -0
- package/kits/java/src/dev/sdkloom/runtime/EventListener.java +20 -0
- package/kits/java/src/dev/sdkloom/runtime/EventStream.java +232 -0
- package/kits/java/src/dev/sdkloom/runtime/FakeClock.java +37 -0
- package/kits/java/src/dev/sdkloom/runtime/Headers.java +42 -0
- package/kits/java/src/dev/sdkloom/runtime/HttpRequest.java +88 -0
- package/kits/java/src/dev/sdkloom/runtime/HttpResponse.java +69 -0
- package/kits/java/src/dev/sdkloom/runtime/JdkTransport.java +97 -0
- package/kits/java/src/dev/sdkloom/runtime/Js.java +230 -0
- package/kits/java/src/dev/sdkloom/runtime/Json.java +281 -0
- package/kits/java/src/dev/sdkloom/runtime/JsonValue.java +15 -0
- package/kits/java/src/dev/sdkloom/runtime/Links.java +62 -0
- package/kits/java/src/dev/sdkloom/runtime/Middleware.java +16 -0
- package/kits/java/src/dev/sdkloom/runtime/MiddlewareEntry.java +43 -0
- package/kits/java/src/dev/sdkloom/runtime/MockRoute.java +111 -0
- package/kits/java/src/dev/sdkloom/runtime/MockTransport.java +62 -0
- package/kits/java/src/dev/sdkloom/runtime/MultipartForm.java +111 -0
- package/kits/java/src/dev/sdkloom/runtime/Next.java +16 -0
- package/kits/java/src/dev/sdkloom/runtime/OperationInfo.java +35 -0
- package/kits/java/src/dev/sdkloom/runtime/OperationMock.java +105 -0
- package/kits/java/src/dev/sdkloom/runtime/OperationSpec.java +125 -0
- package/kits/java/src/dev/sdkloom/runtime/Pages.java +55 -0
- package/kits/java/src/dev/sdkloom/runtime/Pipeline.java +29 -0
- package/kits/java/src/dev/sdkloom/runtime/RawResult.java +31 -0
- package/kits/java/src/dev/sdkloom/runtime/RecordedEvent.java +36 -0
- package/kits/java/src/dev/sdkloom/runtime/SdkInfo.java +38 -0
- package/kits/java/src/dev/sdkloom/runtime/SendOptions.java +34 -0
- package/kits/java/src/dev/sdkloom/runtime/ServerSentEvent.java +58 -0
- package/kits/java/src/dev/sdkloom/runtime/State.java +58 -0
- package/kits/java/src/dev/sdkloom/runtime/Test.java +21 -0
- package/kits/java/src/dev/sdkloom/runtime/TestRunner.java +63 -0
- package/kits/java/src/dev/sdkloom/runtime/Transport.java +16 -0
- package/kits/java/src/dev/sdkloom/runtime/TransportException.java +30 -0
- package/kits/java/src/dev/sdkloom/runtime/UrlConnectionTransport.java +195 -0
- package/kits/java/src/dev/sdkloom/runtime/Urls.java +116 -0
- package/kits/java/src/dev/sdkloom/runtime/Waits.java +53 -0
- package/kits/java/src/dev/sdkloom/runtime/WebhookSignatureException.java +24 -0
- package/kits/java/src/dev/sdkloom/runtime/Webhooks.java +146 -0
- package/kits/java/tests/dev/sdkloom/runtime/ClientTest.java +266 -0
- package/kits/java/tests/dev/sdkloom/runtime/EventStreamTest.java +131 -0
- package/kits/java/tests/dev/sdkloom/runtime/HttpTest.java +95 -0
- package/kits/java/tests/dev/sdkloom/runtime/JdkTransportTest.java +12 -0
- package/kits/java/tests/dev/sdkloom/runtime/JsTest.java +102 -0
- package/kits/java/tests/dev/sdkloom/runtime/JsonTest.java +87 -0
- package/kits/java/tests/dev/sdkloom/runtime/LinksTest.java +22 -0
- package/kits/java/tests/dev/sdkloom/runtime/MultipartFormTest.java +30 -0
- package/kits/java/tests/dev/sdkloom/runtime/PagesTest.java +75 -0
- package/kits/java/tests/dev/sdkloom/runtime/TestData.java +53 -0
- package/kits/java/tests/dev/sdkloom/runtime/TestingTest.java +156 -0
- package/kits/java/tests/dev/sdkloom/runtime/TransportContract.java +201 -0
- package/kits/java/tests/dev/sdkloom/runtime/UrlConnectionTransportTest.java +49 -0
- package/kits/java/tests/dev/sdkloom/runtime/WaitsTest.java +43 -0
- package/kits/java/tests/dev/sdkloom/runtime/WebhooksTest.java +45 -0
- package/kits/java/versions.json +41 -0
- package/kits/python/kit.json +6 -0
- package/kits/python/src/sdkloom_runtime/__init__.py +1 -0
- package/kits/python/src/sdkloom_runtime/client.py +232 -0
- package/kits/python/src/sdkloom_runtime/compat.py +195 -0
- package/kits/python/src/sdkloom_runtime/context.py +134 -0
- package/kits/python/src/sdkloom_runtime/errors.py +65 -0
- package/kits/python/src/sdkloom_runtime/http.py +192 -0
- package/kits/python/src/sdkloom_runtime/lang.py +207 -0
- package/kits/python/src/sdkloom_runtime/models.py +221 -0
- package/kits/python/src/sdkloom_runtime/multipart.py +69 -0
- package/kits/python/src/sdkloom_runtime/pagination.py +61 -0
- package/kits/python/src/sdkloom_runtime/pipeline.py +39 -0
- package/kits/python/src/sdkloom_runtime/sse.py +121 -0
- package/kits/python/src/sdkloom_runtime/testing.py +224 -0
- package/kits/python/src/sdkloom_runtime/transport.py +120 -0
- package/kits/python/src/sdkloom_runtime/wait.py +44 -0
- package/kits/python/src/sdkloom_runtime/webhooks.py +84 -0
- package/kits/python/tests/support.py +28 -0
- package/kits/python/tests/test_client.py +275 -0
- package/kits/python/tests/test_compat.py +87 -0
- package/kits/python/tests/test_http.py +94 -0
- package/kits/python/tests/test_lang.py +104 -0
- package/kits/python/tests/test_models.py +110 -0
- package/kits/python/tests/test_multipart.py +37 -0
- package/kits/python/tests/test_pagination.py +70 -0
- package/kits/python/tests/test_sse.py +61 -0
- package/kits/python/tests/test_testing.py +130 -0
- package/kits/python/tests/test_transport.py +124 -0
- package/kits/python/tests/test_wait.py +46 -0
- package/kits/python/tests/test_webhooks.py +57 -0
- package/kits/python/versions.json +33 -0
- package/kits/rust/kit.json +6 -0
- package/kits/rust/src/runtime/client.rs +212 -0
- package/kits/rust/src/runtime/context.rs +182 -0
- package/kits/rust/src/runtime/crypto.rs +84 -0
- package/kits/rust/src/runtime/errors.rs +147 -0
- package/kits/rust/src/runtime/http.rs +248 -0
- package/kits/rust/src/runtime/json.rs +424 -0
- package/kits/rust/src/runtime/lang.rs +296 -0
- package/kits/rust/src/runtime/mod.rs +30 -0
- package/kits/rust/src/runtime/models.rs +266 -0
- package/kits/rust/src/runtime/multipart.rs +81 -0
- package/kits/rust/src/runtime/pagination.rs +117 -0
- package/kits/rust/src/runtime/sse.rs +195 -0
- package/kits/rust/src/runtime/testing.rs +308 -0
- package/kits/rust/src/runtime/transport.rs +324 -0
- package/kits/rust/src/runtime/wait.rs +29 -0
- package/kits/rust/src/runtime/webhooks.rs +142 -0
- package/kits/rust/tests/client.rs +152 -0
- package/kits/rust/tests/http.rs +44 -0
- package/kits/rust/tests/json.rs +82 -0
- package/kits/rust/tests/lang.rs +74 -0
- package/kits/rust/tests/multipart.rs +21 -0
- package/kits/rust/tests/pagination.rs +61 -0
- package/kits/rust/tests/sse.rs +77 -0
- package/kits/rust/tests/testing.rs +90 -0
- package/kits/rust/tests/transport.rs +218 -0
- package/kits/rust/tests/wait.rs +32 -0
- package/kits/rust/tests/webhooks.rs +73 -0
- package/kits/rust/versions.json +6 -0
- package/kits/typescript/kit.json +6 -0
- package/kits/typescript/src/client.ts +223 -0
- package/kits/typescript/src/context.ts +108 -0
- package/kits/typescript/src/errors.ts +63 -0
- package/kits/typescript/src/helpers.ts +49 -0
- package/kits/typescript/src/http.ts +192 -0
- package/kits/typescript/src/models.ts +139 -0
- package/kits/typescript/src/multipart.ts +66 -0
- package/kits/typescript/src/pagination.ts +54 -0
- package/kits/typescript/src/pipeline.ts +25 -0
- package/kits/typescript/src/sse.ts +102 -0
- package/kits/typescript/src/testing.ts +217 -0
- package/kits/typescript/src/transport.ts +101 -0
- package/kits/typescript/src/wait.ts +36 -0
- package/kits/typescript/src/webhooks.ts +168 -0
- package/kits/typescript/tests/client.test.ts +269 -0
- package/kits/typescript/tests/helpers.test.ts +27 -0
- package/kits/typescript/tests/http.test.ts +80 -0
- package/kits/typescript/tests/models.test.ts +71 -0
- package/kits/typescript/tests/multipart.test.ts +28 -0
- package/kits/typescript/tests/pagination.test.ts +76 -0
- package/kits/typescript/tests/sse.test.ts +82 -0
- package/kits/typescript/tests/testing.test.ts +103 -0
- package/kits/typescript/tests/transport.test.ts +127 -0
- package/kits/typescript/tests/wait.test.ts +31 -0
- package/kits/typescript/tests/webhooks.test.ts +53 -0
- package/kits/typescript/versions.json +15 -0
- package/package.json +37 -4
- package/packages/cli/recipes/apiKeyHeader.ts +16 -0
- package/packages/cli/recipes/apiVersionHeader.ts +16 -0
- package/packages/cli/recipes/basic.ts +13 -0
- package/packages/cli/recipes/bearer.ts +10 -0
- package/packages/cli/recipes/hmacSign.ts +23 -0
- package/packages/cli/recipes/idempotencyKey.ts +19 -0
- package/packages/cli/recipes/oauthClientCredentials.ts +50 -0
- package/packages/cli/recipes/retry.ts +42 -0
- package/packages/cli/recipes/timing.ts +12 -0
- package/packages/cli/recipes/userAgent.ts +8 -0
- package/packages/cli/src/build.js +93 -0
- package/packages/cli/src/commands/build.js +31 -0
- package/packages/cli/src/commands/conformance-check.js +50 -0
- package/packages/cli/src/commands/contract.js +181 -0
- package/packages/cli/src/commands/diff.js +113 -0
- package/packages/cli/src/commands/doctor.js +43 -0
- package/packages/cli/src/commands/import-openapi.js +42 -0
- package/packages/cli/src/commands/init.js +27 -0
- package/packages/cli/src/commands/kit-check.js +145 -0
- package/packages/cli/src/commands/publish.js +81 -0
- package/packages/cli/src/commands/recipe.js +49 -0
- package/packages/cli/src/commands/record.js +181 -0
- package/packages/cli/src/commands/serve.js +156 -0
- package/packages/cli/src/commands/spec-check.js +57 -0
- package/packages/cli/src/commands/test.js +177 -0
- package/packages/cli/src/commands/variants.js +32 -0
- package/packages/cli/src/commands/verify.js +57 -0
- package/packages/cli/src/commands/version.js +63 -0
- package/packages/cli/src/containers.js +82 -0
- package/packages/cli/src/coverage.js +72 -0
- package/packages/cli/src/git.js +25 -0
- package/packages/cli/src/io.js +50 -0
- package/packages/cli/src/java/ScenarioHarness.java +297 -0
- package/packages/cli/src/java/SchemaRoundTrip.java +63 -0
- package/packages/cli/src/java.js +70 -0
- package/packages/cli/src/js/harness.mjs +120 -0
- package/packages/cli/src/kits.js +103 -0
- package/packages/cli/src/languages/csharp.js +274 -0
- package/packages/cli/src/languages/go.js +185 -0
- package/packages/cli/src/languages/index.js +18 -0
- package/packages/cli/src/languages/java.js +187 -0
- package/packages/cli/src/languages/python.js +120 -0
- package/packages/cli/src/languages/rust.js +189 -0
- package/packages/cli/src/languages/types.js +93 -0
- package/packages/cli/src/languages/typescript.js +112 -0
- package/packages/cli/src/main.js +13 -0
- package/packages/cli/src/packaging.js +39 -0
- package/packages/cli/src/portability.js +45 -0
- package/packages/cli/src/profiles.js +55 -0
- package/packages/cli/src/project.js +56 -0
- package/packages/cli/src/python/scenario_harness.py +162 -0
- package/packages/cli/src/python/schema_roundtrip.py +42 -0
- package/packages/cli/src/registries.js +214 -0
- package/packages/cli/src/resolve-core.js +39 -0
- package/packages/cli/src/run.js +133 -0
- package/packages/cli/src/runtimes.js +61 -0
- package/packages/cli/src/scenarios-java.js +82 -0
- package/packages/cli/src/scenarios-javascript.js +54 -0
- package/packages/cli/src/scenarios-python.js +90 -0
- package/packages/cli/src/scenarios.js +183 -0
- package/packages/cli/src/schema.js +84 -0
- package/packages/cli/src/templates.js +85 -0
- package/packages/cli/src/toolchains.js +68 -0
- package/packages/cli/src/transpiled-tests.js +92 -0
- package/packages/codegen/src/canonical-json.js +31 -0
- package/packages/codegen/src/capabilities.js +50 -0
- package/packages/codegen/src/code-writer.js +54 -0
- package/packages/codegen/src/file-tree.js +25 -0
- package/packages/codegen/src/hash.js +6 -0
- package/packages/codegen/src/index.js +16 -0
- package/packages/codegen/src/lock.js +106 -0
- package/packages/conformance/src/case.js +143 -0
- package/packages/conformance/src/checks.js +40 -0
- package/packages/conformance/src/index.js +3 -0
- package/packages/conformance/src/matrix.js +48 -0
- package/packages/conformance/src/suite.js +48 -0
- package/packages/differ/src/diff.js +500 -0
- package/packages/differ/src/index.js +3 -0
- package/packages/differ/src/notes.js +112 -0
- package/packages/differ/src/support.js +34 -0
- package/packages/gen-csharp/src/emit-client.js +397 -0
- package/packages/gen-csharp/src/emit-harness.js +333 -0
- package/packages/gen-csharp/src/emit-mock.js +111 -0
- package/packages/gen-csharp/src/emit-models.js +119 -0
- package/packages/gen-csharp/src/emit-webhooks.js +57 -0
- package/packages/gen-csharp/src/generate.js +245 -0
- package/packages/gen-csharp/src/index.js +5 -0
- package/packages/gen-csharp/src/names.js +284 -0
- package/packages/gen-go/src/emit-client.js +405 -0
- package/packages/gen-go/src/emit-harness.js +294 -0
- package/packages/gen-go/src/emit-mock.js +112 -0
- package/packages/gen-go/src/emit-models.js +146 -0
- package/packages/gen-go/src/emit-webhooks.js +58 -0
- package/packages/gen-go/src/generate.js +205 -0
- package/packages/gen-go/src/index.js +5 -0
- package/packages/gen-go/src/names.js +197 -0
- package/packages/gen-java/src/emit-client.js +441 -0
- package/packages/gen-java/src/emit-models.js +342 -0
- package/packages/gen-java/src/emit-testing.js +123 -0
- package/packages/gen-java/src/emit-webhooks.js +70 -0
- package/packages/gen-java/src/generate.js +244 -0
- package/packages/gen-java/src/index.js +4 -0
- package/packages/gen-java/src/names.js +197 -0
- package/packages/gen-python/src/emit-client.js +272 -0
- package/packages/gen-python/src/emit-models.js +139 -0
- package/packages/gen-python/src/emit-testing.js +93 -0
- package/packages/gen-python/src/emit-webhooks.js +48 -0
- package/packages/gen-python/src/generate.js +237 -0
- package/packages/gen-python/src/index.js +2 -0
- package/packages/gen-python/src/names.js +161 -0
- package/packages/gen-python/src/style.js +67 -0
- package/packages/gen-rust/src/emit-client.js +379 -0
- package/packages/gen-rust/src/emit-harness.js +278 -0
- package/packages/gen-rust/src/emit-mock.js +121 -0
- package/packages/gen-rust/src/emit-models.js +117 -0
- package/packages/gen-rust/src/emit-webhooks.js +58 -0
- package/packages/gen-rust/src/generate.js +243 -0
- package/packages/gen-rust/src/index.js +5 -0
- package/packages/gen-rust/src/names.js +223 -0
- package/packages/gen-typescript/src/emit-client.js +434 -0
- package/packages/gen-typescript/src/emit-models.js +93 -0
- package/packages/gen-typescript/src/emit-package.js +90 -0
- package/packages/gen-typescript/src/emit-testing.js +138 -0
- package/packages/gen-typescript/src/emit-webhooks.js +52 -0
- package/packages/gen-typescript/src/generate.js +70 -0
- package/packages/gen-typescript/src/index.js +3 -0
- package/packages/gen-typescript/src/naming.js +64 -0
- package/packages/gen-typescript/src/runtime.js +81 -0
- package/packages/gen-typescript/src/types.js +136 -0
- package/packages/ir/src/api-surface.js +22 -0
- package/packages/ir/src/checks.js +52 -0
- package/packages/ir/src/index.js +32 -0
- package/packages/ir/src/ir.js +214 -0
- package/packages/ir/src/pages.js +18 -0
- package/packages/ir/src/validate-ir.js +462 -0
- package/packages/ir/src/validate-types.js +140 -0
- package/packages/kit/src/block-parser.js +196 -0
- package/packages/kit/src/index.js +15 -0
- package/packages/kit/src/kit-check.js +203 -0
- package/packages/kit/src/sources.js +53 -0
- package/packages/kit/src/version-policy.js +133 -0
- package/packages/kit/src/version-range.js +132 -0
- package/packages/openapi/src/import.js +292 -0
- package/packages/openapi/src/index.js +1 -0
- package/packages/openapi/src/names.js +50 -0
- package/packages/openapi/src/schemas.js +264 -0
- package/packages/packager/src/archive.js +106 -0
- package/packages/packager/src/crate.js +39 -0
- package/packages/packager/src/gomodule.js +11 -0
- package/packages/packager/src/index.js +9 -0
- package/packages/packager/src/maven.js +85 -0
- package/packages/packager/src/metadata.js +34 -0
- package/packages/packager/src/npm.js +71 -0
- package/packages/packager/src/nuget.js +78 -0
- package/packages/packager/src/sbom.js +56 -0
- package/packages/packager/src/wheel.js +62 -0
- package/packages/spec/src/index.js +11 -0
- package/packages/spec/src/requirements.js +139 -0
- package/packages/transpile/src/ast.js +51 -0
- package/packages/transpile/src/check.js +669 -0
- package/packages/transpile/src/emit-csharp.js +469 -0
- package/packages/transpile/src/emit-go.js +529 -0
- package/packages/transpile/src/emit-java.js +360 -0
- package/packages/transpile/src/emit-python.js +320 -0
- package/packages/transpile/src/emit-rust.js +499 -0
- package/packages/transpile/src/errors.js +15 -0
- package/packages/transpile/src/index.js +13 -0
- package/packages/transpile/src/lexer.js +161 -0
- package/packages/transpile/src/parser.js +543 -0
- package/packages/transpile/src/tests/ast.js +46 -0
- package/packages/transpile/src/tests/parse.js +376 -0
- package/packages/transpile/src/tests/translate.js +849 -0
- package/packages/transpile/src/transpile.js +57 -0
|
@@ -0,0 +1,849 @@
|
|
|
1
|
+
// Translates parsed test cases to Python unittest and Java (the kit's @Test runner), typed by the IR.
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
import { TranspileError } from "../errors.ts";
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
// The generated SDKs add "_" to names that are keywords (`from` → `from_`, `class` → `class_`); tests use the same
|
|
39
|
+
// names. These sets match packages/gen-python/src/names.ts and packages/gen-java/src/names.ts.
|
|
40
|
+
const PYTHON_KEYWORDS = new Set([
|
|
41
|
+
"False", "None", "True", "and", "as", "assert", "async", "await", "break", "class", "continue", "def", "del", "elif", "else", "except",
|
|
42
|
+
"finally", "for", "from", "global", "if", "import", "in", "is", "lambda", "nonlocal", "not", "or", "pass", "raise", "return", "try",
|
|
43
|
+
"while", "with", "yield",
|
|
44
|
+
]);
|
|
45
|
+
const JAVA_KEYWORDS = new Set([
|
|
46
|
+
"abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "default", "do", "double", "else", "enum",
|
|
47
|
+
"extends", "final", "finally", "float", "for", "goto", "if", "implements", "import", "instanceof", "int", "interface", "long", "native", "new",
|
|
48
|
+
"package", "private", "protected", "public", "return", "short", "static", "strictfp", "super", "switch", "synchronized", "this", "throw",
|
|
49
|
+
"throws", "transient", "try", "void", "volatile", "while", "true", "false", "null", "var", "record", "yield",
|
|
50
|
+
]);
|
|
51
|
+
|
|
52
|
+
const snake = (name ) => {
|
|
53
|
+
const base = name.replace(/([a-z0-9])([A-Z])/g, "$1_$2").replace(/[^A-Za-z0-9]+/g, "_").replace(/^_+|_+$/g, "").toLowerCase();
|
|
54
|
+
return PYTHON_KEYWORDS.has(base) ? `${base}_` : base;
|
|
55
|
+
};
|
|
56
|
+
const camel = (name ) => {
|
|
57
|
+
const parts = name.split(/[^A-Za-z0-9]+/).filter((part) => part !== "");
|
|
58
|
+
const joined = parts.map((part, index) => (index === 0 ? part.charAt(0).toLowerCase() + part.slice(1) : part.charAt(0).toUpperCase() + part.slice(1))).join("");
|
|
59
|
+
return JAVA_KEYWORDS.has(joined) ? `${joined}_` : joined;
|
|
60
|
+
};
|
|
61
|
+
const pascal = (name ) => {
|
|
62
|
+
const value = camel(name).replace(/_$/, "");
|
|
63
|
+
return value.charAt(0).toUpperCase() + value.slice(1);
|
|
64
|
+
};
|
|
65
|
+
const tsErrorClass = (name ) => (name.endsWith("Error") ? name : `${name}Error`);
|
|
66
|
+
const javaException = (name ) => `${name.replace(/(Error|Exception)$/, "")}Exception`;
|
|
67
|
+
|
|
68
|
+
class Translator {
|
|
69
|
+
vars = new Map ();
|
|
70
|
+
ir ;
|
|
71
|
+
file ;
|
|
72
|
+
py ;
|
|
73
|
+
|
|
74
|
+
constructor(ir , file , target ) {
|
|
75
|
+
this.ir = ir;
|
|
76
|
+
this.file = file;
|
|
77
|
+
this.py = target.language === "python";
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
fail(line , message , rule = "SL-S003") {
|
|
81
|
+
throw new TranspileError(this.file.file, line, message, rule);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
named(name ) {
|
|
85
|
+
return this.ir.types.find((candidate) => candidate.name === name);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// ---- literals and coercion
|
|
89
|
+
|
|
90
|
+
/** Keyed entries of an object literal; spread is only allowed where plain JSON is built. */
|
|
91
|
+
keyed(expr ) {
|
|
92
|
+
return expr.entries.map((entry ) => ("spread" in entry ? this.fail(expr.line, "spread is only supported in JSON values (returns, deepEqual)") : entry));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
isJson(expr ) {
|
|
96
|
+
if (expr.kind === "name") {
|
|
97
|
+
const constant = this.file.constants.get(expr.name);
|
|
98
|
+
return constant !== undefined && !this.vars.has(expr.name) && this.isJson(constant);
|
|
99
|
+
}
|
|
100
|
+
if (expr.kind === "unary") return expr.op === "-" && expr.value.kind === "number";
|
|
101
|
+
if (expr.kind === "object") return expr.entries.every((entry) => !("spread" in entry) && this.isJson(entry.value));
|
|
102
|
+
if (expr.kind === "array") return expr.items.every((item) => this.isJson(item));
|
|
103
|
+
return expr.kind === "number" || expr.kind === "string" || expr.kind === "boolean" || expr.kind === "null";
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
jsonValue(expr ) {
|
|
107
|
+
switch (expr.kind) {
|
|
108
|
+
case "name":
|
|
109
|
+
return this.jsonValue(this.file.constants.get(expr.name) ?? expr);
|
|
110
|
+
case "unary":
|
|
111
|
+
return -Number(this.jsonValue(expr.value));
|
|
112
|
+
case "number":
|
|
113
|
+
return expr.value;
|
|
114
|
+
case "string":
|
|
115
|
+
return expr.value;
|
|
116
|
+
case "boolean":
|
|
117
|
+
return expr.value;
|
|
118
|
+
case "null":
|
|
119
|
+
return null;
|
|
120
|
+
case "object":
|
|
121
|
+
return Object.fromEntries(this.keyed(expr).map((entry) => [entry.key, this.jsonValue(entry.value)]));
|
|
122
|
+
case "array":
|
|
123
|
+
return expr.items.map((item) => this.jsonValue(item));
|
|
124
|
+
default:
|
|
125
|
+
return this.fail(expr.line, "not a JSON literal");
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
pyJson(value ) {
|
|
130
|
+
if (value === null) return "None";
|
|
131
|
+
if (typeof value === "boolean") return value ? "True" : "False";
|
|
132
|
+
if (typeof value === "number") return Number.isInteger(value) ? String(value) : String(value);
|
|
133
|
+
if (typeof value === "string") return JSON.stringify(value);
|
|
134
|
+
if (Array.isArray(value)) return `[${value.map((item) => this.pyJson(item)).join(", ")}]`;
|
|
135
|
+
return `{${Object.entries(value ).map(([key, item]) => `${JSON.stringify(key)}: ${this.pyJson(item)}`).join(", ")}}`;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** A JSON value as plain data in the target: Python literals, or Json.parse("…") in Java. */
|
|
139
|
+
rawJson(source ) {
|
|
140
|
+
const expr = this.literal(source);
|
|
141
|
+
if (expr.kind === "object" && expr.entries.some((entry) => "spread" in entry)) {
|
|
142
|
+
if (this.py) {
|
|
143
|
+
return `{${expr.entries.map((entry) => ("spread" in entry ? `**${this.rawJson(entry.spread)}` : `${JSON.stringify(entry.key)}: ${this.rawJson(entry.value)}`)).join(", ")}}`;
|
|
144
|
+
}
|
|
145
|
+
return expr.entries.reduce(
|
|
146
|
+
(acc, entry) => ("spread" in entry ? `spreadAll(${acc}, ${this.rawJson(entry.spread)})` : `spread(${acc}, ${JSON.stringify(entry.key)}, ${this.rawJson(entry.value)})`),
|
|
147
|
+
"Map.of()",
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
if (this.isJson(expr)) {
|
|
151
|
+
const value = this.jsonValue(expr);
|
|
152
|
+
return this.py ? this.pyJson(value) : `Json.parse(${JSON.stringify(JSON.stringify(value))})`;
|
|
153
|
+
}
|
|
154
|
+
if (expr.kind === "object") {
|
|
155
|
+
const entries = this.keyed(expr);
|
|
156
|
+
if (this.py) return `{${entries.map((entry) => `${JSON.stringify(entry.key)}: ${this.rawJson(entry.value)}`).join(", ")}}`;
|
|
157
|
+
return entries.reduce((acc, entry) => `spread(${acc}, ${JSON.stringify(entry.key)}, ${this.rawJson(entry.value)})`, "Map.of()");
|
|
158
|
+
}
|
|
159
|
+
if (expr.kind === "array") {
|
|
160
|
+
const items = expr.items.map((item) => this.rawJson(item));
|
|
161
|
+
return this.py ? `[${items.join(", ")}]` : `java.util.Arrays.asList(${items.join(", ")})`;
|
|
162
|
+
}
|
|
163
|
+
const out = this.expr(expr);
|
|
164
|
+
return this.py ? `_plain(${out.code})` : `plain(${out.code})`;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
literal(expr ) {
|
|
168
|
+
return expr.kind === "name" && !this.vars.has(expr.name) && this.file.constants.has(expr.name) ? (this.file.constants.get(expr.name) ?? expr) : expr;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** The expression as a value of an IR type: object literals become typed models, decimals Decimal/BigDecimal. */
|
|
172
|
+
coerce(source , type ) {
|
|
173
|
+
const expr = this.literal(source);
|
|
174
|
+
if (expr.kind === "null") return this.py ? "None" : "null";
|
|
175
|
+
if (type.kind === "primitive" && (type.name === "bytes" || type.name === "stream")) {
|
|
176
|
+
const out = this.expr(expr);
|
|
177
|
+
if (out.tv.k !== "bytes") return this.fail(expr.line, `${type.name} values must be bytes, e.g. new TextEncoder().encode("…")`);
|
|
178
|
+
return !this.py && type.name === "stream" ? `new java.io.ByteArrayInputStream(${out.code})` : out.code;
|
|
179
|
+
}
|
|
180
|
+
if (!this.isJson(expr) || (expr.kind !== "object" && expr.kind !== "array" && type.kind === "ref" && this.named(type.name)?.type.kind !== "object" && this.named(type.name)?.type.kind !== "union")) {
|
|
181
|
+
return this.expr(expr).code;
|
|
182
|
+
}
|
|
183
|
+
switch (type.kind) {
|
|
184
|
+
case "primitive":
|
|
185
|
+
return this.primitive(expr, type.name);
|
|
186
|
+
case "enum":
|
|
187
|
+
return JSON.stringify(String(this.jsonValue(expr)));
|
|
188
|
+
case "list": {
|
|
189
|
+
if (expr.kind !== "array") return this.fail(expr.line, "expected an array");
|
|
190
|
+
const items = expr.items.map((item) => this.coerce(item, type.item));
|
|
191
|
+
return this.py ? `[${items.join(", ")}]` : `java.util.Arrays.asList(${items.join(", ")})`;
|
|
192
|
+
}
|
|
193
|
+
case "map": {
|
|
194
|
+
if (expr.kind !== "object") return this.fail(expr.line, "expected an object");
|
|
195
|
+
const entries = this.keyed(expr).map((entry) => [JSON.stringify(entry.key), this.coerce(entry.value, type.value)] );
|
|
196
|
+
return this.py ? `{${entries.map(([key, value]) => `${key}: ${value}`).join(", ")}}` : `Map.ofEntries(${entries.map(([key, value]) => `Map.entry(${key}, ${value})`).join(", ")})`;
|
|
197
|
+
}
|
|
198
|
+
case "ref":
|
|
199
|
+
return this.model(expr, type.name);
|
|
200
|
+
default:
|
|
201
|
+
return this.fail(expr.line, "inline object types are not supported");
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
primitive(expr , name ) {
|
|
206
|
+
const value = this.jsonValue(expr);
|
|
207
|
+
const numeric = name === "int" || name === "number";
|
|
208
|
+
const kindOk = value === null || (numeric ? typeof value === "number" : name === "bool" ? typeof value === "boolean" : name === "decimal" ? typeof value === "string" || typeof value === "number" : typeof value === "string");
|
|
209
|
+
if (!kindOk) return this.fail(expr.line, `${JSON.stringify(value)} is not a ${name}`);
|
|
210
|
+
switch (name) {
|
|
211
|
+
case "decimal":
|
|
212
|
+
return this.py ? `Decimal(${JSON.stringify(String(value))})` : `new java.math.BigDecimal(${JSON.stringify(String(value))})`;
|
|
213
|
+
case "int":
|
|
214
|
+
return this.py ? String(value) : `${String(value)}L`;
|
|
215
|
+
case "number":
|
|
216
|
+
return this.py || !Number.isInteger(value) ? String(value) : `${String(value)}.0`;
|
|
217
|
+
case "bool":
|
|
218
|
+
return this.py ? (value === true ? "True" : "False") : String(value);
|
|
219
|
+
case "bytes":
|
|
220
|
+
case "stream":
|
|
221
|
+
return this.fail(expr.line, "bytes and streams are not supported in transpiled tests");
|
|
222
|
+
default:
|
|
223
|
+
return JSON.stringify(String(value));
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
model(expr , name ) {
|
|
228
|
+
const named = this.named(name);
|
|
229
|
+
if (named === undefined || expr.kind !== "object") return this.fail(expr.line, `expected a ${name}`);
|
|
230
|
+
const type = named.type;
|
|
231
|
+
const entries = this.keyed(expr);
|
|
232
|
+
if (type.kind === "union") {
|
|
233
|
+
const tag = entries.find((entry) => entry.key === type.discriminator)?.value;
|
|
234
|
+
const variant = type.variants.find((candidate) => tag?.kind === "string" && candidate.tag === tag.value);
|
|
235
|
+
if (variant === undefined) return this.fail(expr.line, `${name} needs a known "${type.discriminator}"`);
|
|
236
|
+
return this.model({ ...expr, entries: entries.filter((entry) => entry.key !== type.discriminator) }, variant.type);
|
|
237
|
+
}
|
|
238
|
+
if (type.kind !== "object") return this.fail(expr.line, `${name} is not a model`);
|
|
239
|
+
const parts = entries.map((entry) => {
|
|
240
|
+
const field = type.fields.find((candidate) => candidate.name === entry.key);
|
|
241
|
+
if (field === undefined) return this.fail(entry.value.line, `${entry.key} is not a field of ${name}`);
|
|
242
|
+
const value = this.coerce(entry.value, field.type);
|
|
243
|
+
return this.py ? `${snake(field.name)}=${value}` : `.${camel(field.name)}(${value})`;
|
|
244
|
+
});
|
|
245
|
+
return this.py ? `${name}(${parts.join(", ")})` : `${name}.builder()${parts.join("")}.build()`;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// ---- expressions
|
|
249
|
+
|
|
250
|
+
expr(expr ) {
|
|
251
|
+
switch (expr.kind) {
|
|
252
|
+
case "number":
|
|
253
|
+
return { code: this.py || !Number.isInteger(expr.value) ? expr.text : `${expr.text}L`, tv: { k: "number", java: Number.isInteger(expr.value) ? "long" : "double" } };
|
|
254
|
+
case "string":
|
|
255
|
+
return { code: JSON.stringify(expr.value), tv: { k: "string" } };
|
|
256
|
+
case "boolean":
|
|
257
|
+
return { code: this.py ? (expr.value ? "True" : "False") : String(expr.value), tv: { k: "boolean" } };
|
|
258
|
+
case "null":
|
|
259
|
+
return { code: this.py ? "None" : "null", tv: { k: "null" } };
|
|
260
|
+
case "object":
|
|
261
|
+
case "array":
|
|
262
|
+
return { code: this.rawJson(expr), tv: { k: "literal" } };
|
|
263
|
+
case "unary":
|
|
264
|
+
if (expr.op === "-" && expr.value.kind === "number") return this.expr({ ...expr.value, value: -expr.value.value, text: `-${expr.value.text}` });
|
|
265
|
+
return this.fail(expr.line, "`!` is not supported in tests");
|
|
266
|
+
case "await":
|
|
267
|
+
return this.expr(expr.value);
|
|
268
|
+
case "name":
|
|
269
|
+
return this.name(expr);
|
|
270
|
+
case "member":
|
|
271
|
+
return this.member(expr);
|
|
272
|
+
case "index":
|
|
273
|
+
return this.index(expr);
|
|
274
|
+
case "call":
|
|
275
|
+
return this.call(expr);
|
|
276
|
+
case "binary":
|
|
277
|
+
return this.fail(expr.line, "comparisons are only supported inside assert.ok(…)");
|
|
278
|
+
case "arrow":
|
|
279
|
+
case "block":
|
|
280
|
+
return this.fail(expr.line, "lambdas are only supported in assert.throws, assert.rejects and list.map", "SL-S002");
|
|
281
|
+
case "new":
|
|
282
|
+
return this.construct(expr);
|
|
283
|
+
case "coalesce": {
|
|
284
|
+
const left = this.expr(expr.left);
|
|
285
|
+
const right = this.expr(expr.right);
|
|
286
|
+
return { code: this.py ? `_coalesce(${left.code}, ${right.code})` : `coalesce(${left.code}, ${right.code})`, tv: left.tv };
|
|
287
|
+
}
|
|
288
|
+
case "instanceof":
|
|
289
|
+
return this.fail(expr.line, "instanceof is only supported inside assert.ok(…)", "SL-S004");
|
|
290
|
+
case "and":
|
|
291
|
+
return this.fail(expr.line, "&& is only supported in an assert.rejects check", "SL-S004");
|
|
292
|
+
case "regex":
|
|
293
|
+
return this.fail(expr.line, "regular expressions are only supported in assert.match(…)", "SL-S004");
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
construct(expr ) {
|
|
298
|
+
if (expr.name === "TextEncoder") return { code: "", tv: { k: "textEncoder" } };
|
|
299
|
+
if (expr.name === "TextDecoder") return { code: "", tv: { k: "textDecoder" } };
|
|
300
|
+
if (expr.name === "Uint8Array") {
|
|
301
|
+
const items = expr.args[0];
|
|
302
|
+
if (items?.kind !== "array" || !items.items.every((item) => item.kind === "number")) return this.fail(expr.line, "new Uint8Array needs an array of numbers");
|
|
303
|
+
const numbers = items.items.map((item) => String(this.jsonValue(item)));
|
|
304
|
+
// bytearray: bytes on Python 2.7 too, where bytes is str.
|
|
305
|
+
return { code: this.py ? `bytearray([${numbers.join(", ")}])` : `new byte[] {${numbers.map((value) => `(byte) ${value}`).join(", ")}}`, tv: { k: "bytes" } };
|
|
306
|
+
}
|
|
307
|
+
return this.fail(expr.line, `new ${expr.name} is not supported in tests; TextEncoder, TextDecoder and Uint8Array are`);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/** list.map((x) => …) and list.at(i) on calls, events and list values. */
|
|
311
|
+
listMethod(expr , list , name ) {
|
|
312
|
+
const element = list.tv.k === "calls" ? { k: "request" } : list.tv.k === "events" ? { k: "event" } : { k: "value", type: list.tv.k === "value" && list.tv.type?.kind === "list" ? list.tv.type.item : undefined };
|
|
313
|
+
const [arg] = expr.args;
|
|
314
|
+
if (name === "at") {
|
|
315
|
+
if (arg === undefined || (arg.kind !== "number" && !(arg.kind === "unary" && arg.value.kind === "number"))) return this.fail(expr.line, "at(i) needs a number literal");
|
|
316
|
+
const at = Number(this.jsonValue(arg));
|
|
317
|
+
return { code: this.py ? `${list.code}[${at}]` : `${list.code}.get(${at < 0 ? `${list.code}.size() - ${-at}` : at})`, tv: element };
|
|
318
|
+
}
|
|
319
|
+
if (arg?.kind !== "arrow" || arg.params.length !== 1) return this.fail(expr.line, "map needs (item) => expression");
|
|
320
|
+
const param = arg.params[0] ?? "item";
|
|
321
|
+
const local = this.py ? snake(param) : param;
|
|
322
|
+
const saved = this.vars.get(param);
|
|
323
|
+
this.vars.set(param, { code: local, tv: element });
|
|
324
|
+
const body = this.expr(arg.body);
|
|
325
|
+
if (saved === undefined) this.vars.delete(param);
|
|
326
|
+
else this.vars.set(param, saved);
|
|
327
|
+
const code = this.py ? `[${body.code} for ${local} in ${list.code}]` : `${list.code}.stream().map((${local}) -> ${body.code}).collect(java.util.stream.Collectors.toList())`;
|
|
328
|
+
return { code, tv: { k: "value", type: undefined } };
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
name(expr ) {
|
|
332
|
+
const local = this.vars.get(expr.name);
|
|
333
|
+
if (local !== undefined) return local;
|
|
334
|
+
const constant = this.file.constants.get(expr.name);
|
|
335
|
+
if (constant !== undefined) return this.expr(constant);
|
|
336
|
+
if (expr.name === "assert") return { code: "", tv: { k: "assert" } };
|
|
337
|
+
if (expr.name === "JSON") return { code: "", tv: { k: "json" } };
|
|
338
|
+
const imported = this.file.imports.get(expr.name);
|
|
339
|
+
if (imported === "examples") return { code: this.py ? "examples" : `${this.ir.name}Examples`, tv: { k: "examples" } };
|
|
340
|
+
if (imported !== undefined) {
|
|
341
|
+
const builtins = {
|
|
342
|
+
ApiError: ["ApiError", "ApiException"],
|
|
343
|
+
DecodeError: ["DecodeError", "DecodeException"],
|
|
344
|
+
TransportError: ["TransportError", "TransportException"],
|
|
345
|
+
};
|
|
346
|
+
const builtin = builtins[imported];
|
|
347
|
+
if (builtin !== undefined) return { code: "", tv: { k: "errorClass", python: builtin[0], java: builtin[1] } };
|
|
348
|
+
const error = this.ir.operations.flatMap((operation) => operation.errors).find((candidate) => tsErrorClass(candidate.name) === imported);
|
|
349
|
+
if (error !== undefined) return { code: "", tv: { k: "errorClass", python: tsErrorClass(error.name), java: javaException(error.name) } };
|
|
350
|
+
}
|
|
351
|
+
return this.fail(expr.line, `${expr.name} is not available in transpiled tests`);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
operation(name , line ) {
|
|
355
|
+
const operation = this.ir.operations.find((candidate) => candidate.name === name);
|
|
356
|
+
return operation ?? this.fail(line, `${name} is not an operation`);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
member(expr ) {
|
|
360
|
+
const object = this.expr(expr.object);
|
|
361
|
+
const { tv, code } = object;
|
|
362
|
+
const name = expr.name;
|
|
363
|
+
const method = (on ) => ({ code, tv: { k: "method", name, on } });
|
|
364
|
+
switch (tv.k) {
|
|
365
|
+
case "client":
|
|
366
|
+
if (name === "raw") return { code: this.py ? `${code}.raw` : `${code}.raw()`, tv: { k: "rawClient" } };
|
|
367
|
+
return { code: `${code}.${this.py ? snake(name) : camel(name)}`, tv: { k: "clientOp", op: this.operation(name, expr.line), raw: false } };
|
|
368
|
+
case "rawClient":
|
|
369
|
+
return { code: `${code}.${this.py ? snake(name) : camel(name)}`, tv: { k: "clientOp", op: this.operation(name, expr.line), raw: true } };
|
|
370
|
+
case "mock":
|
|
371
|
+
if (name === "clock") return { code: this.py ? `${code}.clock` : `${code}.clock()`, tv: { k: "clock" } };
|
|
372
|
+
if (name === "events") return { code: this.py ? `${code}.events` : `${code}.events()`, tv: { k: "events" } };
|
|
373
|
+
if (name === "transport") return this.fail(expr.line, "mock.transport is not supported in transpiled tests");
|
|
374
|
+
return { code: this.py ? `${code}.${snake(name)}` : `${code}.${camel(name)}()`, tv: { k: "opMock", op: this.operation(name, expr.line) } };
|
|
375
|
+
case "opMock":
|
|
376
|
+
if (name === "then") return { code: this.py ? `${code}.then` : `${code}.then()`, tv };
|
|
377
|
+
if (name === "calls") return { code: this.py ? `${code}.calls` : `${code}.calls()`, tv: { k: "calls" } };
|
|
378
|
+
if (name === "returns" || name === "fails") return method(tv);
|
|
379
|
+
return this.fail(expr.line, `mock operations have no ${name} in transpiled tests`);
|
|
380
|
+
case "calls":
|
|
381
|
+
case "events":
|
|
382
|
+
if (name === "length") return { code: this.py ? `len(${code})` : `${code}.size()`, tv: { k: "number", java: "int" } };
|
|
383
|
+
if (name === "map" || name === "at") return method(tv);
|
|
384
|
+
return this.fail(expr.line, `${name} is not supported on a list here`);
|
|
385
|
+
case "json":
|
|
386
|
+
if (name === "parse") return method(tv);
|
|
387
|
+
return this.fail(expr.line, `JSON.${name} is not supported in tests`);
|
|
388
|
+
case "textEncoder":
|
|
389
|
+
if (name === "encode") return method(tv);
|
|
390
|
+
return this.fail(expr.line, `TextEncoder has encode(), not ${name}`);
|
|
391
|
+
case "textDecoder":
|
|
392
|
+
if (name === "decode") return method(tv);
|
|
393
|
+
return this.fail(expr.line, `TextDecoder has decode(), not ${name}`);
|
|
394
|
+
case "error": {
|
|
395
|
+
const reads = {
|
|
396
|
+
status: [".status", ".status()", { k: "number", java: "int" }],
|
|
397
|
+
body: [".body", ".body()", { k: "value", type: undefined }],
|
|
398
|
+
retryable: [".retryable", ".retryable()", { k: "boolean" }],
|
|
399
|
+
name: [".name", ".name()", { k: "string" }],
|
|
400
|
+
requestId: [".request_id", ".requestId()", { k: "string" }],
|
|
401
|
+
message: ["", ".getMessage()", { k: "string" }],
|
|
402
|
+
};
|
|
403
|
+
const read = reads[name];
|
|
404
|
+
if (read === undefined) return this.fail(expr.line, `errors have status, body, retryable, name, requestId and message, not ${name}`);
|
|
405
|
+
if (name === "message" && this.py) return { code: `str(${code})`, tv: read[2] };
|
|
406
|
+
return { code: `${code}${this.py ? read[0] : read[1]}`, tv: read[2] };
|
|
407
|
+
}
|
|
408
|
+
case "request":
|
|
409
|
+
if (name === "body") return { code: this.py ? `${code}.body` : `${code}.body()`, tv: { k: "bytes" } };
|
|
410
|
+
if (name === "header") return method(tv);
|
|
411
|
+
if (name === "url" || name === "method") return { code: this.py ? `${code}.${name}` : `${code}.${name}()`, tv: { k: "string" } };
|
|
412
|
+
return this.fail(expr.line, `a recorded request has no ${name} in transpiled tests`);
|
|
413
|
+
case "clock":
|
|
414
|
+
if (name === "now") return method(tv);
|
|
415
|
+
return this.fail(expr.line, `the clock has no ${name}`);
|
|
416
|
+
case "event":
|
|
417
|
+
if (name === "name") return { code: this.py ? `${code}[0]` : `${code}.name()`, tv: { k: "string" } };
|
|
418
|
+
if (name === "fields") return { code: this.py ? `${code}[1]` : `${code}.fields()`, tv: { k: "value", type: undefined } };
|
|
419
|
+
return this.fail(expr.line, `an event has name and fields, not ${name}`);
|
|
420
|
+
case "raw":
|
|
421
|
+
if (name === "data") return { code: this.py ? `${code}.data` : `${code}.data()`, tv: { k: "value", type: tv.op.returns } };
|
|
422
|
+
if (name === "response") return { code: this.py ? `${code}.response` : `${code}.response()`, tv: { k: "response" } };
|
|
423
|
+
return this.fail(expr.line, `a raw result has data and response, not ${name}`);
|
|
424
|
+
case "response":
|
|
425
|
+
if (name === "status") return { code: this.py ? `${code}.status` : `${code}.status()`, tv: { k: "number", java: "int" } };
|
|
426
|
+
if (name === "header") return method(tv);
|
|
427
|
+
return this.fail(expr.line, `a response has no ${name} in transpiled tests`);
|
|
428
|
+
case "examples": {
|
|
429
|
+
const named = this.named(name);
|
|
430
|
+
if (named === undefined) return this.fail(expr.line, `examples.${name} does not exist`);
|
|
431
|
+
return { code: this.py ? `${code}.${name}` : `${code}.${camel(name)}`, tv: { k: "example", type: named } };
|
|
432
|
+
}
|
|
433
|
+
case "value":
|
|
434
|
+
return this.valueMember(expr, object, name);
|
|
435
|
+
default:
|
|
436
|
+
return this.fail(expr.line, `${name} is not supported here`);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
valueMember(expr , object , name ) {
|
|
441
|
+
const type = object.tv.k === "value" ? object.tv.type : undefined;
|
|
442
|
+
if (type?.kind === "list" && name === "length") return { code: this.py ? `len(${object.code})` : `${object.code}.size()`, tv: { k: "number", java: "int" } };
|
|
443
|
+
if (type?.kind === "list" && (name === "map" || name === "at")) return { code: object.code, tv: { k: "method", name, on: object.tv } };
|
|
444
|
+
const named = type?.kind === "ref" ? this.named(type.name) : undefined;
|
|
445
|
+
if (named?.type.kind !== "object") return this.fail(expr.line, `reading ${name} needs a model value`);
|
|
446
|
+
const field = named.type.fields.find((candidate) => candidate.name === name);
|
|
447
|
+
if (field === undefined) return this.fail(expr.line, `${named.name} has no field ${name}`);
|
|
448
|
+
const read = this.py ? `${object.code}.${snake(field.name)}` : `${object.code}.${camel(field.name)}()`;
|
|
449
|
+
const code = field.optional ? (this.py ? `_opt(${read})` : `${read}.orElse(null)`) : read;
|
|
450
|
+
return { code, tv: { k: "value", type: field.type } };
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
index(expr ) {
|
|
454
|
+
const object = this.expr(expr.object);
|
|
455
|
+
if (expr.index.kind !== "number" || !Number.isInteger(expr.index.value)) return this.fail(expr.line, "indexes must be integer literals");
|
|
456
|
+
const at = String(expr.index.value);
|
|
457
|
+
const element = (tv ) => ({ code: this.py ? `${object.code}[${at}]` : `${object.code}.get(${at})`, tv });
|
|
458
|
+
if (object.tv.k === "calls") return element({ k: "request" });
|
|
459
|
+
if (object.tv.k === "events") return element({ k: "event" });
|
|
460
|
+
if (object.tv.k === "value" && object.tv.type?.kind === "list") return element({ k: "value", type: object.tv.type.item });
|
|
461
|
+
return this.fail(expr.line, "indexing is supported on calls, events and lists");
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
clientCall(expr , callee ) {
|
|
465
|
+
const op = callee.tv.op;
|
|
466
|
+
const argument = expr.args[0] === undefined ? undefined : this.literal(expr.args[0]);
|
|
467
|
+
if (argument !== undefined && argument.kind !== "object") return this.fail(expr.line, "pass operation arguments as an object literal");
|
|
468
|
+
const given = new Map((argument === undefined ? [] : this.keyed(argument)).map((entry) => [entry.key, entry.value] ));
|
|
469
|
+
for (const key of given.keys()) {
|
|
470
|
+
if (key !== "body" && !op.params.some((param) => param.name === key)) return this.fail(expr.line, `${op.name} has no parameter ${key}`);
|
|
471
|
+
}
|
|
472
|
+
const positional = op.params.filter((param) => param.in === "path").map((param) => {
|
|
473
|
+
const value = given.get(param.name);
|
|
474
|
+
return value === undefined ? this.fail(expr.line, `${op.name} needs ${param.name}`) : this.coerce(value, param.type);
|
|
475
|
+
});
|
|
476
|
+
const body = given.get("body");
|
|
477
|
+
if (op.body !== undefined) {
|
|
478
|
+
if (body === undefined) return this.fail(expr.line, `${op.name} needs a body`);
|
|
479
|
+
positional.push(this.coerce(body, op.body));
|
|
480
|
+
}
|
|
481
|
+
const named = op.params.flatMap((param) => {
|
|
482
|
+
const value = param.in === "path" ? undefined : given.get(param.name);
|
|
483
|
+
return value === undefined ? [] : [{ param, value: this.coerce(value, param.type) }];
|
|
484
|
+
});
|
|
485
|
+
let args ;
|
|
486
|
+
if (this.py) {
|
|
487
|
+
args = [...positional, ...named.map(({ param, value }) => `${snake(param.name)}=${value}`)];
|
|
488
|
+
} else {
|
|
489
|
+
const needsParams = named.length > 0 || op.params.some((param) => param.in !== "path" && !param.optional);
|
|
490
|
+
const params = `${pascal(op.name)}Params.builder()${named.map(({ param, value }) => `.${camel(param.name)}(${value})`).join("")}.build()`;
|
|
491
|
+
args = needsParams ? [...positional, params] : positional;
|
|
492
|
+
}
|
|
493
|
+
const code = `${callee.code}(${args.join(", ")})`;
|
|
494
|
+
const bytes = op.returns?.kind === "primitive" && (op.returns.name === "bytes" || op.returns.name === "stream");
|
|
495
|
+
return { code, tv: callee.tv.raw ? { k: "raw", op } : op.returns === undefined ? { k: "null" } : bytes ? { k: "bytes" } : { k: "value", type: op.returns } };
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
call(expr ) {
|
|
499
|
+
const callee = this.expr(expr.callee);
|
|
500
|
+
const tv = callee.tv;
|
|
501
|
+
if (tv.k === "clientOp") return this.clientCall(expr, { ...callee, tv });
|
|
502
|
+
if (tv.k === "example") return this.example(expr, callee.code, tv.type);
|
|
503
|
+
if (tv.k !== "method") return this.fail(expr.line, "this call is not supported in transpiled tests");
|
|
504
|
+
const on = tv.on;
|
|
505
|
+
if (tv.name === "map" || tv.name === "at") return this.listMethod(expr, { code: callee.code, tv: on }, tv.name);
|
|
506
|
+
const [first] = expr.args;
|
|
507
|
+
if (on.k === "json" && first !== undefined) return { code: this.py ? `json.loads(${this.expr(first).code})` : `Json.parse(${this.expr(first).code})`, tv: { k: "value", type: undefined } };
|
|
508
|
+
if (on.k === "textEncoder" && first !== undefined) {
|
|
509
|
+
const text = this.expr(first).code;
|
|
510
|
+
return { code: this.py ? `bytearray((${text}).encode("utf-8"))` : `${text}.getBytes(java.nio.charset.StandardCharsets.UTF_8)`, tv: { k: "bytes" } };
|
|
511
|
+
}
|
|
512
|
+
if (on.k === "textDecoder" && first !== undefined) {
|
|
513
|
+
const bytes = this.expr(first).code;
|
|
514
|
+
return { code: this.py ? `(${bytes}).decode("utf-8")` : `new String(${bytes}, java.nio.charset.StandardCharsets.UTF_8)`, tv: { k: "string" } };
|
|
515
|
+
}
|
|
516
|
+
if (on.k === "opMock" && tv.name === "returns") {
|
|
517
|
+
const value = expr.args[0];
|
|
518
|
+
const op = on.op;
|
|
519
|
+
const literal = value === undefined ? undefined : this.literal(value);
|
|
520
|
+
const spread = literal?.kind === "object" && literal.entries.some((entry) => "spread" in entry);
|
|
521
|
+
const code =
|
|
522
|
+
value === undefined || value.kind === "null"
|
|
523
|
+
? this.py ? "None" : "null"
|
|
524
|
+
: spread
|
|
525
|
+
? this.rawJson(value)
|
|
526
|
+
: op.returns !== undefined && this.isJson(value)
|
|
527
|
+
? this.tryCoerce(value, op.returns)
|
|
528
|
+
: this.expr(value).code;
|
|
529
|
+
return { code: `${callee.code}.returns(${code})`, tv: on };
|
|
530
|
+
}
|
|
531
|
+
if (on.k === "opMock" && tv.name === "fails") {
|
|
532
|
+
const [status, body] = expr.args;
|
|
533
|
+
if (status?.kind !== "number") return this.fail(expr.line, "fails(status) needs a number literal");
|
|
534
|
+
return { code: `${callee.code}.fails(${[String(status.value), ...(body === undefined ? [] : [this.rawJson(body)])].join(", ")})`, tv: on };
|
|
535
|
+
}
|
|
536
|
+
if ((on.k === "request" || on.k === "response") && tv.name === "header") {
|
|
537
|
+
const [name] = expr.args;
|
|
538
|
+
if (name === undefined) return this.fail(expr.line, "header(name) needs a name");
|
|
539
|
+
return { code: `${callee.code}.header(${this.expr(name).code})`, tv: { k: "string" } };
|
|
540
|
+
}
|
|
541
|
+
if (on.k === "clock" && tv.name === "now") return { code: `${callee.code}.now()`, tv: { k: "number", java: "long" } };
|
|
542
|
+
return this.fail(expr.line, `${tv.name}(…) is not supported in transpiled tests`);
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
/** returns() prefers a typed model; a value the types cannot express (an unknown variant) is sent as raw JSON. */
|
|
546
|
+
tryCoerce(value , type ) {
|
|
547
|
+
try {
|
|
548
|
+
return this.coerce(value, type);
|
|
549
|
+
} catch (error ) {
|
|
550
|
+
if (error instanceof TranspileError) return this.rawJson(value);
|
|
551
|
+
throw error;
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
example(expr , code , named ) {
|
|
556
|
+
const overrides = expr.args[0] === undefined ? undefined : this.literal(expr.args[0]);
|
|
557
|
+
if (named.type.kind === "union") return { code: this.py ? `${code}()` : `${code}()`, tv: { k: "value", type: { kind: "ref", name: named.name } } };
|
|
558
|
+
if (named.type.kind !== "object") return this.fail(expr.line, `examples.${named.name} is not a model`);
|
|
559
|
+
if (overrides !== undefined && overrides.kind !== "object") return this.fail(expr.line, "example overrides must be an object literal");
|
|
560
|
+
const fields = named.type.fields;
|
|
561
|
+
const parts = (overrides === undefined ? [] : this.keyed(overrides)).map((entry) => {
|
|
562
|
+
const field = fields.find((candidate) => candidate.name === entry.key);
|
|
563
|
+
if (field === undefined) return this.fail(entry.value.line, `${entry.key} is not a field of ${named.name}`);
|
|
564
|
+
const value = this.coerce(entry.value, field.type);
|
|
565
|
+
return this.py ? `${snake(field.name)}=${value}` : `.${camel(field.name)}(${value})`;
|
|
566
|
+
});
|
|
567
|
+
return { code: this.py ? `${code}(${parts.join(", ")})` : `${code}()${parts.join("")}.build()`, tv: { k: "value", type: { kind: "ref", name: named.name } } };
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
// ---- statements
|
|
571
|
+
|
|
572
|
+
/** Lines of one statement; Python indentation is relative to the test body. */
|
|
573
|
+
statement(statement ) {
|
|
574
|
+
switch (statement.kind) {
|
|
575
|
+
case "mockClient":
|
|
576
|
+
return this.mockClient(statement);
|
|
577
|
+
case "const": {
|
|
578
|
+
const out = this.expr(statement.value);
|
|
579
|
+
const name = this.py ? snake(statement.name) : statement.name;
|
|
580
|
+
this.vars.set(statement.name, { code: name, tv: out.tv.k === "literal" ? { k: "value", type: undefined } : out.tv });
|
|
581
|
+
return [this.py ? `${name} = ${out.code}` : `var ${name} = ${out.code};`];
|
|
582
|
+
}
|
|
583
|
+
case "expression":
|
|
584
|
+
return this.expressionStatement(statement.value);
|
|
585
|
+
case "return":
|
|
586
|
+
if (statement.value.kind === "boolean" && statement.value.value) return [];
|
|
587
|
+
return this.fail(statement.line, "a check ends with `return true`; put conditions in assertions", "SL-S004");
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
mockClient(statement ) {
|
|
592
|
+
const config = statement.config === undefined ? undefined : this.literal(statement.config);
|
|
593
|
+
if (config !== undefined && config.kind !== "object") return this.fail(statement.line, "mockClient takes an object literal", "SL-S002");
|
|
594
|
+
const fields = this.ir.middlewares.flatMap((middleware) => middleware.config);
|
|
595
|
+
const settings = (config === undefined ? [] : this.keyed(config)).map((entry) => {
|
|
596
|
+
const field = fields.find((candidate) => candidate.name === entry.key);
|
|
597
|
+
if (field === undefined) return this.fail(entry.value.line, `${entry.key} is not a client option`, "SL-S002");
|
|
598
|
+
return [field.name, this.coerce(entry.value, field.type)] ;
|
|
599
|
+
});
|
|
600
|
+
this.vars.set("client", { code: "client", tv: { k: "client" } });
|
|
601
|
+
this.vars.set("mock", { code: "mock", tv: { k: "mock" } });
|
|
602
|
+
if (this.py) return [`client, mock = mock_client(${settings.map(([name, value]) => `${snake(name)}=${value}`).join(", ")})`];
|
|
603
|
+
const mock = `${this.ir.name}Mock`;
|
|
604
|
+
const create = settings.length === 0 ? `${mock}.create()` : `${mock}.create((builder) -> builder${settings.map(([name, value]) => `.${camel(name)}(${value})`).join("")})`;
|
|
605
|
+
return [`var mock = ${create};`, `var client = mock.client();`];
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
expressionStatement(value ) {
|
|
609
|
+
const call = value.kind === "await" ? value.value : value;
|
|
610
|
+
if (call.kind === "call" && call.callee.kind === "member" && call.callee.object.kind === "name" && call.callee.object.name === "assert" && !this.vars.has("assert")) {
|
|
611
|
+
return this.assertion(call.callee.name, call.args, call.line);
|
|
612
|
+
}
|
|
613
|
+
const out = this.expr(value);
|
|
614
|
+
return [this.py ? out.code : `${out.code};`];
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
actual(expr ) {
|
|
618
|
+
const out = this.expr(expr);
|
|
619
|
+
return this.py ? `_plain(${out.code})` : `plain(${out.code})`;
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
errorClass(expr , line ) {
|
|
623
|
+
const out = this.expr(expr);
|
|
624
|
+
return out.tv.k === "errorClass" ? out.tv : this.fail(line, "expected an error class imported from the SDK", "SL-S004");
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
assertion(name , args , line ) {
|
|
628
|
+
const [first, second] = args;
|
|
629
|
+
switch (name) {
|
|
630
|
+
case "equal":
|
|
631
|
+
case "strictEqual":
|
|
632
|
+
case "deepEqual":
|
|
633
|
+
case "deepStrictEqual":
|
|
634
|
+
case "notEqual":
|
|
635
|
+
case "notStrictEqual":
|
|
636
|
+
case "notDeepStrictEqual": {
|
|
637
|
+
if (first === undefined || second === undefined) return this.fail(line, `assert.${name} needs two values`, "SL-S004");
|
|
638
|
+
const negate = name.startsWith("not");
|
|
639
|
+
const left = this.expr(first);
|
|
640
|
+
if (left.tv.k === "bytes") {
|
|
641
|
+
const right = this.expr(second).code;
|
|
642
|
+
return [this.py ? `self.${negate ? "assertNotEqual" : "assertEqual"}(${left.code}, ${right})` : `assertEquals(${right}, ${left.code});`];
|
|
643
|
+
}
|
|
644
|
+
const actual = this.py ? `_plain(${left.code})` : `plain(${left.code})`;
|
|
645
|
+
const expected = this.rawJson(second);
|
|
646
|
+
if (this.py) return [`self.${negate ? "assertFalse" : "assertTrue"}(_strict(${actual}, ${expected}), (${actual}, ${expected}))`];
|
|
647
|
+
return [negate ? `assertTrue(!java.util.Objects.equals(${expected}, ${actual}), "values should differ");` : `assertEquals(${expected}, ${actual});`];
|
|
648
|
+
}
|
|
649
|
+
case "match": {
|
|
650
|
+
if (first === undefined || second?.kind !== "regex") return this.fail(line, "assert.match needs a value and a regular expression literal", "SL-S004");
|
|
651
|
+
if (/[^i]/.test(second.flags)) return this.fail(line, `regular expression flags other than i are not supported (/${second.pattern}/${second.flags})`, "SL-S004");
|
|
652
|
+
const pattern = `${second.flags.includes("i") ? "(?i)" : ""}${second.pattern}`;
|
|
653
|
+
const value = this.expr(first).code;
|
|
654
|
+
return [this.py ? `self.assertRegex(${value}, ${JSON.stringify(pattern)})` : `assertTrue(java.util.regex.Pattern.compile(${JSON.stringify(pattern)}).matcher(${value}).find(), ${JSON.stringify(pattern)});`];
|
|
655
|
+
}
|
|
656
|
+
case "ok": {
|
|
657
|
+
if (first?.kind === "instanceof") {
|
|
658
|
+
const type = this.errorClass(first.type, line);
|
|
659
|
+
const value = this.expr(first.value).code;
|
|
660
|
+
return [this.py ? `self.assertIsInstance(${value}, ${type.python})` : `assertTrue(${value} instanceof ${type.java}, "instanceof ${type.java}");`];
|
|
661
|
+
}
|
|
662
|
+
if (first?.kind !== "binary") return this.fail(line, "assert.ok is supported for a === b, a !== b and x instanceof ErrorClass", "SL-S004");
|
|
663
|
+
const same = this.py ? `_strict(${this.actual(first.left)}, ${this.rawJson(first.right)})` : `java.util.Objects.equals(${this.rawJson(first.right)}, ${this.actual(first.left)})`;
|
|
664
|
+
const condition = first.op === "===" ? same : this.py ? `not (${same})` : `!${same}`;
|
|
665
|
+
return [this.py ? `self.assertTrue(${condition})` : `assertTrue(${condition}, "assert.ok");`];
|
|
666
|
+
}
|
|
667
|
+
case "rejects":
|
|
668
|
+
case "throws": {
|
|
669
|
+
if (first === undefined || second === undefined) return this.fail(line, `assert.${name} needs a call and an error class`, "SL-S004");
|
|
670
|
+
const action = name === "throws" ? (first.kind === "arrow" ? first.body : this.fail(line, "assert.throws needs () => …", "SL-S004")) : first;
|
|
671
|
+
const code = this.expr(action).code;
|
|
672
|
+
if (second.kind === "block") return this.rejectsWith(code, second, line);
|
|
673
|
+
if (second.kind === "arrow") return this.rejectsWith(code, { kind: "block", line: second.line, params: second.params, statements: this.conjuncts(second.body) }, line);
|
|
674
|
+
const error = this.errorClass(second, line);
|
|
675
|
+
return this.py ? [`with self.assertRaises(${error.python}):`, ` ${code}`] : [`assertThrows(${error.java}.class, () -> ${code});`];
|
|
676
|
+
}
|
|
677
|
+
default:
|
|
678
|
+
return this.fail(line, `assert.${name} is not supported in transpiled tests`, "SL-S004");
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
/** (error) => a && b && c as a check: each part becomes an assertion. */
|
|
683
|
+
conjuncts(body ) {
|
|
684
|
+
if (body.kind === "and") return [...this.conjuncts(body.left), ...this.conjuncts(body.right)];
|
|
685
|
+
const assertOk = (value ) => ({ kind: "expression", line: body.line, value: { kind: "call", line: body.line, callee: { kind: "member", line: body.line, object: { kind: "name", line: body.line, name: "assert" }, name: "ok" }, args: [value] } });
|
|
686
|
+
if (body.kind === "instanceof" || body.kind === "binary") return [assertOk(body)];
|
|
687
|
+
return [assertOk({ kind: "binary", line: body.line, op: "===", left: body, right: { kind: "boolean", line: body.line, value: true } })];
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
/** assert.rejects(call, (error) => { assert…; return true; }): the error is caught, then checked. */
|
|
691
|
+
rejectsWith(call , check , line ) {
|
|
692
|
+
const param = check.params[0];
|
|
693
|
+
if (param === undefined) return this.fail(line, "the check needs the error as its parameter", "SL-S004");
|
|
694
|
+
const instance = check.statements.flatMap((statement) => {
|
|
695
|
+
const value = statement.kind === "expression" ? statement.value : undefined;
|
|
696
|
+
const arg = value?.kind === "call" ? value.args[0] : undefined;
|
|
697
|
+
return arg?.kind === "instanceof" && arg.value.kind === "name" && arg.value.name === param ? [this.errorClass(arg.type, line)] : [];
|
|
698
|
+
})[0];
|
|
699
|
+
const type = instance ?? { k: "errorClass" , python: "ApiError", java: "ApiException" };
|
|
700
|
+
const local = this.py ? snake(param) : param;
|
|
701
|
+
this.vars.set(param, { code: local, tv: { k: "error", java: type.java } });
|
|
702
|
+
const body = check.statements.flatMap((statement) => this.statement(statement));
|
|
703
|
+
return this.py
|
|
704
|
+
? [`with self.assertRaises(${type.python}) as caught:`, ` ${call}`, `${local} = caught.exception`, ...body]
|
|
705
|
+
: // A block of its own: a test may check several rejections, each with an `error` of its own.
|
|
706
|
+
["{", ` var ${local} = assertThrows(${type.java}.class, () -> ${call});`, ...body.map((line) => ` ${line}`), "}"];
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
resetVars() {
|
|
710
|
+
this.vars = new Map();
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
function methodName(name , taken , python ) {
|
|
715
|
+
const base = python ? `test_${snake(name) || "case"}` : `test${pascal(name) || "Case"}`;
|
|
716
|
+
let candidate = /^[A-Za-z_]/.test(base) ? base : `test_${base}`;
|
|
717
|
+
for (let index = 2; taken.has(candidate); index += 1) candidate = `${base}${python ? "_" : ""}${index}`;
|
|
718
|
+
taken.add(candidate);
|
|
719
|
+
return candidate;
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
/** Translates every test of a file it can; the rest are listed with the rule they break. */
|
|
723
|
+
export function translateTests(ir , file , target ) {
|
|
724
|
+
const translator = new Translator(ir, file, target);
|
|
725
|
+
const python = target.language === "python";
|
|
726
|
+
const base = file.file.replace(/^.*\//, "").replace(/\.test\.ts$/, "").replace(/\.ts$/, "");
|
|
727
|
+
const methods = [];
|
|
728
|
+
const translated = [];
|
|
729
|
+
const skipped = [];
|
|
730
|
+
const taken = new Set ();
|
|
731
|
+
for (const testCase of file.tests) {
|
|
732
|
+
if (!Array.isArray(testCase.body)) {
|
|
733
|
+
skipped.push({ name: testCase.name, reason: (testCase.body ).skipped });
|
|
734
|
+
continue;
|
|
735
|
+
}
|
|
736
|
+
translator.resetVars();
|
|
737
|
+
try {
|
|
738
|
+
const lines = (testCase.body ).flatMap((statement) => translator.statement(statement));
|
|
739
|
+
const method = methodName(testCase.name, taken, python);
|
|
740
|
+
methods.push(
|
|
741
|
+
python
|
|
742
|
+
? [` def ${method}(self):`, ` """${testCase.name.replaceAll("\\", "\\\\").replaceAll('"', '\\"')}"""`, ...lines.map((line) => ` ${line}`)]
|
|
743
|
+
: [` /** ${testCase.name.replaceAll("*/", "*/")} */`, " @Test", ` public void ${method}() {`, ...lines.map((line) => ` ${line}`), " }"],
|
|
744
|
+
);
|
|
745
|
+
translated.push(testCase.name);
|
|
746
|
+
} catch (error ) {
|
|
747
|
+
if (!(error instanceof TranspileError)) throw error;
|
|
748
|
+
skipped.push({ name: testCase.name, reason: error.message });
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
const header = `Transpiled by sdkloom from ${file.file}. Do not edit: change the TypeScript test instead.`;
|
|
752
|
+
if (target.language === "python") {
|
|
753
|
+
const code = [
|
|
754
|
+
`# ${header}`,
|
|
755
|
+
"# -*- coding: utf-8 -*-",
|
|
756
|
+
"# Python 2.7 and 3: the tests also run against the py27 variant.",
|
|
757
|
+
"from __future__ import absolute_import, division, unicode_literals",
|
|
758
|
+
"",
|
|
759
|
+
"import json # noqa: F401",
|
|
760
|
+
"import unittest",
|
|
761
|
+
"import warnings",
|
|
762
|
+
"from decimal import Decimal # noqa: F401",
|
|
763
|
+
"",
|
|
764
|
+
'if not hasattr(unittest.TestCase, "assertRegex"): # Python 2.7',
|
|
765
|
+
" unittest.TestCase.assertRegex = unittest.TestCase.assertRegexpMatches",
|
|
766
|
+
"",
|
|
767
|
+
`from ${target.module} import * # noqa: F401,F403`,
|
|
768
|
+
`from ${target.module}._runtime.models import UNSET, to_plain`,
|
|
769
|
+
`from ${target.module}.testing import examples, mock_client # noqa: F401`,
|
|
770
|
+
"",
|
|
771
|
+
"",
|
|
772
|
+
"# Deprecated operations still run in tests; their warnings are noise here.",
|
|
773
|
+
'warnings.simplefilter("ignore", DeprecationWarning)',
|
|
774
|
+
"",
|
|
775
|
+
"",
|
|
776
|
+
"def _opt(value):",
|
|
777
|
+
" return None if value is UNSET else value",
|
|
778
|
+
"",
|
|
779
|
+
"",
|
|
780
|
+
"def _plain(value):",
|
|
781
|
+
" return to_plain(_opt(value))",
|
|
782
|
+
"",
|
|
783
|
+
"",
|
|
784
|
+
"def _strict(left, right):",
|
|
785
|
+
' """Deep equality like assert.deepStrictEqual on JSON values: true is not 1, 2 is 2.0."""',
|
|
786
|
+
" if isinstance(left, bool) or isinstance(right, bool):",
|
|
787
|
+
" return isinstance(left, bool) and isinstance(right, bool) and left == right",
|
|
788
|
+
" if isinstance(left, dict) and isinstance(right, dict):",
|
|
789
|
+
" return left.keys() == right.keys() and all(_strict(left[key], right[key]) for key in left)",
|
|
790
|
+
" if isinstance(left, (list, tuple)) and isinstance(right, (list, tuple)):",
|
|
791
|
+
" return len(left) == len(right) and all(_strict(a, b) for a, b in zip(left, right))",
|
|
792
|
+
" if left != right:",
|
|
793
|
+
" return False",
|
|
794
|
+
" return isinstance(left, (int, float)) == isinstance(right, (int, float))",
|
|
795
|
+
"",
|
|
796
|
+
"",
|
|
797
|
+
"def _coalesce(value, other):",
|
|
798
|
+
" return other if value is None or value is UNSET else value",
|
|
799
|
+
"",
|
|
800
|
+
"",
|
|
801
|
+
`class ${pascal(base)}Test(unittest.TestCase):`,
|
|
802
|
+
...(methods.length === 0 ? [" pass"] : methods.flatMap((method, index) => [...(index === 0 ? [] : [""]), ...method])),
|
|
803
|
+
"",
|
|
804
|
+
].join("\n");
|
|
805
|
+
return { path: `test_${snake(base)}_transpiled.py`, code, translated, skipped };
|
|
806
|
+
}
|
|
807
|
+
const className = `${pascal(base)}TranspiledTest`;
|
|
808
|
+
const pkg = target.javaPackage;
|
|
809
|
+
const code = [
|
|
810
|
+
`// ${header}`,
|
|
811
|
+
"package transpiled;",
|
|
812
|
+
"",
|
|
813
|
+
`import static ${pkg}.runtime.Assert.assertEquals;`,
|
|
814
|
+
`import static ${pkg}.runtime.Assert.assertThrows;`,
|
|
815
|
+
`import static ${pkg}.runtime.Assert.assertTrue;`,
|
|
816
|
+
"",
|
|
817
|
+
`import ${pkg}.*;`,
|
|
818
|
+
`import ${pkg}.runtime.*;`,
|
|
819
|
+
`import ${pkg}.testing.*;`,
|
|
820
|
+
"import java.util.Map;",
|
|
821
|
+
"import java.util.Optional;",
|
|
822
|
+
"",
|
|
823
|
+
`@SuppressWarnings({"deprecation", "unused"})`,
|
|
824
|
+
`public class ${className} {`,
|
|
825
|
+
" private static Object plain(Object value) {",
|
|
826
|
+
" Object present = value instanceof Optional ? ((Optional<?>) value).orElse(null) : value;",
|
|
827
|
+
" return Json.parse(Json.write(present));",
|
|
828
|
+
" }",
|
|
829
|
+
"",
|
|
830
|
+
" private static <T> T coalesce(T value, T other) {",
|
|
831
|
+
" return value != null ? value : other;",
|
|
832
|
+
" }",
|
|
833
|
+
"",
|
|
834
|
+
" @SuppressWarnings(\"unchecked\")",
|
|
835
|
+
" private static Map<String, Object> spreadAll(Object base, Object other) {",
|
|
836
|
+
" Map<String, Object> result = new java.util.LinkedHashMap<>((Map<String, Object>) base);",
|
|
837
|
+
" result.putAll((Map<String, Object>) other);",
|
|
838
|
+
" return result;",
|
|
839
|
+
" }",
|
|
840
|
+
"",
|
|
841
|
+
" private static Map<String, Object> spread(Object base, String key, Object value) {",
|
|
842
|
+
" return spreadAll(base, java.util.Collections.singletonMap(key, value));",
|
|
843
|
+
" }",
|
|
844
|
+
...methods.flatMap((method) => ["", ...method]),
|
|
845
|
+
"}",
|
|
846
|
+
"",
|
|
847
|
+
].join("\n");
|
|
848
|
+
return { path: `transpiled/${className}.java`, code, className: `transpiled.${className}`, translated, skipped };
|
|
849
|
+
}
|