zsh-workers
 help / color / mirror / code / Atom feed
7ec43adee6cbed373d3df54154752a3dc281d6db blob 24387 bytes (raw)

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
 
# There are certain usages of typeset and its synonyms that it is not
# possible to test here, because they must appear at the top level and
# everything that follows is processed by an "eval" within a function.

# Equivalences:
#  declare	typeset
#  export	typeset -xg
#  float	typeset -E
#  functions	typeset -f
#  integer	typeset -i
#  local	typeset +g -m		approximately
#  readonly	typeset -r

# Tested elsewhere:
#  Equivalence of autoload and typeset -fu 	A05execution
#  Associative array creation & assignment	D04parameter, D06subscript
#  Effects of GLOBAL_EXPORT			E01options
#  Function tracing (typeset -ft)		E02xtrace

# Not yet tested:
#  Assorted illegal flag combinations

# For a few tests, we include a
#    typeset -p param
#    typeset -m param
#    typeset +m param
# to test the proper output of typeset for a number of different types
# of variables. Note that we can't use a dedicated function to factorize
# that code, as that would affect the scoping.

%prep
  ## Do not remove the next line, it's used by V10private.ztst
  # test_zsh_param_private

  mkdir typeset.tmp && cd typeset.tmp

  setopt noglob

  scalar=scalar
  array=(a r r a y)

  scope00() {
    typeset scalar
    scalar=local
    typeset -a array
    array=(l o c a l)
    print $scalar $array
    typeset -p scalar array
    typeset -m scalar array
    typeset +m scalar array
  }
  scope01() {
    local scalar
    scalar=local
    local -a array
    array=(l o c a l)
    print $scalar $array
    typeset -p scalar array
    typeset -m scalar array
    typeset +m scalar array
  }
  scope02() {
    declare scalar
    scalar=local
    declare -a array
    array=(l o c a l)
    print $scalar $array
    typeset -p scalar array
    typeset -m scalar array
    typeset +m scalar array
  }
  scope10() {
    export outer=outer
    /bin/sh -fc 'echo $outer'
    typeset -p outer
    typeset -m outer
    typeset +m outer
  }
  scope11() {
    typeset -x outer=outer
    /bin/sh -fc 'echo $outer'
  }
  scope12() {
    local -x outer=inner
    /bin/sh -fc 'echo $outer'
  }
  scope13() {
    local -xT OUTER outer
    outer=(i n n e r)
    /bin/sh -fc 'echo $OUTER'
    typeset -p OUTER outer
    typeset -m OUTER outer
    typeset +m OUTER outer
  }

  # Bug?  `typeset -h' complains that ! # $ * - ? @ are not identifiers.
  stress00() {
    typeset -h +g -m [[:alpha:]_]*
    unset -m [[:alpha:]_]*
    typeset +m [[:alpha:]_]*
  }

%test

 typeset -p scalar array
 typeset -m scalar array
 typeset +m scalar array
0:Report types for global variables
>typeset -g scalar=scalar
>typeset -g -a array=( a r r a y )
>scalar=scalar
>array=( a r r a y )
>scalar
>array array

 scope00
 print $scalar $array
0:Simple local declarations
>local l o c a l
>typeset scalar=local
>typeset -a array=( l o c a l )
>scalar=local
>array=( l o c a l )
>local scalar
>array local array
>scalar a r r a y

 scope01
 print $scalar $array
0:Equivalence of local and typeset in functions
>local l o c a l
>typeset scalar=local
>typeset -a array=( l o c a l )
>scalar=local
>array=( l o c a l )
>local scalar
>array local array
>scalar a r r a y

 scope02
 print $scalar $array
0:Basic equivalence of declare and typeset
>local l o c a l
>typeset scalar=local
>typeset -a array=( l o c a l )
>scalar=local
>array=( l o c a l )
>local scalar
>array local array
>scalar a r r a y

 declare +m scalar
0:declare previously lacked -m/+m options
>scalar

 scope10
 print $outer
0:Global export
>outer
>export outer=outer
>outer=outer
>outer
>outer

 scope11
 print $outer
0:Equivalence of export and typeset -x
>outer
>outer

 scope12
 print $outer
0:Local export
>inner
>outer

 float f=3.14159
 typeset +m f
 float -E3 f
 print $f
 float -F f
 print $f
 typeset -p f
 typeset -m f
 typeset +m f
