-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1454 from demergent-labs/property_test_arbitrary_…
…depth_variant Property test arbitrary depth variant
- Loading branch information
Showing
46 changed files
with
997 additions
and
663 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 0 additions & 118 deletions
118
property_tests/arbitraries/candid/constructed/opt_arb.ts
This file was deleted.
Oops, something went wrong.
89 changes: 89 additions & 0 deletions
89
property_tests/arbitraries/candid/constructed/opt_arb/base.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import fc from 'fast-check'; | ||
import { CandidType } from '../../candid_type_arb'; | ||
import { CandidMeta } from '../../candid_arb'; | ||
import { Opt } from './index'; | ||
import { UniqueIdentifierArb } from '../../../unique_identifier_arb'; | ||
|
||
type SomeOrNone = 'Some' | 'None'; | ||
|
||
export function OptArb( | ||
candidTypeArb: fc.Arbitrary<CandidMeta<CandidType>> | ||
): fc.Arbitrary<CandidMeta<Opt>> { | ||
return fc | ||
.tuple( | ||
UniqueIdentifierArb('typeDeclaration'), | ||
fc.constantFrom('Some', 'None') as fc.Arbitrary<SomeOrNone>, | ||
candidTypeArb, | ||
fc.boolean() | ||
) | ||
.map(([name, someOrNone, innerType, useTypeDeclaration]) => { | ||
const candidType = useTypeDeclaration | ||
? name | ||
: generateCandidType(innerType); | ||
|
||
const typeDeclaration = generateTypeDeclaration( | ||
name, | ||
innerType, | ||
useTypeDeclaration | ||
); | ||
|
||
return { | ||
src: { | ||
candidType, | ||
imports: generateImports(innerType), | ||
typeDeclaration, | ||
valueLiteral: generateValueLiteral(someOrNone, innerType) | ||
}, | ||
agentArgumentValue: generateValue(someOrNone, innerType), | ||
agentResponseValue: generateValue(someOrNone, innerType, true) | ||
}; | ||
}); | ||
} | ||
|
||
function generateTypeDeclaration( | ||
name: string, | ||
innerType: CandidMeta<CandidType>, | ||
useTypeDeclaration: boolean | ||
) { | ||
if (useTypeDeclaration) { | ||
return `${ | ||
innerType.src.typeDeclaration ?? '' | ||
}\nconst ${name} = ${generateCandidType(innerType)}`; | ||
} | ||
return innerType.src.typeDeclaration; | ||
} | ||
|
||
function generateCandidType(innerType: CandidMeta<CandidType>): string { | ||
return `Opt(${innerType.src.candidType})`; | ||
} | ||
|
||
function generateImports(innerType: CandidMeta<CandidType>): Set<string> { | ||
return new Set([...innerType.src.imports, 'Opt', 'Some', 'None']); | ||
} | ||
|
||
function generateValue( | ||
someOrNone: SomeOrNone, | ||
innerType: CandidMeta<CandidType>, | ||
useAgentResponseValue: boolean = false | ||
): Opt { | ||
if (someOrNone === 'Some') { | ||
return [ | ||
useAgentResponseValue | ||
? innerType.agentResponseValue | ||
: innerType.agentArgumentValue | ||
]; | ||
} else { | ||
return []; | ||
} | ||
} | ||
|
||
function generateValueLiteral( | ||
someOrNone: SomeOrNone, | ||
innerType: CandidMeta<CandidType> | ||
): string { | ||
if (someOrNone === 'Some') { | ||
return `Some(${innerType.src.valueLiteral})`; | ||
} else { | ||
return `None`; | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
property_tests/arbitraries/candid/constructed/opt_arb/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { CandidType, CandidTypeArb } from '../../candid_type_arb'; | ||
import { OptArb as Base } from './base'; | ||
|
||
export type Opt = [CandidType] | never[]; | ||
|
||
export const OptArb = Base(CandidTypeArb); |
Oops, something went wrong.