# tabify.test:  tests for the tabify sub-package of the textutil package.
# -*- tcl -*-
# 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.
#

if {[lsearch [namespace children] ::tcltest] == -1} {
    package require tcltest
    namespace import ::tcltest::*
}

if { [ lsearch [ namespace children ] "::textutil" ] == -1 } then {
    source [file join [file dirname [info script]] textutil.tcl]
}

###################################################

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

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

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

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

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

test tabify-1.3 {untabify string with 5 chars} {
    ::textutil::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::tabify2 "hello    world" 3
    #                    ---|||---|||--
} "hello \tworld"

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

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

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

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

test tabify-2.6 {version 2: tabify, tab size 8, less spaces than needed} {
    ::textutil::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::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::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::tabify2 "hello\tworld   bye"
    #                    hello...world   bye
    #                    --------||||||||---
} "hello\tworld\tbye"

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


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

test tabify-3.2 {version 2: untabify, tab size 3, tab to single space} {
    ::textutil::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::untabify2 "hello\tworld"
} "hello   world"

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

test tabify-3.5 {version 2: untabify, tab size 8, requires 'long' space string} {
    ::textutil::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::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::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 "" } ""
test tabify-4.2 {untabify empty string}  	{ textutil::untabify ""} ""
test tabify-4.3 {tabify2 empty string}   	{ textutil::tabify2 "" } ""
test tabify-4.4 {untabify2 empty string}	{ textutil::untabify2 ""} ""

