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