Skip to content

Commit

Permalink
feat: unknown type
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerAberbach committed Dec 7, 2024
1 parent c6ed079 commit 0e4d433
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/arbitrary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type Arbitrary = BaseArbitrary &
| NullArbitrary
| UndefinedArbitrary
| NeverArbitrary
| UnknownArbitrary
| BooleanArbitrary
| NumberArbitrary
| BigIntArbitrary
Expand Down Expand Up @@ -38,6 +39,10 @@ export type NeverArbitrary = {
type: `never`
}

export type UnknownArbitrary = {
type: `unknown`
}

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 @@ -144,6 +144,8 @@ const ArbitraryDefinition = ({
return UndefinedArbitrary()
case `never`:
return NeverArbitrary()
case `unknown`:
return UnknownArbitrary()
case `boolean`:
return BooleanArbitrary()
case `number`:
Expand Down Expand Up @@ -177,6 +179,8 @@ const NeverArbitrary = (): Child => code`
})
`

const UnknownArbitrary = (): Child => code`fc.anything()`

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

const NumberArbitrary = ({
Expand Down
9 changes: 8 additions & 1 deletion src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,9 @@ const convertIntrinsic = (intrinsic: IntrinsicType): Arbitrary => {
return convertVoid(intrinsic)
case `never`:
return convertNever(intrinsic)
case `ErrorType`:
case `unknown`:
return convertUnknown(intrinsic)
case `ErrorType`:
throw new Error(`Unhandled Intrinsic: ${intrinsic.name}`)
}
}
Expand All @@ -136,6 +137,9 @@ const convertVoid = ($void: IntrinsicType): Arbitrary =>
const convertNever = (never: IntrinsicType): Arbitrary =>
memoize({ type: `never`, name: never.name })

const convertUnknown = (unknown: IntrinsicType): Arbitrary =>
memoize({ type: `unknown`, name: unknown.name })

const convertScalar = (
program: Program,
scalar: Scalar,
Expand Down Expand Up @@ -312,6 +316,8 @@ const getArbitraryKey = (arbitrary: Arbitrary): ArbitraryKey => {
return keyalesce([arbitrary.type, arbitrary.name])
case `never`:
return keyalesce([arbitrary.type, arbitrary.name])
case `unknown`:
return keyalesce([arbitrary.type, arbitrary.name])
case `boolean`:
return keyalesce([arbitrary.type, arbitrary.name])
case `number`:
Expand Down Expand Up @@ -431,6 +437,7 @@ const getDirectArbitraryDependencies = (
case `null`:
case `undefined`:
case `never`:
case `unknown`:
case `boolean`:
case `number`:
case `bigint`:
Expand Down
8 changes: 8 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ test.each([
}
`,
},
{
name: `unknown`,
code: `
model M {
property: unknown
}
`,
},
{
name: `boolean`,
code: `
Expand Down
5 changes: 5 additions & 0 deletions test/snapshots/unknown/arbitraries.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.anything(),
});
38 changes: 38 additions & 0 deletions test/snapshots/unknown/samples.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
export const samples = {
'M': [
{
'property': [
null
]
},
{
'property': {
'2Spc0sZ': [
'%.%}ayW&;'
],
'A$(;2O': [],
'MU6\'': {
'^bu': '',
'B.gE': false
}
}
},
{
'property': [
' ',
[
true,
{
'"': 'n'
}
]
]
},
{
'property': 'g'
},
{
'property': []
}
]
};

0 comments on commit 0e4d433

Please sign in to comment.