Artifact 19b73d012b9afa8bc84359161c9a2dd47304c8565c0ef3cf4e0064ce75debad4:

  • File tests/parser.test — part of check-in [9ba9808c25] at 2021-11-12 09:59:26 on branch experimental/bulk-xml-parse — more tests (covering properly stop on error/return in bulk XML-end handler) (user: sebres size: 27084)

     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
# Features covered:  Parser functions
#
# This file tests the parser's basic functions.
#
#    parser-1.*: parser creation
#    parser-2.*: return code 'break' from callback
#    parser-3.*: return code 'continue' from callback
#    parser-4.*: return code 'error' from callback
#    parser-5.*: parse input from channel
#    parser-6.*: reuse parser 
#    parser-7.*: parser reset
#    parser-8.*: parser free
#    parser-9.*: parser parse
#    parser-10.*: return code 'return' from callback
#    parser-11.*: parser input from filename
#    parser-12.*: parser currentmarkup
#
# Copyright (c) 1999-2000 Zveno Pty Ltd.
# Copyright (c) 2002-2015 Rolf Ade
#
# $Id$

source [file join [file dir [info script]] loadtdom.tcl]

proc parray arrayName {
    upvar #0 $arrayName arr
    foreach key [lsort [array names $arrayName]] {
	lappend result $key $arr($key)
    }
    return $result
}

catch {unset count} 
proc Count {args} {
    if {![info exists ::count]} {
        set ::count 1
    } else {
        incr ::count
    }
}

catch {unset started}
proc Start {name atList args} {
    array set opts $args
    array set atts $atList

    if {![info exists ::started($name)]} {
	set ::started($name) 1
    } else {
	incr ::started($name)
    }
    if {[info exists atts(class)]} {
	switch $atts(class) {
	    continue {
		return -code continue
	    }
	    break {
		return -code break
	    }
	    error {
		return -code error "error condition in callback"
	    }
            return {
                return -code return
            }
	    default {
		return -code $atts(class) 
	    }
	}
    }
}
catch {unset ended}
proc End {name args} {
    array set opts $args
    if {![info exists ::ended($name)]} {
	set ::ended($name) 1
    } else {
	incr ::ended($name)
    }
}
proc PI {name args} {
    return -code $name
}

catch {unset elList}
proc ElStart {name atList args} {
    array set opts {-empty 0}
    array set opts $args
    lappend ::elList start $name $opts(-empty)
}
proc ElEnd {name args} {
    array set opts {-empty 0}
    array set opts $args
    lappend ::elList end $name $opts(-empty)
}

test parser-1.1 {parser creation} {
    set p [::xml::parser]
    regexp {^xmlparser[0-9]+$} $p
} 1

test parser-1.2 {parser creation, only options} {
    set p [::xml::parser -elementstartcommand Start]
    regexp {^xmlparser[0-9]+$} $p
} 1

test parser-1.3 {parser creation, named} {
    catch {rename parser-1.3 {}}
    ::xml::parser parser-1.3
} parser-1.3

test parser-1.4 {parser creation, named with options} {
    catch {rename parser-1.4 {}}
    ::xml::parser parser-1.4 -elementstartcommand Start
} parser-1.4

test parser-1.5 {parser freeing, wrong nr of args} {
    set p [expat]
    if {[set result [catch {$p free wrongarg}]]} {
        $p free
    }
    set result
} 1

test parser-1.6 {parser create syntax check} {
    set result [catch {set parser [expat -paramentityparsing wrong]} errMsg]
    lappend result $errMsg
} {1 {bad value "wrong": must be always, never, or notstandalone}}

