( node: nodeOfKind<"intersection">, ctx: Ctx )
| 146 | } |
| 147 | |
| 148 | export const buildStructureArbitrary = ( |
| 149 | node: nodeOfKind<"intersection">, |
| 150 | ctx: Ctx |
| 151 | ): fc.Arbitrary<unknown> => { |
| 152 | const structure = node.structure |
| 153 | |
| 154 | if (node.basis?.is("domain") || (!node.basis && !structure?.sequence)) { |
| 155 | if (structure === undefined) |
| 156 | throwInternalError("Expected a structure node.") |
| 157 | |
| 158 | if (!hasKey(structure, "index") || !structure.index) |
| 159 | return buildObjectArbitrary(structure, ctx) |
| 160 | |
| 161 | const indexArbitrary = buildIndexSignatureArbitrary( |
| 162 | structure.index, |
| 163 | ctx, |
| 164 | structure.undeclared |
| 165 | ) |
| 166 | if (!structure.props.length) return indexArbitrary |
| 167 | |
| 168 | // declared props replace index keys that share their name |
| 169 | return fc |
| 170 | .tuple(indexArbitrary, buildObjectArbitrary(structure, ctx)) |
| 171 | .map(([indexed, declared]) => { |
| 172 | const result: Record<PropertyKey, unknown> = |
| 173 | Object.getPrototypeOf(declared) === null ? Object.create(null) : {} |
| 174 | for (const key of Reflect.ownKeys(indexed)) |
| 175 | defineValue(result, key, indexed[key as never]) |
| 176 | for (const prop of structure.props) delete result[prop.key] |
| 177 | for (const key of Reflect.ownKeys(declared)) |
| 178 | defineValue(result, key, declared[key as never]) |
| 179 | return result |
| 180 | }) |
| 181 | } |
| 182 | |
| 183 | const seq = structure?.sequence |
| 184 | const fcArrayConstraints: fc.ArrayConstraints = {} |
| 185 | if (seq) { |
| 186 | if (seq.minLength > 0) fcArrayConstraints.minLength = seq.minLength |
| 187 | if (seq.maxLength !== null) fcArrayConstraints.maxLength = seq.maxLength |
| 188 | } |
| 189 | if (node.inner.exactLength) { |
| 190 | fcArrayConstraints.minLength = Math.max( |
| 191 | fcArrayConstraints.minLength ?? 0, |
| 192 | node.inner.exactLength.rule |
| 193 | ) |
| 194 | fcArrayConstraints.maxLength = |
| 195 | fcArrayConstraints.maxLength !== undefined ? |
| 196 | Math.min(fcArrayConstraints.maxLength, node.inner.exactLength.rule) |
| 197 | : node.inner.exactLength.rule |
| 198 | } |
| 199 | if (node.inner.minLength) { |
| 200 | fcArrayConstraints.minLength = Math.max( |
| 201 | fcArrayConstraints.minLength ?? 0, |
| 202 | node.inner.minLength.rule |
| 203 | ) |
| 204 | } |
| 205 | if (node.inner.maxLength) { |
no test coverage detected