# -*- tcl -*-
# tabify.test:  tests for the tabify sub-package of the textutil package.
#
# This file contains a collection of tests for one or more of the Tcl
# built-in commands.  Sourcing this file into Tcl runs the tests and
# generates output for errors.  No output means no errors were found.
#

# -------------------------------------------------------------------------

source [file join \
	[file dirname [file dirname [file join [pwd] [info script]]]] \
	devtools testutilities.tcl]

testsNeedTcl     8.5
testsNeedTcltest 1.0

support {
    useLocal     repeat.tcl   textutil::repeat
}
testing {
    useLocal     tabify.tcl   textutil::tabify
}

# -------------------------------------------------------------------------

test tabify-0.1 {tabify string} {
    ::textutil::tabify::tabify "        hello,        world        "
} "\thello,\tworld\t"

test tabify-0.2 {tabify string with 4 chars} {
    ::textutil::tabify::tabify "        hello,        world        " 4
} "\t\thello,\t\tworld\t\t"

test tabify-0.3 {tabify string with 5 chars} {
    ::textutil::tabify::tabify "        hello,        world        " 5
} "\t   hello,\t   world\t   "

test tabify-1.1 {untabify string} {
    ::textutil::tabify::untabify "\thello,\tworld\t"
} "        hello,        world        "

test tabify-1.2 {untabify string with 4 chars} {
    ::textutil::tabify::untabify "\t\thello,\t\tworld\t\t" 4
} "        hello,        world        "

test tabify-1.3 {untabify string with 5 chars} {
    ::textutil::tabify::untabify "\t   hello,\t   world\t   " 5
} "        hello,        world        "

#
# Tests for version 2 of (un)tabify
#

#
# tests 2.1 - 2.3: see how a single space (after 'hello') is not converted
# to a tab
#
test tabify-2.1 {version 2: tabify, tab size 3} {
    ::textutil::tabify::tabify2 "hello    world" 3
    #                    ---|||---|||--
} "hello \tworld"

test tabify-2.2 {version 2: tabify, tab size 3, more spaces than needed} {
    ::textutil::tabify::tabify2 "hello      world" 3
} "hello \t  world"

test tabify-2.3 {version 2: tabify, tab size 3, less spaces than needed} {
    ::textutil::tabify::tabify2 "hello   world" 3
} "hello   world"

test tabify-2.4 {version 2: tabify, tab size 8} {
    ::textutil::tabify::tabify2 "hello   world"
} "hello\tworld"

test tabify-2.5 {version 2: tabify, tab size 8, more spaces than needed} {
    ::textutil::tabify::tabify2 "hello     world"
} "hello\t  world"

test tabify-2.6 {version 2: tabify, tab size 8, less spaces than needed} {
    ::textutil::tabify::tabify2 "hello  world"
} "hello  world"

#
# tests 2.7 & 2.8: 'end of line' (\n or not) of last line is preserved
#
test tabify-2.7 {version 2: tabify, tab size 8, multi line} {
    ::textutil::tabify::tabify2 "line 1  \n        line 2\nline 3  \n        line 4"
} "line 1\t\n\tline 2\nline 3\t\n\tline 4"

test tabify-2.8 {version 2: tabify, tab size 8, multi line} {
    ::textutil::tabify::tabify2 "line 1  \n        line 2\nline 3  \n        line 4\n"
} "line 1\t\n\tline 2\nline 3\t\n\tline 4\n"

# Test handling of existing tabs ... 2.9 as test and 2.10 the
# discrimator to check that it is correct if I use spaces
# instead of a tab, to see that my understanding is basically correct.

test tabify-2.9 {version 2: handling of existing tabs} {
    ::textutil::tabify::tabify2 "hello\tworld   bye"
    #                    hello...world   bye
    #                    --------||||||||---
} "hello\tworld\tbye"

test tabify-2.10 {version 2: handling of existing tabs} {
    ::textutil::tabify::tabify2 "hello   world   bye"
} "hello\tworld\tbye"


#
# untabify
#
test tabify-3.1 {version 2: untabify, tab size 3} {
    ::textutil::tabify::untabify2 "hello \tworld" 3
} "hello    world"

test tabify-3.2 {version 2: untabify, tab size 3, tab to single space} {
    ::textutil::tabify::untabify2 "hello\t\tworld" 3
} "hello    world"

#
# The change in tab size from 3 to 8 (silently) results in building the
# appropriate 'Spaces' strings (in 3.5 'Spaces(6)' is needed)
#
test tabify-3.3 {version 2: untabify, tab size 8} {
    ::textutil::tabify::untabify2 "hello\tworld"
} "hello   world"

test tabify-3.4 {version 2: untabify, tab size 8, mix of tab and spaces} {
    ::textutil::tabify::untabify2 "hello  \tworld"
} "hello   world"

test tabify-3.5 {version 2: untabify, tab size 8, requires 'long' space string} {
    ::textutil::tabify::untabify2 "hello\tmy\tworld"
} "hello   my      world"


#
# tests 3.6 & 3.7: 'end of line' (\n or not) of last line is preserved
#
test tabify-3.6 {version 2: untabify, tab size 8, multi line} {
    ::textutil::tabify::untabify2 "line 1\t\n\tline 2\nline 3\t\n\tline 4"
} "line 1  \n        line 2\nline 3  \n        line 4"

test tabify-3.7 {version 2: untabify, tab size 8, multi line} {
    ::textutil::tabify::untabify2 "line 1\t\n\tline 2\nline 3\t\n\tline 4\n"
} "line 1  \n        line 2\nline 3  \n        line 4\n"

#
# Edge cases: test for empty string
#
test tabify-4.1 {tabify empty string}    { ::textutil::tabify::tabify    "" } ""
test tabify-4.2 {untabify empty string}  { ::textutil::tabify::untabify  "" } ""
test tabify-4.3 {tabify2 empty string}   { ::textutil::tabify::tabify2   "" } ""
test tabify-4.4 {untabify2 empty string} { ::textutil::tabify::untabify2 "" } ""

