@@ -307,7 +307,12 @@ where
307
307
308
308
#[ cfg( test) ]
309
309
mod tests {
310
- use super :: parse_num;
310
+ use super :: * ;
311
+
312
+ /// Test functions to manually build trees
313
+ fn leaf ( name : & str ) -> Tree { Tree { name, args : vec ! [ ] } }
314
+
315
+ fn paren_node < ' a > ( name : & ' a str , args : Vec < Tree < ' a > > ) -> Tree < ' a > { Tree { name, args } }
311
316
312
317
#[ test]
313
318
fn test_parse_num ( ) {
@@ -327,4 +332,63 @@ mod tests {
327
332
}
328
333
assert_eq ! ( valid_chars, super :: VALID_CHARS ) ;
329
334
}
335
+
336
+ #[ test]
337
+ fn parse_tree_basic ( ) {
338
+ assert_eq ! ( Tree :: from_str( "thresh" ) . unwrap( ) , leaf( "thresh" ) ) ;
339
+
340
+ assert ! ( matches!( Tree :: from_str( "thresh," ) , Err ( Error :: Unexpected ( s) ) if s == "," ) ) ;
341
+
342
+ assert ! ( matches!(
343
+ Tree :: from_str( "thresh,thresh" ) ,
344
+ Err ( Error :: Unexpected ( s) ) if s == ",thresh" ,
345
+ ) ) ;
346
+
347
+ assert ! ( matches!(
348
+ Tree :: from_str( "thresh()thresh()" ) ,
349
+ Err ( Error :: Unexpected ( s) ) if s == "thresh()" ,
350
+ ) ) ;
351
+
352
+ assert_eq ! ( Tree :: from_str( "thresh()" ) . unwrap( ) , paren_node( "thresh" , vec![ leaf( "" ) ] ) ) ;
353
+
354
+ // FIXME even for our current crappy error handling, this one is pretty bad
355
+ assert ! ( matches!( Tree :: from_str( "thresh(a()b)" ) , Err ( Error :: ExpectedChar ( ')' ) ) ) ) ;
356
+
357
+ assert ! ( matches!( Tree :: from_str( "thresh()xyz" ) , Err ( Error :: Unexpected ( s) ) if s == "xyz" ) ) ;
358
+ }
359
+
360
+ #[ test]
361
+ fn parse_tree_parens ( ) {
362
+ assert ! ( matches!( Tree :: from_str( "a(" ) , Err ( Error :: ExpectedChar ( ')' ) ) ) ) ;
363
+
364
+ assert ! ( matches!( Tree :: from_str( ")" ) , Err ( Error :: Unexpected ( s) ) if s == ")" ) ) ;
365
+
366
+ assert ! ( matches!( Tree :: from_str( "x(y))" ) , Err ( Error :: Unexpected ( s) ) if s == ")" ) ) ;
367
+
368
+ // In next commit will add tests related to {}s; currently we ignore
369
+ // these except in Taproot mode.
370
+ }
371
+
372
+ #[ test]
373
+ fn parse_tree_desc ( ) {
374
+ let keys = [
375
+ "02c2fd50ceae468857bb7eb32ae9cd4083e6c7e42fbbec179d81134b3e3830586c" ,
376
+ "0257f4a2816338436cccabc43aa724cf6e69e43e84c3c8a305212761389dd73a8a" ,
377
+ ] ;
378
+ let desc = format ! ( "wsh(t:or_c(pk({}),v:pkh({})))" , keys[ 0 ] , keys[ 1 ] ) ;
379
+
380
+ assert_eq ! (
381
+ Tree :: from_str( & desc) . unwrap( ) ,
382
+ paren_node(
383
+ "wsh" ,
384
+ vec![ paren_node(
385
+ "t:or_c" ,
386
+ vec![
387
+ paren_node( "pk" , vec![ leaf( keys[ 0 ] ) ] ) ,
388
+ paren_node( "v:pkh" , vec![ leaf( keys[ 1 ] ) ] ) ,
389
+ ]
390
+ ) ]
391
+ ) ,
392
+ ) ;
393
+ }
330
394
}
0 commit comments