# Test coverage for the grapheme command
# Copyright © 2026 Ashok P. Nadkarni
#
# See the file license.terms for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
if {[lsearch [namespace children] ::tcltest] == -1} {
package require tcltest
namespace import ::tcltest::*
}
tcltest::loadTestedCommands
package require tcl::test
source [file join [file dirname [info script]] tcltests.tcl]
source [file join [file dirname [info script]] ucdUtils.tcl]
namespace eval grapheme::test {
namespace path ::tcltests::ucd
variable testCase
variable comment
variable testNum
variable graphemes
variable text
variable l
proc cleanupVars {} {
unset -nocomplain {*}
}
# The -ucd- tests all run off the test vectors from the Unicode Character
# Database. The test numbering is based on the corresponding line from
# the GraphemeBreakTest.txt file and may therefore be unstable.
### grapheme next
tcltests::testnumargs "grapheme next" "string strIndex" ""
foreach testCase [getGraphemeData] {
lassign $testCase testNum comment graphemes
test grapheme-next-ucd-$testNum.0 $comment -body {
set text [join $graphemes ""]
set l {}
set i 0
while {[set grapheme [grapheme next $text i]] ne ""} {
lappend l $grapheme
}
set l
} -result $graphemes
}
### grapheme prev
tcltests::testnumargs "grapheme prev" "string strIndex" ""
foreach testCase [getGraphemeData] {
lassign $testCase testNum comment graphemes
test grapheme-prev-ucd-$testNum.0 $comment -body {
set text [join $graphemes ""]
set l {}
set i end
while {[set grapheme [grapheme prev $text i]] ne ""} {
lappend l $grapheme
}
lreverse $l
} -result $graphemes
}
### grapheme length
tcltests::testnumargs "grapheme length" "string" ""
foreach testCase [getGraphemeData] {
lassign $testCase testNum comment graphemes
test grapheme-length-ucd-$testNum.0 $comment -body {
grapheme length [join $graphemes ""]
} -result [llength $graphemes]
}
### grapheme index
tcltests::testnumargs "grapheme index" "string strIndex" ""
foreach testCase [getGraphemeData] {
lassign $testCase testNum comment graphemes
set text [join $graphemes ""]
set len [llength $graphemes]
foreach index [list -1 {*}[lseq $len] $len end end-1] {
test grapheme-index-ucd-$testNum.$index $comment -body {
grapheme index $text $index
} -result [lindex $graphemes $index]
}
}
}
::tcltest::cleanupTests
namespace delete grapheme::test
return