Skip to content

Commit

Permalink
feat: support remaining type docs (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerAberbach authored Dec 25, 2024
1 parent 8906560 commit 653f153
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 = (
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -373,7 +378,11 @@ const convertUnion = (
),
)
return union.name
? referenceArbitrary({ name: union.name, arbitrary })
? referenceArbitrary({
name: union.name,
comment: getDoc(program, union),
arbitrary,
})
: arbitrary
}

Expand Down
39 changes: 39 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand Down
35 changes: 35 additions & 0 deletions test/snapshots/comments/arbitraries.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
42 changes: 42 additions & 0 deletions test/snapshots/comments/samples.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
export const samples = {
'MultiLineEnum': [
'A',
'A',
'A',
'A',
'A'
],
'MultiLineNamespace': {
'MultiLineNestedNamespace': {},
'MultiLineNamespaceModel': [
Expand Down Expand Up @@ -55,13 +62,34 @@ export const samples = {
}
}
],
'MultiLineScalar': [
'',
'W|%=2Spc',
'X1DZwS',
'gp',
''
],
'MultiLineSharedModel': [
{},
{},
{},
{},
{}
],
'MultiLineUnion': [
-2,
false,
-11,
'',
542422934
],
'SingleLineEnum': [
'A',
'A',
'A',
'A',
'A'
],
'SingleLineNamespace': {
'SingleLineNestedNamespace': {},
'SingleLineNamespaceModel': [
Expand Down Expand Up @@ -118,11 +146,25 @@ export const samples = {
}
}
],
'SingleLineScalar': [
'',
'W|%=2Spc',
'X1DZwS',
'gp',
''
],
'SingleLineSharedModel': [
{},
{},
{},
{},
{}
],
'SingleLineUnion': [
-2,
false,
-11,
'',
542422934
]
};

0 comments on commit 653f153

Please sign in to comment.