File: tree.test

package info (click to toggle)
tk-html3 3.0~fossil20110109-6
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 6,644 kB
  • ctags: 5,882
  • sloc: ansic: 48,994; tcl: 26,030; sh: 1,190; yacc: 161; makefile: 24
file content (325 lines) | stat: -rw-r--r-- 6,521 bytes parent folder | download | duplicates (5)
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
# Tkhtml test file
proc sourcefile {file} {
  set fname [file join [file dirname [info script]] $file] 
  uplevel #0 [list source $fname]
}
sourcefile common.tcl

set ::script_handler_count 0
proc scriptHandler {attr data} {
  incr ::script_handler_count
  return ""
}

proc get_tree {{indent 0} {node ""}} {
  if {$node eq ""} {
    set node [.h node]
  }

  set output [string repeat " " $indent]

  set tag [$node tag]
  if {$tag eq ""} {
    append output "[$node text -tokens]\n"
  } else {
    append output "<${tag}>\n"
  }

  foreach child [$node children] {
    append output [get_tree [expr $indent + 2] $child]
  }

  if {$tag ne ""} {
    append output [string repeat " " $indent]
    append output "</${tag}>\n"
  }

  return $output
}

#--------------------------------------------------------------------------
# Test cases tree-1.* test the [widget handler script ...] command,
# including in conjunction with incremental document parsing.
#
tcltest::test tree-1.0 {} -body {
  html .h
  .h handler script script scriptHandler 
  set ::script_handler_count
} -result 0

tcltest::test tree-1.1 {} -body {
  .h parse -final [string trim {
    <html><body>
    <p>Hello World
    <script>
      A whole bunch of script!!!
      A whole bunch of script!!!
      A whole bunch of script!!!
    </script>
    <p>Hello Again World
    </body>
    </html>
  }]
  set ::script_handler_count
} -result 1

tcltest::test tree-1.2 {} -body {
  string trim [get_tree]
} -result [string trim {
<html>
  <head>
  </head>
  <body>
    {space 4}
    <p>
      {text Hello} {space 1} {text World} {newline 1} {space 4}
      {newline 1} {space 4}
    </p>
    <p>
      {text Hello} {space 1} {text Again} {space 1} {text World} {newline 1} {space 4}
      {newline 1} {space 4}
    </p>
  </body>
</html>
}]

tcltest::test tree-1.3 {} -body {
  .h reset
  .h parse [string trim {
    <html>
    <body>
    <p>Hello World
    <script>
      A whole bunch of script!!!
      A whole bunch of script!!!
  }]

  .h parse -final [string trim {
      A whole bunch of script!!!
    </script>
    <p>Hello Again World
    </body>
    </html>
  }]
  set ::script_handler_count
} -result 2

tcltest::test tree-1.4 {} -body {
  string trim [get_tree]
} -result [string trim {
<html>
  <head>
  </head>
  <body>
    {space 4}
    {space 4}
    <p>
      {text Hello} {space 1} {text World} {newline 1} {space 4}
      {newline 1} {space 4}
    </p>
    <p>
      {text Hello} {space 1} {text Again} {space 1} {text World} {newline 1} {space 4}
      {newline 1} {space 4}
    </p>
  </body>
</html>
}]

tcltest::test tree-1.5 {} -body {
  .h reset
  .h parse [string trim {
    <html><body>
    <script>
      First line of script
  }]
  .h parse [string trim {
      .
      Second line of script.
    </script>
    <p>Paragraph one
  }]
  .h parse -final [string trim {
    <p>Paragraph two
  }]
  set ::script_handler_count
} -result 3

tcltest::test tree-1.6 {} -body {
  string trim [get_tree]
} -result [string trim {
<html>
  <head>
  </head>
  <body>
    {space 4}
    {newline 1} {space 4}
    <p>
      {text Paragraph} {space 1} {text one}
    </p>
    <p>
      {text Paragraph} {space 1} {text two}
    </p>
  </body>
</html>
}]