0:Floating point, adding a precision, and fixed point
>float local f
>3.14e+00
>3.142
>typeset -F f=3.142
>f=3.142
>float local f

 integer i=3.141
 typeset +m i
 integer -i2 i
 print $i
 typeset -p i
 typeset -m i
 typeset +m i
0:Integer and changing the base
>integer local i
>2#11
>typeset -i2 i=3
>i=3
>integer 2 local i

 float -E3 f=3.141
 typeset +m f
 integer -i2 f
 typeset +m f
 print $f
0:Conversion of floating point to integer
>float local f
>integer 2 local f
>2#11

 typeset -f
0q:Equivalence of functions and typeset -f
>$(functions)

 readonly r=success
 print $r
 r=failure
1:Readonly declaration
>success
?(eval):3: read-only variable: r

 typeset r=success
 readonly r
 print $r
 r=failure
1:Convert to readonly
>success
?(eval):4: read-only variable: r

 typeset -gU array
 print $array
 typeset -p array
 typeset -m array
 typeset +m array
0:Uniquified arrays and non-local scope
>a r y
>typeset -g -aU array=( a r y )
>array=( a r y )
>array unique array

 typeset -T SCALAR=l:o:c:a:l array
 print $array
 typeset -U SCALAR
 print $SCALAR $array
 typeset -p SCALAR array
 typeset -m SCALAR array
 typeset +m SCALAR array
 print ${(t)SCALAR} ${(t)array}
