@hegeldev/hegel 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (144) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +59 -0
  3. package/dist/binary.d.ts +27 -0
  4. package/dist/binary.d.ts.map +1 -0
  5. package/dist/binary.js +50 -0
  6. package/dist/binary.js.map +1 -0
  7. package/dist/collections.d.ts +114 -0
  8. package/dist/collections.d.ts.map +1 -0
  9. package/dist/collections.js +313 -0
  10. package/dist/collections.js.map +1 -0
  11. package/dist/combinators.d.ts +34 -0
  12. package/dist/combinators.d.ts.map +1 -0
  13. package/dist/combinators.js +152 -0
  14. package/dist/combinators.js.map +1 -0
  15. package/dist/conformance.d.ts +31 -0
  16. package/dist/conformance.d.ts.map +1 -0
  17. package/dist/conformance.js +60 -0
  18. package/dist/conformance.js.map +1 -0
  19. package/dist/connection.d.ts +82 -0
  20. package/dist/connection.d.ts.map +1 -0
  21. package/dist/connection.js +231 -0
  22. package/dist/connection.js.map +1 -0
  23. package/dist/crc32.d.ts +13 -0
  24. package/dist/crc32.d.ts.map +1 -0
  25. package/dist/crc32.js +30 -0
  26. package/dist/crc32.js.map +1 -0
  27. package/dist/derive.d.ts +225 -0
  28. package/dist/derive.d.ts.map +1 -0
  29. package/dist/derive.js +296 -0
  30. package/dist/derive.js.map +1 -0
  31. package/dist/embedded.d.ts +38 -0
  32. package/dist/embedded.d.ts.map +1 -0
  33. package/dist/embedded.js +237 -0
  34. package/dist/embedded.js.map +1 -0
  35. package/dist/floats.d.ts +57 -0
  36. package/dist/floats.d.ts.map +1 -0
  37. package/dist/floats.js +100 -0
  38. package/dist/floats.js.map +1 -0
  39. package/dist/formats.d.ts +62 -0
  40. package/dist/formats.d.ts.map +1 -0
  41. package/dist/formats.js +164 -0
  42. package/dist/formats.js.map +1 -0
  43. package/dist/generator.d.ts +80 -0
  44. package/dist/generator.d.ts.map +1 -0
  45. package/dist/generator.js +128 -0
  46. package/dist/generator.js.map +1 -0
  47. package/dist/generators/collections.d.ts +20 -0
  48. package/dist/generators/collections.d.ts.map +1 -0
  49. package/dist/generators/collections.js +209 -0
  50. package/dist/generators/collections.js.map +1 -0
  51. package/dist/generators/combinators.d.ts +15 -0
  52. package/dist/generators/combinators.d.ts.map +1 -0
  53. package/dist/generators/combinators.js +143 -0
  54. package/dist/generators/combinators.js.map +1 -0
  55. package/dist/generators/compose.d.ts +27 -0
  56. package/dist/generators/compose.d.ts.map +1 -0
  57. package/dist/generators/compose.js +82 -0
  58. package/dist/generators/compose.js.map +1 -0
  59. package/dist/generators/core.d.ts +53 -0
  60. package/dist/generators/core.d.ts.map +1 -0
  61. package/dist/generators/core.js +134 -0
  62. package/dist/generators/core.js.map +1 -0
  63. package/dist/generators/index.d.ts +18 -0
  64. package/dist/generators/index.d.ts.map +1 -0
  65. package/dist/generators/index.js +15 -0
  66. package/dist/generators/index.js.map +1 -0
  67. package/dist/generators/numeric.d.ts +40 -0
  68. package/dist/generators/numeric.d.ts.map +1 -0
  69. package/dist/generators/numeric.js +128 -0
  70. package/dist/generators/numeric.js.map +1 -0
  71. package/dist/generators/primitives.d.ts +138 -0
  72. package/dist/generators/primitives.d.ts.map +1 -0
  73. package/dist/generators/primitives.js +240 -0
  74. package/dist/generators/primitives.js.map +1 -0
  75. package/dist/generators/strings.d.ts +73 -0
  76. package/dist/generators/strings.d.ts.map +1 -0
  77. package/dist/generators/strings.js +215 -0
  78. package/dist/generators/strings.js.map +1 -0
  79. package/dist/generators/tuples.d.ts +11 -0
  80. package/dist/generators/tuples.d.ts.map +1 -0
  81. package/dist/generators/tuples.js +43 -0
  82. package/dist/generators/tuples.js.map +1 -0
  83. package/dist/generators.d.ts +408 -0
  84. package/dist/generators.d.ts.map +1 -0
  85. package/dist/generators.js +898 -0
  86. package/dist/generators.js.map +1 -0
  87. package/dist/index.d.ts +16 -0
  88. package/dist/index.d.ts.map +1 -0
  89. package/dist/index.js +13 -0
  90. package/dist/index.js.map +1 -0
  91. package/dist/install.d.ts +6 -0
  92. package/dist/install.d.ts.map +1 -0
  93. package/dist/install.js +91 -0
  94. package/dist/install.js.map +1 -0
  95. package/dist/integers.d.ts +37 -0
  96. package/dist/integers.d.ts.map +1 -0
  97. package/dist/integers.js +63 -0
  98. package/dist/integers.js.map +1 -0
  99. package/dist/labels.d.ts +21 -0
  100. package/dist/labels.d.ts.map +1 -0
  101. package/dist/labels.js +20 -0
  102. package/dist/labels.js.map +1 -0
  103. package/dist/objects.d.ts +39 -0
  104. package/dist/objects.d.ts.map +1 -0
  105. package/dist/objects.js +98 -0
  106. package/dist/objects.js.map +1 -0
  107. package/dist/primitives.d.ts +14 -0
  108. package/dist/primitives.d.ts.map +1 -0
  109. package/dist/primitives.js +51 -0
  110. package/dist/primitives.js.map +1 -0
  111. package/dist/protocol.d.ts +25 -0
  112. package/dist/protocol.d.ts.map +1 -0
  113. package/dist/protocol.js +77 -0
  114. package/dist/protocol.js.map +1 -0
  115. package/dist/runner.d.ts +115 -0
  116. package/dist/runner.d.ts.map +1 -0
  117. package/dist/runner.js +455 -0
  118. package/dist/runner.js.map +1 -0
  119. package/dist/session.d.ts +19 -0
  120. package/dist/session.d.ts.map +1 -0
  121. package/dist/session.js +157 -0
  122. package/dist/session.js.map +1 -0
  123. package/dist/spans.d.ts +23 -0
  124. package/dist/spans.d.ts.map +1 -0
  125. package/dist/spans.js +51 -0
  126. package/dist/spans.js.map +1 -0
  127. package/dist/strings.d.ts +67 -0
  128. package/dist/strings.d.ts.map +1 -0
  129. package/dist/strings.js +107 -0
  130. package/dist/strings.js.map +1 -0
  131. package/dist/testCase.d.ts +118 -0
  132. package/dist/testCase.d.ts.map +1 -0
  133. package/dist/testCase.js +186 -0
  134. package/dist/testCase.js.map +1 -0
  135. package/dist/uv-install.sh +2226 -0
  136. package/dist/uv.d.ts +20 -0
  137. package/dist/uv.d.ts.map +1 -0
  138. package/dist/uv.js +103 -0
  139. package/dist/uv.js.map +1 -0
  140. package/dist/wtf8.d.ts +16 -0
  141. package/dist/wtf8.d.ts.map +1 -0
  142. package/dist/wtf8.js +52 -0
  143. package/dist/wtf8.js.map +1 -0
  144. package/package.json +60 -0
