Add unit test and fix

This commit is contained in:
PedroEdiaz
2025-05-20 13:35:03 -06:00
parent dda306aa43
commit 4e2a98bf8c
3 changed files with 116 additions and 6 deletions

View File

@@ -27,8 +27,11 @@ main :: proc() {
"name": "Alice", "name": "Alice",
"place": "Odinland", "place": "Odinland",
} }
defer delete(values)
result := template.template(fmt, values) result := template.template(fmt, values)
defer delete(result)
fmt.println(result) // Output: Hello, Alice! Welcome to Odinland. fmt.println(result) // Output: Hello, Alice! Welcome to Odinland.
} }
``` ```
@@ -71,3 +74,4 @@ Keys must exactly match the strings in the dictionary.
## Contributing ## Contributing
Contributions are welcome! Feel free to submit issues or pull requests. Contributions are welcome! Feel free to submit issues or pull requests.
For every issue please add a test

View File

@@ -28,22 +28,24 @@ template :: proc(fmt: string, dict: map[string]string ) -> string {
case .open_bracket: case .open_bracket:
s=.reading_key s=.reading_key
case .close_bracket: case .close_bracket:
strings.write_rune(&b, '}' ) strings.write_string(&b, "}{" )
s=.writing s=.writing
case .writing: case .writing:
s=.open_bracket s=.open_bracket
case .reading_key: case .reading_key:
strings.write_rune(&key, '{' )
} }
case '}': case '}':
switch s { switch s {
case .open_bracket: case .open_bracket:
strings.write_rune(&b, '{' ) strings.write_string(&b, "{}" )
s=.close_bracket
case .close_bracket:
s=.writing s=.writing
case .close_bracket:
strings.write_string(&b, dict[strings.to_string(key)] ) strings.write_string(&b, dict[strings.to_string(key)] )
strings.builder_reset(&key) strings.builder_reset(&key)
s=.writing
case .writing: case .writing:
strings.write_rune(&b, '}' )
case .reading_key: case .reading_key:
s=.close_bracket s=.close_bracket
} }
@@ -52,9 +54,11 @@ template :: proc(fmt: string, dict: map[string]string ) -> string {
case .open_bracket: case .open_bracket:
strings.write_rune(&b, '{' ) strings.write_rune(&b, '{' )
strings.write_rune(&b, c ) strings.write_rune(&b, c )
s=.writing
case .close_bracket: case .close_bracket:
strings.write_rune(&b, '}' ) strings.write_rune(&key, '}' )
strings.write_rune(&b, c ) strings.write_rune(&key, c )
s=.reading_key
case .writing: case .writing:
strings.write_rune(&b, c ) strings.write_rune(&b, c )
case .reading_key: case .reading_key:
@@ -66,9 +70,12 @@ template :: proc(fmt: string, dict: map[string]string ) -> string {
case .open_bracket: case .open_bracket:
strings.write_rune(&b, '{' ) strings.write_rune(&b, '{' )
case .close_bracket: case .close_bracket:
strings.write_string(&b,"{{")
strings.write_string(&b,strings.to_string(key))
strings.write_rune(&b, '}' ) strings.write_rune(&b, '}' )
case .writing: case .writing:
case .reading_key: case .reading_key:
strings.write_string(&b,"{{")
strings.write_string(&b,strings.to_string(key)) strings.write_string(&b,strings.to_string(key))
} }

99
testing.odin Normal file
View File

@@ -0,0 +1,99 @@
package template
import "core:testing"
@(test)
test1 :: proc(t: ^testing.T){
fmt := "{"
tmp := template(fmt,{})
defer delete(tmp)
testing.expect(t, tmp==fmt, tmp)
}
@(test)
test2 :: proc(t: ^testing.T){
fmt := "}"
tmp := template(fmt,{})
defer delete(tmp)
testing.expect(t, tmp==fmt, tmp)
}
@(test)
test3 :: proc(t: ^testing.T){
fmt := "{{"
tmp := template(fmt,{})
defer delete(tmp)
testing.expect(t, tmp==fmt, tmp)
}
@(test)
test4 :: proc(t: ^testing.T){
fmt := "{{}"
tmp := template(fmt,{})
defer delete(tmp)
testing.expect(t, tmp==fmt, tmp)
}
@(test)
test5 :: proc(t: ^testing.T){
fmt := "{{}}"
tmp := template(fmt,{})
defer delete(tmp)
testing.expect(t, tmp=="", tmp)
}
@(test)
test6 :: proc(t: ^testing.T){
fmt := "{{foo}}"
dict := map[string]string{"foo"="var"}
defer delete(dict)
tmp := template(fmt, dict)
defer delete(tmp)
testing.expect(t, tmp=="var", tmp)
}
@(test)
test7 :: proc(t: ^testing.T){
fmt := "{{{}}"
tmp := template(fmt,{})
defer delete(tmp)
testing.expect(t, tmp=="", tmp)
}
@(test)
test8 :: proc(t: ^testing.T){
fmt := "{{}}}"
tmp := template(fmt,{})
defer delete(tmp)
testing.expect(t, tmp=="}", tmp)
}
@(test)
test9 :: proc(t: ^testing.T){
fmt := "{{{}}}"
tmp := template(fmt,{})
defer delete(tmp)
testing.expect(t, tmp=="}", tmp)
}
@(test)
test10 :: proc(t: ^testing.T){
fmt := "{{} }}"
tmp := template(fmt,{})
defer delete(tmp)
testing.expect(t, tmp=="", tmp)
}
@(test)
test11 :: proc(t: ^testing.T){
fmt := "{{{} }}"
tmp := template(fmt,{})
defer delete(tmp)
testing.expect(t, tmp=="", tmp)
}
@(test)
test12 :: proc(t: ^testing.T){
fmt := " {{{} }}"
tmp := template(fmt,{})
defer delete(tmp)
testing.expect(t, tmp==" ", tmp)
}
@(test)
test13 :: proc(t: ^testing.T){
fmt := "{{{} }} "
tmp := template(fmt,{})
defer delete(tmp)
testing.expect(t, tmp==" ", tmp)
}