1# -*- tcl -*-
2# split.test:  tests for the split sub-package 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.5
16testsNeedTcltest 1.0
17
18testing {
19    useLocal     split.tcl    textutil::split
20}
21
22# -------------------------------------------------------------------------
23
24test splitn-0.1 {split empty string} {
25    ::textutil::split::splitn ""
26} [list]
27
28test splitn-0.2 {split empty string with explicit length 1} {
29    ::textutil::split::splitn "" 1
30} [list]
31
32test splitn-0.3 {split empty string with explicit length 2} {
33    ::textutil::split::splitn "" 2
34} [list]
35
36test splitn-1.1 {split simple string} {
37    ::textutil::split::splitn "abc"
38} [list a b c]
39
40test splitn-1.2 {split simple string with explicit length 1} {
41    ::textutil::split::splitn "abc" 1
42} [list a b c]
43
44test splitn-1.3 {split simple string with explicit length 2} {
45    ::textutil::split::splitn "abc" 2
46} [list ab c]
47
48test splitn-2.1 {split with nonpositive length ->error!} {
49    catch {::textutil::split::splitn "abc" 0} msg
50    set msg
51} {len must be > 0}
52
53###################################################
54
55test splitx-0.1 {split simple string} {
56    ::textutil::split::splitx "Hello, Word"
57} [ list Hello, Word ]
58
59test splitx-0.2 {split simple string with spaces} {
60    ::textutil::split::splitx "Hello,     Word"
61} [ list Hello, Word ]
62
63test splitx-0.3 {split simple string with tabs} {
64    ::textutil::split::splitx "Hello,\tWord"
65} [ list Hello, Word ]
66
67test splitx-0.4 {split simple string with tabs and spaces ...} {
68    ::textutil::split::splitx "Hello,\t  \r   \n\n\n  \r \r \t\t  Word"
69} [ list Hello, Word ]
70
71test splitx-0.5 {split simple string with beginning and ending tabs} {
72    ::textutil::split::splitx "\t  \r   \n\Hello, \t   Word \t  \r   \n\n"
73} [ list {} Hello, Word {} ]
74
75test splitx-1.1 {split simple string with regexp} {
76    ::textutil::split::splitx "Hello,\t,\n, Word" "\[ ,\t\r\n\]+"
77} [ list Hello Word ]
78
79test splitx-1.2 {split simple string with buggy regexp} {
80    ::textutil::split::splitx "Hello, Word,\t,\n" "\[ ,\t\r\n\]"
81} [ list Hello {} Word {} {} {} {} ]
82
83test splitx-2.1 {split text} {
84    ::textutil::split::splitx "
85Determines whether the regular expression exp matches part or all of
86string and returns 1 if it does, 0 if it doesn't, unless -inline is
87specified (see below). (Regular expression matching is described in the
88re_syntax reference page.) If additional arguments are specified after
89string then they are treated as the names of variables in which to
90return information about which part(s) of string matched exp. MatchVar
91will be set to the range of string that matched all of exp. The first
92subMatchVar will contain the characters in string that matched the
93leftmost parenthesized subexpression within exp, the next subMatchVar
94will contain the characters that matched the next parenthesized
95subexpression to the right in exp , and so on.
96"
97} [ list {} Determines whether the regular expression exp matches part or all of string and returns 1 if it does, 0 if it doesn't, unless -inline is specified (see below). (Regular expression matching is described in the re_syntax reference page.) If additional arguments are specified after string then they are treated as the names of variables in which to return information about which part(s) of string matched exp. MatchVar will be set to the range of string that matched all of exp. The first subMatchVar will contain the characters in string that matched the leftmost parenthesized subexpression within exp, the next subMatchVar will contain the characters that matched the next parenthesized subexpression to the right in exp , and so on. {} ]
98
99test splitx-2.2 {split text with regexp} {
100    ::textutil::split::splitx "
101Determines whether the regular expression exp matches part or all of
102string and returns 1 if it does, 0 if it doesn't, unless -inline is
103specified (see below). (Regular expression matching is described in the
104re_syntax reference page.) If additional arguments are specified after
105string then they are treated as the names of variables in which to
106return information about which part(s) of string matched exp. MatchVar
107will be set to the range of string that matched all of exp. The first
108subMatchVar will contain the characters in string that matched the
109leftmost parenthesized subexpression within exp, the next subMatchVar
110will contain the characters that matched the next parenthesized
111subexpression to the right in exp , and so on.
112" "\[ ,()\.\t\r\n\]+"
113} [ list {} Determines whether the regular expression exp matches part or all of string and returns 1 if it does 0 if it doesn't unless -inline is specified see below Regular expression matching is described in the re_syntax reference page If additional arguments are specified after string then they are treated as the names of variables in which to return information about which part s of string matched exp MatchVar will be set to the range of string that matched all of exp The first subMatchVar will contain the characters in string that matched the leftmost parenthesized subexpression within exp the next subMatchVar will contain the characters that matched the next parenthesized subexpression to the right in exp and so on {} ]
114
115# these tests show the effect inducted by the usage of parenthesed in
116# the regexp Basically, the parenthesed operator is returned with the
117# splitted list The 3.5 and 3.6 show complex cases. Try to understand.
118
119test splitx-3.1 {split string with simple regexp} {
120    ::textutil::split::splitx "Nobody is perfect" "\[oe\]+"
121} [ list N b [ list dy is p ] rf  ct ]
122
123test splitx-3.2 {split string with the same simple regexp but parenthesed} {
124    ::textutil::split::splitx "Nobody is perfect" "(\[oe\]+)"
125} [ list N o b o [ list dy is p ] e rf e ct ]
126
127test splitx-3.3 {split string with a not so simple parenthesed regexp} {
128    ::textutil::split::splitx "Nobody is perfect" "o+|(rf)"
129} [ list N b [ list dy is pe ] rf  ect ]
130
131test splitx-3.4 {split string with a more complexe parenthesed regexp} {
132    ::textutil::split::splitx "Nobody is perfect" "\[oe\]+|(rf)"
133} [ list N b [ list dy is p ] {} rf {} ct ]
134
135test splitx-3.5 {split string with an even more complexe parenthesed regexp} {
136    ::textutil::split::splitx "Nobody is perfect" "(\[oe\]+)|(rf)"
137} [ list N o b o [ list dy is p ] e {} {} e ct ]
138
139test splitx-3.6 {split string with a totally parenthesed regexp} {
140    ::textutil::split::splitx "Nobody is perfect" "(\[oe\]+|rf)"
141} [ list N o b o [ list dy is p ] e {} rf {} e ct ]
142
143
144test splitx-4.0 {splitting of empty strings} {
145    ::textutil::split::splitx "" "f"
146} {}
147
148test splitx-4.1 {splitting of empty strings} {
149    ::textutil::split::splitx ""
150} {}
151
152test splitx-4.2 {splitting of empty strings} {
153    ::textutil::split::splitx "" ""
154} {}
155
156test splitx-5.0 {splitting using an empty regexp} {
157    ::textutil::split::splitx "fooo bar bas" ""
158} {f o o o { } b a r { } b a s}
159
160
161test splitx-6.0 {split with regexp matching "" causes infinite loop eating RAM} {
162    list [catch {
163	::textutil::split::splitx "Hello, Word" "|"
164    } msg] $msg
165} {1 {splitting on regexp "|" would cause infinite loop}}
166