0:Tied parameters and uniquified colon-arrays
>l o c a l
>l:o:c:a l o c a
>typeset -UT SCALAR array=( l o c a )
>typeset -aT SCALAR array=( l o c a )
>SCALAR=l:o:c:a
>array=( l o c a )
>local unique tied array SCALAR
>array local tied SCALAR array
>scalar-local-tied-unique array-local-tied

 (setopt NO_multibyte cbases
 LC_ALL=C 2>/dev/null
 typeset -T SCALAR=$'l\x83o\x83c\x83a\x83l' array $'\x83'
 print $array
 typeset -U SCALAR
 for (( i = 1; i <= ${#SCALAR}; i++ )); do
   char=$SCALAR[i]
   print $(( [#16] #char ))
 done
 print $array)
0:Tied parameters and uniquified arrays with meta-character as separator
>l o c a l
>0x6C
>0x83
>0x6F
>0x83
>0x63
>0x83
>0x61
>l o c a

 typeset -T SCALAR=$'l\000o\000c\000a\000l' array $'\000'
 typeset -U SCALAR
 print $array
 typeset -p SCALAR array
 typeset -m SCALAR array
 typeset +m SCALAR array
 [[ $SCALAR == $'l\000o\000c\000a' ]]
0:Tied parameters and uniquified arrays with NUL-character as separator
>l o c a
>typeset -UT SCALAR array=( l o c a ) ''
>typeset -aT SCALAR array=( l o c a ) ''
>SCALAR=$'l\C-@o\C-@c\C-@a'
>array=( l o c a )
>local unique tied array SCALAR
>array local tied SCALAR array

 typeset -T SCALAR array
 typeset +T SCALAR
1:Untying is prohibited
?(eval):typeset:2: use unset to remove tied variables

 OUTER=outer
 scope13
 print $OUTER
0:Export of tied parameters
>i:n:n:e:r
>typeset -xT OUTER outer=( i n n e r )
>typeset -aT OUTER outer=( i n n e r )
>OUTER=i:n:n:e:r
>outer=( i n n e r )
>local exported tied outer OUTER
>array local tied OUTER outer
>outer

 typeset -TU MORESTUFF=here-we-go-go-again morestuff '-'
 print -l $morestuff
 typeset -p MORESTUFF morestuff
 typeset -m MORESTUFF morestuff
 typeset +m MORESTUFF morestuff
0:Tied arrays with separator specified
>here
>we
>go
>again
>typeset -UT MORESTUFF morestuff=( here we go again ) -
>typeset -aUT MORESTUFF morestuff=( here we go again ) -
>MORESTUFF=here-we-go-again
>morestuff=( here we go again )
>local unique tied morestuff MORESTUFF
>array local unique tied MORESTUFF morestuff

 typeset -T THIS will not work
1:Tied array syntax
?(eval):typeset:1: too many arguments for -T

 local array[2]=x
1:Illegal local array element assignment
?(eval):local:1: array[2]: can't create local array elements

 local -a array
 typeset array[1]=a array[2]=b array[3]=c
 print $array
0:Legal local array element assignment
>a b c

 local -A assoc
 local b=1 ;: to stomp assoc[1] if assoc[b] is broken
 typeset assoc[1]=a assoc[b]=2 assoc[3]=c
 print $assoc[1] $assoc[b] $assoc[3]
 typeset -p assoc
 typeset -m assoc
 typeset +m assoc
0:Legal local associative array element assignment
>a 2 c
>typeset -A assoc=( [1]=a [3]=c [b]=2 )
>assoc=( [1]=a [3]=c [b]=2 )
>association local assoc

 local scalar scalar[1]=a scalar[2]=b scalar[3]=c
 print $scalar
 typeset -p scalar
 typeset -m scalar
 typeset +m scalar
0:Local scalar subscript assignment
>abc
>typeset scalar=abc
>scalar=abc
>local scalar

 typeset -L 10 fools
 for fools in "   once" "twice"  "      thrice" "   oops too long here"; do
   print "'$fools'"
 done
0:Left justification of scalars
>'once      '
>'twice     '
>'thrice    '
>'oops too l'

 typeset -L 10 -F 3 foolf
 for foolf in 1.3 4.6 -2.987 -4.91031; do
   print "'$foolf'"
 done
 typeset -p foolf
 typeset -m foolf
 typeset +m foolf
0:Left justification of floating point
>'1.300     '
>'4.600     '
>'-2.987    '
>'-4.910    '
>typeset -FL10 foolf=-4.910
>foolf=-4.910
>float local left justified 10 foolf

 typeset -L 10 -Z foolzs
 for foolzs in 001.3 04.6 -2.987 -04.91231; do
   print "'$foolzs'"
 done
0:Left justification of scalars with zero suppression
>'1.3       '
>'4.6       '
>'-2.987    '
>'-04.91231 '

 typeset -R 10 foors
 for foors in short longer even-longer; do
   print "'$foors'"
 done
 typeset -p foors
 typeset -m foors
 typeset +m foors
0:Right justification of scalars
>'     short'
>'    longer'
>'ven-longer'
>typeset -R10 foors=even-longer
>foors=even-longer
>local right justified 10 foors

 typeset -Z 10 foozs
 for foozs in 42 -42 " 43" " -43"; do
   print "'$foozs'"
 done
0:Right justification of scalars with zeroes
>'0000000042'
>'       -42'
>' 000000043'
>'       -43'

 integer -Z 10 foozi
 for foozi in 42 -42 " 43" " -43"; do
   print "'$foozi'"
 done
0:Right justification of integers with zero, no initial base
>'0000000042'
>'-000000042'
>'0000000043'
>'-000000043'
# In case you hadn't twigged, the spaces are absorbed in the initial
# math evaluation, so don't get through.

 unsetopt cbases
 integer -Z 10 -i 16 foozi16
 for foozi16 in 42 -42 " 43" " -43"; do
   print "'$foozi16'"
 done
0:Right justification of integers with zero, base 16, C_BASES off
>'16#000002A'
>'-16#00002A'
>'16#000002B'
>'-16#00002B'

 setopt cbases
 integer -Z 10 -i 16 foozi16c
 for foozi16c in 42 -42 " 43" " -43"; do
   print "'$foozi16c'"
 done
0:Right justification of integers with zero, base 16, C_BASES on
>'0x0000002A'
>'-0x000002A'
>'0x0000002B'
>'-0x000002B'

 setopt cbases
 integer -Z 10 -i 16 foozi16c
 for foozi16c in 0x1234 -0x1234; do
   for (( i = 1; i <= 5; i++ )); do
       print "'${foozi16c[i,11-i]}'"
   done
   print "'${foozi16c[-2]}'"
 done
0:Extracting substrings from padded integers
>'0x00001234'
>'x0000123'
>'000012'
>'0001'
>'00'
>'3'
>'-0x0001234'
>'0x000123'
>'x00012'
>'0001'
>'00'
>'3'

 typeset -F 3 -Z 10 foozf
 for foozf in 3.14159 -3.14159 4 -4; do
   print "'$foozf'"
 done
0:Right justification of fixed point numbers with zero
>'000003.142'
>'-00003.142'
>'000004.000'
>'-00004.000'

 stress00
 print $scalar $array
0q:Stress test: all parameters are local and unset, using -m
>scalar a r y

 local parentenv=preserved
 fn() {
  typeset -h +g -m \*
  unset -m \*
  integer i=9
  float -H f=9
  declare -t scalar
  declare -H -a array
  typeset
  typeset +
 }
 fn
 echo $parentenv
0:Parameter hiding and tagging, printing types and values
>array local array
>float local f
>integer local i=9
>local tagged scalar=''
>array local array
>float local f
>integer local i
>local tagged scalar
>preserved

 export ENVFOO=bar
 print ENVFOO in environment
 env | grep '^ENVFOO'
 print Changing ENVFOO
 ENVFOO="not bar any more"
 env | grep '^ENVFOO'
 unset ENVFOO
 print ENVFOO no longer in environment
 env | grep '^ENVFOO'
1:Adding and removing values to and from the environment
>ENVFOO in environment
>ENVFOO=bar
>Changing ENVFOO
>ENVFOO=not bar any more
>ENVFOO no longer in environment

 (export FOOENV=BAR
 env | grep '^FOOENV'
 print Exec
 exec $ZTST_testdir/../Src/zsh -fc '
 print Unset
 unset FOOENV
 env | grep "^FOOENV"')
1:Can unset environment variables after exec
>FOOENV=BAR
>Exec
>Unset

 local case1=upper
 typeset -u case1
 print $case1
 upper="VALUE OF \$UPPER"
 print ${(P)case1}
 typeset -p case1
 typeset -m case1
 typeset +m case1
0:Upper case conversion, does not apply to values used internally
>UPPER
>VALUE OF $UPPER
>typeset -u case1=upper
>case1=upper
>local uppercase case1

 local case2=LOWER
 typeset -l case2
 print $case2
 LOWER="value of \$lower"
 print ${(P)case2}
 typeset -p case2
 typeset -m case2
 typeset +m case2
0:Lower case conversion, does not apply to values used internally
>lower
>value of $lower
>typeset -l case2=LOWER
>case2=LOWER
>local lowercase case2

 typeset -a array
 array=(foo bar)
 fn() { typeset -p array nonexistent; }
 fn
1:typeset -p shouldn't create scoped values
>typeset -g -a array=( foo bar )
?fn:typeset: no such variable: nonexistent

 unsetopt typesetsilent
 silent1(){ typeset -g silence; }
 silent2(){ local silence; silent1; }
 silent2
0:typeset -g should be silent even without TYPESET_SILENT

 typeset -T TIED_SCALAR tied_array
 TIED_SCALAR=foo:bar
 print $tied_array
 typeset -T TIED_SCALAR=goo:car tied_array
 print $tied_array
 typeset -T TIED_SCALAR tied_array=(poo par)
 print $TIED_SCALAR
0:retying arrays to same array works
>foo bar
>goo car
>poo:par

 (
   setopt POSIXBUILTINS
   readonly pbro
   print ${+pbro} >&2
   (typeset -g pbro=3)
   (pbro=4)
   readonly -p >&2        # shows up as "readonly" although unset
   typeset -gr pbro       # idempotent (no error)...
   print ${+pbro} >&2     # ...so still readonly...
   typeset -g +r pbro     # ...can't turn it off
 )
1:readonly with POSIX_BUILTINS
?0
?(eval):5: read-only variable: pbro
?(eval):6: read-only variable: pbro
?readonly pbro
?0
?(eval):10: read-only variable: pbro

  readonly foo=bar novalue
  readonly -p
0:readonly -p output (no readonly specials)
>typeset -r foo=bar
>typeset -r novalue=''

  local -a a1 a2
  local -r r1=yes r2=no
  a1=(one two) a2=(three four)
  readonly a1
  typeset -pm 'a[12]'
  typeset -pm 'r[12]'
0:readonly -p output
>typeset -ar a1=( one two )
>typeset -a a2=( three four )
>typeset -r r1=yes
>typeset -r r2=no

  one=hidden two=hidden three=hidden four=hidden five=hidden
  fn() {
     local bleugh="four=vier"
     typeset -R10 one=eins two=(zwei dio) three $bleugh five=(cinq cinque)
     three=drei
     print -l $one $two $three $four $five
  }
  fn
  print -l $one $two $three $four $five
0:typeset reserved word interface: basic
>      eins
>zwei
>dio
>      drei
>      vier
>cinq
>cinque
>hidden
>hidden
>hidden
>hidden
>hidden

  (
  setopt glob
  mkdir -p arrayglob
  touch arrayglob/{one,two,three,four,five,six,seven}
  fn() {
    typeset array=(arrayglob/[tf]*)
    print -l ${array:t}
    #
    typeset {first,second,third}=the_same_value array=(
    extends
    over
    multiple
    lines
    )
    print -l $first $second $third "$array"
    #
    integer i=$(echo 1 + 2 + 3 + 4)
    print $i
    #
    # only noted by accident this was broken..
    # we need to turn off special recognition
    # of assignments within assignments...
    typeset careful=( i=1 j=2 k=3 )
    print -l $careful
  }
  fn
  )
0:typeset reserved word, more complicated cases
>five
>four
>three
>two
>the_same_value
>the_same_value
>the_same_value
>extends over multiple lines
>10
>i=1
>j=2
>k=3

  (
     # reserved word is recognised at parsing.
     # yes, this is documented.
     # anyway, that means we need to
     # re-eval the function...
     fn='
     fn() {
        typeset foo=`echo one word=two`
        print $foo
        print $word
     }
     '
     print reserved
     eval $fn; fn
     print builtin
     disable -r typeset
     eval $fn; fn
     enable -r typeset
     disable typeset
     print reserved
     eval $fn; fn
  )
0:reserved word and builtin interfaces
>reserved
>one word=two
>
>builtin
>one
>two
>reserved
>one word=two
>

  fn() {
    emulate -L zsh
    setopt typeset_silent
    local k
    typeset -A hash=(k1 v1 k2 v2)
    typeset foo=word array=(more than one word)
    for k in ${(ko)hash}; do
      print $k $hash[$k]
    done
    print -l $foo $array
    typeset -A hash
    typeset foo array
    for k in ${(ko)hash}; do
      print $k $hash[$k]
    done
    print -l $foo $array
    typeset hash=(k3 v3 k4 v4) array=(odd number here)
    for k in ${(ko)hash}; do
      print $k $hash[$k]
    done
    print -l $array
  }
  fn
0:typeset preserves existing variable types
>k1 v1
>k2 v2
>word
>more
>than
>one
>word
>k1 v1
>k2 v2
>word
>more
>than
>one
>word
>k3 v3
>k4 v4
>odd
>number
>here

  fn() { typeset foo bar thing=this stuff=(that other) more=woevva; }
  which -x2 fn
  fn2() { typeset assignfirst=(why not); }
  which -x2 fn2
0:text output from typeset
>function fn {
>  typeset foo bar thing=this stuff=(that other) more=woevva 
>}
>function fn2 {
>  typeset assignfirst=(why not) 
>}

  fn() {
    typeset array=()
    print ${(t)array} ${#array}
    typeset gnothergarray=() gnothergarray[1]=yes gnothergarray[2]=no
    print -l ${(t)gnothergarray} $gnothergarray
  }
  fn
0:can set empty array
>array-local 0
>array-local
>yes
>no

  array=(nothing to see here)
  fn() {
    typeset array=(one two three four five)
    typeset array[2,4]=(umm er)
    print ${#array} $array
    typeset array[2,3]=()
    print ${#array} $array
  }
  fn
  print ${#array} $array
0:can update array slices in typeset
>4 one umm er five
>2 one five
>4 nothing to see here

  array=(no really nothing here)
  fn() {
    typeset array=() array[2]=two array[4]=four
    typeset -p array
    typeset array=() array[3]=three array[1]=one
    typeset -p array
  }
  fn
  print $array
0:setting empty array in typeset
>typeset -a array=( '' two '' four )
>typeset -a array=( one '' three )
>no really nothing here

  readonly isreadonly=yes
  typeset isreadonly=still
1:typeset returns status 1 if setting readonly variable
?(eval):2: read-only variable: isreadonly

  if (( UID )); then
    UID=$((UID+1)) date; echo "Status is printed, $?"
  else
    ZTST_skip="cannot test setuid error when tests run as superuser"
  fi
0:when cannot change UID, the command isn't run
# 'date' did not run.
>Status is printed, 1
*?*: failed to change user ID: *

 typeset -A keyvalhash=([one]=eins [two]=zwei)
 keyvalhash+=([three]=drei)
 for key in ${(ok)keyvalhash}; do
   print $key $keyvalhash[$key]
 done
0:[key]=val for hashes
>one eins
>three drei
>two zwei

  local keyvalarray=([1]=one [3]=three)
  print -l "${keyvalarray[@]}"
  keyvalarray+=([2]=two)
  print -l "${keyvalarray[@]}"
  local keyvalarray=([1]=one [3]=three)
  print -l "${keyvalarray[@]}"
0:[key]=val for normal arrays
>one
>
>three
>one
>two
>three
>one
>
>three

 touch foo Xnot_globbedX
 inkey="another key" val="another value"
 typeset -A keyvalhash=([$(echo the key)]=$(echo the value)
                        [$inkey]=$val
	                [*]=?not_globbed?)
 for key in ${(ok)keyvalhash}; do
   print -l $key $keyvalhash[$key]
 done
 typeset -A keyvalhash=([$(echo the key)]=$(echo the value)
                        [$inkey]=$val
	                [*]=?not_globbed?)
 for key in ${(ok)keyvalhash}; do
   print -l $key $keyvalhash[$key]
 done
 typeset -p keyvalhash
0:Substitution in [key]=val syntax
>*
>?not_globbed?
>another key
>another value
>the key
>the value
>*
>?not_globbed?
>another key
>another value
>the key
>the value
>typeset -A keyvalhash=( ['*']='?not_globbed?' ['another key']='another value' ['the key']='the value' )

  local keyvalarray=(first [2]=second third [6]=sixth seventh [5]=fifth new_sixth)
  print -l "${keyvalarray[@]}"
0:mixed syntax [key]=val with normal arrays
>first
>second
>third
>
>fifth
>new_sixth
>seventh

  local -A keyvalhash=(1 one [2]=two 3 three)
1:Mixed syntax with [key]=val not allowed for hash.
?(eval):1: bad [key]=value syntax for associative array

  local -a myarray
  typeset -p1 myarray
  myarray=("&" sand '""' "" plugh)
  typeset -p1 myarray
0:typeset -p1 output for array
>typeset -a myarray=()
>typeset -a myarray=(
>  '&'
>  sand
>  '""'
>  ''
>  plugh
>)

  local -A myhash
  typeset -p1 myhash
  myhash=([one]=two [three]= [four]="[]")
  typeset -p1 myhash
0:typeset -p1 output for associative array
>typeset -A myhash=()
>typeset -A myhash=(
>  [four]='[]'
>  [one]=two
>  [three]=''
>)

 (export PATH MANPATH
 path=(/bin)
 MANPATH=/
 # read-only special params like zsh_eval_context are not output by typeset -p
 specials=(path PATH manpath MANPATH zsh_eval_context ZSH_EVAL_CONTEXT)
 typeset -p $specials
 typeset -m $specials
 typeset +m $specials
 for var ($specials) print $var: ${(Pt)var}
 )
0:typeset output for some special tied parameters
>typeset -g -aT PATH path=( /bin )
>export -T PATH path=( /bin )
>typeset -g -aT MANPATH manpath=( / )
>export -T MANPATH manpath=( / )
>path=( /bin )
>PATH=/bin
>manpath=( / )
>MANPATH=/
>zsh_eval_context=( toplevel shfunc shfunc shfunc eval )
>ZSH_EVAL_CONTEXT=toplevel:shfunc:shfunc:shfunc:eval
>array tied PATH path
>tied path PATH
>array tied MANPATH manpath
>tied manpath MANPATH
>array readonly tied ZSH_EVAL_CONTEXT zsh_eval_context
>readonly tied zsh_eval_context ZSH_EVAL_CONTEXT
>path: array-tied-special
>PATH: scalar-tied-export-special
>manpath: array-tied-special
>MANPATH: scalar-tied-export-special
>zsh_eval_context: array-readonly-tied-special
>ZSH_EVAL_CONTEXT: scalar-readonly-tied-special

 typeset -T VAR var=(a b a b)
 typeset -UuT VAR var +
 print $VAR
0:redeclare a tied variable with different attributes
>A+B

 typeset -T VAR=a+b var
 typeset -T VAR var +
 print $var
0:colonarray re-split when changing the join character
>a b

 readonly -T VAR var=(a b)
 readonly -T VAR var +
1:cannot change the join character on a readonly tied variable
?(eval):1: read-only variable: var

 typeset -T FOO manpath
1:Can't tie a special tied array to a different variable
?(eval):typeset:1: manpath special parameter can only be tied to special parameter MANPATH

 typeset -T MANPATH foo
1:Can't tie a special tied scalar to a different variable
?(eval):typeset:1: MANPATH special parameter can only be tied to special parameter manpath

 typeset -T MANPATH manpath +
1:Can't change the join character of a special tied variable
?(eval):typeset:1: cannot change the join character of special tied parameters

 (){
   typeset -h path
   typeset -T PATH path=(x)
 }
 (){
   typeset -h PATH
   typeset -T PATH path=(x)
 }
1:reject attempt to tie special to downgraded peer
?(anon):typeset:2: PATH special parameter can only be tied to special parameter path
?(anon):typeset:2: path special parameter can only be tied to special parameter PATH

 typeset MANPATH
 manpath=(/ /)
 typeset -UT MANPATH manpath
 print $manpath
0:OK to run typeset -T on tied specials as long as peer and joinchar are unchanged
>/

 typeset FOO=a:b
 export FOO
 typeset +x -T FOO foo
 typeset -p FOO
0:Make sure +x is honoured when tying a parameter
>typeset -T FOO foo=( a b )

 $ZTST_testdir/../Src/zsh --emulate sh -f -c '
 PATH=/bin; export PATH; readonly PATH
 export -p PATH		# Should be a no-op, -p ignored
 typeset -p PATH
 readonly -p'
0: readonly/export output for exported+readonly+special when started as sh
>export -r PATH=/bin
>readonly PATH=/bin

 function {
 emulate -L sh
 MANPATH=/bin; export MANPATH; readonly MANPATH
 export -p MANPATH	# Should be a no-op, -p ignored
 typeset -p MANPATH
 readonly -p
 }
0: readonly/export output for exported+readonly+tied+special after switching to sh emulation
>export -rT MANPATH manpath=( /bin )
>readonly MANPATH=/bin

 function {
   local -rax zsh_exported_readonly_array=(2)
   local -rAx zsh_exported_readonly_hash=(3 3)
   local -rx zsh_exported_readonly_scalar=1
   print zsh:
   export -p | grep zsh_exported_readonly
   readonly -p | grep zsh_exported_readonly
   print sh:
   emulate -L sh
   export -p | grep zsh_exported_readonly
   readonly -p | grep zsh_exported_readonly
   print still asking for arrays:
   export -ap | grep zsh_exported_readonly
   readonly -ap | grep zsh_exported_readonly
 }
0: no array/hash in POSIX export/readonly -p
>zsh:
>typeset -arx zsh_exported_readonly_array=( 2 )
>typeset -Arx zsh_exported_readonly_hash=( [3]=3 )
>typeset -rx zsh_exported_readonly_scalar=1
>typeset -arx zsh_exported_readonly_array=( 2 )
>typeset -Arx zsh_exported_readonly_hash=( [3]=3 )
>typeset -rx zsh_exported_readonly_scalar=1
>sh:
>export zsh_exported_readonly_scalar=1
>readonly zsh_exported_readonly_scalar=1
>still asking for arrays:
>export zsh_exported_readonly_array=( 2 )
>export zsh_exported_readonly_scalar=1
>readonly zsh_exported_readonly_array=( 2 )
>readonly zsh_exported_readonly_scalar=1

  # The second case was buggy as it needs special handling in postassigns
  (typeset {X})
  (typeset Q= {X})
1:Regression test for {...} parsing in typeset
?(eval):typeset:2: not valid in this context: {X}
?(eval):typeset:3: not valid in this context: {X}
debug log:

solving 7ec43adee ...
found 7ec43adee in https://inbox.vuxu.org/zsh-workers/1980097513.514587.1653987797049@mail2.virginmedia.com/
found 8b3988151 in https://git.vuxu.org/mirror/zsh/
preparing index
index prepared:
100644 8b39881515f0a929fcaf9e8789ac15ac2689fe36	Test/B02typeset.ztst

applying [1/1] https://inbox.vuxu.org/zsh-workers/1980097513.514587.1653987797049@mail2.virginmedia.com/
diff --git a/Test/B02typeset.ztst b/Test/B02typeset.ztst
index 8b3988151..7ec43adee 100644

Checking patch Test/B02typeset.ztst...
Applied patch Test/B02typeset.ztst cleanly.

index at:
100644 7ec43adee6cbed373d3df54154752a3dc281d6db	Test/B02typeset.ztst

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).