Skip to content

Commit

Permalink
Generalize test example for Godot 3.x compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
ashtonmeuser committed Sep 14, 2023
1 parent c3839a6 commit 921688b
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion examples/wasm-test/TestGeneral.gd
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func test_load():

func test_invalid_binary():
var wasm = Wasm.new()
var buffer = "asdf".to_utf8_buffer()
var buffer = Utils.to_utf8("asdf")
var error = wasm.compile(buffer)
expect_eq(error, ERR_INVALID_DATA)
expect_error("Invalid binary")
Expand Down
6 changes: 3 additions & 3 deletions examples/wasm-test/TestMemory.gd
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,12 @@ func test_memory_marshal():
var result = wasm.memory.seek(offset).get_var()
expect_eq(result, data)

func test_ascii_sum():
func test_string_sum():
var wasm = load_wasm("memory")
var offset = wasm.global("offset")
var string = "test_string" # String to store in Wasm memory
var sum = 0 # Sum of ASCII character values
for c in string.to_ascii_buffer(): sum += c
var sum = 0 # Sum of character UTF8 values
for c in Utils.to_utf8(string): sum += c
# Write string to memory using StreamPeer interface
# Note that Godot string marshalling prepends 32-bit string length
# See https://docs.godotengine.org/en/stable/classes/class_streampeer.html#class-streampeer-method-put-string
Expand Down
2 changes: 1 addition & 1 deletion examples/wasm-test/utils/TestSuite.gd
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func expect_error(s: String):
# Account for error stack trace
var regex = Utils.make_regex("^\\s+at:\\s")
var position = _log_file.get_position()
while _log_file.get_position() < _log_file.get_length():
while _log_file.get_position() < Utils.file_length(_log_file):
if !regex.search(_log_file.get_line()): break
position = _log_file.get_position()
_log_file.seek(position)
Expand Down
6 changes: 6 additions & 0 deletions examples/wasm-test/utils/Utils.gd
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ static func make_regex(pattern: String) -> RegEx:
var regex = RegEx.new()
assert(regex.compile(pattern) == OK, "Invalid regex pattern: %s" % pattern)
return regex

static func file_length(f: FileAccess) -> int:
return f.get_length()

static func to_utf8(s: String) -> PackedByteArray:
return s.to_utf8_buffer()

0 comments on commit 921688b

Please sign in to comment.