Port tests\core\fmt

This commit is contained in:
Jeroen van Rijn
2024-05-30 16:52:29 +02:00
committed by Feoramund
parent d334b8c72a
commit 6f7c5a7577
3 changed files with 22 additions and 43 deletions
+1 -1
View File
@@ -57,7 +57,7 @@ filepath_test:
$(ODIN) test path/filepath $(COMMON) -out:test_core_filepath
fmt_test:
$(ODIN) run fmt $(COMMON) -out:test_core_fmt
$(ODIN) test fmt $(COMMON) -out:test_core_fmt
hash_test:
$(ODIN) run hash $(COMMON) -o:speed -out:test_hash
+1 -1
View File
@@ -42,7 +42,7 @@ echo ---
echo ---
echo Running core:fmt tests
echo ---
%PATH_TO_ODIN% run fmt %COMMON% %COLLECTION% -out:test_core_fmt.exe || exit /b
%PATH_TO_ODIN% test fmt %COMMON% -out:test_core_fmt.exe || exit /b
echo ---
echo Running core:hash tests
+20 -41
View File
@@ -1,47 +1,8 @@
package test_core_fmt
import "core:fmt"
import "core:os"
import "core:testing"
import "core:mem"
TEST_count := 0
TEST_fail := 0
when ODIN_TEST {
expect :: testing.expect
log :: testing.log
} else {
expect :: proc(t: ^testing.T, condition: bool, message: string, loc := #caller_location) {
TEST_count += 1
if !condition {
TEST_fail += 1
fmt.printf("[%v] %v\n", loc, message)
return
}
}
log :: proc(t: ^testing.T, v: any, loc := #caller_location) {
fmt.printf("[%v] ", loc)
fmt.printf("log: %v\n", v)
}
}
main :: proc() {
t := testing.T{}
test_fmt_memory(&t)
test_fmt_doc_examples(&t)
test_fmt_options(&t)
fmt.printf("%v/%v tests successful.\n", TEST_count - TEST_fail, TEST_count)
if TEST_fail > 0 {
os.exit(1)
}
}
check :: proc(t: ^testing.T, exp: string, format: string, args: ..any, loc := #caller_location) {
got := fmt.tprintf(format, ..args)
expect(t, got == exp, fmt.tprintf("(%q, %v): %q != %q", format, args, got, exp), loc)
}
import "core:testing"
@(test)
test_fmt_memory :: proc(t: ^testing.T) {
@@ -75,7 +36,7 @@ test_fmt_doc_examples :: proc(t: ^testing.T) {
}
@(test)
test_fmt_options :: proc(t: ^testing.T) {
test_fmt_escaping_prefixes :: proc(t: ^testing.T) {
// Escaping
check(t, "% { } 0 { } } {", "%% {{ }} {} {{ }} }} {{", 0 )
@@ -86,7 +47,10 @@ test_fmt_options :: proc(t: ^testing.T) {
check(t, "+3", "%+i", 3 )
check(t, "0b11", "%#b", 3 )
check(t, "0xA", "%#X", 10 )
}
@(test)
test_fmt_indexing :: proc(t: ^testing.T) {
// Specific index formatting
check(t, "1 2 3", "%i %i %i", 1, 2, 3)
check(t, "1 2 3", "%[0]i %[1]i %[2]i", 1, 2, 3)
@@ -95,7 +59,10 @@ test_fmt_options :: proc(t: ^testing.T) {
check(t, "1 2 3", "%i %[1]i %i", 1, 2, 3)
check(t, "1 3 2", "%i %[2]i %i", 1, 2, 3)
check(t, "1 1 1", "%[0]i %[0]i %[0]i", 1)
}
@(test)
test_fmt_width_precision :: proc(t: ^testing.T) {
// Width
check(t, "3.140", "%f", 3.14)
check(t, "3.140", "%4f", 3.14)
@@ -133,7 +100,10 @@ test_fmt_options :: proc(t: ^testing.T) {
check(t, "3.140", "%*[1].*[2][0]f", 3.14, 5, 3)
check(t, "3.140", "%*[2].*[1]f", 3.14, 3, 5)
check(t, "3.140", "%5.*[1]f", 3.14, 3)
}
@(test)
test_fmt_arg_errors :: proc(t: ^testing.T) {
// Error checking
check(t, "%!(MISSING ARGUMENT)%!(NO VERB)", "%" )
@@ -156,7 +126,10 @@ test_fmt_options :: proc(t: ^testing.T) {
check(t, "%!(BAD ARGUMENT NUMBER)%!(NO VERB)%!(EXTRA 0)", "%[1]", 0)
check(t, "3.1%!(EXTRA 3.14)", "%.1f", 3.14, 3.14)
}
@(test)
test_fmt_python_syntax :: proc(t: ^testing.T) {
// Python-like syntax
check(t, "1 2 3", "{} {} {}", 1, 2, 3)
check(t, "3 2 1", "{2} {1} {0}", 1, 2, 3)
@@ -181,3 +154,9 @@ test_fmt_options :: proc(t: ^testing.T) {
check(t, "%!(MISSING CLOSE BRACE)%!(EXTRA 1)", "{", 1)
check(t, "%!(MISSING CLOSE BRACE)%!(EXTRA 1)", "{0", 1 )
}
@(private)
check :: proc(t: ^testing.T, exp: string, format: string, args: ..any) {
got := fmt.tprintf(format, ..args)
testing.expectf(t, got == exp, "(%q, %v): %q != %q", format, args, got, exp)
}