test parser-1.7 {parser cget syntax} {
    catch {rename parser-1.7 {}}
    set parser [expat parser-1.7]
    set result [catch {$parser cget} errMsg]
    lappend result $errMsg
} {1 {wrong # args: should be "parser-1.7 cget ?-handlerset handlersetname? switch"}}

test parser-1.8 {parser cget syntax} {
    catch {rename parser-1.8 {}}
    set parser [expat parser-1.8]
    set result [catch {$parser cget -handlerset -final} errMsg]
    lappend result $errMsg
} {1 {wrong # args: should be "?-handlerset handlersetname? switch"}}

test parser-1.9 {parser cget syntax} {
    catch {rename parser-1.9 {}}
    set parser [expat parser-1.9]
    set result [catch {$parser cget -handlerset dontexist -baseurl} errMsg]
    lappend result $errMsg
} {1 {invalid handlerset name: dontexist}}

test parser-1.10 {parser cget syntax} {
    catch {rename parser-1.10 {}}
    set parser [expat parser-1.10]
    set result [$parser cget -handlerset default -baseurl]
} {}

test parser-1.11 {parser cget syntax} {
    catch {rename parser-1.11 {}}
    set parser [expat parser-1.11 -baseurl http://foo.org/]
    set result [$parser cget -handlerset default -baseurl]
} {http://foo.org/}

test parser-1.12 {parser cget} {
    catch {rename parser-1.12 {}}
    set parser [expat parser-1.12]
    set result [$parser cget -baseurl]
} {}

proc esh_1_13_1 {args} {
    incr ::esh_1_13_1
}
proc esh_1_13_2 {args} {
    incr ::esh_1_13_2
}
test parser-1.13 {parser configure} {
    set ::esh_1_13_1 0
    set ::esh_1_13_2 0
    catch {rename parser-1.13 {}}
    set p [expat parser-1.13 -elementstartcommand esh_1_13_1]
    $p configure -elementstartcommand esh_1_13_2
    $p parse {<root><a/><b/></root>}
    list $::esh_1_13_1 $::esh_1_13_2
} {0 3}

test parser-1.14 {parser get} {
    catch {rename parser-1.14 {}}
    set parser [expat parser-1.14]
    set result [catch {$parser get}]
    $parser free
    set result
} {1}
test parser-1.15 {parser get} {
    catch {rename parser-1.15 {}}
    set parser [expat parser-1.15]
    set result [catch {$parser get foo bar}]
    $parser free
    set result
} {1}
test parser-1.16 {parser get} {
    catch {rename parser-1.16 {}}
    set parser [expat parser-1.16]
    set result [$parser get -currentbytecount]
    $parser free
    set result
} {0}

test parser-1.17 {parser delete} {
    expat parser-1.17
    parser-1.17 delete
} {}

proc cdh-1.18 {data} {
    if {[string trim $data] ne ""} {
        append ::result "cdh:$data"
    }
}
proc dh-1.18 {data} {
    if {[string trim $data] ne ""} {
        append ::result "dh:$data"
    }
}

test parser-1.18 {parser option -noexpand} {
    catch {rename parser-1.18 {}}
    set parser [expat parser-1.18]
    $parser configure \
        -noexpand 1 \
        -defaultcommand dh-1.18 \
        -characterdatacommand cdh-1.18
    set result ""
    $parser parse {<!DOCTYPE xml [
      <!ELEMENT xml (#PCDATA) >                                  
      <!ENTITY xxx "this was the xxx entity">
     ]>
     <xml>foo&xxx;bar</xml>}
    set result
} {cdh:foodh:&xxx;cdh:bar}

test parser-1.19 {parser option -noexpand} {
    catch {rename parser-1.19 {}}
    set parser [expat parser-1.19]
    $parser configure \
        -noexpand 0 \
        -defaultcommand dh-1.18 \
        -characterdatacommand cdh-1.18
    set result ""
    $parser parse {<!DOCTYPE xml [
      <!ELEMENT xml (#PCDATA) >                                  
      <!ENTITY xxx "this was the xxx entity">
     ]>
     <xml>foo&xxx;bar</xml>}
    set result
} {cdh:foothis was the xxx entitybar}

test parser-1.20 {parser option -noexpand} {
    catch {rename parser-1.20 {}}
    set parser [expat parser-1.20]
    $parser configure \
        -noexpand 1 \
        -characterdatacommand cdh-1.18
    set result ""
    $parser parse {<!DOCTYPE xml [
      <!ELEMENT xml (#PCDATA) >                                  
      <!ENTITY xxx "this was the xxx entity">
     ]>
     <xml>foo&xxx;bar</xml>}
    set result
} {cdh:foocdh:bar}

test parser-1.21 {parser option -noexpand} {
    catch {rename parser-1.21 {}}
    set parser [expat parser-1.21]
    $parser configure \
        -noexpand 1 \
        -defaultcommand dh-1.18
    set result ""
    $parser parse {<!DOCTYPE xml [
      <!ELEMENT xml (#PCDATA) >                                  
      <!ENTITY xxx "this was the xxx entity">
     ]>
     <xml>foo&xxx;bar</xml>}
    set result
} {dh:&xxx;}

test parser-1.22 {parser option -noexpand} {
    catch {rename parser-1.22 {}}
    set parser [expat parser-1.18]
    $parser configure \
        -noexpand 1 \
        -defaultcommand dh-1.18
    set result ""
    $parser parse {<!DOCTYPE xml [
      <!ELEMENT xml (#PCDATA) >                                  
      <!ENTITY xxx "this was the xxx entity">
     ]>
     <xml>foo&xxx;bar</xml>}
    set result
} {dh:&xxx;}

test parser-1.23 {Unknown option flag} {
    catch {rename xml::parser-1.23 {}}
    catch {
        set parser [xml::parser parser-1.23 \
                        -elementstartcommand EStart \
                        -boo]
    }
} 1

test parser-1.24 {Missing option argument} {
    catch {rename xml::parser-1.24 {}}
    catch {
        set parser [xml::parser parser-1.23 \
                        -elementstartcommand]
                        
    }
} 1

# Test break return code from callback

test parser-2.1 {break in callback} {
    catch {unset ::started}

    catch {rename parser-2.1 {}}
    set p [::xml::parser parser-2.1 -elementstartcommand Start]
    $p parse {<?xml version="1.0"?>
<Test>
<Element id="el1">Should see this data</Element>
<Element class="break" id="el2">Should not see this data<Element id="el3"/></Element>
<Element id='el4'>Should not see this data</Element>
</Test>
}
    set ::started(Element)
} 2

test parser-2.2 {break in callback} {
    catch {unset ::started}

    catch {rename parser-2.2 {}}
    set p [::xml::parser parser-2.2 -elementstartcommand Start]
    $p parse {<?xml version="1.0"?>
<Test>
<Element>Should see this data</Element>
<Element>Should see this data<Element class="break"/></Element>
<Element>Should not see this data</Element>
</Test>
}
    set ::started(Element)
} 3

test parser-2.3 {break in callback} {
    catch {unset ::started}

    catch {rename parser-2.3 {}}
    set p [::xml::parser parser-2.3 -elementstartcommand Start]
    $p parse {<?xml version="1.0"?>
<Test>
<Element>Should see this data</Element>
<Element>Should see this data
  <?break?>
</Element>
<Element>Should not see this data</Element>
</Test>
}
    set ::started(Element)
} 3

test parser-3.1 {continue in callback} {
    catch {unset ::started}

    catch {rename parser-3.1 {}}
    set p [::xml::parser parser-3.1 -elementstartcommand Start]
    $p parse {<?xml version="1.0"?>
<Test>
<Element id='el1'>Should see this data</Element>
<Element class="continue" id='el2'>Should not see this data
  <Element id='el3'/>
</Element>
<Element id='el4'>Should see this data</Element>
</Test>
}
    set ::started(Element)
} 3

test parser-3.2 {continue in callback} {
    catch {unset ::started}

    catch {rename parser-3.2 {}}
    set p [::xml::parser parser-3.2 -elementstartcommand Start]
    $p parse {<?xml version="1.0"?>
<Test>
<Element>Should see this data</Element>
<Element>Should see this data
  <Element class="continue">
    Should not see this data
    <Element/>
  </Element>
  Should see this data
  <Element/>
</Element>
<Element>Should see this data</Element>
</Test>
}
    set ::started(Element)
} 5

test parser-3.3 {continue in callback} {
    catch {unset ::started}

    catch {rename parser-3.3 {}}
    set p [::xml::parser parser-3.3 -elementstartcommand Start]
    $p parse {<?xml version="1.0"?>
<Test>
<Element>Should see this data</Element>
<Element>Should see this data
  <Element class="continue">
    Should not see this data
    <Element class="break"/>
    break will have no effect
  </Element>
  Should see this data
  <Element/>
</Element>
<Element>Should see this data</Element>
</Test>
}
    set ::started(Element)
} 5

proc esh-3.4 {name attList} {
    incr ::eshcounter
    return -code continue
}

proc eeh-3.4 {name} {
    incr ::eehcounter
}

proc cdh-3.4 {data} {
    incr ::cdhcounter
}

test parser-3.4 {continue} {
    set ::eshcounter 0
    set ::eehcounter 0
    set ::cdhcounter 0
    set p [expat -elementstartcommand esh-3.4 -elementendcommand eeh-3.4 \
              -characterdatacommand chd-3.4]
    $p parse {<root><a/>foo<a>foo<b><c/>foo</b></a></root>}
    $p free
    list $::eshcounter $::eehcounter $::cdhcounter
} {1 1 0}

proc esh-3.5 {name attList} {
    incr ::eshcounter2
}

proc eeh-3.5 {name} {
    incr ::eehcounter2
}

proc cdh-3.5 {data} {
    incr ::cdhcounter2
}

test parser-3.5 {continue with more than one handlerset} {
    set ::eshcounter 0
    set ::eehcounter 0
    set ::cdhcounter 0
    set ::eshcounter2 0
    set ::eehcounter2 0
    set ::cdhcounter2 0
    set p [expat -elementstartcommand esh-3.4 -elementendcommand eeh-3.4 \
              -characterdatacommand chd-3.4 -handlerset second \
              -elementstartcommand esh-3.5 -elementendcommand eeh-3.5 \
              -characterdatacommand cdh-3.5]
    $p parse {<root><a/>foo<a>foo<b><c/>foo</b></a></root>}
    $p free
    list $::eshcounter $::eehcounter $::cdhcounter \
        $::eshcounter2 $::eehcounter2 $::cdhcounter2
} {1 1 0 5 5 3}

test parser-4.1 {error in callback} {
    catch {unset ::started}

    catch {rename parser-4.1 {}}
    set p [::xml::parser parser-4.1 -elementstartcommand Start]
    set errcode [catch {$p parse {<?xml version="1.0"?>
<Test>
<Element>Should see this data</Element>
<Element class="error"/>
<Element>Should not see this data</Element>
</Test>
    }} result]
    list $errcode $::started(Element)
} {1 2}

test parser-4.2 {error in callback} {
    catch {unset ::started}

    catch {rename parser-4.2 {}}
    set p [::xml::parser parser-4.2 -elementstartcommand Start]
    set errcode [catch {$p parse {<?xml version="1.0"?>
<Test>
<Element>Should see this data</Element>
<Element class="13"/>
<Element>Should not see this data</Element>
</Test>
}} result]
    list $::errcode $::started(Element)
} {13 2}

test parser-5.1 {parse channel input} {
    catch {unset ::count}

    catch {rename parser-5.1 {}}
    set parser [::xml::parser parser-5.1 -elementstartcommand Count]
    set fd [open [file join [pwd] [file dir [info script]] data/books.xml]]
    $parser parsechannel $fd
    close $fd
    list $::count
} {42}

proc elementstart {args} {
    global parser

    if {![info exists ::count]} {
        set ::count 1
    } else {
        incr ::count
    }
    set fd [open [file join [pwd] [file dir [info script]] data/books.xml]]
    catch {$parser parsechannel $fd}
    close $fd
}

test parser-5.2 {parse channel input with catched try to recursive parsing} {
    catch {unset ::count}

    catch {rename parser-5.2 {}}
    set parser [::xml::parser parser-5.2 -elementstartcommand elementstart]
    set fd [open [file join [pwd] [file dir [info script]] data/books.xml]]
    $parser parsechannel $fd
    close $fd
    list $::count
} {42}

test parser-5.3 {parse channel - xml wrong} -setup {
    set xmlFile [makeFile {<root><a></root>} parser.xml]
} -body {
    set fd [open $xmlFile]
    catch {rename parser-5.3 {}}
    set parser [::xml::parser parser-5.3 -elementstartcommand elementstart]
    set result [catch {$parser parsechannel $fd}]
    close $fd
    set result
} -cleanup {
    removeFile parser.xml
} -result 1

proc elementstart-5.4 {args} {
    error "Error raised by elementstart-5.4"
}

test parser-5.4 {parse channel - error raised in handler} {
    catch {parser-5.4 free}
    ::xml::parser parser-5.4 -elementstartcommand elementstart-5.4
    set file [file join [pwd] [file dir [info script]] data/books.xml]
    catch {parser-5.4 parsefile $file} errMsg
    parser-5.4 free
    set errMsg
} "Error raised by elementstart-5.4"

test parser-6.1 {reuse parser} {
    catch {rename parser-6.1 {}}
    set parser [expat parser-6.1 -baseurl file:///foo/bar]
    set result [$parser cget -baseurl]
    $parser parse <data/>
    lappend result [$parser cget -baseurl]
    $parser configure -baseurl file:///bar/foo
    lappend result [$parser cget -baseurl]
    $parser parse <otherdata/>
    lappend result [$parser cget -baseurl]
    set result
} {file:///foo/bar {} file:///bar/foo {}}


proc elementstart {args} {
    global parser
    
    $parser reset
}

test parser-7.1 {parser reset called from within callback proc} {
    set parser [expat -elementstartcommand elementstart]
    set result [catch {$parser parse <root>foo</root>} errMsg]
    lappend result $errMsg
    $parser free
    set result
} {1 {parser reset not allowed from within callback}}

test parser-7.2 {parser reset} {
    set parser [expat -final 0]
    $parser parse "<ro"
    $parser reset
    set result [list]
    foreach option {-final -baseurl -ignorewhitecdata -useForeignDTD} {
        lappend result [$parser cget $option]
    }
    $parser parse "<doc/>"
    $parser free
    set result
} {1 {} 0 0}

proc elementstart {args} {
    global parser
    
    $parser free
}

test parser-8.1 {parser free called from within callback proc} {
    set parser [expat -elementstartcommand elementstart]
    set result [catch {$parser parse <root>foo</root>} errMsg]
    lappend result $errMsg
    $parser free
    set result
} {1 {parser delete not allowed from within callback}}

proc elementstart {args} {
    global parser

    $parser parse {<root>foo bar</root>}
}

test parser-9.1 {try to use the parser from within one of its callbacks} {
    set parser [expat -elementstartcommand elementstart]
    set result [catch {$parser parse <root>foo</root>} errMsg]
    lappend result $errMsg
    $parser free
    set result
} {1 {Parser already in use.}}

proc calledFromElementstart {args} {
    global parser

    $parser parse {<root>foo bar</root>}
}

proc elementstart {args} {
    calledFromElementstart
}

test parser-9.2 {try to use the parser from within one of its callbacks} {
    set parser [expat -elementstartcommand elementstart]
    set result [catch {$parser parse <root>foo</root>} errMsg]
    lappend result $errMsg
    $parser free
    set result
} {1 {Parser already in use.}}


test parser-10.1 {return -code return in callback} {
    catch {unset ::started}

    catch {rename parser-10.1 {}}
    set p [::xml::parser parser-10.1 -elementstartcommand Start]
    set errcode [catch {$p parse {<?xml version="1.0"?>
<Test>
<Element>Should see this data</Element>
<Element class="return"/>
<Element>Should not see this data</Element>
</Test>
}} result]
    list $errcode $::started(Element)
} {0 2}

test parser-10.2 {return -code return in callback} {
    catch {unset ::started}

    catch {rename parser-10.2 {}}
    set p [::xml::parser parser-10.2 -elementstartcommand Start]
    set errcode [catch {$p parse {<?xml version="1.0"?>
<Test>
<Element>Should see this data</Element>
<Element class="return"/>
<Element>Should not see this data</Element>
</Test>}} errMsg]
    set result [list $errcode $::started(Element)]
    $p reset
    catch {unset ::started}
    set errcode [catch {$p parse {<?xml version="1.0"?>
<Test>
<Element>Should see this data</Element>
<Element class="ok"/>
<Element>Should see this data</Element>
</Test>}} errMsg]
    lappend result $errcode $::started(Element)
} {0 2 0 3}

test parser-11.1 {parse parsefile} {
    catch {unset ::count}

    catch {rename parser-11.1 {}}
    set parser [::xml::parser parser-11.1 -elementstartcommand Count]
    set file [file join [pwd] [file dir [info script]] data/books.xml]
    $parser parsefile $file
    set ::count
} {42}

proc elementstart-11.2 {args} {
    error "Error raised by elementstart-11.2"
}

test parser-11.2 {parse parsefile - error raised in handler} {
    catch {parser-11.2 free}
    ::xml::parser parser-11.2 -elementstartcommand elementstart-11.2
    set file [file join [pwd] [file dir [info script]] data/books.xml]
    catch {parser-11.2 parsefile $file} errMsg
    parser-11.2 free
    set errMsg
} "Error raised by elementstart-11.2"

proc elementstart-12.1 {parser args} {
    global result
    append result [$parser currentmarkup]
}

proc elementend-12.1 {parser args} {
    global result
    append result [$parser currentmarkup]
}

test parser-12.1 {currentmarkup method} {
    catch {unset result}
    set result ""
    set p [expat parser-12.1]
    $p configure \
        -elementstartcommand [list elementstart-12.1 $p] \
        -elementendcommand [list elementend-12.1 $p]
    $p parse {<root rootatt="rootatt">text<a
        a_att1="a_att1"
        a_att2 = "a_att2"/><b>more text</b></root>}
    $p free
    set result
} {<root rootatt="rootatt"><a
        a_att1="a_att1"
        a_att2 = "a_att2"/><b></b></root>}

proc characterdata-12.2 {parser data} {
    global result
    append result [$parser currentmarkup]
}
test parser-12.2 {currentmarkup method} {
    catch {unset result}
    set result ""
    set p [expat parser-12.2]
    $p configure \
        -characterdatacommand [list characterdata-12.2 $p] 
    $p parse {<root rootatt="rootatt">text<a
        a_att1="a_att1"
        a_att2 = "a_att2"/><b>more text</b></root>}
    $p free
    set result
} {}

test parser-12.3 {currentmarkup method} {
    set p [expat parser-12.3]
    set result [$p currentmarkup]
    $p free
    set result
} {}

proc elementstart-12.4 {parser handlerset args} {
    global result
    append result "$handlerset: [$parser currentmarkup]\n"
}
proc elementend-12.4 {parser handlerset args} {
    global result
    append result "$handlerset: [$parser currentmarkup]\n"
}
test parser-12.4 {currentmarkup method - multiple handler set} {
    catch {unset result}
    set result ""
    set p [expat parser-12.4]
    $p configure \
        -elementstartcommand [list elementstart-12.4 $p default] \
        -elementendcommand [list elementend-12.4 $p default] \
        -handlerset "additional" \
        -elementstartcommand [list elementstart-12.4 $p "additional"] \
        -elementendcommand [list elementend-12.4 $p "additional"]
    $p parse {<root rootatt="rootatt">text<a
        a_att1="a_att1"
        a_att2 = "a_att2"/><b>more text</b></root>}
    $p free
    set result
} {default: <root rootatt="rootatt">
additional: <root rootatt="rootatt">
default: <a
        a_att1="a_att1"
        a_att2 = "a_att2"/>
additional: <a
        a_att1="a_att1"
        a_att2 = "a_att2"/>
default: 
additional: 
default: <b>
additional: <b>
default: </b>
additional: </b>
default: </root>
additional: </root>
}

proc elementstart-12.5 {parser args} {
    global result
    append result "[$parser currentmarkup]"
}
test parser-12.5 {currentmarkup method - empty element shortcut -elementstartcommand} {
    catch {unset result}
    set result ""
    set p [expat parser-12.5]
    $p configure \
        -elementstartcommand [list elementstart-12.5 $p] 
    $p parse {<root><elem/></root>}
    $p free
    set result
} {<root><elem/>}

proc elementend-12.6 {parser args} {
    global result
    if {[$parser currentmarkup] eq ""} {
        append result "<elementend called, but currentmarkup return empty string>"
    }
    append result "[$parser currentmarkup]"
}
test parser-12.6 {currentmarkup method - empty element shortcut -elementendcommand} {
    catch {unset result}
    set result ""
    set p [expat parser-12.6]
    $p configure \
        -elementendcommand [list elementend-12.6 $p] 
    $p parse {<root><elem/></root>}
    $p free
    set result
} {<elementend called, but currentmarkup return empty string></root>}
    
foreach parser [info commands xmlparser*] {
    $parser free
}
foreach parser [info commands parser-*] {
    $parser free
}

proc elementdeclcommand-12.7 {parser args} {
    global result
    append result "elementdeclcommand: [$parser currentmarkup]"
}

proc entitydeclcommand-12.7 {parser args} {
    global result
    append result "entitydeclcommand: [$parser currentmarkup]"
}

test parser-12.7 {currentmarkup method - not for doctype markup handler} {
    catch {unset result}
    set result ""
    set p [expat parser-12.7]
    $p configure \
        -elementdeclcommand [list elementdeclcommand-12.7 $p] \
        -entitydeclcommand [list entitydeclcommand-12.7 $p]
    $p parse {<!DOCTYPE test [
<!ELEMENT test (#PCDATA) >
<!ENTITY % xx '&#37;zz;'>
<!ENTITY % zz '&#60;!ENTITY tricky "error-prone" >' >
%xx;
]>
<test>This sample shows a &tricky; method.</test>}
    $p free
    set result
} {elementdeclcommand: entitydeclcommand: entitydeclcommand: }

proc pi-12.8 {parser args} {
    global result
    append result "pi: [$parser currentmarkup]"
}
test parser-12.8 {currentmarkup method - processing instruction} {
    catch {unset result}
    set result ""
    set p [expat parser-12.8]
    $p configure \
        -processinginstructioncommand [list pi-12.8 $p]
    $p parse {<doc><?xml-stylesheet type="text/xsl" href="style.xsl"?></doc>}
    $p free
    set result
} {pi: <?xml-stylesheet type="text/xsl" href="style.xsl"?>}

proc comment-12.9 {parser args} {
    global result
    append result "comment: [$parser currentmarkup]"
}
test parser-12.9 {currentmarkup method - comment} {
    catch {unset result}
    set result ""
    set p [expat parser-12.9]
    $p configure \
        -commentcommand [list comment-12.9 $p]
    $p parse {<doc><!-- A comment --></doc>}
    $p free
    set result
} {comment: <!-- A comment -->}


test parser-13.0 {no-bulk parsing - error by 2nd XML} -body {
    set p [xml::parser -final 1]
    $p parse {<X id="1"><a><b></b></a></X>  <Y id="2"><c><d></d></c></Y>  <Z id="3"><e><f>+2</f></e></Z>  }
} -cleanup {
    $p free
} -returnCodes error -match glob -result {*junk after document element* line 1 char* 30}

test parser-13.1 {bulk parsing - common handling} -setup {
    set ::res {}
} -body {
    set p [xml::parser]
    $p configure -final 1 -elementstartcommand {apply {{name atList args} {
        lappend ::res $name {*}$atList ->
    }}} -elementendcommand {apply {{name} {
        lappend ::res <- //$name
    }}} -bulkxmlendcommand {apply {{} {
        lappend ::res "."
    }}}
    $p parse {
      <X id="1"><a><b></b></a></X>
      <Y id="2"><c><d></d></c></Y>
      <Z id="3"><e><f></f></e></Z>
    }
    set ::res
} -cleanup {
    $p free
    unset -nocomplain ::res
} -result [list \
      X id 1 -> a -> b -> <- //b <- //a <- //X . \
      Y id 2 -> c -> d -> <- //d <- //c <- //Y . \
      Z id 3 -> e -> f -> <- //f <- //e <- //Z . \
]

test parser-13.2 {bulk parsing - position of XML in bulkxmlend-handler} -setup {
    set ::res {0}
} -body {
    set p [xml::parser]
    $p configure -final 1 -bulkxmlendcommand [list apply {p {
        lappend ::res [$p get -currentbyteindex]
        if {[$p get -currentbytecount]} {return -code error "unexpected: currentbytecount must be 0."}
    }} $p]
    $p parse {<X id="1"><a><b></b></a></X>  <Y id="2"><c><d></d></c></Y>  <Z id="3"><e><f>+2</f></e></Z>  }
    set ::res
} -cleanup {
    $p free
    unset -nocomplain ::res
} -result {0 30 60 92}

test parser-13.4 {bulk parsing - stop on error in XML-end handler} -body {
    set p [xml::parser]
    $p configure -final 1 -bulkxmlendcommand [list apply {p {
        return -code error "test error at [$p get -currentbyteindex]"
    }} $p]
    $p parse {<X id="1"><a><b></b></a></X>  <Y id="2"><c><d></d></c></Y>  <Z id="3"><e><f>+2</f></e></Z>  }
} -cleanup {
    $p free
} -returnCodes error -match glob -result {*test error at 30*}

test parser-13.4 {bulk parsing - stop on return in XML-end handler} -setup {
    set ::res {}
} -body {
    set p [xml::parser]
    $p configure -final 1 -elementendcommand {apply {{name} {
        set ::res $name
    }}} -bulkxmlendcommand [list apply {p {
        if {$::res eq "Y"} {
            return -code return
        }
    }} $p]
    $p parse {<X id="1"><a><b></b></a></X>  <Y id="2"><c><d></d></c></Y>  <Z id="3"><e><f>+2</f></e></Z>  }
    set ::res
} -cleanup {
    $p free
    unset -nocomplain ::res
} -result Y

# cleanup
::tcltest::cleanupTests
return