Skip to content

Commit

Permalink
feat: never type (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerAberbach authored Dec 7, 2024
1 parent c34b393 commit 6195f97
Show file tree
Hide file tree
Showing 31 changed files with 65 additions and 26 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"@vitest/coverage-v8": "^2.1.8",
"cross-env": "^7.0.3",
"eslint": "^9.16.0",
"fast-check": "^3.23.1",
"prettier": "^3.4.1",
"source-map-support": "^0.5.21",
"tomer": "^6.1.1",
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/arbitrary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type Arbitrary = BaseArbitrary &
(
| NullArbitrary
| UndefinedArbitrary
| NeverArbitrary
| BooleanArbitrary
| NumberArbitrary
| BigIntArbitrary
Expand All @@ -33,6 +34,10 @@ export type UndefinedArbitrary = {
type: `undefined`
}

export type NeverArbitrary = {
type: `never`
}

export type BooleanArbitrary = {
type: `boolean`
}
Expand Down
10 changes: 9 additions & 1 deletion src/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const ArbitraryFile = ({
sharedArbitraries: ReadonlySet<Arbitrary>
}): Child =>
ay.Output().children(ts.SourceFile({ path: `arbitraries.js` }).code`
import * as fc from 'fast-check'
import * as fc from 'fast-check';
${GlobalArbitraryNamespace({ namespace, sharedArbitraries })}
`)
Expand Down Expand Up @@ -142,6 +142,8 @@ const ArbitraryDefinition = ({
return NullArbitrary()
case `undefined`:
return UndefinedArbitrary()
case `never`:
return NeverArbitrary()
case `boolean`:
return BooleanArbitrary()
case `number`:
Expand Down Expand Up @@ -169,6 +171,12 @@ const NullArbitrary = (): Child => code`fc.constant(null)`

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

const NeverArbitrary = (): Child => code`
fc.constant(null).map(() => {
throw new Error('never');
})
`

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 @@ -119,8 +119,9 @@ const convertIntrinsic = (intrinsic: IntrinsicType): Arbitrary => {
return convertNull(intrinsic)
case `void`:
return convertVoid(intrinsic)
case `ErrorType`:
case `never`:
return convertNever(intrinsic)
case `ErrorType`:
case `unknown`:
throw new Error(`Unhandled Intrinsic: ${intrinsic.name}`)
}
Expand All @@ -132,6 +133,9 @@ const convertNull = ($null: IntrinsicType): Arbitrary =>
const convertVoid = ($void: IntrinsicType): Arbitrary =>
memoize({ type: `undefined`, name: $void.name })

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

const convertScalar = (
program: Program,
scalar: Scalar,
Expand Down Expand Up @@ -306,6 +310,8 @@ const getArbitraryKey = (arbitrary: Arbitrary): ArbitraryKey => {
return keyalesce([arbitrary.type, arbitrary.name])
case `undefined`:
return keyalesce([arbitrary.type, arbitrary.name])
case `never`:
return keyalesce([arbitrary.type, arbitrary.name])
case `boolean`:
return keyalesce([arbitrary.type, arbitrary.name])
case `number`:
Expand Down Expand Up @@ -424,6 +430,7 @@ const getDirectArbitraryDependencies = (
switch (arbitrary.type) {
case `null`:
case `undefined`:
case `never`:
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 @@ -78,6 +78,14 @@ test.each([
}
`,
},
{
name: `never`,
code: `
model M {
property: never
}
`,
},
{
name: `boolean`,
code: `
Expand Down
2 changes: 1 addition & 1 deletion test/snapshots/boolean.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import * as fc from 'fast-check'
import * as fc from 'fast-check';

export const Boolean = fc.boolean();
2 changes: 1 addition & 1 deletion test/snapshots/bytes.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import * as fc from 'fast-check'
import * as fc from 'fast-check';

export const Bytes = fc.uint8Array();
2 changes: 1 addition & 1 deletion test/snapshots/decimal.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as fc from 'fast-check'
import * as fc from 'fast-check';

export const Decimal = fc.double();

Expand Down
2 changes: 1 addition & 1 deletion test/snapshots/decimal128.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as fc from 'fast-check'
import * as fc from 'fast-check';

export const Decimal128 = fc.double();

Expand Down
2 changes: 1 addition & 1 deletion test/snapshots/empty.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import * as fc from 'fast-check'
import * as fc from 'fast-check';

2 changes: 1 addition & 1 deletion test/snapshots/file-model.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as fc from 'fast-check'
import * as fc from 'fast-check';

export const Pet = fc.record({

Expand Down
2 changes: 1 addition & 1 deletion test/snapshots/file-namespace.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as fc from 'fast-check'
import * as fc from 'fast-check';

export const PetStore = {

Expand Down
2 changes: 1 addition & 1 deletion test/snapshots/float32.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as fc from 'fast-check'
import * as fc from 'fast-check';

export const Float32 = fc.float();

Expand Down
2 changes: 1 addition & 1 deletion test/snapshots/float64.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as fc from 'fast-check'
import * as fc from 'fast-check';

export const Float64 = fc.double();

Expand Down
2 changes: 1 addition & 1 deletion test/snapshots/int16.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as fc from 'fast-check'
import * as fc from 'fast-check';

export const Int16 = fc.integer({
min: -32768,
Expand Down
2 changes: 1 addition & 1 deletion test/snapshots/int32.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as fc from 'fast-check'
import * as fc from 'fast-check';

export const Int32 = fc.integer();

Expand Down
2 changes: 1 addition & 1 deletion test/snapshots/int64.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as fc from 'fast-check'
import * as fc from 'fast-check';

export const Int64 = fc.bigInt({
min: -9223372036854775808n,
Expand Down
2 changes: 1 addition & 1 deletion test/snapshots/int8.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as fc from 'fast-check'
import * as fc from 'fast-check';

export const Int8 = fc.integer({
min: -128,
Expand Down
2 changes: 1 addition & 1 deletion test/snapshots/integer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as fc from 'fast-check'
import * as fc from 'fast-check';

export const Integer = fc.bigInt();

Expand Down
2 changes: 1 addition & 1 deletion test/snapshots/model-and-namespace.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as fc from 'fast-check'
import * as fc from 'fast-check';

export const PetStore = {

Expand Down
2 changes: 1 addition & 1 deletion test/snapshots/model-in-namespace.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as fc from 'fast-check'
import * as fc from 'fast-check';

export const PetStore = {
Pet: fc.record({
Expand Down
2 changes: 1 addition & 1 deletion test/snapshots/model.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as fc from 'fast-check'
import * as fc from 'fast-check';

export const Dog = fc.record({
name: fc.string({
Expand Down
2 changes: 1 addition & 1 deletion test/snapshots/namespace-in-file-namespace.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as fc from 'fast-check'
import * as fc from 'fast-check';

export const PetStore = {
Pets: {
Expand Down
2 changes: 1 addition & 1 deletion test/snapshots/namespace.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as fc from 'fast-check'
import * as fc from 'fast-check';

export const PetStore = {

Expand Down
7 changes: 7 additions & 0 deletions test/snapshots/never.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as fc from 'fast-check';

export const M = fc.record({
property: fc.constant(null).map(() => {
throw new Error('never');
}),
});
2 changes: 1 addition & 1 deletion test/snapshots/null.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as fc from 'fast-check'
import * as fc from 'fast-check';

export const M = fc.record({
property: fc.constant(null),
Expand Down
2 changes: 1 addition & 1 deletion test/snapshots/numeric.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as fc from 'fast-check'
import * as fc from 'fast-check';

export const Numeric = fc.double();

Expand Down
2 changes: 1 addition & 1 deletion test/snapshots/safeint.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as fc from 'fast-check'
import * as fc from 'fast-check';

export const SafeInt = fc.maxSafeInteger();

Expand Down
2 changes: 1 addition & 1 deletion test/snapshots/string.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as fc from 'fast-check'
import * as fc from 'fast-check';

export const String = fc.string();

Expand Down
2 changes: 1 addition & 1 deletion test/snapshots/void.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as fc from 'fast-check'
import * as fc from 'fast-check';

export const M = fc.record({
property: fc.constant(undefined),
Expand Down

0 comments on commit 6195f97

Please sign in to comment.