Merge pull request #2445 from Lperlind/documentation/strings_returns

Document return values of strings and add allocator errors where possible
This commit is contained in:
Jeroen van Rijn
2023-04-08 10:05:39 +02:00
committed by GitHub
9 changed files with 504 additions and 425 deletions
+26 -12
View File
@@ -5,6 +5,7 @@ import "core:testing"
import "core:fmt"
import "core:os"
import "core:runtime"
import "core:mem"
TEST_count := 0
TEST_fail := 0
@@ -105,33 +106,46 @@ Case_Kind :: enum {
Ada_Case,
}
test_cases := [Case_Kind]struct{s: string, p: proc(r: string, allocator: runtime.Allocator) -> string}{
Case_Proc :: proc(r: string, allocator: runtime.Allocator) -> (string, mem.Allocator_Error)
test_cases := [Case_Kind]struct{s: string, p: Case_Proc}{
.Lower_Space_Case = {"hellope world", to_lower_space_case},
.Upper_Space_Case = {"HELLOPE WORLD", to_upper_space_case},
.Lower_Snake_Case = {"hellope_world", strings.to_snake_case},
.Upper_Snake_Case = {"HELLOPE_WORLD", strings.to_upper_snake_case},
.Lower_Kebab_Case = {"hellope-world", strings.to_kebab_case},
.Upper_Kebab_Case = {"HELLOPE-WORLD", strings.to_upper_kebab_case},
.Camel_Case = {"hellopeWorld", strings.to_camel_case},
.Pascal_Case = {"HellopeWorld", strings.to_pascal_case},
.Ada_Case = {"Hellope_World", strings.to_ada_case},
.Lower_Snake_Case = {"hellope_world", to_snake_case},
.Upper_Snake_Case = {"HELLOPE_WORLD", to_upper_snake_case},
.Lower_Kebab_Case = {"hellope-world", to_kebab_case},
.Upper_Kebab_Case = {"HELLOPE-WORLD", to_upper_kebab_case},
.Camel_Case = {"hellopeWorld", to_camel_case},
.Pascal_Case = {"HellopeWorld", to_pascal_case},
.Ada_Case = {"Hellope_World", to_ada_case},
}
to_lower_space_case :: proc(r: string, allocator: runtime.Allocator) -> string {
to_lower_space_case :: proc(r: string, allocator: runtime.Allocator) -> (string, mem.Allocator_Error) {
return strings.to_delimiter_case(r, ' ', false, allocator)
}
to_upper_space_case :: proc(r: string, allocator: runtime.Allocator) -> string {
to_upper_space_case :: proc(r: string, allocator: runtime.Allocator) -> (string, mem.Allocator_Error) {
return strings.to_delimiter_case(r, ' ', true, allocator)
}
// NOTE: we have these wrappers as having #optional_allocator_error changes the type to not be equivalent
to_snake_case :: proc(r: string, allocator: runtime.Allocator) -> (string, mem.Allocator_Error) { return strings.to_snake_case(r, allocator) }
to_upper_snake_case :: proc(r: string, allocator: runtime.Allocator) -> (string, mem.Allocator_Error) { return strings.to_upper_snake_case(r, allocator) }
to_kebab_case :: proc(r: string, allocator: runtime.Allocator) -> (string, mem.Allocator_Error) { return strings.to_kebab_case(r, allocator) }
to_upper_kebab_case :: proc(r: string, allocator: runtime.Allocator) -> (string, mem.Allocator_Error) { return strings.to_upper_kebab_case(r, allocator) }
to_camel_case :: proc(r: string, allocator: runtime.Allocator) -> (string, mem.Allocator_Error) { return strings.to_camel_case(r, allocator) }
to_pascal_case :: proc(r: string, allocator: runtime.Allocator) -> (string, mem.Allocator_Error) { return strings.to_pascal_case(r, allocator) }
to_ada_case :: proc(r: string, allocator: runtime.Allocator) -> (string, mem.Allocator_Error) { return strings.to_ada_case(r, allocator) }
@test
test_case_conversion :: proc(t: ^testing.T) {
for entry in test_cases {
for test_case, case_kind in test_cases {
result := entry.p(test_case.s, context.allocator)
result, err := entry.p(test_case.s, context.allocator)
msg := fmt.tprintf("ERROR: We got the allocation error '{}'\n", err)
expect(t, err == nil, msg)
defer delete(result)
msg := fmt.tprintf("ERROR: Input `{}` to converter {} does not match `{}`, got `{}`.\n", test_case.s, case_kind, entry.s, result)
msg = fmt.tprintf("ERROR: Input `{}` to converter {} does not match `{}`, got `{}`.\n", test_case.s, case_kind, entry.s, result)
expect(t, result == entry.s, msg)
}
}