@@ -70,6 +70,19 @@ func ExampleCompile() {
7070 // Output: true
7171}
7272
73+ func ExampleEval_bytes_literal () {
74+ // Bytes literal returns []byte.
75+ output , err := expr .Eval (`b"abc"` , nil )
76+ if err != nil {
77+ fmt .Printf ("%v" , err )
78+ return
79+ }
80+
81+ fmt .Printf ("%v" , output )
82+
83+ // Output: [97 98 99]
84+ }
85+
7386func TestDisableIfOperator_AllowsIfFunction (t * testing.T ) {
7487 env := map [string ]any {
7588 "if" : func (x int ) int { return x + 1 },
@@ -2929,3 +2942,60 @@ func TestDisableShortCircuit(t *testing.T) {
29292942 assert .Equal (t , 3 , count )
29302943 assert .True (t , got .(bool ))
29312944}
2945+
2946+ func TestBytesLiteral (t * testing.T ) {
2947+ tests := []struct {
2948+ code string
2949+ want []byte
2950+ }{
2951+ {`b"hello"` , []byte ("hello" )},
2952+ {`b'world'` , []byte ("world" )},
2953+ {`b""` , []byte {}},
2954+ {`b'\x00\xff'` , []byte {0 , 255 }},
2955+ {`b"\x41\x42\x43"` , []byte ("ABC" )},
2956+ {`b'\101\102\103'` , []byte ("ABC" )},
2957+ {`b'\n\t\r'` , []byte {'\n' , '\t' , '\r' }},
2958+ {`b'hello\x00world'` , []byte ("hello\x00 world" )},
2959+ {`b"ÿ"` , []byte {0xc3 , 0xbf }}, // UTF-8 encoding of ÿ
2960+ }
2961+
2962+ for _ , tt := range tests {
2963+ t .Run (tt .code , func (t * testing.T ) {
2964+ program , err := expr .Compile (tt .code )
2965+ require .NoError (t , err )
2966+
2967+ output , err := expr .Run (program , nil )
2968+ require .NoError (t , err )
2969+ assert .Equal (t , tt .want , output )
2970+ })
2971+ }
2972+ }
2973+
2974+ func TestBytesLiteral_type (t * testing.T ) {
2975+ env := map [string ]any {
2976+ "data" : []byte ("test" ),
2977+ }
2978+
2979+ // Verify bytes literal has []byte type and can be compared with []byte
2980+ program , err := expr .Compile (`data == b"test"` , expr .Env (env ))
2981+ require .NoError (t , err )
2982+
2983+ output , err := expr .Run (program , env )
2984+ require .NoError (t , err )
2985+ assert .Equal (t , true , output )
2986+ }
2987+
2988+ func TestBytesLiteral_errors (t * testing.T ) {
2989+ // \u and \U escapes should not be allowed in bytes literals
2990+ errorCases := []string {
2991+ `b'\u0041'` ,
2992+ `b"\U00000041"` ,
2993+ }
2994+
2995+ for _ , code := range errorCases {
2996+ t .Run (code , func (t * testing.T ) {
2997+ _ , err := expr .Compile (code )
2998+ require .Error (t , err )
2999+ })
3000+ }
3001+ }
0 commit comments