@@ -0,0 +1,225 @@
1
+ /**
2
+ * Type-directed generator derivation for the Hegel library.
3
+ *
4
+ * TypeScript erases interface and type information at compile time, but
5
+ * **classes** persist at runtime with real constructors and field names.
6
+ * This module uses TypeScript legacy decorators (`experimentalDecorators`)
7
+ * to associate generators with class fields. Users annotate their class
8
+ * with `@field(gen)` on each property, then call `deriveGenerator(MyClass)`
9
+ * to get a composite generator that builds instances by generating each
10
+ * field independently.
11
+ *
12
+ * For plain-object records (no class needed), use {@link recordGenerator}.
13
+ *
14
+ * For sum types (discriminated unions), {@link variantGenerator} builds
15
+ * a generator that picks a variant uniformly at random and produces an
16
+ * object with the discriminant tag and generated fields.
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * class Point {
21
+ * \@field(floats(-100, 100))
22
+ * x!: number;
23
+ *
24
+ * \@field(floats(-100, 100))
25
+ * y!: number;
26
+ * }
27
+ *
28
+ * const gen = deriveGenerator(Point);
29
+ * // draw(gen) returns a Point instance with random x, y
30
+ * ```
31
+ *
32
+ * @packageDocumentation
33
+ */
34
+ import { Generator } from "./generators/index.js";
35
+ import type { TestCaseData } from "./runner.js";
36
+ /**
37
+ * Metadata entry for a single field.
38
+ * @internal
39
+ */
40
+ export interface FieldMeta {
41
+ /** The property name on the class instance. */
42
+ name: string | symbol;
43
+ /** The generator to use for this field. */
44
+ generator: Generator;
45
+ /** The order the decorator was applied (for deterministic iteration). */
46
+ order: number;
47
+ }
48
+ /**
49
+ * Type for class constructors in the metadata map.
50
+ * @internal
51
+ */
52
+ type AnyConstructor = new (...args: any[]) => any;
53
+ /**
54
+ * Map from constructor → ordered list of field metadata.
55
+ * Each class that uses `@field(...)` gets an entry here.
56
+ * @internal
57
+ */
58
+ export declare const _classFieldMeta: Map<AnyConstructor, FieldMeta[]>;
59
+ /**
60
+ * Reset the field order counter. Only for testing.
61
+ * @internal
62
+ */
63
+ export declare function _resetFieldOrder(): void;
64
+ /**
65
+ * Decorator that associates a {@link Generator} with a class field.
66
+ *
67
+ * Use this on each field of a class before calling {@link deriveGenerator}.
68
+ * Fields are generated in decorator-application order (typically top to
69
+ * bottom in the class body).
70
+ *
71
+ * Requires `experimentalDecorators: true` in `tsconfig.json`.
72
+ *
73
+ * @example
74
+ * ```ts
75
+ * class User {
76
+ * \@field(text(1, 50))
77
+ * name!: string;
78
+ *
79
+ * \@field(integers(18, 120))
80
+ * age!: number;
81
+ * }
82
+ * ```
83
+ *
84
+ * @param gen - The generator to use for this field's values.
85
+ */
86
+ export declare function field<T>(gen: Generator<T>): PropertyDecorator;
87
+ /**
88
+ * A generator that produces instances of a class by generating each
89
+ * decorated field independently.
90
+ *
91
+ * Uses a FIXED_DICT span (label 10) to group the field generations,
92
+ * matching the Hegel protocol's semantic for record/struct types.
93
+ *
94
+ * @typeParam T - The class type being generated.
95
+ */
96
+ export declare class DerivedGenerator<T> extends Generator<T> {
97
+ /** @internal */
98
+ readonly _ctor: new () => T;
99
+ /** @internal */
100
+ readonly _fields: ReadonlyArray<FieldMeta>;
101
+ constructor(ctor: new () => T, fields: FieldMeta[]);
102
+ doDraw(data: TestCaseData): Promise<T>;
103
+ }
104
+ /**
105
+ * Derive a generator for a class from its `@field(...)` annotations.
106
+ *
107
+ * Reads the generator metadata registered by the {@link field} decorator
108
+ * and returns a {@link DerivedGenerator} that produces instances of the
109
+ * class with each field independently generated.
110
+ *
111
+ * @param ctor - The class constructor (must have a no-arg constructor).
112
+ * @throws {Error} If the class has no `@field` annotations.
113
+ *
114
+ * @example
115
+ * ```ts
116
+ * class Config {
117
+ * \@field(booleans()) debug!: boolean;
118
+ * \@field(integers(1, 65535)) port!: number;
119
+ * }
120
+ * const gen = deriveGenerator(Config);
121
+ * const cfg = await draw(gen); // Config { debug: true, port: 8080 }
122
+ * ```
123
+ */
124
+ export declare function deriveGenerator<T>(ctor: new () => T): DerivedGenerator<T>;
125
+ /**
126
+ * A generator that produces plain objects from a schema mapping.
127
+ *
128
+ * Uses a FIXED_DICT span (label 10) internally.
129
+ *
130
+ * @typeParam T - The resulting object type.
131
+ */
132
+ export declare class RecordDerivedGenerator<T> extends Generator<T> {
133
+ /** @internal */
134
+ readonly _entries: ReadonlyArray<[string, Generator]>;
135
+ constructor(entries: Array<[string, Generator]>);
136
+ doDraw(data: TestCaseData): Promise<T>;
137
+ }
138
+ /**
139
+ * Derive a generator for a plain-object record type from a schema mapping
140
+ * field names to generators.
141
+ *
142
+ * This is the "no-class" alternative: the user provides the structure
143
+ * explicitly as an object mapping field names to generators. The result
144
+ * is a generator that produces plain objects with the specified fields.
145
+ *
146
+ * @example
147
+ * ```ts
148
+ * const pointGen = recordGenerator({
149
+ * x: floats(-100, 100),
150
+ * y: floats(-100, 100),
151
+ * });
152
+ * const pt = await draw(pointGen); // { x: 42.5, y: -3.14 }
153
+ * ```
154
+ *
155
+ * @param schema - Mapping from field name to its generator.
156
+ * @throws {Error} If the schema has no fields.
157
+ */
158
+ export declare function recordGenerator<S extends Record<string, Generator>>(schema: S): RecordDerivedGenerator<{
159
+ [K in keyof S]: S[K] extends Generator<infer V> ? V : never;
160
+ }>;
161
+ /**
162
+ * A single variant in a discriminated union.
163
+ */
164
+ export interface VariantDef {
165
+ /** The discriminant value (e.g., "circle", "rectangle"). */
166
+ tag: string;
167
+ /**
168
+ * Generator for the variant's fields. When `null`, the variant has no
169
+ * extra data — the generator produces `{ type: tag }` only.
170
+ */
171
+ fields: Generator | null;
172
+ }
173
+ /**
174
+ * A generator that produces values of a discriminated union type.
175
+ *
176
+ * Picks a variant uniformly at random, generates its fields, and returns
177
+ * an object with the discriminant key set to the variant tag, plus the
178
+ * generated field values spread in. Uses an ENUM_VARIANT span (label 15)
179
+ * for each generated value.
180
+ *
181
+ * @typeParam T - The union type being generated.
182
+ */
183
+ export declare class VariantGenerator<T> extends Generator<T> {
184
+ /** @internal */
185
+ readonly _variants: ReadonlyArray<VariantDef>;
186
+ /** @internal */
187
+ readonly _discriminant: string;
188
+ /** @internal */
189
+ private readonly _indexGen;
190
+ constructor(variants: VariantDef[], discriminant: string);
191
+ doDraw(data: TestCaseData): Promise<T>;
192
+ }
193
+ /**
194
+ * Derive a generator for a discriminated union (sum type).
195
+ *
196
+ * Takes a mapping from variant tag names to their field generators (or
197
+ * `null` for data-less variants). Returns a {@link VariantGenerator}
198
+ * that picks a variant uniformly at random and generates its fields.
199
+ *
200
+ * @example
201
+ * ```ts
202
+ * type Shape =
203
+ * | { type: "circle"; radius: number }
204
+ * | { type: "rectangle"; width: number; height: number };
205
+ *
206
+ * const shapeGen = variantGenerator<Shape>({
207
+ * circle: recordGenerator({ radius: floats(0.1, 100) }),
208
+ * rectangle: recordGenerator({
209
+ * width: floats(0.1, 100),
210
+ * height: floats(0.1, 100),
211
+ * }),
212
+ * });
213
+ *
214
+ * const shape = await draw(shapeGen);
215
+ * // { type: "circle", radius: 42.5 } or { type: "rectangle", width: 10, height: 20 }
216
+ * ```
217
+ *
218
+ * @param variants - Mapping from tag name to field generator (or `null`
219
+ * for data-less variants).
220
+ * @param discriminant - The discriminant property name. Defaults to `"type"`.
221
+ * @throws {Error} If fewer than 2 variants are provided.
222
+ */
223
+ export declare function variantGenerator<T>(variants: Record<string, Generator | null>, discriminant?: string): VariantGenerator<T>;
224
+ export {};
225
+ //# sourceMappingURL=derive.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"derive.d.ts","sourceRoot":"","sources":["../src/derive.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AAEH,OAAO,EAAE,SAAS,EAAkB,MAAM,uBAAuB,CAAC;AAElE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAMhD;;;GAGG;AACH,MAAM,WAAW,SAAS;IACxB,+CAA+C;IAC/C,IAAI,EAAE,MAAM,GAAG,MAAM,C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AAG,OAAO,CAAC,CAAC,CAAC;CAY7C;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,eAAe,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,EACjE,MAAM,EAAE,CAAC,GACR,sBAAsB,CAAC;KAAG,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,SAAS,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,GAAG,KAAK;CAAE,CAAC,CAQzF;AAMD;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,4DAA4D;IAC5D,GAAG,EAAE,MAAM,CAAC;IACZ;;;OAGG;IACH,MAAM,EAAE,SAAS,GAAG,IAAI,CAAC;CAC1B;AAED;;;;;;;;;GASG;AACH,qBAAa,gBAAgB,CAAC,CAAC,CAAE,SAAQ,SAAS,CAAC,CAAC,CAAC;IACnD,gBAAgB;IAChB,QAAQ,CAAC,SAAS,EAAE,aAAa,CAAC,UAAU,CAAC,CAAC;IAC9C,gBAAgB;IAChB,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;IAC/B,gBAAgB;IAChB,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAyB;gBAEvC,QAAQ,EAAE,UAAU,EAAE,EAAE,YAAY,EAAE,MAAM;IAWlD,MAAM,CAAC,IAAI,EAAE,YAAY,GAAG,OAAO,CAAC,CAAC,CAAC;CAc7C;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,EAChC,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,GAAG,IAAI,CAAC,EAC1C,YAAY,SAAS,GACpB,gBAAgB,CAAC,CAAC,CAAC,CAUrB"}
package/dist/derive.js ADDED
@@ -0,0 +1,296 @@
1
+ /**
2
+ * Type-directed generator derivation for the Hegel library.
3
+ *
4
+ * TypeScript erases interface and type information at compile time, but
5
+ * **classes** persist at runtime with real constructors and field names.
6
+ * This module uses TypeScript legacy decorators (`experimentalDecorators`)
7
+ * to associate generators with class fields. Users annotate their class
8
+ * with `@field(gen)` on each property, then call `deriveGenerator(MyClass)`
9
+ * to get a composite generator that builds instances by generating each
10
+ * field independently.
11
+ *
12
+ * For plain-object records (no class needed), use {@link recordGenerator}.
13
+ *
14
+ * For sum types (discriminated unions), {@link variantGenerator} builds
15
+ * a generator that picks a variant uniformly at random and produces an
16
+ * object with the discriminant tag and generated fields.
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * class Point {
21
+ * \@field(floats(-100, 100))
22
+ * x!: number;
23
+ *
24
+ * \@field(floats(-100, 100))
25
+ * y!: number;
26
+ * }
27
+ *
28
+ * const gen = deriveGenerator(Point);
29
+ * // draw(gen) returns a Point instance with random x, y
30
+ * ```
31
+ *
32
+ * @packageDocumentation
33
+ */
34
+ import { Generator, BasicGenerator } from "./generators/index.js";
35
+ import { startSpan, stopSpan, Labels } from "./runner.js";
36
+ /**
37
+ * Map from constructor → ordered list of field metadata.
38
+ * Each class that uses `@field(...)` gets an entry here.
39
+ * @internal
40
+ */
41
+ export const _classFieldMeta = new Map();
42
+ /** Global counter to ensure deterministic field ordering. @internal */
43
+ let _fieldOrder = 0;
44
+ /**
45
+ * Reset the field order counter. Only for testing.
46
+ * @internal
47
+ */
48
+ export function _resetFieldOrder() {
49
+ _fieldOrder = 0;
50
+ }
51
+ // ---------------------------------------------------------------------------
52
+ // @field decorator
53
+ // ---------------------------------------------------------------------------
54
+ /**
55
+ * Decorator that associates a {@link Generator} with a class field.
56
+ *
57
+ * Use this on each field of a class before calling {@link deriveGenerator}.
58
+ * Fields are generated in decorator-application order (typically top to
59
+ * bottom in the class body).
60
+ *
61
+ * Requires `experimentalDecorators: true` in `tsconfig.json`.
62
+ *
63
+ * @example
64
+ * ```ts
65
+ * class User {
66
+ * \@field(text(1, 50))
67
+ * name!: string;
68
+ *
69
+ * \@field(integers(18, 120))
70
+ * age!: number;
71
+ * }
72
+ * ```
73
+ *
74
+ * @param gen - The generator to use for this field's values.
75
+ */
76
+ export function field(gen) {
77
+ return function (target, propertyKey) {
78
+ const ctor = target.constructor;
79
+ let list = _classFieldMeta.get(ctor);
80
+ if (!list) {
81
+ list = [];
82
+ _classFieldMeta.set(ctor, list);
83
+ }
84
+ list.push({ name: propertyKey, generator: gen, order: _fieldOrder++ });
85
+ };
86
+ }
87
+ // ---------------------------------------------------------------------------
88
+ // DerivedGenerator
89
+ // ---------------------------------------------------------------------------
90
+ /**
91
+ * A generator that produces instances of a class by generating each
92
+ * decorated field independently.
93
+ *
94
+ * Uses a FIXED_DICT span (label 10) to group the field generations,
95
+ * matching the Hegel protocol's semantic for record/struct types.
96
+ *
97
+ * @typeParam T - The class type being generated.
98
+ */
99
+ export class DerivedGenerator extends Generator {
100
+ /** @internal */
101
+ _ctor;
102
+ /** @internal */
103
+ _fields;
104
+ constructor(ctor, fields) {
105
+ super();
106
+ this._ctor = ctor;
107
+ this._fields = [...fields].sort((a, b) => a.order - b.order);
108
+ }
109
+ async doDraw(data) {
110
+ await startSpan(Labels.FIXED_DICT, data);
111
+ try {
112
+ const instance = new this._ctor();
113
+ for (const f of this._fields) {
114
+ instance[f.name] = await f.generator.doDraw(data);
115
+ }
116
+ return instance;
117
+ }
118
+ finally {
119
+ await stopSpan({}, data);
120
+ }
121
+ }
122
+ }
123
+ // ---------------------------------------------------------------------------
124
+ // deriveGenerator
125
+ // ---------------------------------------------------------------------------
126
+ /**
127
+ * Derive a generator for a class from its `@field(...)` annotations.
128
+ *
129
+ * Reads the generator metadata registered by the {@link field} decorator
130
+ * and returns a {@link DerivedGenerator} that produces instances of the
131
+ * class with each field independently generated.
132
+ *
133
+ * @param ctor - The class constructor (must have a no-arg constructor).
134
+ * @throws {Error} If the class has no `@field` annotations.
135
+ *
136
+ * @example
137
+ * ```ts
138
+ * class Config {
139
+ * \@field(booleans()) debug!: boolean;
140
+ * \@field(integers(1, 65535)) port!: number;
141
+ * }
142
+ * const gen = deriveGenerator(Config);
143
+ * const cfg = await draw(gen); // Config { debug: true, port: 8080 }
144
+ * ```
145
+ */
146
+ export function deriveGenerator(ctor) {
147
+ const fields = _classFieldMeta.get(ctor);
148
+ if (!fields || fields.length === 0) {
149
+ throw new Error(`No @field annotations found on ${ctor.name}. ` +
150
+ `Decorate fields with @field(generator) before calling deriveGenerator().`);
151
+ }
152
+ return new DerivedGenerator(ctor, fields);
153
+ }
154
+ // ---------------------------------------------------------------------------
155
+ // recordGenerator — anonymous record derivation (no class needed)
156
+ // ---------------------------------------------------------------------------
157
+ /**
158
+ * A generator that produces plain objects from a schema mapping.
159
+ *
160
+ * Uses a FIXED_DICT span (label 10) internally.
161
+ *
162
+ * @typeParam T - The resulting object type.
163
+ */
164
+ export class RecordDerivedGenerator extends Generator {
165
+ /** @internal */
166
+ _entries;
167
+ constructor(entries) {
168
+ super();
169
+ this._entries = entries;
170
+ }
171
+ async doDraw(data) {
172
+ await startSpan(Labels.FIXED_DICT, data);
173
+ try {
174
+ const result = {};
175
+ for (const [key, gen] of this._entries) {
176
+ result[key] = await gen.doDraw(data);
177
+ }
178
+ return result;
179
+ }
180
+ finally {
181
+ await stopSpan({}, data);
182
+ }
183
+ }
184
+ }
185
+ /**
186
+ * Derive a generator for a plain-object record type from a schema mapping
187
+ * field names to generators.
188
+ *
189
+ * This is the "no-class" alternative: the user provides the structure
190
+ * explicitly as an object mapping field names to generators. The result
191
+ * is a generator that produces plain objects with the specified fields.
192
+ *
193
+ * @example
194
+ * ```ts
195
+ * const pointGen = recordGenerator({
196
+ * x: floats(-100, 100),
197
+ * y: floats(-100, 100),
198
+ * });
199
+ * const pt = await draw(pointGen); // { x: 42.5, y: -3.14 }
200
+ * ```
201
+ *
202
+ * @param schema - Mapping from field name to its generator.
203
+ * @throws {Error} If the schema has no fields.
204
+ */
205
+ export function recordGenerator(schema) {
206
+ const entries = Object.entries(schema);
207
+ if (entries.length === 0) {
208
+ throw new Error("recordGenerator requires at least one field.");
209
+ }
210
+ return new RecordDerivedGenerator(entries);
211
+ }
212
+ /**
213
+ * A generator that produces values of a discriminated union type.
214
+ *
215
+ * Picks a variant uniformly at random, generates its fields, and returns
216
+ * an object with the discriminant key set to the variant tag, plus the
217
+ * generated field values spread in. Uses an ENUM_VARIANT span (label 15)
218
+ * for each generated value.
219
+ *
220
+ * @typeParam T - The union type being generated.
221
+ */
222
+ export class VariantGenerator extends Generator {
223
+ /** @internal */
224
+ _variants;
225
+ /** @internal */
226
+ _discriminant;
227
+ /** @internal */
228
+ _indexGen;
229
+ constructor(variants, discriminant) {
230
+ super();
231
+ this._variants = variants;
232
+ this._discriminant = discriminant;
233
+ this._indexGen = new BasicGenerator({
234
+ type: "integer",
235
+ min_value: 0,
236
+ max_value: variants.length - 1,
237
+ });
238
+ }
239
+ async doDraw(data) {
240
+ await startSpan(Labels.ENUM_VARIANT, data);
241
+ try {
242
+ const index = await this._indexGen.doDraw(data);
243
+ const variant = this._variants[index];
244
+ if (variant.fields !== null) {
245
+ const fields = await variant.fields.doDraw(data);
246
+ return { [this._discriminant]: variant.tag, ...fields };
247
+ }
248
+ return { [this._discriminant]: variant.tag };
249
+ }
250
+ finally {
251
+ await stopSpan({}, data);
252
+ }
253
+ }
254
+ }
255
+ /**
256
+ * Derive a generator for a discriminated union (sum type).
257
+ *
258
+ * Takes a mapping from variant tag names to their field generators (or
259
+ * `null` for data-less variants). Returns a {@link VariantGenerator}
260
+ * that picks a variant uniformly at random and generates its fields.
261
+ *
262
+ * @example
263
+ * ```ts
264
+ * type Shape =
265
+ * | { type: "circle"; radius: number }
266
+ * | { type: "rectangle"; width: number; height: number };
267
+ *
268
+ * const shapeGen = variantGenerator<Shape>({
269
+ * circle: recordGenerator({ radius: floats(0.1, 100) }),
270
+ * rectangle: recordGenerator({
271
+ * width: floats(0.1, 100),
272
+ * height: floats(0.1, 100),
273
+ * }),
274
+ * });
275
+ *
276
+ * const shape = await draw(shapeGen);
277
+ * // { type: "circle", radius: 42.5 } or { type: "rectangle", width: 10, height: 20 }
278
+ * ```
279
+ *
280
+ * @param variants - Mapping from tag name to field generator (or `null`
281
+ * for data-less variants).
282
+ * @param discriminant - The discriminant property name. Defaults to `"type"`.
283
+ * @throws {Error} If fewer than 2 variants are provided.
284
+ */
285
+ export function variantGenerator(variants, discriminant = "type") {
286
+ const entries = Object.entries(variants);
287
+ if (entries.length < 2) {
288
+ throw new Error("variantGenerator requires at least 2 variants.");
289
+ }
290
+ const defs = entries.map(([tag, gen]) => ({
291
+ tag,
292
+ fields: gen,
293
+ }));
294
+ return new VariantGenerator(defs, discriminant);
295
+ }
296
+ //# sourceMappingURL=derive.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"derive.js","sourceRoot":"","sources":["../src/derive.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AAEH,OAAO,EAAE,SAAS,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAClE,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AA2B1D;;;;GAIG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,IAAI,GAAG,EAA+B,CAAC;AAEtE,uEAAuE;AACvE,IAAI,WAAW,GAAG,CAAC,CAAC;AAEpB;;;GAGG;AACH,MAAM,UAAU,gBAAgB;IAC9B,WAAW,GAAG,CAAC,CAAC;AAClB,CAAC;AAED,8EAA8E;AAC9E,mBAAmB;AACnB,8EAA8E;AAE9E;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,UAAU,KAAK,CAAI,GAAiB;IACxC,OAAO,UAAU,MAAc,EAAE,WAA4B;QAC3D,MAAM,IAAI,GAAG,MAAM,CAAC,WAA6B,CAAC;QAClD,IAAI,IAAI,GAAG,eAAe,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QACrC,IAAI,CAAC,IAAI,EAAE,CAAC;YACV,IAAI,GAAG,EAAE,CAAC;YACV,eAAe,CAAC,GAAG,C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pD,CAAC;gBAAS,CAAC;YACT,MAAM,QAAQ,CAAC,EAAE,EAAE,IAAI,CAAC,CAAC;QAC3B,CAAC;IACH,CAAC;CACF;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,MAAM,UAAU,gBAAgB,CAC9B,QAA0C,EAC1C,YAAY,GAAG,MAAM;IAErB,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IACzC,IAAI,OAAO,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACvB,MAAM,IAAI,KAAK,CAAC,gDAAgD,CAAC,CAAC;IACpE,CAAC;IACD,MAAM,IAAI,GAAiB,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,GAAG,CAAC,EAAE,EAAE,CAAC,CAAC;QACtD,GAAG;QACH,MAAM,EAAE,GAAG;KACZ,CAAC,CAAC,CAAC;IACJ,OAAO,IAAI,gBAAgB,CAAI,IAAI,EAAE,YAAY,CAAC,CAAC;AACrD,CAAC"}
@@ -0,0 +1,38 @@
1
+ export declare enum Verbosity {
2
+ Quiet = "quiet",
3
+ Normal = "normal",
4
+ Verbose = "verbose",
5
+ Debug = "debug"
6
+ }
7
+ /**
8
+ * Special error thrown by assume() to signal test rejection.
9
+ */
10
+ export declare class RejectError extends Error {
11
+ constructor();
12
+ }
13
+ /**
14
+ * Builder class for configuring and running Hegel tests.
15
+ */
16
+ export declare class Hegel {
17
+ private testFn;
18
+ private _testCases;
19
+ private _verbosity;
20
+ constructor(testFn: () => void);
21
+ /**
22
+ * Set the number of test cases to run.
23
+ */
24
+ testCases(n: number): this;
25
+ /**
26
+ * Set the verbosity level.
27
+ */
28
+ verbosity(v: Verbosity): this;
29
+ /**
30
+ * Run the test.
31
+ */
32
+ run(): Promise<void>;
33
+ }
34
+ /**
35
+ * Simple wrapper for running a Hegel test with default options.
36
+ */
37
+ export declare function hegel(testFn: () => void): Promise<void>;
38
+ //# sourceMappingURL=embedded.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"embedded.d.ts","sourceRoot":"","sources":["../src/embedded.ts"],"names":[],"mappings":"AAkBA,oBAAY,SAAS;IACnB,KAAK,UAAU;IACf,MAAM,WAAW;IACjB,OAAO,YAAY;IACnB,KAAK,UAAU;CAChB;AAED;;GAEG;AACH,qBAAa,WAAY,SAAQ,KAAK;;CAKrC;AAED;;GAEG;AACH,qBAAa,KAAK;IAChB,OAAO,CAAC,MAAM,CAAY;IAC1B,OAAO,CAAC,UAAU,CAAc;IAChC,OAAO,CAAC,UAAU,CAA8B;gBAEpC,MAAM,EAAE,MAAM,IAAI;IAI9B;;OAEG;IACH,SAAS,CAAC,CAAC,EAAE,MAAM,GAAG,IAAI;IAK1B;;OAEG;IACH,SAAS,CAAC,CAAC,EAAE,SAAS,GAAG,IAAI;IAK7B;;OAEG;IACH,GAAG,IAAI,OAAO,CAAC,IAAI,CAAC;CAGrB;AAED;;GAEG;AACH,wBAAgB,KAAK,CAAC,MAAM,EAAE,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAEvD"}