diff --git a/src/convert.ts b/src/convert.ts index 0f94287..854ef33 100644 --- a/src/convert.ts +++ b/src/convert.ts @@ -153,7 +153,7 @@ const convertType = ( arbitrary = convertScalar(program, type, constraints) break case `Enum`: - arbitrary = convertEnum(type) + arbitrary = convertEnum(program, type) break case `Tuple`: arbitrary = convertTuple(program, type, constraints) @@ -272,7 +272,11 @@ const convertScalar = ( return isTypeSpecNamespace(scalar.namespace) ? arbitrary - : referenceArbitrary({ name: scalar.name, arbitrary }) + : referenceArbitrary({ + name: scalar.name, + comment: getDoc(program, scalar), + arbitrary, + }) } const convertNumber = ( @@ -333,9 +337,10 @@ const convertString = (constraints: Constraints): StringArbitrary => maxLength: constraints.maxLength?.asNumber() ?? undefined, }) -const convertEnum = ($enum: Enum): Arbitrary => +const convertEnum = (program: Program, $enum: Enum): Arbitrary => referenceArbitrary({ name: $enum.name, + comment: getDoc(program, $enum), arbitrary: enumArbitrary( pipe( $enum.members, @@ -373,7 +378,11 @@ const convertUnion = ( ), ) return union.name - ? referenceArbitrary({ name: union.name, arbitrary }) + ? referenceArbitrary({ + name: union.name, + comment: getDoc(program, union), + arbitrary, + }) : arbitrary } diff --git a/test/index.ts b/test/index.ts index 5bc95d3..062c41c 100644 --- a/test/index.ts +++ b/test/index.ts @@ -743,6 +743,45 @@ test.each([ { name: `comments`, code: ` + /** Scalar comment. */ + scalar SingleLineScalar extends string; + + /** + * Scalar + * comment. + */ + scalar MultiLineScalar extends string; + + /** Enum comment. */ + enum SingleLineEnum { + A + } + + /** + * Enum + * comment. + */ + enum MultiLineEnum { + A + } + + /** Union comment. */ + union SingleLineUnion { + string: string, + int32: int32, + boolean: boolean + } + + /** + * Union + * comment. + */ + union MultiLineUnion { + string: string, + int32: int32, + boolean: boolean + } + /** Shared model comment. */ model SingleLineSharedModel {} diff --git a/test/snapshots/comments/arbitraries.js b/test/snapshots/comments/arbitraries.js index 97ceeb8..6020e9a 100644 --- a/test/snapshots/comments/arbitraries.js +++ b/test/snapshots/comments/arbitraries.js @@ -83,3 +83,38 @@ export const MultiLineNonSharedModel = fc.record({ */ property: MultiLineSharedModel, }); + +/** Union comment. */ +export const SingleLineUnion = fc.oneof( + fc.string(), + fc.integer(), + fc.boolean(), +); + +/** + * Union + * comment. + */ +export const MultiLineUnion = fc.oneof( + fc.string(), + fc.integer(), + fc.boolean(), +); + +/** Enum comment. */ +export const SingleLineEnum = fc.constantFrom("A"); + +/** + * Enum + * comment. + */ +export const MultiLineEnum = fc.constantFrom("A"); + +/** Scalar comment. */ +export const SingleLineScalar = fc.string(); + +/** + * Scalar + * comment. + */ +export const MultiLineScalar = fc.string(); diff --git a/test/snapshots/comments/samples.js b/test/snapshots/comments/samples.js index 2599a8c..c3030fe 100644 --- a/test/snapshots/comments/samples.js +++ b/test/snapshots/comments/samples.js @@ -1,4 +1,11 @@ export const samples = { + 'MultiLineEnum': [ + 'A', + 'A', + 'A', + 'A', + 'A' + ], 'MultiLineNamespace': { 'MultiLineNestedNamespace': {}, 'MultiLineNamespaceModel': [ @@ -55,6 +62,13 @@ export const samples = { } } ], + 'MultiLineScalar': [ + '', + 'W|%=2Spc', + 'X1DZwS', + 'gp', + '' + ], 'MultiLineSharedModel': [ {}, {}, @@ -62,6 +76,20 @@ export const samples = { {}, {} ], + 'MultiLineUnion': [ + -2, + false, + -11, + '', + 542422934 + ], + 'SingleLineEnum': [ + 'A', + 'A', + 'A', + 'A', + 'A' + ], 'SingleLineNamespace': { 'SingleLineNestedNamespace': {}, 'SingleLineNamespaceModel': [ @@ -118,11 +146,25 @@ export const samples = { } } ], + 'SingleLineScalar': [ + '', + 'W|%=2Spc', + 'X1DZwS', + 'gp', + '' + ], 'SingleLineSharedModel': [ {}, {}, {}, {}, {} + ], + 'SingleLineUnion': [ + -2, + false, + -11, + '', + 542422934 ] };