From c2b853bcb4bbfae6b0d468034ec7047b27f51285 Mon Sep 17 00:00:00 2001 From: YanagiEiichi <576398868@qq.com> Date: Mon, 8 Jul 2024 13:57:08 +0800 Subject: [PATCH] Fix test cases --- .../src/tests/codegen/all-types.test.ts | 2 +- .../tests/codegen/ts-special-map-key.test.ts | 60 +++++-------------- .../test-tools/buildMethodWithParameters.ts | 2 +- packages/runtime/src/NadInvoker.ts | 6 +- packages/runtime/src/tests/NadInvoker.test.ts | 12 ++-- packages/runtime/src/tests/libs/mock-xhr.ts | 6 +- 6 files changed, 28 insertions(+), 60 deletions(-) diff --git a/packages/builder/src/tests/codegen/all-types.test.ts b/packages/builder/src/tests/codegen/all-types.test.ts index eb3f632..1dec6c6 100644 --- a/packages/builder/src/tests/codegen/all-types.test.ts +++ b/packages/builder/src/tests/codegen/all-types.test.ts @@ -10,7 +10,7 @@ const typeVector = describe.each([ ['java.util.List', 'unknown[]', 'NSArray*'], ['java.util.List', 'Long[]', 'NSArray*'], ['java.util.List', 'void[]', 'NSArray*'], - ['java.util.Map', 'Record', 'NSDictionary*'], + ['java.util.Map', 'Record', 'NSDictionary*'], ['groovy.lang.Tuple2', '[ string, Long ]', 'NSArray*'], ['java.util.List>', 'Optional[]', 'NSArray*' ], ['java.util.List>', 'Optional[]', 'NSArray*' ], diff --git a/packages/builder/src/tests/codegen/ts-special-map-key.test.ts b/packages/builder/src/tests/codegen/ts-special-map-key.test.ts index 7032d61..59da010 100644 --- a/packages/builder/src/tests/codegen/ts-special-map-key.test.ts +++ b/packages/builder/src/tests/codegen/ts-special-map-key.test.ts @@ -1,49 +1,17 @@ import { buildTsMethodWithParameters } from '../test-tools/buildMethodWithParameters'; -test('java.util.Map', () => { - const { currentTestName: type = '' } = expect.getState(); - const code = buildTsMethodWithParameters({ name: 'map', type }); - expect(code).toContain(`map?: Record`); -}); - -test('java.util.Map', () => { - const { currentTestName: type = '' } = expect.getState(); - const code = buildTsMethodWithParameters({ name: 'map', type }); - expect(code).toContain(`map?: Record`); -}); - -test('java.util.Map', () => { - const { currentTestName: type = '' } = expect.getState(); - const code = buildTsMethodWithParameters({ name: 'map', type }); - expect(code).toContain(`map?: Record`); -}); - -test('java.util.Map', () => { - const { currentTestName: type = '' } = expect.getState(); - const code = buildTsMethodWithParameters({ name: 'map', type }); - expect(code).toContain(`map?: Record`); -}); - -test('java.util.Map', () => { - const { currentTestName: type = '' } = expect.getState(); - const code = buildTsMethodWithParameters({ name: 'map', type }); - expect(code).toContain(`map?: Record`); -}); - -test('java.util.Map', () => { - const { currentTestName: type = '' } = expect.getState(); - const code = buildTsMethodWithParameters({ name: 'map', type }); - expect(code).toContain(`map?: Record`); -}); - -test('java.util.Map', () => { - const { currentTestName: type = '' } = expect.getState(); - const code = buildTsMethodWithParameters({ name: 'map', type }); - expect(code).toContain(`map?: Record`); -}); - -test('java.util.Map', () => { - const { currentTestName: type = '' } = expect.getState(); - const code = buildTsMethodWithParameters({ name: 'map', type }); - expect(code).toContain(`map?: Record`); +test.each([ + ['test.UserType', 'UserType'], + ['test.User', 'PropertyKey'], + ['java.lang.Long', 'Long'], + ['java.lang.Integer', 'number'], + ['java.lang.Double', 'number'], + ['java.lang.String', 'string'], + ['java.math.BigDecimal', 'BigDecimal'], + ['java.math.BigInteger', 'BigInteger'], +])('java.util.Map<%s, java.lang.Long>', (keyType, tsType) => { + const code = buildTsMethodWithParameters({ name: 'map', type: `java.util.Map<${keyType}, java.lang.Long>` }); + expect(code).toMatchCode( + `async foo(map?: Record<${tsType}, Long | undefined>, settings?: Partial) {`, + ); }); diff --git a/packages/builder/src/tests/test-tools/buildMethodWithParameters.ts b/packages/builder/src/tests/test-tools/buildMethodWithParameters.ts index 94c5d83..6defc46 100644 --- a/packages/builder/src/tests/test-tools/buildMethodWithParameters.ts +++ b/packages/builder/src/tests/test-tools/buildMethodWithParameters.ts @@ -26,7 +26,7 @@ export const buildMethodWithParameters = (target: 'oc' | 'ts', ...parameters: De }; const defs = { routes: [foo], classes: [User], enums: [UserType] }; const { code } = new Builder({ target, base: '', defs }); - return code.replace(/\s+/g, ' '); + return code; }; export const buildTsMethodWithParameters = (...parameters: DeepPartial[]) => diff --git a/packages/runtime/src/NadInvoker.ts b/packages/runtime/src/NadInvoker.ts index 5c6b652..a3a071b 100644 --- a/packages/runtime/src/NadInvoker.ts +++ b/packages/runtime/src/NadInvoker.ts @@ -1,5 +1,5 @@ import { - APPLICATION_JSON, + CONTENT_TYPE, InvokeParams, InvokeResult, WWW_FORM_URLENCODED, @@ -294,7 +294,7 @@ export class NadInvoker implements NadRuntime { // const headers = { ...runtime.headers, ...this.headers, ...settings?.headers }; - const contentType = insensitiveGet(headers, 'Content-Type'); + const contentType = insensitiveGet(headers, CONTENT_TYPE); const uri = joinPath(base, buildPath(rawPath, pathVariables)); @@ -332,7 +332,7 @@ export class NadInvoker implements NadRuntime { // In this case, the data can be passed using payload. const tIsWwwFormUrlEncoded = contentType && isWwwFormUrlEncoded(contentType); if (canTakePayload && (!contentType || tIsWwwFormUrlEncoded || tIsMultipartFormData)) { - if (!contentType) headers['Content-Type'] = WWW_FORM_URLENCODED; + if (!contentType) headers[CONTENT_TYPE] = WWW_FORM_URLENCODED; const data = requestParams; const qs = prependQuestionMarkIfNotEmpty(buildQs(staticParams)); return { method, url: uri + qs, timeout, headers, data, ...extensions }; diff --git a/packages/runtime/src/tests/NadInvoker.test.ts b/packages/runtime/src/tests/NadInvoker.test.ts index fbfc3e1..661f578 100644 --- a/packages/runtime/src/tests/NadInvoker.test.ts +++ b/packages/runtime/src/tests/NadInvoker.test.ts @@ -140,7 +140,7 @@ describe('addRequestParam', () => { method: 'POST', url: `${base}/test`, data: 'id=123&name=hehe', - headers: { 'Content-Type': WWW_FORM_URLENCODED }, + headers: { [CONTENT_TYPE]: WWW_FORM_URLENCODED }, }); }); @@ -182,7 +182,7 @@ describe('addStaticParam', () => { method: 'POST', url: `${base}/test?action=wtf`, data: 'name=hehe', - headers: { 'Content-Type': WWW_FORM_URLENCODED }, + headers: { [CONTENT_TYPE]: WWW_FORM_URLENCODED }, }); }); }); @@ -199,7 +199,7 @@ describe('addRequestBody', () => { test('qs data object in body', async () => { const res = await new Localhost() - .open('POST', '/test', { headers: { 'Content-Type': WWW_FORM_URLENCODED } }) + .open('POST', '/test', { headers: { [CONTENT_TYPE]: WWW_FORM_URLENCODED } }) .addRequestBody({ a: 1 }) .execute(); expect(res).toMatchObject({ @@ -211,7 +211,7 @@ describe('addRequestBody', () => { test('form data object in body', async () => { const res = await new Localhost() - .open('POST', '/test', { headers: { 'Content-Type': MULTIPART_FORM_DATA } }) + .open('POST', '/test', { headers: { [CONTENT_TYPE]: MULTIPART_FORM_DATA } }) .addRequestBody({ a: 1 }) .execute(); expect(res).toMatchObject({ @@ -250,7 +250,7 @@ describe('addModelAttribute', () => { test('with json', async () => { const res = await new Localhost() .open('POST', '/test') - .addHeader('Content-Type', APPLICATION_JSON) + .addHeader(CONTENT_TYPE, APPLICATION_JSON) .addModelAttribute({ a: 1, b: { c: 2 } }) .execute(); expect(res).toMatchObject({ @@ -307,7 +307,7 @@ describe('addModelAttribute', () => { method: 'POST', url: `${base}/test`, data: 'user.name=hehe&user.age=18&user.v=1&user.v=2', - headers: { 'Content-Type': WWW_FORM_URLENCODED }, + headers: { [CONTENT_TYPE]: WWW_FORM_URLENCODED }, }); }); diff --git a/packages/runtime/src/tests/libs/mock-xhr.ts b/packages/runtime/src/tests/libs/mock-xhr.ts index 3f9c8d9..7359a4e 100644 --- a/packages/runtime/src/tests/libs/mock-xhr.ts +++ b/packages/runtime/src/tests/libs/mock-xhr.ts @@ -1,6 +1,6 @@ import { EventEmitter } from 'events'; import { readAsDataURL } from './readAsDataURL'; -import { APPLICATION_JSON, MULTIPART_FORM_DATA } from '@huolala-tech/request'; +import { APPLICATION_JSON, CONTENT_TYPE, MULTIPART_FORM_DATA } from '@huolala-tech/request'; export const MULTIPART_FORM_DATA_WITH_BOUNDARY = `${MULTIPART_FORM_DATA}; boundary=----WebKitFormBoundaryHehehehe`; @@ -63,7 +63,7 @@ global.XMLHttpRequest = class { } } else if (body instanceof FormData) { if (!Object.keys(this.headers).some((s) => /^Content-Type$/i.test(s))) { - this.headers['Content-Type'] = MULTIPART_FORM_DATA_WITH_BOUNDARY; + this.headers[CONTENT_TYPE] = MULTIPART_FORM_DATA_WITH_BOUNDARY; } const temp: Record = {}; const tasks = Array.from(body, async ([k, v]) => { @@ -96,7 +96,7 @@ global.XMLHttpRequest = class { this.headers[key] = value; } getResponseHeader(key: string) { - if (key === 'Content-Type') return APPLICATION_JSON; + if (key === CONTENT_TYPE) return APPLICATION_JSON; return null; } getAllResponseHeaders() {