@@ -236,30 +236,43 @@ mod tests {
236
236
use std:: collections:: HashMap ;
237
237
use std:: sync:: Arc ;
238
238
239
+ fn get_root ( input : & Option < Arc < Value > > ) -> & ' static str {
240
+ if input. is_none ( ) {
241
+ "main"
242
+ } else {
243
+ "expr"
244
+ }
245
+ }
246
+
239
247
fn assert_finalize_ok < J : Jet > (
240
248
s : & str ,
241
249
witness : & HashMap < Arc < str > , Arc < Value > > ,
242
250
env : & J :: Environment ,
251
+ input : Option < Arc < Value > > ,
243
252
) {
244
253
let program = Forest :: < J > :: parse ( s)
245
254
. expect ( "Failed to parse human encoding" )
246
- . to_witness_node ( witness)
255
+ . to_witness_node_expression ( witness, get_root ( & input ) )
247
256
. expect ( "Forest is missing expected root" )
248
257
. finalize ( )
249
258
. expect ( "Failed to finalize" ) ;
250
259
let mut mac = BitMachine :: for_program ( & program) ;
260
+ if let Some ( value) = input {
261
+ mac. input ( value) . expect ( "Failed to provide input" ) ;
262
+ }
251
263
mac. exec ( & program, env) . expect ( "Failed to run program" ) ;
252
264
}
253
265
254
266
fn assert_finalize_err < J : Jet > (
255
267
s : & str ,
256
268
witness : & HashMap < Arc < str > , Arc < Value > > ,
257
269
env : & J :: Environment ,
270
+ input : Option < Arc < Value > > ,
258
271
err_msg : & ' static str ,
259
272
) {
260
273
let program = match Forest :: < J > :: parse ( s)
261
274
. expect ( "Failed to parse human encoding" )
262
- . to_witness_node ( witness)
275
+ . to_witness_node_expression ( witness, get_root ( & input ) )
263
276
. expect ( "Forest is missing expected root" )
264
277
. finalize ( )
265
278
{
@@ -270,6 +283,12 @@ mod tests {
270
283
}
271
284
} ;
272
285
let mut mac = BitMachine :: for_program ( & program) ;
286
+ if let Some ( value) = input {
287
+ if let Err ( error) = mac. input ( value) {
288
+ assert_eq ! ( error. to_string( ) . as_str( ) , err_msg) ;
289
+ return ;
290
+ }
291
+ }
273
292
match mac. exec ( & program, env) {
274
293
Ok ( _) => panic ! ( "Execution is expected to fail" ) ,
275
294
Err ( error) => assert_eq ! ( & error. to_string( ) , err_msg) ,
@@ -292,13 +311,19 @@ mod tests {
292
311
( Arc :: from ( "a" ) , Value :: u8 ( 0x00 ) ) ,
293
312
( Arc :: from ( "b" ) , Value :: u8 ( 0x01 ) ) ,
294
313
] ) ;
295
- assert_finalize_ok :: < Core > ( s, & a_less_than_b, & ( ) ) ;
314
+ assert_finalize_ok :: < Core > ( s, & a_less_than_b, & ( ) , None ) ;
296
315
297
316
let b_greater_equal_a = HashMap :: from ( [
298
317
( Arc :: from ( "a" ) , Value :: u8 ( 0x01 ) ) ,
299
318
( Arc :: from ( "b" ) , Value :: u8 ( 0x01 ) ) ,
300
319
] ) ;
301
- assert_finalize_err :: < Core > ( s, & b_greater_equal_a, & ( ) , "Jet failed during execution" ) ;
320
+ assert_finalize_err :: < Core > (
321
+ s,
322
+ & b_greater_equal_a,
323
+ & ( ) ,
324
+ None ,
325
+ "Jet failed during execution" ,
326
+ ) ;
302
327
}
303
328
304
329
#[ test]
@@ -314,6 +339,7 @@ mod tests {
314
339
" ,
315
340
& witness,
316
341
& ( ) ,
342
+ None ,
317
343
"unable to satisfy program" ,
318
344
) ;
319
345
}
@@ -326,7 +352,7 @@ mod tests {
326
352
main := comp (pair wit1 unit) case unit wit2
327
353
" ;
328
354
let wit2_is_pruned = HashMap :: from ( [ ( Arc :: from ( "wit1" ) , Value :: u1 ( 0 ) ) ] ) ;
329
- assert_finalize_ok :: < Core > ( s, & wit2_is_pruned, & ( ) ) ;
355
+ assert_finalize_ok :: < Core > ( s, & wit2_is_pruned, & ( ) , None ) ;
330
356
331
357
let wit2_is_missing = HashMap :: from ( [ ( Arc :: from ( "wit1" ) , Value :: u1 ( 1 ) ) ] ) ;
332
358
// FIXME The finalization should fail
@@ -335,15 +361,15 @@ mod tests {
335
361
assert_finalize_err :: < Core > (
336
362
s,
337
363
& wit2_is_missing,
338
- & ( ) ,
364
+ & ( ) , None ,
339
365
"Execution reached a pruned branch: bf12681a76fc7c00c63e583c25cc97237337d6aca30d3f4a664075445385c648"
340
366
) ;
341
367
342
368
let wit2_is_present = HashMap :: from ( [
343
369
( Arc :: from ( "wit1" ) , Value :: u1 ( 1 ) ) ,
344
370
( Arc :: from ( "wit2" ) , Value :: unit ( ) ) ,
345
371
] ) ;
346
- assert_finalize_ok :: < Core > ( s, & wit2_is_present, & ( ) ) ;
372
+ assert_finalize_ok :: < Core > ( s, & wit2_is_present, & ( ) , None ) ;
347
373
}
348
374
349
375
#[ test]
@@ -357,6 +383,7 @@ mod tests {
357
383
" ,
358
384
& empty,
359
385
& ( ) ,
386
+ None ,
360
387
) ;
361
388
}
362
389
@@ -370,6 +397,7 @@ mod tests {
370
397
" ,
371
398
& empty,
372
399
& ( ) ,
400
+ None ,
373
401
"unable to satisfy program" ,
374
402
) ;
375
403
}
@@ -382,9 +410,41 @@ mod tests {
382
410
main := comp wit2 iden
383
411
" ;
384
412
let wit1_populated = HashMap :: from ( [ ( Arc :: from ( "wit1" ) , Value :: unit ( ) ) ] ) ;
385
- assert_finalize_err :: < Core > ( s, & wit1_populated, & ( ) , "unable to satisfy program" ) ;
413
+ assert_finalize_err :: < Core > ( s, & wit1_populated, & ( ) , None , "unable to satisfy program" ) ;
386
414
387
415
let wit2_populated = HashMap :: from ( [ ( Arc :: from ( "wit2" ) , Value :: unit ( ) ) ] ) ;
388
- assert_finalize_ok :: < Core > ( s, & wit2_populated, & ( ) ) ;
416
+ assert_finalize_ok :: < Core > ( s, & wit2_populated, & ( ) , None ) ;
417
+ }
418
+
419
+ #[ test]
420
+ fn expression ( ) {
421
+ let s = "
422
+ expr := iden
423
+ " ;
424
+ let empty = HashMap :: new ( ) ;
425
+ assert_finalize_ok :: < Core > ( s, & empty, & ( ) , Some ( Value :: unit ( ) ) ) ;
426
+
427
+ let s = "
428
+ expr := comp
429
+ comp
430
+ pair iden (comp unit const 0xff)
431
+ jet_eq_8
432
+ jet_verify
433
+ " ;
434
+ assert_finalize_err :: < Core > (
435
+ s,
436
+ & empty,
437
+ & ( ) ,
438
+ Some ( Value :: unit ( ) ) ,
439
+ "Expected input of type: 2^8" ,
440
+ ) ;
441
+ assert_finalize_ok :: < Core > ( s, & empty, & ( ) , Some ( Value :: u8 ( 0xff ) ) ) ;
442
+ assert_finalize_err :: < Core > (
443
+ s,
444
+ & empty,
445
+ & ( ) ,
446
+ Some ( Value :: u8 ( 0x00 ) ) ,
447
+ "Jet failed during execution" ,
448
+ ) ;
389
449
}
390
450
}
0 commit comments