@@ -215,3 +215,46 @@ describe('or', () => {
215215 expect ( estimator ( [ 1 , 2 , 3 ] ) ) . toBe ( maxEncodingCapacity ( [ 1 , 2 , 3 ] ) ) ;
216216 } ) ;
217217} ) ;
218+
219+ describe ( 'standalone codegen function' , ( ) => {
220+ test ( 'generates capacity estimator equivalent to compileCapacityEstimator' , ( ) => {
221+ const system = new TypeSystem ( ) ;
222+ const type = system . t . Array ( system . t . str ) ;
223+
224+ // Compare standalone codegen function with the class method
225+ const { codegen} = require ( '../estimators' ) ;
226+ const standaloneEstimator = codegen ( type , { } ) ;
227+ const classEstimator = type . compileCapacityEstimator ( { } ) ;
228+
229+ const testData = [ 'hello' , 'world' , 'test' ] ;
230+ expect ( standaloneEstimator ( testData ) ) . toBe ( classEstimator ( testData ) ) ;
231+ expect ( standaloneEstimator ( testData ) ) . toBe ( maxEncodingCapacity ( testData ) ) ;
232+ } ) ;
233+
234+ test ( 'works with complex nested types' , ( ) => {
235+ const system = new TypeSystem ( ) ;
236+ const type = system . t . Object (
237+ system . t . prop ( 'name' , system . t . str ) ,
238+ system . t . prop ( 'items' , system . t . Array ( system . t . num ) )
239+ ) ;
240+
241+ const { codegen} = require ( '../estimators' ) ;
242+ const standaloneEstimator = codegen ( type , { } ) ;
243+ const classEstimator = type . compileCapacityEstimator ( { } ) ;
244+
245+ const testData = { name : 'test' , items : [ 1 , 2 , 3 , 4 , 5 ] } ;
246+ expect ( standaloneEstimator ( testData ) ) . toBe ( classEstimator ( testData ) ) ;
247+ expect ( standaloneEstimator ( testData ) ) . toBe ( maxEncodingCapacity ( testData ) ) ;
248+ } ) ;
249+
250+ test ( 'works with const types' , ( ) => {
251+ const system = new TypeSystem ( ) ;
252+ const type = system . t . Const ( 'hello world' ) ;
253+
254+ const { codegen} = require ( '../estimators' ) ;
255+ const standaloneEstimator = codegen ( type , { } ) ;
256+
257+ // For const types, the value doesn't matter
258+ expect ( standaloneEstimator ( null ) ) . toBe ( maxEncodingCapacity ( 'hello world' ) ) ;
259+ } ) ;
260+ } ) ;
0 commit comments