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
|
# -*- tcl -*-
# rb.test: test samples for the yaml library.
# http://yaml4r.sourceforge.net/cookbook/
#
if {[lsearch [namespace children] ::tcltest] == -1} {
# single test
set selfrun 1
lappend auto_path [pwd]
package require tcltest
namespace import ::tcltest::*
puts [source huddle.tcl]
puts [source yaml.tcl]
} else {
# all.tcl
source [file join \
[file dirname [file dirname [file join [pwd] [info script]]]] \
devtools testutilities.tcl]
testsNeedTcl 8.3
testsNeedTcltest 1.0
if {$::tcl_version < 8.5} {
if {[catch {package require dict}]} {
puts " Aborting the tests found in \"[file tail [info script]]\""
puts " Requiring dict package, not found."
return
}
}
testing {
useLocal yaml.tcl yaml
}
}
proc dictsort {dict} {
array set a $dict
set out [list]
foreach key [lsort [array names a]] {
lappend out $key $a($key)
}
return $out
}
proc dictsort2 {dict {pattern d}} {
set cur [lindex $pattern 0]
set subs [lrange $pattern 1 end]
foreach {tag sw} $cur break
set out {}
if {$sw ne ""} {array set msubs $sw}
if {$tag eq "l"} { ; # list
set i 0
foreach {node} $dict {
set subs1 $subs
if {$sw ne "" && [info exists msubs($i)]} {
set subs1 $msubs($i)
}
if {$subs1 ne ""} {
set node [dictsort2 $node $subs1]
}
lappend out $node
incr i
}
return $out
}
if {$tag eq "d"} { ; # dict
array set map $dict
foreach key [lsort [array names map]] {
set node $map($key)
set subs1 $subs
if {$sw ne "" && [info exists msubs($key)]} {
set subs1 $msubs($key)
}
if {$subs1 ne ""} {
set node [dictsort2 $node $subs1]
}
lappend out $key $node
}
return $out
}
error
}
test yaml.rb-1 "Simple Sequence" -body {
set data {
---
- apple
- banana
- carrot
}
yaml::yaml2dict $data
} -result {apple banana carrot}
test yaml.rb-2 "Nested Sequences" -body {
set data {
---
-
- foo
- bar
- baz
}
yaml::yaml2dict $data
} -result {{foo bar baz}}
test yaml.rb-3 "Mixed Sequences" -body {
set data {
---
-
- fo o
-
- x1 23
- bana na
- carr ot
}
yaml::yaml2dict $data
} -result {{{fo o}} {{x1 23}} {bana na} {carr ot}}
test yaml.rb-4 "Deeply Nested Sequences" -body {
set data {
---
-
-
- uno
- dos
}
yaml::yaml2dict $data
} -result {{{uno dos}}}
test yaml.rb-5 "Simple Mapping" -body {
set data {
---
foo: whatever
bar: stuff
}
yaml::yaml2dict $data
} -result {foo whatever bar stuff}
test yaml.rb-6 "Sequence in a Mapping" -body {
set data {
---
foo: whatever
bar:
- uno
- dos
}
yaml::yaml2dict $data
} -result {foo whatever bar {uno dos}}
test yaml.rb-7 "Nested Mappings" -body {
set data {
---
foo: whatever
bar:
fruit: apple
name: steve
sport: baseball
}
yaml::yaml2dict $data
} -result [dict create foo whatever bar [dict create fruit apple name steve sport baseball]]
test yaml.rb-8 "Mixed Mapping" -body {
set data {
---
foo: whatever
bar:
-
fruit: apple
name: steve
sport: baseball
- more
-
python: rocks
perl: papers
ruby: scissorses
}
yaml::yaml2dict $data
} -result [dict create foo whatever bar [list [dict create fruit apple name steve sport baseball] more [dict create python rocks perl papers ruby scissorses]]]
test yaml.rb-9 "Mapping-in-Sequence Shortcut" -body {
set data {
---
- work on YAML.py:
- work on Store
}
yaml::yaml2dict $data
} -result {{{work on YAML.py} {{work on Store}}}}
test yaml.rb-10 "Sequence-in-Mapping Shortcut" -body {
set data {
---
allow:
- 'localhost'
- '%.sourceforge.net'
- '%.freepan.org'
}
yaml::yaml2dict $data
} -result {allow {localhost %.sourceforge.net %.freepan.org}}
test yaml.rb-11 "Merge key" -body {
set data {
---
mapping:
name: Joe
job: Accountant
<<:
age: 38
}
dictsort2 [yaml::yaml2dict $data] {d d}
} -result [dictsort2 {mapping {name Joe job Accountant age 38}} {d d}]
test yaml.rb-12 "Simple Inline Array" -body {
set data {
---
seq: [ a, b, c ]
}
yaml::yaml2dict $data
} -result {seq {a b c}}
test yaml.rb-13 "Simple Inline Hash" -body {
set data {
---
hash: { name: Steve, foo: bar }
}
dictsort2 [yaml::yaml2dict $data] {d d}
} -result [dictsort2 {hash {name Steve foo bar}} {d d}]
test yaml.rb-14 "Multi-line Inline Collections" -body {
set data {
---
languages: [ Ruby,
Perl,
Python ]
websites: { YAML: yaml.org,
Ruby: ruby-lang.org,
Python: python.org,
Perl: use.perl.org }
}
dictsort2 [yaml::yaml2dict $data] {{d {websites d}}}
} -result [dictsort2 {languages {Ruby Perl Python} websites {YAML yaml.org Ruby ruby-lang.org Python python.org Perl use.perl.org}} \
{{d {websites d}}} ]
test yaml.rb-15 "Commas in Values" -body {
set data {
---
attendances: [ 45,123, 70,000, 17,222 ]
}
yaml::yaml2dict $data
} -result {attendances {45 123 70 000 17 222}}
test yaml.rb-16 "Strings" -body {
set data {
--- String
}
yaml::yaml2dict $data
} -result {String}
test yaml.rb-17 "String characters" -body {
set data {
---
- What's Yaml?
- It's for writing data structures in plain text.
- And?
- And what? That's not good enough for you?
- No, I mean, "And what about Yaml?"
- Oh, oh yeah. Uh.. Yaml for Ruby.
}
yaml::yaml2dict $data
} -result {{What's Yaml?} {It's for writing data structures in plain text.} And? {And what? That's not good enough for you?} {No, I mean, "And what about Yaml?"} {Oh, oh yeah. Uh.. Yaml for Ruby.}}
test yaml.rb-18 "Indicators in Strings" -body {
set data {
---
the colon followed by space is an indicator: but is a string:right here
same for the pound sign: here we have it#in a string
the comma can, honestly, be used in most cases: [ but not in, inline collections ]
}
yaml::yaml2dict $data
} -result [dict create {the colon followed by space is an indicator} {but is a string:right here} {same for the pound sign} {here we have it#in a string} {the comma can, honestly, be used in most cases} {{but not in} {inline collections}}]
test yaml.rb-19 "Forcing Strings" -body {
set data {
---
date string: !!str 2001-08-01
number string: !!str 192
}
yaml::yaml2dict $data
} -result [dict create {date string} 2001-08-01 {number string} 192]
test yaml.rb-20 "Single-quoted Strings" -body {
set data {
---
all my favorite symbols: '#:!/%.)'
a few i hate: '&(*'
why do i hate them?: 'it''s very hard to explain'
}
yaml::yaml2dict $data
} -result {{all my favorite symbols} #:!/%.) {a few i hate} &(* {why do i hate them?} {it's very hard to explain}}
test yaml.rb-21 "Double-quoted Strings" -body {
set data {
---
i know where i want my line breaks: "one here\nand another here\n"
}
yaml::yaml2dict $data
} -result {{i know where i want my line breaks} {one here
and another here
}}
test yaml.rb-22 "Multi-line Quoted Strings" -body {
set data {
---
i want a long string: "so i'm going to
let it go on and on to other lines
until i end it with a quote."
}
yaml::yaml2dict $data
} -result {{i want a long string} {so i'm going to let it go on and on to other lines until i end it with a quote.}}
test yaml.rb-23 "Plain scalars" -body {
set data {
---
- My little toe is broken in two places;
- I'm crazy to have skied this way;
- I'm not the craziest he's seen, since there was always the German guy
who skied for 3 hours on a broken shin bone (just below the kneecap);
- Nevertheless, second place is respectable, and he doesn't
recommend going for the record;
- He's going to put my foot in plaster for a month;
- This would impair my skiing ability somewhat for the
duration, as can be imagined.
}
yaml::yaml2dict $data
} -result {{My little toe is broken in two places;} {I'm crazy to have skied this way;} {I'm not the craziest he's seen, since there was always the German guy who skied for 3 hours on a broken shin bone (just below the kneecap);} {Nevertheless, second place is respectable, and he doesn't recommend going for the record;} {He's going to put my foot in plaster for a month;} {This would impair my skiing ability somewhat for the duration, as can be imagined.}}
test yaml.rb-24 "Null" -body {
set data {
---
name: Mr. Show
hosted by: Bob and David
date of next season: ~
}
yaml::yaml2dict $data
} -result {name {Mr. Show} {hosted by} {Bob and David} {date of next season} {}}
test yaml.rb-25 "Boolean" -body {
set data {
---
Is Gus a Liar?: true
Do I rely on Gus for Sustenance?: false
}
yaml::yaml2dict $data
} -result [dict create {Is Gus a Liar?} 1 {Do I rely on Gus for Sustenance?} 0]
test yaml.rb-26 "Integers" -body {
set data {
---
zero: 0
simple: 12
one-thousand: 1,000
negative one-thousand: -1,000
}
yaml::yaml2dict $data
} -result [dict create zero 0 simple 12 one-thousand 1000 {negative one-thousand} -1000]
test yaml.rb-27 "Integers as Map Keys" -body {
set data {
---
1: one
2: two
3: three
}
yaml::yaml2dict $data
} -result {1 one 2 two 3 three}
test yaml.rb-28 "Floats" -body {
set data {
---
a simple float: 2.00
larger float: 1,000.09
scientific notation: 1.00009e+3
}
yaml::yaml2dict $data
} -result [dict create {a simple float} 2.00 {larger float} 1000.09 {scientific notation} 1.00009e+3]
if {$::tcl_version < 8.5} {
test yaml.rb-29 "Time" -body {
set data {
---
iso8601: 2001-12-14t21:59:43.10-05:00
space seperated: 2001-12-14 21:59:43.10 -05:00
}
yaml::yaml2dict $data
} -result [eval dict create [string map \
[list TIMESTAMP1 [clock scan "+5 hours" -base [clock scan "2001-12-14 21:59:43" -gmt 1]] \
TIMESTAMP2 [clock scan "+5 hours" -base [clock scan "2001-12-14 21:59:43" -gmt 1]]] \
{iso8601 TIMESTAMP1 {space seperated} TIMESTAMP2}]]
} else {
test yaml.rb-29 "Time" -body {
set data {
---
iso8601: 2001-12-14t21:59:43.10-05:00
space seperated: 2001-12-14 21:59:43.10 -05:00
}
yaml::yaml2dict $data
} -result [string map [list TIMESTAMP1 [clock scan "2001-12-14 21:59:43 -05:00" -format {%Y-%m-%d %k:%M:%S %Z}] \
TIMESTAMP2 [clock scan "2001-12-14 21:59:43 -05:00" -format {%Y-%m-%d %k:%M:%S %Z}]] \
{iso8601 TIMESTAMP1 {space seperated} TIMESTAMP2}]
}
test yaml.rb-30 "Date" -body {
set data {
--- 1976-07-31
}
yaml::yaml2dict $data
} -result [clock scan "1976-07-31"]
test yaml.rb-31 "Blocks" -body {
set data {
---
this: |
Foo
Bar
}
yaml::yaml2dict $data
} -result {this {Foo
Bar
}}
test yaml.rb-32 "The '+' indicator" -body {
set data {
---
normal: |
extra new lines not kept
preserving: |+
extra new lines are kept
dummy: value
}
dictsort [yaml::yaml2dict $data]
} -result [dictsort {normal {extra new lines not kept
} preserving {extra new lines are kept
} dummy value}]
test yaml.rb-33 "Three trailing newlines in literals" -body {
set data {
---
clipped: |
This has one newline.
same as "clipped" above: "This has one newline.\n"
stripped: |-
This has no newline.
same as "stripped" above: "This has no newline."
kept: |+
This has four newlines.
same as "kept" above: "This has four newlines.\n\n\n\n"
}
dictsort [yaml::yaml2dict $data]
} -result [dictsort {clipped {This has one newline.
} {same as "clipped" above} {This has one newline.
} stripped {This has no newline.} {same as "stripped" above} {This has no newline.} kept {This has four newlines.
} {same as "kept" above} {This has four newlines.
}}]
test yaml.rb-34 "Extra trailing newlines with spaces" -body {
set data {
---
this: |
Foo
kept: |+
Foo
}
yaml::yaml2dict $data
} -result [dict create this {Foo
} kept {Foo
}]
test yaml.rb-35 "Folded Block in a Sequence" -body {
set data {
---
- apple
- banana
- >
can't you see
the beauty of yaml?
hmm
- dog
}
yaml::yaml2dict $data
} -result {apple banana {can't you see the beauty of yaml? hmm
} dog}
test yaml.rb-36 "Folded Block as a Mapping Value" -body {
set data {
---
quote: >
Mark McGwire's
year was crippled
by a knee injury.
source: espn
}
yaml::yaml2dict $data
} -result [dict create quote {Mark McGwire's year was crippled by a knee injury.
} source espn]
test yaml.rb-37 "Three trailing newlines in folded blocks" -body {
set data {
---
clipped: >
This has one newline.
same as "clipped" above: "This has one newline.\n"
stripped: >-
This has no newline.
same as "stripped" above: "This has no newline."
kept: >+
This has four newlines.
same as "kept" above: "This has four newlines.\n\n\n\n"
}
dictsort [yaml::yaml2dict $data]
} -result [dictsort {clipped {This has one newline.
} {same as "clipped" above} {This has one newline.
} stripped {This has no newline.} {same as "stripped" above} {This has no newline.} kept {This has four newlines.
} {same as "kept" above} {This has four newlines.
}}]
test yaml.rb-38 "Extra trailing newlines with spaces" -body {
set data {
---
- &showell Steve
- Clark
- Brian
- Oren
- *showell
}
yaml::yaml2dict $data
} -result {Steve Clark Brian Oren Steve}
test yaml.rb-39 "Alias of a Mapping" -body {
set data {
---
- &hello
Meat: pork
Starch: potato
- banana
- *hello
}
yaml::yaml2dict $data
} -result [list [dict create Meat pork Starch potato] banana [dict create Meat pork Starch potato]]
#test yaml.rb-40 "Trailing Document Separator" -body {
# set data {
#- foo: 1
# bar: 2
#---
#more: stuff
#}
# yaml::yaml2dict $data
#} -result {Steve Clark Brian Oren Steve}
test yaml.rb-41 "Alias of a Mapping" -body {
set data {
--- %YAML:1.0
foo: 1
bar: 2
}
yaml::yaml2dict $data
} -result {foo 1 bar 2}
test yaml.rb-42 "Red Herring Document Separator" -body {
set data {
---
foo: |
---
}
yaml::yaml2dict $data
} -result {foo {---
}}
test yaml.rb-43 "Multiple Document Separators in Block" -body {
set data {
---
foo: |
---
foo: bar
---
yo: baz
bar: |
fooness
}
yaml::yaml2dict $data
} -result {foo {---
foo: bar
---
yo: baz
} bar {fooness
}}
# YAML For Ruby
# (ignored)
# ... Tests of addStrings ...
# (Requires introspection of parser state)
if [info exists selfrun] {
tcltest::cleanupTests
} else {
testsuiteCleanup
}
|