Skip to content

Commit

Permalink
feat: null intrinsic
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerAberbach committed Dec 7, 2024
1 parent 31ebbf4 commit 4d5f246
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/arbitrary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type Arbitrary = BaseArbitrary &
| BytesArbitrary
| StringArbitrary
| BooleanArbitrary
| NullArbitrary
)

export type BaseArbitrary = {
Expand Down Expand Up @@ -75,3 +76,7 @@ export type StringArbitrary = {
export type BooleanArbitrary = {
type: `boolean`
}

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

Expand Down Expand Up @@ -297,6 +299,8 @@ const StringArbitrary = ({

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

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

const Options = ({
properties,
emitEmpty = false,
Expand Down
21 changes: 21 additions & 0 deletions src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from '@typespec/compiler'
import type {
Enum,
IntrinsicType,
Model,
Namespace,
Numeric,
Expand Down Expand Up @@ -101,6 +102,8 @@ const convertType = (
return convertEnum(type, options)
case `Scalar`:
return convertScalar(program, type, options)
case `Intrinsic`:
return convertIntrinsic(type)
}

throw new Error(`Unhandled type: ${type.kind}`)
Expand Down Expand Up @@ -250,6 +253,21 @@ const convertString = (
const convertBoolean = (boolean: Scalar): Arbitrary =>
memoize({ type: `boolean`, name: boolean.name })

const convertIntrinsic = (intrinsic: IntrinsicType): Arbitrary => {
switch (intrinsic.name) {
case `null`:
return convertNull(intrinsic)
case `ErrorType`:
case `void`:
case `never`:
case `unknown`:
throw new Error(`Unhandled Intrinsic: ${intrinsic.name}`)
}
}

const convertNull = ($null: IntrinsicType): Arbitrary =>
memoize({ type: `null`, name: $null.name })

const getConstraints = (program: Program, type: Type): Constraints =>
pipe(
entries({
Expand Down Expand Up @@ -324,6 +342,8 @@ const getArbitraryKey = (arbitrary: Arbitrary): ArbitraryKey => {
])
case `boolean`:
return keyalesce([arbitrary.type, arbitrary.name])
case `null`:
return keyalesce([arbitrary.type, arbitrary.name])
}
}

Expand Down Expand Up @@ -410,6 +430,7 @@ const getDirectArbitraryDependencies = (
case `bytes`:
case `string`:
case `boolean`:
case `null`:
return new Set()
}
}
Expand Down
8 changes: 8 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,14 @@ test.each([
scalar Boolean extends boolean;
`,
},
{
name: `null`,
code: `
model M {
property: null
}
`,
},

// Models
{
Expand Down
5 changes: 5 additions & 0 deletions test/snapshots/null.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(null),
});

0 comments on commit 4d5f246

Please sign in to comment.