diff --git a/packages/plugin-framework/spec/descriptor-registry.spec.ts b/packages/plugin-framework/spec/descriptor-registry.spec.ts deleted file mode 100644 index 48845b7e..00000000 --- a/packages/plugin-framework/spec/descriptor-registry.spec.ts +++ /dev/null @@ -1,83 +0,0 @@ -import {getFixtureCodeGeneratorRequest, getFixtureFileDescriptor} from "./support/helpers"; -import {DescriptorRegistry} from "../src"; -import {DescriptorProto, FileDescriptorProto} from "../src"; - - -describe('DescriptorRegistry', function () { - - it('can be created from FileDescriptorProto', () => { - const file = getFixtureFileDescriptor("comments.proto"); - const registry = DescriptorRegistry.createFrom(file); - expect(registry).toBeDefined(); - expect(registry.allFiles().length).toBeGreaterThanOrEqual(1); - }); - - it('can be created from CodeGeneratorRequest', () => { - const request = getFixtureCodeGeneratorRequest({ - includeFiles: ["comments.proto"] - }); - const registry = DescriptorRegistry.createFrom(request); - expect(registry).toBeDefined(); - expect(registry.allFiles().length).toBeGreaterThanOrEqual(1); - }); - -}); - - -describe('DescriptorRegistry.resolveTypeName()', function () { - - const registry = DescriptorRegistry.createFrom(getFixtureFileDescriptor("comments.proto")); - - it('resolves message', () => { - const message = registry.resolveTypeName('spec.MessageWithComments'); - expect(message).toBeDefined(); - }); - - it('throws for unknown type', () => { - expect(() => registry.resolveTypeName('spec.ThisTypeShouldNeverBeFound')).toThrow(); - }); - -}); - - -describe('DescriptorRegistry.peekTypeName()', function () { - - const registry = DescriptorRegistry.createFrom(getFixtureFileDescriptor("comments.proto")); - - it('resolves message', () => { - const message = registry.peekTypeName('spec.MessageWithComments'); - expect(message).toBeDefined(); - }); - - it('does not throw for unknown type', () => { - const message = registry.peekTypeName('spec.ThisTypeShouldNeverBeFound'); - expect(message).toBeUndefined(); - }); - -}); - - -describe('DescriptorRegistry.ancestors()', function () { - - const registry = DescriptorRegistry.createFrom(getFixtureFileDescriptor("msg-nesting.proto")); - const type = registry.resolveTypeName('spec.ParentMessage.ChildMessage.GrandChildMessage'); - - it('returns ancestors up to file', () => { - const ancestors = registry.ancestorsOf(type); - expect(ancestors.length).toBe(3); - expect(FileDescriptorProto.is(ancestors[0])).toBeTrue(); - if (DescriptorProto.is(ancestors[0])) { - expect(ancestors[0].name).toBe('nested-messages.proto'); - } - expect(DescriptorProto.is(ancestors[1])).toBeTrue(); - if (DescriptorProto.is(ancestors[1])) { - expect(ancestors[1].name).toBe('ParentMessage'); - } - expect(DescriptorProto.is(ancestors[2])).toBeTrue(); - if (DescriptorProto.is(ancestors[2])) { - expect(ancestors[2].name).toBe('ChildMessage'); - } - }); - -}); - diff --git a/packages/plugin-framework/spec/descriptor-tree.spec.ts b/packages/plugin-framework/spec/descriptor-tree.spec.ts deleted file mode 100644 index b1c48600..00000000 --- a/packages/plugin-framework/spec/descriptor-tree.spec.ts +++ /dev/null @@ -1,57 +0,0 @@ -import {DescriptorProto, FieldDescriptorProto, visitDescriptorTree} from "../src/"; -import {getFixtureFileDescriptor} from "./support/helpers"; - - -describe('visitDescriptor()', function () { - - const scalarValuesProto = getFixtureFileDescriptor("msg-scalar.proto"); - - - it('visits messages', () => { - const messageNames: string[] = []; - visitDescriptorTree(scalarValuesProto, descriptor => { - if (DescriptorProto.is(descriptor)) { - messageNames.push(descriptor.name ?? ''); - } - }); - expect(messageNames.length).toBe(2); - expect(messageNames).toContain('ScalarValuesMessage'); - expect(messageNames).toContain('RepeatedScalarValuesMessage'); - }); - - - it('visits fields', () => { - const fieldNames: string[] = []; - visitDescriptorTree(scalarValuesProto, descriptor => { - if (FieldDescriptorProto.is(descriptor)) { - fieldNames.push(descriptor.name ?? ''); - } - }); - expect(fieldNames.length).toBeGreaterThanOrEqual(28); - }); - - - it('includes carry', () => { - let messageCount = 0; - let fieldCount = 0; - - visitDescriptorTree(scalarValuesProto, (descriptor, carry) => { - if (DescriptorProto.is(descriptor)) { - messageCount++; - expect(carry.length).toBe(1); - expect(carry[0]).toBe(scalarValuesProto); - } - if (FieldDescriptorProto.is(descriptor)) { - fieldCount++; - expect(carry.length).toBe(2); - expect(carry[0]).toBe(scalarValuesProto); - expect(DescriptorProto.is(carry[1])).toBeTrue(); - } - }); - expect(messageCount).toBeGreaterThanOrEqual(1); - expect(fieldCount).toBeGreaterThanOrEqual(1); - }); - - -}); - diff --git a/packages/plugin-framework/spec/google-protobuf-descriptor.spec.ts b/packages/plugin-framework/spec/google-protobuf-descriptor.spec.ts deleted file mode 100644 index bebfa1bd..00000000 --- a/packages/plugin-framework/spec/google-protobuf-descriptor.spec.ts +++ /dev/null @@ -1,69 +0,0 @@ -import {getFixtureCodeGeneratorRequest} from "./support/helpers"; -import {fixtures} from "../../test-fixtures"; -import {DescriptorProto, DescriptorRegistry} from "../src/"; -import {assert} from "@protobuf-ts/runtime"; - - -describe('google/protobuf/descriptor.ts', function () { - - - let registry = DescriptorRegistry.createFrom(getFixtureCodeGeneratorRequest({})); - - - describe('provides deprecation info as expected by fixture', function () { - - fixtures.usingDeprecation((typeName, explicitlyDeprecated, implicitlyDeprecated, deprecatedFieldNames) => { - - if (explicitlyDeprecated) { - - it(`${typeName}`, function () { - let descriptor = registry.resolveTypeName(typeName); - assert(DescriptorProto.is(descriptor)); - expect(descriptor.options?.deprecated) - .toBe(true, `${typeName} should be deprecated`); - }); - - } else if (implicitlyDeprecated) { - - it(`${typeName}`, function () { - let descriptor = registry.resolveTypeName(typeName); - let file = registry.fileOf(descriptor); - assert(DescriptorProto.is(descriptor)); - expect(descriptor.options?.deprecated ?? false) - .toBe(false, `${typeName} should not be deprecated`); - expect(file.options?.deprecated) - .toBe(true, `${typeName}'s file ${file.name} should be deprecated`); - }); - - } else { - - it(`${typeName}`, function () { - let descriptor = registry.resolveTypeName(typeName); - let file = registry.fileOf(descriptor); - assert(DescriptorProto.is(descriptor)); - expect(descriptor.options?.deprecated ?? false) - .toBe(false, `${typeName} should not be deprecated`); - expect(file.options?.deprecated ?? false) - .toBe(false, `${typeName}'s file ${file.name} should not be deprecated`); - }); - - } - - - let descriptor = registry.resolveTypeName(typeName); - assert(DescriptorProto.is(descriptor)); - for (let field of descriptor.field) { - - it(`${typeName}.${field.name}`, function () { - expect(field.options?.deprecated ?? false).toBe(deprecatedFieldNames.includes(field.name ?? '')); - }); - } - - - }); - - }); - - -}); - diff --git a/packages/plugin-framework/spec/source-code-info.spec.ts b/packages/plugin-framework/spec/source-code-info.spec.ts index 9d217254..9f4bab54 100644 --- a/packages/plugin-framework/spec/source-code-info.spec.ts +++ b/packages/plugin-framework/spec/source-code-info.spec.ts @@ -1,55 +1,4 @@ -import {getFixtureFileDescriptor} from "./support/helpers"; -import { - DescriptorParentFn, - DescriptorProto, - DescriptorRegistry, - FileDescriptorProto, - filterSourceCodeLocations, - makeSourceCodePath, - makeSourceCodePathComponent, - sourceCodeLocationToComment, - sourceCodeLocationToCursor -} from "../src/"; -import {assert} from "@protobuf-ts/runtime"; - -const pathTo_MessageWithComments_Foo = [4, 0, 2, 0]; -const cursor_MessageWithComments_Foo = [28, 5]; - - -describe('sourceCodeLocationToComment()', function () { - const all = getFixtureFileDescriptor("comments.proto").sourceCodeInfo?.location ?? []; - const locations = filterSourceCodeLocations(all, pathTo_MessageWithComments_Foo); - it('seems to recognize comments correctly', function () { - const comments = sourceCodeLocationToComment(locations); - expect(comments.leadingDetached.length).toBe(1); - expect(comments.leading).toBeDefined(); - expect(comments.trailing).toBeDefined(); - }); -}); - - -describe('sourceCodeLocationToCursor()', function () { - const all = getFixtureFileDescriptor("comments.proto").sourceCodeInfo?.location ?? []; - const locations = filterSourceCodeLocations(all, pathTo_MessageWithComments_Foo); - it('returns expected cursor', function () { - const cursor = sourceCodeLocationToCursor(locations); - expect(cursor).toEqual(cursor_MessageWithComments_Foo); - }); -}); - - -describe('makeSourceCodePath()', function () { - const registry = DescriptorRegistry.createFrom(getFixtureFileDescriptor("comments.proto")); - const parents: DescriptorParentFn = d => registry.parentOf(d); - const message = registry.resolveTypeName('spec.MessageWithComments'); - assert(DescriptorProto.is(message)); - const field = message.field[0]; - it('returns expected path', function () { - const path = makeSourceCodePath(parents, field); - expect(path).toEqual(pathTo_MessageWithComments_Foo); - }); -}); - +import {DescriptorProto, FileDescriptorProto, makeSourceCodePathComponent} from "../src/"; describe('makeSourceCodePathComponent()', function () { it('returns undefined for unknown combination ', () => { diff --git a/packages/plugin-framework/spec/support/helpers.ts b/packages/plugin-framework/spec/support/helpers.ts deleted file mode 100644 index b53c3548..00000000 --- a/packages/plugin-framework/spec/support/helpers.ts +++ /dev/null @@ -1,84 +0,0 @@ -import {CodeGeneratorRequest} from "../../src"; -import {FileDescriptorProto, FileDescriptorSet} from "../../src"; -import {join, normalize} from "path"; -import {existsSync, readFileSync} from "fs"; - - -const fixtureDescriptorSetPath = normalize(join(__dirname, '../../../test-fixtures/all.descriptorset')); -let fixtureDescriptorSet: FileDescriptorSet; - - -interface MakeCodeGeneratorRequestOptions { - parameter?: string; - includeFiles?: string[]; // defaults to all files - fileToGenerate?: string[]; // defaults to includeFiles -} - -/** - * Synthesize a CodeGeneratorRequest from a pre-generated descriptor set. - * - * You can specify options to simulate a plugin request. - * - * To work correctly, the descriptor set must be generated with the protoc - * options --include_source_info and --include_imports. - */ -export function getFixtureCodeGeneratorRequest(options: MakeCodeGeneratorRequestOptions): CodeGeneratorRequest { - - // read the pre-generated descriptor set - let allFiles = readFixtureDescriptorSet(); - const availableFilenames = allFiles.map(x => x.name).filter(x => x !== undefined) as string[]; - - // create request - let request = CodeGeneratorRequest.create(); - request.parameter = options.parameter ?? ''; - request.fileToGenerate = options.fileToGenerate ?? options.includeFiles ?? availableFilenames; - request.protoFile = - allFiles.filter(f => (options.includeFiles ?? availableFilenames).some(n => n === f.name)); - - // check if demand is satisfied - const missingFileToGenerate = request.fileToGenerate.filter(n => !request.protoFile.some(f => f.name === n)); - if (missingFileToGenerate.length > 0) { - let msg = `You requested ${missingFileToGenerate.length} files to generate that are not available in ${fixtureDescriptorSetPath}:\n`; - msg += missingFileToGenerate.join('\n'); - - msg += "\n\navailable files: " - msg += availableFilenames.join('\n'); - throw msg; - } - const missingIncludes = (options.includeFiles ?? []).filter(n => !request.protoFile.some(f => f.name === n)); - if (missingIncludes.length > 0) { - let msg = `You requested to include ${missingIncludes.length} files that are not available in ${fixtureDescriptorSetPath}:\n`; - msg += missingIncludes.join('\n'); - throw msg; - } - - // make sure to clone so that our cached descriptors stay unchanged - return CodeGeneratorRequest.create(request); -} - -/** - * Get a FileDescriptorProto from a pre-generated descriptor set. - */ -export function getFixtureFileDescriptor(protoFile: string): FileDescriptorProto { - let allFiles = readFixtureDescriptorSet(); - const file = allFiles.find(f => protoFile === f.name); - if (file === undefined) { - throw new Error(`Missing file: ${protoFile} in ${fixtureDescriptorSetPath}`); - } - return FileDescriptorProto.create(file); -} - - -function readFixtureDescriptorSet(): FileDescriptorProto[] { - if (!fixtureDescriptorSet) { - if (!existsSync(fixtureDescriptorSetPath)) { - const reason = `Did not find '${fixtureDescriptorSetPath}'. \n` - + `The file has to be generated by by protoc and contain a serialized FileDescriptorSet of all fixtures .protos.` - pending(reason); - throw reason; - } - const bytes = readFileSync(fixtureDescriptorSetPath); - fixtureDescriptorSet = FileDescriptorSet.fromBinary(bytes); - } - return FileDescriptorSet.create(fixtureDescriptorSet).file; -} diff --git a/packages/runtime/spec/binary-reader.spec.ts b/packages/runtime/spec/binary-reader.spec.ts index 38add56f..b2224ee9 100644 --- a/packages/runtime/spec/binary-reader.spec.ts +++ b/packages/runtime/spec/binary-reader.spec.ts @@ -1,6 +1,6 @@ import {WireDataDelimited, WireDataPlain} from "./support/wire-data"; import {BinaryReader, WireType} from "../src"; -import {msg_longs_bytes} from "../../test-fixtures/msg-longs.fixtures"; +import {msg_longs_bytes} from "./support/msg-longs-fixture"; describe('BinaryReader', () => { diff --git a/packages/runtime/spec/binary-writer.spec.ts b/packages/runtime/spec/binary-writer.spec.ts index ba27161e..22d6a5b1 100644 --- a/packages/runtime/spec/binary-writer.spec.ts +++ b/packages/runtime/spec/binary-writer.spec.ts @@ -1,6 +1,6 @@ import {BinaryReader, BinaryWriter, WireType} from "../src"; import {WireDataDelimited, WireDataPlain} from "./support/wire-data"; -import {msg_longs_bytes} from "../../test-fixtures/msg-longs.fixtures"; +import {msg_longs_bytes} from "./support/msg-longs-fixture"; describe('BinaryWriter', () => { diff --git a/packages/runtime/spec/reflection-binary-reader.spec.ts b/packages/runtime/spec/reflection-binary-reader.spec.ts deleted file mode 100644 index 49ea0dbb..00000000 --- a/packages/runtime/spec/reflection-binary-reader.spec.ts +++ /dev/null @@ -1,63 +0,0 @@ -import {fixtures} from "../../test-fixtures"; -import {BinaryReader, BinaryWriter, binaryReadOptions, ReflectionBinaryReader, WireType} from "../src"; - -describe('ReflectionBinaryReader', () => { - - beforeEach(function () { - jasmine.addCustomEqualityTester((a, b) => - (a instanceof Uint8Array && b instanceof Uint8Array) ? a.byteLength === b.byteLength : undefined - ); - }); - - describe('reads 0 bytes as default fixture message', function () { - fixtures.usingMessages((typeName, key, msg) => { - if (key !== 'default') return - const format = new ReflectionBinaryReader(fixtures.makeMessageInfo(typeName)); - it(`${typeName}`, function () { - let output = fixtures.getMessage(typeName, key); // need default map and array fields - let reader = new BinaryReader(new Uint8Array(0)); - format.read(reader, output, binaryReadOptions()); - expect(output).toEqual(msg); - }); - }); - }); - - describe('with unknown field', function () { - - it('should throw error if `readUnknownField: "throw"`', function () { - let input = new BinaryWriter().tag(1, WireType.Bit64).fixed64(123).finish(); - const refReader = new ReflectionBinaryReader({typeName: "test", fields: []}); - expect(() => { - refReader.read(new BinaryReader(input), {}, { - readUnknownField: "throw", - readerFactory: bytes => new BinaryReader(bytes), - }); - }).toThrowError("Unknown field 1 (wire type 1) for test"); - }); - - it('should ignore if `readUnknownField: false`', function () { - let input = new BinaryWriter().tag(1, WireType.Bit64).fixed64(123).finish(); - const refReader = new ReflectionBinaryReader({typeName: "test", fields: []}); - refReader.read(new BinaryReader(input), {}, { - readUnknownField: false, - readerFactory: bytes => new BinaryReader(bytes), - }); - expect(true).toBeTrue(); - }); - - it('should call if `readUnknownField: UnknownFieldReader`', function () { - let input = new BinaryWriter().tag(1, WireType.Bit64).fixed64(123).finish(); - let unknown: [string, object, number, WireType, Uint8Array] | undefined; - const refReader = new ReflectionBinaryReader({typeName: "test", fields: []}); - refReader.read(new BinaryReader(input), {}, { - readUnknownField: (typeName, message, fieldNo, wireType, data) => unknown = [typeName, message, fieldNo, wireType, data], - readerFactory: bytes => new BinaryReader(bytes), - }); - expect(unknown).toEqual([ - "test", {}, 1, WireType.Bit64, new Uint8Array([123, 0, 0, 0, 0, 0, 0, 0]) - ]); - }); - - }); - -}); diff --git a/packages/runtime/spec/reflection-binary-roundtrip.spec.ts b/packages/runtime/spec/reflection-binary-roundtrip.spec.ts deleted file mode 100644 index 873a6f9a..00000000 --- a/packages/runtime/spec/reflection-binary-roundtrip.spec.ts +++ /dev/null @@ -1,69 +0,0 @@ -import {fixtures} from "../../test-fixtures"; -import { - BinaryReader, - BinaryReadOptions, - BinaryWriteOptions, - BinaryWriter, - binaryReadOptions, - binaryWriteOptions, - ReflectionBinaryReader, - ReflectionBinaryWriter, - WireType -} from "../src"; - - -describe('ReflectionBinaryWriter and ReflectionBinaryReader', () => { - - beforeEach(function () { - jasmine.addCustomEqualityTester((a, b) => - (a instanceof Uint8Array && b instanceof Uint8Array) ? a.byteLength === b.byteLength : undefined - ); - }); - - - describe('all fixture messages survive binary round trip', function () { - fixtures.usingMessages((typeName, key, msg) => { - const refReader = new ReflectionBinaryReader(fixtures.makeMessageInfo(typeName)); - const refWriter = new ReflectionBinaryWriter(fixtures.makeMessageInfo(typeName)); - it(`${typeName} '${key}'`, function () { - - // write fixture message to binary - let writer = new BinaryWriter(); - refWriter.write(msg, writer, binaryWriteOptions()); - const bytes = writer.finish(); - - // read binary into defaults - let output = fixtures.getMessage(typeName, 'default'); - let reader = new BinaryReader(bytes); - refReader.read(reader, output, binaryReadOptions()); - - // now the output should be same as the original fixture message - expect(output).toEqual(msg); - }); - }); - }); - - - describe('with unknown fields', function () { - const options: BinaryReadOptions & BinaryWriteOptions = { - readerFactory: (data) => new BinaryReader(data), - writerFactory: () => new BinaryWriter(), - writeUnknownFields: true, - readUnknownField: true, - }; - const refReader = new ReflectionBinaryReader({typeName: "test", fields: []}); - const refWriter = new ReflectionBinaryWriter({typeName: "test", fields: []}); - it('should write back unknown fields with the right options', function () { - let input = new BinaryWriter().tag(1, WireType.Bit64).fixed64(123).finish(); - let message = {}; - refReader.read(new BinaryReader(input), message, options); - let writer = new BinaryWriter(); - refWriter.write(message, writer, options); - let output = writer.finish(); - expect(output).toEqual(input); - }); - }); - - -}); - diff --git a/packages/runtime/spec/reflection-binary-writer.spec.ts b/packages/runtime/spec/reflection-binary-writer.spec.ts deleted file mode 100644 index ee0ea25a..00000000 --- a/packages/runtime/spec/reflection-binary-writer.spec.ts +++ /dev/null @@ -1,27 +0,0 @@ -import {fixtures} from "../../test-fixtures"; -import {BinaryWriter, ReflectionBinaryWriter} from "../src"; -import {binaryWriteOptions} from "../src"; - -describe('ReflectionBinaryWriter', () => { - - beforeEach(function () { - jasmine.addCustomEqualityTester((a, b) => - (a instanceof Uint8Array && b instanceof Uint8Array) ? a.byteLength === b.byteLength : undefined - ); - }); - - describe('writes 0 bytes for all default fixture messages', function () { - fixtures.usingMessages((typeName, key, msg) => { - if (key !== 'default') return - const format = new ReflectionBinaryWriter(fixtures.makeMessageInfo(typeName)); - it(`${typeName}`, function () { - // let writer = Writer.create(); - // let writer = new ProtobufJsBinaryWriter(); - let writer = new BinaryWriter(); - format.write(msg, writer, binaryWriteOptions()); - expect(writer.finish().length).toBe(0); - }); - }); - }); - -}); diff --git a/packages/runtime/spec/reflection-json-reader.spec.ts b/packages/runtime/spec/reflection-json-reader.spec.ts deleted file mode 100644 index 6be9beef..00000000 --- a/packages/runtime/spec/reflection-json-reader.spec.ts +++ /dev/null @@ -1,276 +0,0 @@ -import {fixtures} from "../../test-fixtures"; -import {EnumInfo, JsonObject, jsonReadOptions, normalizeFieldInfo, ReflectionJsonReader, RepeatType} from "../src"; - -describe('ReflectionJsonReader', function () { - - - describe('read() returns message as expected by fixture', function () { - fixtures.usingPairs((typeName, key, msg, json) => { - - const reader = new ReflectionJsonReader(fixtures.makeMessageInfo(typeName)); - - it(`${typeName} '${key}'`, function () { - let output = fixtures.getMessage(typeName, 'default'); - reader.read(json, output, {ignoreUnknownFields: false}); - expect(output).toEqual(msg); - }); - }); - }); - - - describe('read() understands original proto field names of fixture', function () { - fixtures.usingPairs((typeName: string, key, msg, json) => { - - const reader = new ReflectionJsonReader(fixtures.makeMessageInfo(typeName)); - - // rewrite json keys to be original proto field names: - for (let field of fixtures.makeMessageInfo(typeName).fields) { - if (!json.hasOwnProperty(field.jsonName)) continue; - if (field.jsonName === field.name) continue; - json[field.name] = json[field.jsonName]; - delete json[field.jsonName]; - } - - it(`${typeName} '${key}'`, function () { - let output = fixtures.getMessage(typeName, 'default'); - reader.read(json, output, {ignoreUnknownFields: false}); - expect(output).toEqual(msg); - }); - }); - }); - - - describe('read() understands json `null` as field default values if expected by fixture', function () { - fixtures.usingJson((typeName, key, json) => { - - if (key !== 'nulls equalling defaults') return; - const reader = new ReflectionJsonReader(fixtures.makeMessageInfo(typeName)); - - it(`${typeName}`, function () { - let output = fixtures.getMessage(typeName, 'default'); - reader.read(json, output, {ignoreUnknownFields: false}); - - let defaults = fixtures.getMessage(typeName, 'default'); - expect(output).toEqual(defaults); - }); - }); - }); - - - describe('read() reads field as expected by fixture', function () { - fixtures.usingJsonReads((typeName, field, key, input, exp, defaults) => { - const reader = new ReflectionJsonReader({typeName: typeName, fields: [field]}); - it(`${typeName}.${field.name} '${key}'`, function () { - reader.read(input, defaults, {ignoreUnknownFields: false}); - expect(defaults[field.localName]).toEqual(exp[field.localName]); - }); - }); - }); - - - describe('read() throws as expected by fixture', function () { - fixtures.usingJsonReadErrors((typeName, field, key, input, exp, defaults) => { - const reader = new ReflectionJsonReader({typeName: typeName, fields: [field]}); - it(`${typeName}.${field.name} '${key}'`, function () { - expect(() => { - reader.read(input, defaults, {ignoreUnknownFields: false}); - }).toThrowError(exp); - - }); - }); - }); - - - describe('read() merges', function () { - - it(`repeated values`, function () { - const reader = new ReflectionJsonReader({ - typeName: 'test', - fields: [ - normalizeFieldInfo({no: 1, name: "field", kind: "scalar", T: 9 /*string*/, repeat: RepeatType.PACKED}), - ] - }); - let output = { - field: ["a"] - }; - reader.read({ - field: ["b"] - }, output, {ignoreUnknownFields: false}); - expect(output.field).toEqual(["a", "b"]); - }); - - it(`maps`, function () { - const reader = new ReflectionJsonReader({ - typeName: 'test', - fields: [ - normalizeFieldInfo({no: 1, name: "field", kind: "map", K: 9 /*string*/, V: {kind: "scalar", T: 9}}), - ] - }); - let output: any = { - field: {a: "A"} - }; - reader.read({ - field: {b: "B"} - }, output, {ignoreUnknownFields: false}); - expect(output.field).toEqual({a: "A", b: "B"}); - }); - - - }); - - - describe('read() oneof', () => { - - const typeName = 'spec.OneofScalarMemberMessage'; - const reader = new ReflectionJsonReader(fixtures.makeMessageInfo(typeName)); - - it('throws on invalid input', () => { - const output = fixtures.getMessage(typeName, 'default'); - const input: JsonObject = { - value: 123, - error: 'hello' - }; - expect(() => reader.read(input, output, jsonReadOptions())) - .toThrowError(/Multiple members of the oneof group/); - }); - - it('null selects kind with scalar default value', () => { - const output = fixtures.getMessage(typeName, 'default'); - const input: JsonObject = { - value: null, - }; - reader.read(input, output, jsonReadOptions()); - expect(output).toEqual({ - result: {oneofKind: 'value', value: 0} - }); - }); - - it('deletes existing oneof member', () => { - const output: object = { - result: { - oneofKind: 'value', - value: 123 - } - }; - const input: JsonObject = { - error: 'message', - }; - reader.read(input, output, jsonReadOptions()); - expect(output).toEqual({ - result: {oneofKind: 'error', error: 'message'} - }); - }); - - }); - - describe('read() enum', function () { - enum SimpleEnum { - ANY = 0 - } - - it('skip map value if the value is invalid', function () { - const reader = new ReflectionJsonReader({ - typeName: 'test', - fields: [ - normalizeFieldInfo({no: 1, name: "field", kind: "map", K: 9 /*string*/, V: {kind: "enum", T: () => ["SimpleEnum", SimpleEnum]}}), - ] - }); - let output: any = {field: {}}; - reader.read({field: {valid: "ANY", invalid: "XXX"}}, output, {ignoreUnknownFields: true}); - expect(output).toEqual({field: {valid: SimpleEnum.ANY}}); - }); - - it('skip array value if the value is invalid', function () { - const reader = new ReflectionJsonReader({ - typeName: 'test', - fields: [ - normalizeFieldInfo({no: 1, name: "field", kind: "enum", T: () => ["SimpleEnum", SimpleEnum], repeat: RepeatType.PACKED}), - ] - }); - let output: any = {field: []}; - reader.read({field: ["ANY", "XXX", "ANY"]}, output, {ignoreUnknownFields: true}); - expect(output).toEqual({field: [SimpleEnum.ANY, SimpleEnum.ANY]}); - }); - - it('skip enum value if the value is invalid', function () { - const reader = new ReflectionJsonReader({ - typeName: 'test', - fields: [ - normalizeFieldInfo({no: 1, name: "field", kind: "enum", T: () => ["SimpleEnum", SimpleEnum]}), - ] - }); - let output = {}; - reader.read({field: "XXX"}, output, {ignoreUnknownFields: true}); - expect(output).toEqual({}); - }); - - }); - - describe('enum()', function () { - enum SimpleEnum { - ANY = 0 - } - - it('throws if unknown value was found', function () { - const reader = new ReflectionJsonReader({typeName: '.test.Message', fields: []}); - expect(() => reader.enum([".spec.SimpleEnum", SimpleEnum], 'test', '', false)).toThrowError(/enum \.spec\.SimpleEnum has no value for "test"/); - }) - - it('return false if unknown value was found, but ignoreUnknownFields was set', function () { - const reader = new ReflectionJsonReader({typeName: '.test.Message', fields: []}); - expect(reader.enum([".spec.SimpleEnum", SimpleEnum], 'test', '', true)).toBeFalse(); - }) - - }); - - - describe('read() throws', function () { - - it('for unknown field', function () { - const reader = new ReflectionJsonReader({typeName: '.test.Message', fields: []}); - expect(() => reader.read({unknown_field: true}, {}, {ignoreUnknownFields: false})) - .toThrowError(/Found unknown field/); - }); - - it('not for unknown field if ignoreUnknownFields: true', function () { - const format = new ReflectionJsonReader({typeName: '.test.Message', fields: []}); - expect(() => format.read({unknown_field: true}, {}, {ignoreUnknownFields: true})).not.toThrow(); - }); - - }); - - - describe('enum() google.protobuf.NullValue', function () { - enum NullValue { - NULL_VALUE = 0 - } - - const reader = new ReflectionJsonReader({typeName: '.test.Message', fields: []}); - const handler: EnumInfo = ["google.protobuf.NullValue", NullValue]; - const field = normalizeFieldInfo({ - no: 1, - name: 'enum_field', - kind: 'enum', - T: () => ["google.protobuf.NullValue", NullValue] - }); - it('`null` parses as `null`', () => { - const val = reader.enum(handler, null, field.name, false); - expect(val).toBe(NullValue.NULL_VALUE); - }); - it('other value throws', () => { - expect(() => reader.enum(handler, 0, field.name, false)) - .toThrow(); - expect(() => reader.enum(handler, 'NULL_VALUE', field.name, false)) - .toThrow(); - }); - it('`0` throws', () => { - expect(() => reader.enum(handler, 0, field.name, false)).toThrowError(/only accepts null/); - }); - it('`NULL_VALUE` throws', () => { - expect(() => reader.enum(handler, 'NULL_VALUE', field.name, false)).toThrowError(/only accepts null/); - }); - - }); - - -}); diff --git a/packages/runtime/spec/reflection-json-roundtrip.spec.ts b/packages/runtime/spec/reflection-json-roundtrip.spec.ts deleted file mode 100644 index be98c0a0..00000000 --- a/packages/runtime/spec/reflection-json-roundtrip.spec.ts +++ /dev/null @@ -1,68 +0,0 @@ -import {fixtures} from "../../test-fixtures"; -import {isJsonObject, ReflectionJsonReader, ReflectionJsonWriter} from "../src"; - - -describe('ReflectionJsonReader and ReflectionJsonWriter', function () { - - describe('all message fixtures survive round trip', function () { - fixtures.usingMessages((typeName, key, message) => { - - const reader = new ReflectionJsonReader(fixtures.makeMessageInfo(typeName)); - const writer = new ReflectionJsonWriter(fixtures.makeMessageInfo(typeName)); - - it(`${typeName} '${key}'`, function () { - let json = writer.write(message, { - emitDefaultValues: false, - enumAsInteger: false, - useProtoFieldName: false - }); - if (!isJsonObject(json)) return fail(); - const output = fixtures.getMessage(typeName, 'default'); - reader.read(json, output, {ignoreUnknownFields: false}); - expect(output).toEqual(message); - }); - }); - }); - - describe('all message fixtures survive round trip with emitDefaultValues:true', function () { - fixtures.usingMessages((typeName, key, message) => { - - const reader = new ReflectionJsonReader(fixtures.makeMessageInfo(typeName)); - const writer = new ReflectionJsonWriter(fixtures.makeMessageInfo(typeName)); - - it(`${typeName} '${key}'`, function () { - let json = writer.write(message, { - emitDefaultValues: true, - enumAsInteger: false, - useProtoFieldName: false - }); - if (!isJsonObject(json)) return fail(); - const output = fixtures.getMessage(typeName, 'default'); - reader.read(json, output, {ignoreUnknownFields: false}); - expect(output).toEqual(message); - }); - }); - }); - - describe('all message fixtures survive round trip with enumAsInteger:true', function () { - fixtures.usingMessages((typeName, key, message) => { - - const reader = new ReflectionJsonReader(fixtures.makeMessageInfo(typeName)); - const writer = new ReflectionJsonWriter(fixtures.makeMessageInfo(typeName)); - - it(`${typeName} '${key}'`, function () { - let json = writer.write(message, { - emitDefaultValues: false, - enumAsInteger: true, - useProtoFieldName: false - }); - if (!isJsonObject(json)) return fail(); - const output = fixtures.getMessage(typeName, 'default'); - reader.read(json, output, {ignoreUnknownFields: false}); - expect(output).toEqual(message); - }); - }); - }); - - -}); diff --git a/packages/runtime/spec/reflection-json-writer.spec.ts b/packages/runtime/spec/reflection-json-writer.spec.ts deleted file mode 100644 index b0fe4082..00000000 --- a/packages/runtime/spec/reflection-json-writer.spec.ts +++ /dev/null @@ -1,86 +0,0 @@ -import {fixtures} from "../../test-fixtures"; -import {scalarTypeToNameForTests} from "./support/helpers"; -import {jsonWriteOptions, PbLong, PbULong, ReflectionJsonWriter, ScalarType} from "../src"; - - -describe('ReflectionJsonWriter', function () { - - - describe('write() returns json as expected by fixture', function () { - fixtures.usingPairs((typeName, key, msg, json) => { - - const format = new ReflectionJsonWriter(fixtures.makeMessageInfo(typeName)); - - it(`${typeName} '${key}'`, function () { - let output = format.write(msg, { - emitDefaultValues: false, - enumAsInteger: false, - useProtoFieldName: false - }); - expect(output).toEqual(json); - }); - }); - }); - - - describe('write() writes field as expected by fixture', function () { - fixtures.usingJsonWrites((typeName, field, key, input, exp, opt) => { - const format = new ReflectionJsonWriter({typeName: typeName, fields: [field]}); - it(`${typeName}.${field.name} '${key}'`, function () { - let written = format.write(input, jsonWriteOptions(opt)); - expect(written).toEqual(exp); - }); - }); - }); - - - describe('scalar()', () => { - const format = new ReflectionJsonWriter({ - typeName: '.test.Message', - fields: [] - }); - const list: Array<[ScalarType, any]> = [ - [ScalarType.DOUBLE, 0], - [ScalarType.FLOAT, 0], - [ScalarType.INT64, PbLong.ZERO.toString()], - [ScalarType.UINT64, PbULong.ZERO.toString()], - [ScalarType.INT32, 0], - [ScalarType.FIXED64, PbULong.ZERO.toString()], - [ScalarType.FIXED32, 0], - [ScalarType.BOOL, false], - [ScalarType.STRING, ""], - [ScalarType.BYTES, new Uint8Array(0)], - [ScalarType.UINT32, 0], - [ScalarType.SFIXED32, 0], - [ScalarType.SFIXED64, PbLong.ZERO.toString()], - [ScalarType.SINT32, 0], - [ScalarType.SINT64, PbLong.ZERO.toString()], - ]; - describe('honors `emitDefaultValue = false`', () => { - for (const [type, defaultVal] of list) { - it(`for ${scalarTypeToNameForTests(type)}`, () => { - const val = format.scalar(type, defaultVal, "fake-field-name", false, false); - expect(val).toBeUndefined(); - }); - } - }); - describe('honors `emitDefaultValue = true`', () => { - for (const [type, defaultVal] of list) { - it(`for ${scalarTypeToNameForTests(type)}`, () => { - const val = format.scalar(type, defaultVal, "fake-field-name", false, true); - expect(val).toBeDefined(); - }); - } - }); - describe('does not skip default value for optional fields', () => { - for (const [type, defaultVal] of list) { - it(`for ${scalarTypeToNameForTests(type)}`, () => { - const val = format.scalar(type, defaultVal, "fake-field-name", true, false); - expect(val).toBeDefined(); - }); - } - }); - }); - - -}); diff --git a/packages/runtime/spec/support/msg-longs-fixture.ts b/packages/runtime/spec/support/msg-longs-fixture.ts new file mode 100644 index 00000000..38a3f1b9 --- /dev/null +++ b/packages/runtime/spec/support/msg-longs-fixture.ts @@ -0,0 +1,107 @@ + +/** + * This data is generated by serializin the `spec.LongsMessage` + * using Grpc.AspNetCore v2.30.0 (C#). + * + * Each field has the appropriate minimum or maximum + * value of the native type. + */ +export const msg_longs_bytes = new Uint8Array([17, 255, 255, 255, 255, 255, 255, 255, 255, 24, 128, 128, 128, 128, 128, 128, 128, 128, 128, 1, 32, 255, 255, 255, 255, 255, 255, 255, 255, 127, 41, 0, 0, 0, 0, 0, 0, 0, 128, 49, 255, 255, 255, 255, 255, 255, 255, 127, 56, 255, 255, 255, 255, 255, 255, 255, 255, 255, 1, 64, 254, 255, 255, 255, 255, 255, 255, 255, 255, 1, 80, 255, 255, 255, 255, 255, 255, 255, 255, 255, 1, 97, 255, 255, 255, 255, 255, 255, 255, 255, 104, 128, 128, 128, 128, 128, 128, 128, 128, 128, 1, 112, 255, 255, 255, 255, 255, 255, 255, 255, 127, 121, 0, 0, 0, 0, 0, 0, 0, 128, 129, 1, 255, 255, 255, 255, 255, 255, 255, 127, 136, 1, 255, 255, 255, 255, 255, 255, 255, 255, 255, 1, 144, 1, 254, 255, 255, 255, 255, 255, 255, 255, 255, 1, 160, 1, 255, 255, 255, 255, 255, 255, 255, 255, 255, 1, 177, 1, 255, 255, 255, 255, 255, 255, 255, 255, 184, 1, 128, 128, 128, 128, 128, 128, 128, 128, 128, 1, 192, 1, 255, 255, 255, 255, 255, 255, 255, 255, 127, 201, 1, 0, 0, 0, 0, 0, 0, 0, 128, 209, 1, 255, 255, 255, 255, 255, 255, 255, 127, 216, 1, 255, 255, 255, 255, 255, 255, 255, 255, 255, 1, 224, 1, 254, 255, 255, 255, 255, 255, 255, 255, 255, 1, 240, 1, 255, 255, 255, 255, 255, 255, 255, 255, 255, 1]); + + +/** + * The file `msg-long.bin`, read by the Javascript package + * google-protobuf v3.12.2. + */ +export const msg_longs_deserialized_by_google_protobuf = { + "fixed64FieldMin": 0, + "fixed64FieldMax": 18446744073709552000, + "int64FieldMin": -9223372036854776000, + "int64FieldMax": 9223372036854776000, + "sfixed64FieldMin": -9223372036854776000, + "sfixed64FieldMax": 9223372036854776000, + "sint64FieldMin": -9223372036854776000, + "sint64FieldMax": 9223372036854776000, + "uint64FieldMin": 0, + "uint64FieldMax": 18446744073709552000, + "fixed64FieldMinStr": "0", + "fixed64FieldMaxStr": "18446744073709551615", + "int64FieldMinStr": "-9223372036854775808", + "int64FieldMaxStr": "9223372036854775807", + "sfixed64FieldMinStr": "-9223372036854775808", + "sfixed64FieldMaxStr": "9223372036854775807", + "sint64FieldMinStr": "-9223372036854775808", + "sint64FieldMaxStr": "9223372036854775807", + "uint64FieldMinStr": "0", + "uint64FieldMaxStr": "18446744073709551615", + "fixed64FieldMinNum": 0, + "fixed64FieldMaxNum": 18446744073709552000, + "int64FieldMinNum": -9223372036854776000, + "int64FieldMaxNum": 9223372036854776000, + "sfixed64FieldMinNum": -9223372036854776000, + "sfixed64FieldMaxNum": 9223372036854776000, + "sint64FieldMinNum": -9223372036854776000, + "sint64FieldMaxNum": 9223372036854776000, + "uint64FieldMinNum": 0, + "uint64FieldMaxNum": 18446744073709552000 +}; + +// msg-longs.proto: +// +// syntax = "proto3"; +// package spec; +// +// +// message LongsMessage { +// +// fixed64 fixed64_field_min = 1 [jstype = JS_NORMAL]; +// fixed64 fixed64_field_max = 2 [jstype = JS_NORMAL]; +// +// int64 int64_field_min = 3 [jstype = JS_NORMAL]; +// int64 int64_field_max = 4 [jstype = JS_NORMAL]; +// +// sfixed64 sfixed64_field_min = 5; +// sfixed64 sfixed64_field_max = 6; +// +// sint64 sint64_field_min = 7; +// sint64 sint64_field_max = 8; +// +// uint64 uint64_field_min = 9; +// uint64 uint64_field_max = 10; +// +// // +// +// fixed64 fixed64_field_min_str = 11 [jstype = JS_STRING]; +// fixed64 fixed64_field_max_str = 12 [jstype = JS_STRING]; +// +// int64 int64_field_min_str = 13 [jstype = JS_STRING]; +// int64 int64_field_max_str = 14 [jstype = JS_STRING]; +// +// sfixed64 sfixed64_field_min_str = 15 [jstype = JS_STRING]; +// sfixed64 sfixed64_field_max_str = 16 [jstype = JS_STRING]; +// +// sint64 sint64_field_min_str = 17 [jstype = JS_STRING]; +// sint64 sint64_field_max_str = 18 [jstype = JS_STRING]; +// +// uint64 uint64_field_min_str = 19 [jstype = JS_STRING]; +// uint64 uint64_field_max_str = 20 [jstype = JS_STRING]; +// +// // +// +// fixed64 fixed64_field_min_num = 21 [jstype = JS_NUMBER]; +// fixed64 fixed64_field_max_num = 22 [jstype = JS_NUMBER]; +// +// int64 int64_field_min_num = 23 [jstype = JS_NUMBER]; +// int64 int64_field_max_num = 24 [jstype = JS_NUMBER]; +// +// sfixed64 sfixed64_field_min_num = 25[jstype = JS_NUMBER]; +// sfixed64 sfixed64_field_max_num = 26 [jstype = JS_NUMBER]; +// +// sint64 sint64_field_min_num = 27 [jstype = JS_NUMBER]; +// sint64 sint64_field_max_num = 28 [jstype = JS_NUMBER]; +// +// uint64 uint64_field_min_num = 29 [jstype = JS_NUMBER]; +// uint64 uint64_field_max_num = 30 [jstype = JS_NUMBER]; +// +// } + diff --git a/packages/test-fixtures/index.ts b/packages/test-fixtures/index.ts index 97d08f48..09466e5d 100644 --- a/packages/test-fixtures/index.ts +++ b/packages/test-fixtures/index.ts @@ -5,7 +5,6 @@ import msgJsonNames from './msg-json-names.fixtures' import msgProto2Optionals from './msg-proto2-optionals.fixtures' import msgProto3Optionals from './msg-proto3-optionals.fixtures' import msgMessage from './msg-message.fixtures' -import msgLongs from './msg-longs.fixtures' import deprecationExplicit from './deprecation-explicit.fixture' import deprecationImplicit from './deprecation-implicit.fixture' import {FieldInfo, MessageInfo, normalizeFieldInfo, PartialFieldInfo} from "@protobuf-ts/runtime"; @@ -554,7 +553,6 @@ fixtures.register(msgJsonNames); fixtures.register(msgProto2Optionals); fixtures.register(msgProto3Optionals); fixtures.register(msgMessage); -fixtures.register(msgLongs); fixtures.register(deprecationExplicit); fixtures.register(deprecationImplicit); diff --git a/packages/test-fixtures/msg-longs.bin b/packages/test-fixtures/msg-longs.bin deleted file mode 100644 index 751438db..00000000 Binary files a/packages/test-fixtures/msg-longs.bin and /dev/null differ diff --git a/packages/test-fixtures/msg-longs.fixtures.ts b/packages/test-fixtures/msg-longs.fixtures.ts deleted file mode 100644 index 8b63c086..00000000 --- a/packages/test-fixtures/msg-longs.fixtures.ts +++ /dev/null @@ -1,208 +0,0 @@ -import type {Fixture} from "./index"; - -/** - * Bytes of the file `msg-longs.bin` - * - * This data is generated using the `spec.LongsMessage` - * using Grpc.AspNetCore v2.30.0 (C#). - * - * Each field has the appropriate minimum or maximum - * value of the native type. - */ -export const msg_longs_bytes = new Uint8Array([17, 255, 255, 255, 255, 255, 255, 255, 255, 24, 128, 128, 128, 128, 128, 128, 128, 128, 128, 1, 32, 255, 255, 255, 255, 255, 255, 255, 255, 127, 41, 0, 0, 0, 0, 0, 0, 0, 128, 49, 255, 255, 255, 255, 255, 255, 255, 127, 56, 255, 255, 255, 255, 255, 255, 255, 255, 255, 1, 64, 254, 255, 255, 255, 255, 255, 255, 255, 255, 1, 80, 255, 255, 255, 255, 255, 255, 255, 255, 255, 1, 97, 255, 255, 255, 255, 255, 255, 255, 255, 104, 128, 128, 128, 128, 128, 128, 128, 128, 128, 1, 112, 255, 255, 255, 255, 255, 255, 255, 255, 127, 121, 0, 0, 0, 0, 0, 0, 0, 128, 129, 1, 255, 255, 255, 255, 255, 255, 255, 127, 136, 1, 255, 255, 255, 255, 255, 255, 255, 255, 255, 1, 144, 1, 254, 255, 255, 255, 255, 255, 255, 255, 255, 1, 160, 1, 255, 255, 255, 255, 255, 255, 255, 255, 255, 1, 177, 1, 255, 255, 255, 255, 255, 255, 255, 255, 184, 1, 128, 128, 128, 128, 128, 128, 128, 128, 128, 1, 192, 1, 255, 255, 255, 255, 255, 255, 255, 255, 127, 201, 1, 0, 0, 0, 0, 0, 0, 0, 128, 209, 1, 255, 255, 255, 255, 255, 255, 255, 127, 216, 1, 255, 255, 255, 255, 255, 255, 255, 255, 255, 1, 224, 1, 254, 255, 255, 255, 255, 255, 255, 255, 255, 1, 240, 1, 255, 255, 255, 255, 255, 255, 255, 255, 255, 1]); - -/** - * The file `msg-long.bin`, read by Grpc.AspNetCore v2.30.0 (C#) - * and printed in canonical JSON format. - */ -export const msg_long_json = { - "fixed64FieldMax": "18446744073709551615", - "int64FieldMin": "-9223372036854775808", - "int64FieldMax": "9223372036854775807", - "sfixed64FieldMin": "-9223372036854775808", - "sfixed64FieldMax": "9223372036854775807", - "sint64FieldMin": "-9223372036854775808", - "sint64FieldMax": "9223372036854775807", - "uint64FieldMax": "18446744073709551615", - "fixed64FieldMaxStr": "18446744073709551615", - "int64FieldMinStr": "-9223372036854775808", - "int64FieldMaxStr": "9223372036854775807", - "sfixed64FieldMinStr": "-9223372036854775808", - "sfixed64FieldMaxStr": "9223372036854775807", - "sint64FieldMinStr": "-9223372036854775808", - "fixed64FieldMaxNum": "18446744073709551615", - "int64FieldMinNum": "-9223372036854775808", - "int64FieldMaxNum": "9223372036854775807", - "sfixed64FieldMinNum": "-9223372036854775808", - "sfixed64FieldMaxNum": "9223372036854775807", - "sint64FieldMinNum": "-9223372036854775808", - "sint64FieldMaxStr": "9223372036854775807", - "uint64FieldMaxStr": "18446744073709551615", - "sint64FieldMaxNum": "9223372036854775807", - "uint64FieldMaxNum": "18446744073709551615" -}; - -/** - * The file `msg-long.bin`, read by the Javascript package - * google-protobuf v3.12.2. - */ -export const msg_longs_deserialized_by_google_protobuf = { - "fixed64FieldMin": 0, - "fixed64FieldMax": 18446744073709552000, - "int64FieldMin": -9223372036854776000, - "int64FieldMax": 9223372036854776000, - "sfixed64FieldMin": -9223372036854776000, - "sfixed64FieldMax": 9223372036854776000, - "sint64FieldMin": -9223372036854776000, - "sint64FieldMax": 9223372036854776000, - "uint64FieldMin": 0, - "uint64FieldMax": 18446744073709552000, - "fixed64FieldMinStr": "0", - "fixed64FieldMaxStr": "18446744073709551615", - "int64FieldMinStr": "-9223372036854775808", - "int64FieldMaxStr": "9223372036854775807", - "sfixed64FieldMinStr": "-9223372036854775808", - "sfixed64FieldMaxStr": "9223372036854775807", - "sint64FieldMinStr": "-9223372036854775808", - "sint64FieldMaxStr": "9223372036854775807", - "uint64FieldMinStr": "0", - "uint64FieldMaxStr": "18446744073709551615", - "fixed64FieldMinNum": 0, - "fixed64FieldMaxNum": 18446744073709552000, - "int64FieldMinNum": -9223372036854776000, - "int64FieldMaxNum": 9223372036854776000, - "sfixed64FieldMinNum": -9223372036854776000, - "sfixed64FieldMaxNum": 9223372036854776000, - "sint64FieldMinNum": -9223372036854776000, - "sint64FieldMaxNum": 9223372036854776000, - "uint64FieldMinNum": 0, - "uint64FieldMaxNum": 18446744073709552000 -}; - - -const f: Fixture[] = []; -export default f; - -f.push({ - typeName: "spec.LongsMessage", - fields: [ - {no: 1, name: "fixed64_field_min", kind: "scalar", T: 6 /* FIXED64 */}, - {no: 2, name: "fixed64_field_max", kind: "scalar", T: 6 /* FIXED64 */}, - {no: 3, name: "int64_field_min", kind: "scalar", T: 3 /* INT64 */}, - {no: 4, name: "int64_field_max", kind: "scalar", T: 3 /* INT64 */}, - {no: 5, name: "sfixed64_field_min", kind: "scalar", T: 16 /* SFIXED64 */}, - {no: 6, name: "sfixed64_field_max", kind: "scalar", T: 16 /* SFIXED64 */}, - {no: 7, name: "sint64_field_min", kind: "scalar", T: 18 /* SINT64 */}, - {no: 8, name: "sint64_field_max", kind: "scalar", T: 18 /* SINT64 */}, - {no: 9, name: "uint64_field_min", kind: "scalar", T: 4 /* UINT64 */}, - {no: 10, name: "uint64_field_max", kind: "scalar", T: 4 /* UINT64 */}, - {no: 11, name: "fixed64_field_min_str", kind: "scalar", T: 6 /* FIXED64 */}, - {no: 12, name: "fixed64_field_max_str", kind: "scalar", T: 6 /* FIXED64 */}, - {no: 13, name: "int64_field_min_str", kind: "scalar", T: 3 /* INT64 */}, - {no: 14, name: "int64_field_max_str", kind: "scalar", T: 3 /* INT64 */}, - {no: 15, name: "sfixed64_field_min_str", kind: "scalar", T: 16 /* SFIXED64 */}, - {no: 16, name: "sfixed64_field_max_str", kind: "scalar", T: 16 /* SFIXED64 */}, - {no: 17, name: "sint64_field_min_str", kind: "scalar", T: 18 /* SINT64 */}, - {no: 18, name: "sint64_field_max_str", kind: "scalar", T: 18 /* SINT64 */}, - {no: 19, name: "uint64_field_min_str", kind: "scalar", T: 4 /* UINT64 */}, - {no: 20, name: "uint64_field_max_str", kind: "scalar", T: 4 /* UINT64 */}, - {no: 21, name: "fixed64_field_min_num", kind: "scalar", T: 6 /* FIXED64 */, L: 2 /* NUMBER */}, - {no: 22, name: "fixed64_field_max_num", kind: "scalar", T: 6 /* FIXED64 */, L: 2 /* NUMBER */}, - {no: 23, name: "int64_field_min_num", kind: "scalar", T: 3 /* INT64 */, L: 2 /* NUMBER */}, - {no: 24, name: "int64_field_max_num", kind: "scalar", T: 3 /* INT64 */, L: 2 /* NUMBER */}, - {no: 25, name: "sfixed64_field_min_num", kind: "scalar", T: 16 /* SFIXED64 */, L: 2 /* NUMBER */}, - {no: 26, name: "sfixed64_field_max_num", kind: "scalar", T: 16 /* SFIXED64 */, L: 2 /* NUMBER */}, - {no: 27, name: "sint64_field_min_num", kind: "scalar", T: 18 /* SINT64 */, L: 2 /* NUMBER */}, - {no: 28, name: "sint64_field_max_num", kind: "scalar", T: 18 /* SINT64 */, L: 2 /* NUMBER */}, - {no: 29, name: "uint64_field_min_num", kind: "scalar", T: 4 /* UINT64 */, L: 2 /* NUMBER */}, - {no: 30, name: "uint64_field_max_num", kind: "scalar", T: 4 /* UINT64 */, L: 2 /* NUMBER */} - ], - messages: { - "default": { - fixed64FieldMin: '0', - fixed64FieldMax: '0', - int64FieldMin: '0', - int64FieldMax: '0', - sfixed64FieldMin: '0', - sfixed64FieldMax: '0', - sint64FieldMin: '0', - sint64FieldMax: '0', - uint64FieldMin: '0', - uint64FieldMax: '0', - fixed64FieldMinStr: '0', - fixed64FieldMaxStr: '0', - int64FieldMinStr: '0', - int64FieldMaxStr: '0', - sfixed64FieldMinStr: '0', - sfixed64FieldMaxStr: '0', - sint64FieldMinStr: '0', - sint64FieldMaxStr: '0', - uint64FieldMinStr: '0', - uint64FieldMaxStr: '0', - fixed64FieldMinNum: 0, - fixed64FieldMaxNum: 0, - int64FieldMinNum: 0, - int64FieldMaxNum: 0, - sfixed64FieldMinNum: 0, - sfixed64FieldMaxNum: 0, - sint64FieldMinNum: 0, - sint64FieldMaxNum: 0, - uint64FieldMinNum: 0, - uint64FieldMaxNum: 0, - }, - "example": { - "fixed64FieldMin": "0", - "fixed64FieldMax": "18446744073709551615", - "int64FieldMin": "-9223372036854775808", - "int64FieldMax": "9223372036854775807", - "sfixed64FieldMin": "-9223372036854775808", - "sfixed64FieldMax": "9223372036854775807", - "sint64FieldMin": "-9223372036854775808", - "sint64FieldMax": "9223372036854775807", - "uint64FieldMin": "0", - "uint64FieldMax": "18446744073709551615", - "fixed64FieldMinStr": "0", - "fixed64FieldMaxStr": "18446744073709551615", - "int64FieldMinStr": "-9223372036854775808", - "int64FieldMaxStr": "9223372036854775807", - "sfixed64FieldMinStr": "-9223372036854775808", - "sfixed64FieldMaxStr": "9223372036854775807", - "sint64FieldMinStr": "-9223372036854775808", - "sint64FieldMaxStr": "9223372036854775807", - "uint64FieldMinStr": "0", - "uint64FieldMaxStr": "18446744073709551615", - fixed64FieldMinNum: 0, - fixed64FieldMaxNum: 0, - int64FieldMinNum: 0, - int64FieldMaxNum: 0, - sfixed64FieldMinNum: 0, - sfixed64FieldMaxNum: 0, - sint64FieldMinNum: 0, - sint64FieldMaxNum: 0, - uint64FieldMinNum: 0, - uint64FieldMaxNum: 0, - }, - }, - json: { - "default": {}, - "example": { - // number representations explicitly excluded because precision loss will fail unit tests - "fixed64FieldMax": "18446744073709551615", - "int64FieldMin": "-9223372036854775808", - "int64FieldMax": "9223372036854775807", - "sfixed64FieldMin": "-9223372036854775808", - "sfixed64FieldMax": "9223372036854775807", - "sint64FieldMin": "-9223372036854775808", - "sint64FieldMax": "9223372036854775807", - "uint64FieldMax": "18446744073709551615", - "fixed64FieldMaxStr": "18446744073709551615", - "int64FieldMinStr": "-9223372036854775808", - "int64FieldMaxStr": "9223372036854775807", - "sfixed64FieldMinStr": "-9223372036854775808", - "sfixed64FieldMaxStr": "9223372036854775807", - "sint64FieldMinStr": "-9223372036854775808", - "sint64FieldMaxStr": "9223372036854775807", - "uint64FieldMaxStr": "18446744073709551615" - }, - }, -}); diff --git a/packages/test-generated/spec/generated-binary-read-compat.spec.ts b/packages/test-generated/spec/generated-binary-read-compat.spec.ts index 0a39672f..375dfc3f 100644 --- a/packages/test-generated/spec/generated-binary-read-compat.spec.ts +++ b/packages/test-generated/spec/generated-binary-read-compat.spec.ts @@ -1,4 +1,3 @@ -import {fixtures} from "../../test-fixtures"; import {IMessageType, MessageType} from "@protobuf-ts/runtime"; import {EnumFieldMessage} from "../ts-out/msg-enum"; import {JsonNamesMessage} from "../ts-out/msg-json-names"; @@ -29,53 +28,15 @@ describe('generated code compatibility', () => { ); }); - describe('generated create() produces same data as reflection', function () { - fixtures.usingTypeNames((typeName) => { - let generatedType = generatedRegistry.find(t => t.typeName === typeName); - if (!generatedType) - return; - - it(`${typeName}`, function () { - let reflectionType = new MessageType(generatedType!.typeName, generatedType!.fields); + describe('generated create()', function () { + for (const generatedType of generatedRegistry) { + it(`should have same result as reflection for ${generatedType.typeName}`, function () { + const reflectionType = new MessageType(generatedType.typeName, generatedType.fields); let reflectionMsg = reflectionType.create(); - let generatedMsg = generatedType!.create(); + let generatedMsg = generatedType.create(); expect(generatedMsg).toEqual(reflectionMsg); }); - }); - }); - - describe('generated toBinary() produces same data as reflection', function () { - // using json because fixture data uses LongType.STRING, but generated code also uses LongType.BIGINT - fixtures.usingJson((typeName, key, json) => { - let generatedType = generatedRegistry.find(t => t.typeName === typeName); - if (!generatedType) - return; - - it(`${typeName} '${key}'`, function () { - let reflectionType = new MessageType(generatedType!.typeName, generatedType!.fields); - let msg = reflectionType.fromJson(json); - let reflectionBytes = reflectionType.toBinary(msg); - let generatedBytes = generatedType!.toBinary(msg); - expect(generatedBytes).toEqual(reflectionBytes); - }); - }); - }); - - - describe('generated fromBinary() produces same data as reflection', function () { - // using json because fixture data uses LongType.STRING, but generated code also uses LongType.BIGINT - fixtures.usingJson((typeName, key, json) => { - let generatedType = generatedRegistry.find(t => t.typeName === typeName); - if (!generatedType) - return; - it(`${typeName} '${key}'`, function () { - let reflectionType = new MessageType(generatedType!.typeName, generatedType!.fields); - let msg = reflectionType.fromJson(json); - let bytes = reflectionType.toBinary(msg); - let msgRead = generatedType!.fromBinary(bytes); - expect(msgRead).toEqual(msg); - }); - }); + } }); }); diff --git a/packages/test-generated/spec/generated-message-info.spec.ts b/packages/test-generated/spec/generated-message-info.spec.ts deleted file mode 100644 index 7e1885bb..00000000 --- a/packages/test-generated/spec/generated-message-info.spec.ts +++ /dev/null @@ -1,61 +0,0 @@ -import {fixtures} from "../../test-fixtures"; -import type {IMessageType} from "@protobuf-ts/runtime"; -import {EnumFieldMessage} from "../ts-out/msg-enum"; -import {JsonNamesMessage} from "../ts-out/msg-json-names"; -import {MessageFieldMessage} from "../ts-out/msg-message"; -import {OneofMessageMemberMessage, OneofScalarMemberMessage} from "../ts-out/msg-oneofs"; -import {Proto2OptionalsMessage} from "../ts-out/msg-proto2-optionals"; -import {Proto3OptionalsMessage} from "../ts-out/msg-proto3-optionals"; -import {DeprecatedFieldMessage, DeprecatedMessage} from "../ts-out/deprecation-explicit"; -import {EnumMapMessage, MessageMapMessage, ScalarMapsMessage} from "../ts-out/msg-maps"; -import {ImplicitlyDeprecatedMessage} from "../ts-out/deprecation-implicit"; - -let generatedRegistry: IMessageType[] = [ - DeprecatedMessage, - JsonNamesMessage, - EnumMapMessage, - MessageMapMessage, - Proto2OptionalsMessage, - OneofMessageMemberMessage, - DeprecatedFieldMessage, - OneofScalarMemberMessage, - ScalarMapsMessage, - EnumFieldMessage, - MessageFieldMessage, - Proto3OptionalsMessage, - ImplicitlyDeprecatedMessage - // will have field info L set if generated with bigints, cant test - // RepeatedScalarValuesMessage, - // LongsMessage, - // ScalarValuesMessage, -]; - - -describe('generated field info equals fixture field infos', function () { - - - beforeEach(function () { - jasmine.addCustomEqualityTester((a, b) => { - if (typeof a !== "function" || typeof b !== "function") { - return undefined; - } - // we ignore T: () => IMessageType, EnumInfo - return true; - }); - }); - - - fixtures.usingTypeNames((typeName) => { - let generatedType = generatedRegistry.find(t => t.typeName === typeName); - if (!generatedType) - return; - it(`${typeName}`, function () { - let generatedFields = generatedType!.fields; - let fixtureFields = fixtures.makeMessageInfo(typeName).fields; - expect(generatedFields).toEqual(fixtureFields); - }); - - }); -}); - -