tcl: classic programs — for-each-line, assert, with-temp-var (+3 tests, 228 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 42s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 42s
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
13
lib/tcl/tests/programs/assert.tcl
Normal file
13
lib/tcl/tests/programs/assert.tcl
Normal file
@@ -0,0 +1,13 @@
|
||||
proc assert {expr_str} {
|
||||
set result [uplevel 1 [list expr $expr_str]]
|
||||
if {!$result} {
|
||||
error "Assertion failed: $expr_str"
|
||||
}
|
||||
}
|
||||
|
||||
set x 42
|
||||
assert {$x == 42}
|
||||
assert {$x > 0}
|
||||
set x 10
|
||||
assert {$x < 100}
|
||||
set x
|
||||
13
lib/tcl/tests/programs/for-each-line.tcl
Normal file
13
lib/tcl/tests/programs/for-each-line.tcl
Normal file
@@ -0,0 +1,13 @@
|
||||
proc for-each-line {var lines body} {
|
||||
foreach item $lines {
|
||||
uplevel 1 [list set $var $item]
|
||||
uplevel 1 $body
|
||||
}
|
||||
}
|
||||
|
||||
# Usage: accumulate lengths of each "line"
|
||||
set total 0
|
||||
for-each-line line {hello world foo} {
|
||||
incr total [string length $line]
|
||||
}
|
||||
set total
|
||||
13
lib/tcl/tests/programs/with-temp-var.tcl
Normal file
13
lib/tcl/tests/programs/with-temp-var.tcl
Normal file
@@ -0,0 +1,13 @@
|
||||
proc with-temp-var {varname tempval body} {
|
||||
upvar 1 $varname v
|
||||
set saved $v
|
||||
set v $tempval
|
||||
uplevel 1 $body
|
||||
set v $saved
|
||||
}
|
||||
|
||||
set x 100
|
||||
with-temp-var x 999 {
|
||||
set captured $x
|
||||
}
|
||||
list $x $captured
|
||||
Reference in New Issue
Block a user