@@ -596,6 +596,30 @@ mod tests {
596
596
use bitcoin:: blockdata:: opcodes:: all as opcodes;
597
597
use quote:: quote;
598
598
599
+ macro_rules! test_opcode {
600
+ ( $name: ident, $input: expr, $expected: expr) => {
601
+ #[ test]
602
+ fn $name( ) {
603
+ let syntax = parse( quote!( $input) ) ;
604
+ if let Syntax :: Opcode ( opcode) = & syntax[ 0 ] . 0 {
605
+ assert_eq!( * opcode, $expected) ;
606
+ } else {
607
+ panic!( "Expected Syntax::Opcode, got {:?}" , syntax[ 0 ] . 0 ) ;
608
+ }
609
+ }
610
+ } ;
611
+ }
612
+
613
+ macro_rules! test_invalid_opcode {
614
+ ( $name: ident, $input: expr) => {
615
+ #[ test]
616
+ fn $name( ) {
617
+ let syntax = parse( quote!( $input) ) ;
618
+ assert!( matches!( syntax[ 0 ] . 0 , Syntax :: Escape ( _) ) ) ;
619
+ }
620
+ } ;
621
+ }
622
+
599
623
#[ test]
600
624
fn parse_empty ( ) {
601
625
assert ! ( parse( quote!( ) ) . is_empty( ) ) ;
@@ -613,6 +637,66 @@ mod tests {
613
637
// parse(quote!(OP_CHECKSIG A B));
614
638
//}
615
639
640
+ // Basic opcode tests
641
+ test_opcode ! ( parse_op_0, OP_0 , OP_0 ) ;
642
+ test_opcode ! ( parse_op_false, FALSE , OP_FALSE ) ;
643
+ test_opcode ! ( parse_op_true, TRUE , OP_TRUE ) ;
644
+ test_opcode ! ( parse_op_checksig, OP_CHECKSIG , OP_CHECKSIG ) ;
645
+ test_opcode ! ( parse_op_hash160, OP_HASH160 , OP_HASH160 ) ;
646
+
647
+ // Test numeric opcodes
648
+ test_opcode ! ( parse_op_1, OP_1 , OP_PUSHNUM_1 ) ;
649
+ test_opcode ! ( parse_op_2, OP_2 , OP_PUSHNUM_2 ) ;
650
+ test_opcode ! ( parse_op_3, OP_3 , OP_PUSHNUM_3 ) ;
651
+ test_opcode ! ( parse_op_16, OP_16 , OP_PUSHNUM_16 ) ;
652
+
653
+ // Test aliases
654
+ test_opcode ! ( parse_checksig_no_prefix, CHECKSIG , OP_CHECKSIG ) ;
655
+ test_opcode ! ( parse_hash160_no_prefix, HASH160 , OP_HASH160 ) ;
656
+
657
+ // Test special cases
658
+ test_opcode ! ( parse_nop2, OP_NOP2 , OP_CLTV ) ;
659
+ test_opcode ! ( parse_nop3, OP_NOP3 , OP_CSV ) ;
660
+ test_opcode ! ( parse_debug, DEBUG , OP_RESERVED ) ;
661
+
662
+ // Test invalid opcodes
663
+ test_invalid_opcode ! ( parse_invalid_opcode, INVALID_OPCODE ) ;
664
+ test_invalid_opcode ! ( parse_unknown_identifier, UNKNOWN ) ;
665
+
666
+ // Test complex scripts
667
+ #[ test]
668
+ fn parse_complex_script ( ) {
669
+ let syntax = parse ( quote ! {
670
+ OP_DUP OP_HASH160 0x14 0x89abcdef89abcdef89abcdef89abcdef89abcdef OP_EQUALVERIFY OP_CHECKSIG
671
+ } ) ;
672
+
673
+ assert_eq ! ( syntax. len( ) , 6 ) ;
674
+ assert ! ( matches!( syntax[ 0 ] . 0 , Syntax :: Opcode ( OP_DUP ) ) ) ;
675
+ assert ! ( matches!( syntax[ 1 ] . 0 , Syntax :: Opcode ( OP_HASH160 ) ) ) ;
676
+ assert ! ( matches!( syntax[ 2 ] . 0 , Syntax :: Int ( 20 ) ) ) ; // 0x14 = 20
677
+ assert ! ( matches!( syntax[ 3 ] . 0 , Syntax :: Bytes ( _) ) ) ;
678
+ assert ! ( matches!( syntax[ 4 ] . 0 , Syntax :: Opcode ( OP_EQUALVERIFY ) ) ) ;
679
+ assert ! ( matches!( syntax[ 5 ] . 0 , Syntax :: Opcode ( OP_CHECKSIG ) ) ) ;
680
+ }
681
+
682
+ #[ test]
683
+ fn parse_p2pkh_script ( ) {
684
+ let syntax = parse ( quote ! {
685
+ OP_DUP
686
+ OP_HASH160
687
+ <pubkey_hash>
688
+ OP_EQUALVERIFY
689
+ OP_CHECKSIG
690
+ } ) ;
691
+
692
+ assert_eq ! ( syntax. len( ) , 5 ) ;
693
+ assert ! ( matches!( syntax[ 0 ] . 0 , Syntax :: Opcode ( OP_DUP ) ) ) ;
694
+ assert ! ( matches!( syntax[ 1 ] . 0 , Syntax :: Opcode ( OP_HASH160 ) ) ) ;
695
+ assert ! ( matches!( syntax[ 2 ] . 0 , Syntax :: Escape ( _) ) ) ;
696
+ assert ! ( matches!( syntax[ 3 ] . 0 , Syntax :: Opcode ( OP_EQUALVERIFY ) ) ) ;
697
+ assert ! ( matches!( syntax[ 4 ] . 0 , Syntax :: Opcode ( OP_CHECKSIG ) ) ) ;
698
+ }
699
+
616
700
#[ test]
617
701
fn parse_opcodes ( ) {
618
702
let syntax = parse ( quote ! ( OP_CHECKSIG OP_HASH160 ) ) ;
0 commit comments