mirror of
https://github.com/sbrow/envr.git
synced 2026-06-27 10:38:33 -04:00
refactor(odin): Fixed AI mistakes.
This commit is contained in:
101
table_test.odin
Normal file
101
table_test.odin
Normal file
@@ -0,0 +1,101 @@
|
||||
package main
|
||||
|
||||
import "core:encoding/json"
|
||||
import "core:fmt"
|
||||
import "core:io"
|
||||
import "core:strings"
|
||||
import "core:testing"
|
||||
|
||||
@(test)
|
||||
test_render_json_rows_normal :: proc(t: ^testing.T) {
|
||||
b: strings.Builder
|
||||
strings.builder_init(&b)
|
||||
defer strings.builder_destroy(&b)
|
||||
|
||||
headers := []string{"name", "path"}
|
||||
rows := [][]string{{"foo", "/home/user/.env"}, {"bar", "/home/user/project/.env"}}
|
||||
|
||||
w := strings.to_writer(&b)
|
||||
render_json_rows(w, headers, rows)
|
||||
|
||||
output := strings.to_string(b)
|
||||
|
||||
result: []map[string]string
|
||||
unmarshal_err := json.unmarshal_string(output, &result)
|
||||
testing.expect(
|
||||
t,
|
||||
unmarshal_err == nil,
|
||||
fmt.aprintf("json unmarshal failed: %v\noutput was: %q", unmarshal_err, output),
|
||||
)
|
||||
testing.expect(t, len(result) == 2, fmt.aprintf("expected 2 rows, got %d", len(result)))
|
||||
testing.expect(
|
||||
t,
|
||||
result[0]["name"] == "foo",
|
||||
fmt.aprintf("expected name=foo, got %q", result[0]["name"]),
|
||||
)
|
||||
testing.expect(t, result[0]["path"] == "/home/user/.env")
|
||||
testing.expect(t, result[1]["name"] == "bar")
|
||||
testing.expect(t, result[1]["path"] == "/home/user/project/.env")
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_render_json_rows_special_chars :: proc(t: ^testing.T) {
|
||||
b: strings.Builder
|
||||
strings.builder_init(&b)
|
||||
defer strings.builder_destroy(&b)
|
||||
|
||||
headers := []string{"key", "value"}
|
||||
rows := [][]string {
|
||||
{"quote", `has "double quotes"`},
|
||||
{"backslash", `path\to\file`},
|
||||
{"newline", "line1\nline2"},
|
||||
{"mixed", `a "b" c\nd`},
|
||||
}
|
||||
|
||||
w := strings.to_writer(&b)
|
||||
render_json_rows(w, headers, rows)
|
||||
|
||||
output := strings.to_string(b)
|
||||
|
||||
result: []map[string]string
|
||||
unmarshal_err := json.unmarshal(transmute([]byte)output, &result)
|
||||
testing.expect(
|
||||
t,
|
||||
unmarshal_err == nil,
|
||||
fmt.aprintf("json unmarshal failed: %v\noutput was: %q", unmarshal_err, output),
|
||||
)
|
||||
testing.expect(t, len(result) == 4)
|
||||
testing.expect(
|
||||
t,
|
||||
result[0]["value"] == `has "double quotes"`,
|
||||
fmt.aprintf("got %q", result[0]["value"]),
|
||||
)
|
||||
testing.expect(t, result[1]["value"] == `path\to\file`)
|
||||
testing.expect(t, result[2]["value"] == "line1\nline2")
|
||||
testing.expect(t, result[3]["value"] == `a "b" c\nd`)
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_render_json_rows_empty :: proc(t: ^testing.T) {
|
||||
b: strings.Builder
|
||||
strings.builder_init(&b)
|
||||
defer strings.builder_destroy(&b)
|
||||
|
||||
headers := []string{"name"}
|
||||
rows: [][]string
|
||||
|
||||
w := strings.to_writer(&b)
|
||||
render_json_rows(w, headers, rows)
|
||||
|
||||
output := strings.to_string(b)
|
||||
|
||||
result: []map[string]string
|
||||
unmarshal_err := json.unmarshal_string(output, &result)
|
||||
testing.expect(
|
||||
t,
|
||||
unmarshal_err == nil,
|
||||
fmt.aprintf("json unmarshal failed: %v\noutput was: %q", unmarshal_err, output),
|
||||
)
|
||||
testing.expect(t, len(result) == 0)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user