tcltest::test tree-1.7 {} -body {
  .h reset
  .h parse -final {
    <html><body><script src=hello></script></body>
  }
  set ::script_handler_count
} -result 4

#--------------------------------------------------------------------------
# Test cases tree-2.* test that ticket #12 has been fixed.
#
tcltest::test tree-2.1 {} -body {
  .h reset
  .h parse -final {<html><body>Hello World}
  string trim [get_tree]
} -result [string trim {
<html>
  <head>
  </head>
  <body>
    {text Hello} {space 1} {text World}
  </body>
</html>
}]

#--------------------------------------------------------------------------
# Test cases tree-3.* test the [fragment] API.
#
tcltest::test tree-3.1.1 {} -body {
  set ::nodelist [.h fragment {<img src="bg.gif">}]
  llength $::nodelist
} -result 1
tcltest::test tree-3.1.2 {} -body {
  set ::node [lindex $::nodelist 0]
  $::node tag
} -result img
tcltest::test tree-3.1.3 {} -body {
  $::node attr src
} -result bg.gif
tcltest::test tree-3.1.4 {} -body {
  $::node parent
} -result ""
tcltest::test tree-3.1.5 {} -body {
  .h reset
  set rc [catch {$::node attr src} msg]
  list $rc $msg
} -result [list 1 "invalid command name \"$::node\""]

tcltest::test tree-3.2.1 {} -body {
  set ::nodelist [.h fragment {<div>hello</div>  text}]
  llength $::nodelist
} -result 2
tcltest::test tree-3.2.2 {} -body {
  set ::textnode [lindex $::nodelist 1]
  $::textnode text -tokens
} -result [list {space 2} {text text}]
tcltest::test tree-3.2.3 {} -body {
  set ::divnode [lindex $::nodelist 0]
  $::divnode tag
} -result div
tcltest::test tree-3.2.4 {} -body {
  set ::divchild [lindex [$::divnode children] 0]
  $::divchild text
} -result hello
tcltest::test tree-3.2.5 {} -body {
  .h reset
  set rc [catch {$::divchild text} msg]
  list $rc $msg
} -result [list 1 "invalid command name \"$::divchild\""]

tcltest::test tree-3.3.1 {} -body {
  .h parse -final "<div>"
  set ::nodelist [.h fragment "<span>some text</span><img src=src.gif>"]
  string trim [get_tree]
} -result [string trim {
<html>
  <head>
  </head>
  <body>
    <div>
    </div>
  </body>
</html>
}]
tcltest::test tree-3.3.2 {} -body {
  set div [.h search div]
  $div insert [lindex $::nodelist 1]
  string trim [get_tree]
} -result [string trim {
<html>
  <head>
  </head>
  <body>
    <div>
      <img>
      </img>
    </div>
  </body>
</html>
}]
tcltest::test tree-3.3.3 {} -body {
  set div [.h search div]
  $div insert -before [lindex [$div children] 0] [lindex $::nodelist 0]
  string trim [get_tree]
} -result [string trim {
<html>
  <head>
  </head>
  <body>
    <div>
      <span>
        {text some} {space 1} {text text}
      </span>
      <img>
      </img>
    </div>
  </body>
</html>
}]
tcltest::test tree-3.3.4 {} -body {
  set div [.h search div]
  $div insert [lindex $::nodelist 0]
  string trim [get_tree]
} -result [string trim {
<html>
  <head>
  </head>
  <body>
    <div>
      <img>
      </img>
      <span>
        {text some} {space 1} {text text}
      </span>
    </div>
  </body>
</html>
}]
tcltest::test tree-3.3.5 {} -body {
  set div [.h search div]
  $div remove [lindex $::nodelist 0]
  string trim [get_tree]
} -result [string trim {
<html>
  <head>
  </head>
  <body>
    <div>
      <img>
      </img>
    </div>
  </body>
</html>
}]

finish_test