Skip to content

Commit

Permalink
feat: void type
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerAberbach committed Dec 7, 2024
1 parent 4018e85 commit 6a5231f
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 13 deletions.
5 changes: 5 additions & 0 deletions src/arbitrary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type ArbitraryNamespace = {
export type Arbitrary = BaseArbitrary &
(
| NullArbitrary
| UndefinedArbitrary
| BooleanArbitrary
| NumberArbitrary
| BigIntArbitrary
Expand All @@ -28,6 +29,10 @@ export type NullArbitrary = {
type: `null`
}

export type UndefinedArbitrary = {
type: `undefined`
}

export type BooleanArbitrary = {
type: `boolean`
}
Expand Down
4 changes: 4 additions & 0 deletions src/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ const ArbitraryDefinition = ({
switch (arbitrary.type) {
case `null`:
return NullArbitrary()
case `undefined`:
return UndefinedArbitrary()
case `boolean`:
return BooleanArbitrary()
case `number`:
Expand All @@ -165,6 +167,8 @@ const ArbitraryDefinition = ({

const NullArbitrary = (): Child => code`fc.constant(null)`

const UndefinedArbitrary = (): Child => code`fc.constant(undefined)`

const BooleanArbitrary = (): Child => code`fc.boolean()`

const NumberArbitrary = ({
Expand Down
33 changes: 20 additions & 13 deletions src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,9 @@ const convertIntrinsic = (intrinsic: IntrinsicType): Arbitrary => {
switch (intrinsic.name) {
case `null`:
return convertNull(intrinsic)
case `ErrorType`:
case `void`:
return convertVoid(intrinsic)
case `ErrorType`:
case `never`:
case `unknown`:
throw new Error(`Unhandled Intrinsic: ${intrinsic.name}`)
Expand All @@ -128,6 +129,9 @@ const convertIntrinsic = (intrinsic: IntrinsicType): Arbitrary => {
const convertNull = ($null: IntrinsicType): Arbitrary =>
memoize({ type: `null`, name: $null.name })

const convertVoid = ($void: IntrinsicType): Arbitrary =>
memoize({ type: `undefined`, name: $void.name })

const convertScalar = (
program: Program,
scalar: Scalar,
Expand Down Expand Up @@ -300,6 +304,8 @@ const getArbitraryKey = (arbitrary: Arbitrary): ArbitraryKey => {
switch (arbitrary.type) {
case `null`:
return keyalesce([arbitrary.type, arbitrary.name])
case `undefined`:
return keyalesce([arbitrary.type, arbitrary.name])
case `boolean`:
return keyalesce([arbitrary.type, arbitrary.name])
case `number`:
Expand Down Expand Up @@ -416,22 +422,23 @@ const getDirectArbitraryDependencies = (
arbitrary: Arbitrary,
): Set<Arbitrary> => {
switch (arbitrary.type) {
case `record`:
return new Set(values(arbitrary.properties))
case `dictionary`:
return new Set([arbitrary.key, arbitrary.value])
case `array`:
return new Set([arbitrary.value])
case `union`:
return new Set(arbitrary.variants)
case `enum`:
case `null`:
case `undefined`:
case `boolean`:
case `number`:
case `bigint`:
case `bytes`:
case `string`:
case `boolean`:
case `null`:
case `bytes`:
case `enum`:
return new Set()
case `array`:
return new Set([arbitrary.value])
case `dictionary`:
return new Set([arbitrary.key, arbitrary.value])
case `union`:
return new Set(arbitrary.variants)
case `record`:
return new Set(values(arbitrary.properties))
}
}

Expand Down
8 changes: 8 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ test.each([
}
`,
},
{
name: `void`,
code: `
model M {
property: void
}
`,
},
{
name: `boolean`,
code: `
Expand Down
5 changes: 5 additions & 0 deletions test/snapshots/void.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as fc from 'fast-check'

export const M = fc.record({
property: fc.constant(undefined),
});

0 comments on commit 6a5231f

Please sign in to comment.