1# -*- tcl -*-
2# trim.test:  tests for the trim command of the textutil package.
3#
4# This file contains a collection of tests for one or more of the Tcl
5# built-in commands.  Sourcing this file into Tcl runs the tests and
6# generates output for errors.  No output means no errors were found.
7#
8
9# -------------------------------------------------------------------------
10
11source [file join \
12	[file dirname [file dirname [file join [pwd] [info script]]]] \
13	devtools testutilities.tcl]
14
15testsNeedTcl     8.2
16testsNeedTcltest 1.0
17
18testing {
19    useLocal     trim.tcl     textutil::trim
20}
21
22# -------------------------------------------------------------------------
23
24test trim-0.1 {trim string on left} {
25    set str [::textutil::trim::trimleft "\t\t hello, world \t " ]
26    set str
27} "hello, world \t "
28
29test trim-0.2 {trim string on right} {
30    set str [::textutil::trim::trimright "\t\t hello, world \t " ]
31    set str 
32} "\t\t hello, world"
33
34test trim-0.3 {trim string on both side} {
35    set str [::textutil::trim::trim "\t\t hello, world \t " ]
36    set str 
37} "hello, world"
38
39test trim-0.4 {trim string with embedded spaces and tabs on both side} {
40    set str [::textutil::trim::trim "\t\t hello,    \t\t  world \t " ]
41    set str 
42} "hello,    \t\t  world"
43
44test trim-1.1 {trim text on left} {
45    set str [::textutil::trim::trimleft "\t\t hello, \t\n \tworld \t " ]
46    set str
47} "hello, \t
48world \t "
49
50test trim-1.2 {trim text on right} {
51    set str [::textutil::trim::trimright "\t\t hello, \t\n \tworld \t " ]
52    set str 
53} "\t\t hello,
54 \tworld"
55
56test trim-1.3 {trim string on both side} {
57    set str [::textutil::trim::trim "\t\t hello, \t\n \tworld \t " ]
58    set str 
59} "hello,
60world"
61
62test trim-1.4 {trim string with embedded spaces and tabs on both side} {
63    set str [::textutil::trim::trim "\t\t hello\t \t, \t\n \tthe\t  \t world \t " ]
64    set str 
65} "hello\t \t,
66the\t  \t world"
67
68test trim-2.1 {trim text on left with regexp} {
69    set str [::textutil::trim::trimleft "\t\t hello, \t\n \tworld \t " "\[ \thwdo\]+" ]
70    set str
71} "ello, \t
72rld \t "
73
74test trim-2.2 {trim text on right} {
75    set str [::textutil::trim::trimright "\t\t hello, \t\n \tworld \t " "\[ \thwdo\]+" ]
76    set str 
77} "\t\t hello,
78 \tworl"
79
80test trim-2.3 {trim string on both side} {
81    set str [::textutil::trim::trim "\t\t hello, \t\n \tworld \t " "\[ \thwdo\]+" ]
82    set str 
83} "ello,
84rl"
85
86test trim-2.4 {trim string with embedded spaces and tabs on both side} {
87    set str [::textutil::trim::trim "\t\t hello\t \t, \t\n \tthe\t  \t world \t " "\[ \thwdo\]+" ]
88    set str 
89} "ello\t \t,
90the\t  \t worl"
91
92# Not the real parray proc, because the default value of pattern is intentionnally omitted
93
94set myparray "\t \tproc myparray {a pattern} {
95    # print nicely an associated array sorted by element
96    upvar 1 \$a array \t
97    if {!\[array exists array\]} {
98	error \"\\\"\$a\\\" isn't an array\" \t
99    }
100    set maxl 0 ; # used to find the longest name of element
101    foreach name \[lsort \[array names array \$pattern\]\] {
102	if {\[string length \$name\] > \$maxl} { \t\t\t 
103	    set maxl \[string length \$name\]
104	}
105    }
106    set maxl \[expr {\$maxl + \[string length \$a\] + 2}\]      \t
107    foreach name \[lsort \[array names array \$pattern\]\] {
108	set nameString \[format %s(%s) \$a \$name\]
109	puts stdout \[format \"%-*s = %s\" \$maxl \$nameString \$array(\$name)\]
110    }
111\t\t}\t\t"
112
113test trim-3.1 {trim block of Tcl code} {
114    set code [::textutil::trim::trim $myparray ]
115    set code
116} "proc myparray {a pattern} {
117# print nicely an associated array sorted by element
118upvar 1 \$a array
119if {!\[array exists array\]} {
120error \"\\\"\$a\\\" isn't an array\"
121}
122set maxl 0 ; # used to find the longest name of element
123foreach name \[lsort \[array names array \$pattern\]\] {
124if {\[string length \$name\] > \$maxl} {
125set maxl \[string length \$name\]
126}
127}
128set maxl \[expr {\$maxl + \[string length \$a\] + 2}\]
129foreach name \[lsort \[array names array \$pattern\]\] {
130set nameString \[format %s(%s) \$a \$name\]
131puts stdout \[format \"%-*s = %s\" \$maxl \$nameString \$array(\$name)\]
132}
133}"
134
135test trim-3.2 {trim block of Tcl code with regexp} {
136    set code [::textutil::trim::trim $myparray "\[\] \t{}pu\]+" ]
137    set code
138} "roc myparray {a pattern
139# print nicely an associated array sorted by element
140var 1 \$a array
141if {!\[array exists array
142error \"\\\"\$a\\\" isn't an array\"
143
144set maxl 0 ; # used to find the longest name of element
145foreach name \[lsort \[array names array \$pattern
146if {\[string length \$name\] > \$maxl
147set maxl \[string length \$name
148
149
150set maxl \[expr {\$maxl + \[string length \$a\] + 2
151foreach name \[lsort \[array names array \$pattern
152set nameString \[format %s(%s) \$a \$name
153ts stdout \[format \"%-*s = %s\" \$maxl \$nameString \$array(\$name)
154
155"
156
157test trim-3.3 {trim block of commented Tcl code with regexp} {
158    set code [::textutil::trim::trim $myparray "(\[ \t\]+)|(\[ \t;\]*#.*)" ]
159    set code
160} "proc myparray {a pattern} {
161
162upvar 1 \$a array
163if {!\[array exists array\]} {
164error \"\\\"\$a\\\" isn't an array\"
165}
166set maxl 0
167foreach name \[lsort \[array names array \$pattern\]\] {
168if {\[string length \$name\] > \$maxl} {
169set maxl \[string length \$name\]
170}
171}
172set maxl \[expr {\$maxl + \[string length \$a\] + 2}\]
173foreach name \[lsort \[array names array \$pattern\]\] {
174set nameString \[format %s(%s) \$a \$name\]
175puts stdout \[format \"%-*s = %s\" \$maxl \$nameString \$array(\$name)\]
176}
177}"
178