diff --git a/lightbug_http/io/bytes.mojo b/lightbug_http/io/bytes.mojo index 388fe771..289de937 100644 --- a/lightbug_http/io/bytes.mojo +++ b/lightbug_http/io/bytes.mojo @@ -65,7 +65,7 @@ struct ByteWriter(Writer): @always_inline fn consuming_write(mut self, owned s: String): - # kind of cursed but seems to work? + # kind of cursed but seems to work? pops the null terminator _ = s._buffer.pop() self._inner.extend(s._buffer^) s._buffer = s._buffer_type() @@ -144,6 +144,18 @@ struct ByteView[origin: Origin](Testable): if self[i] != other[i]: return False return True + + fn __eq__(self, other: Bytes) -> Bool: + # Check if lengths match + if len(self) != len(other): + return False + + # Compare each byte + for i in range(len(self)): + if self[i] != other[i]: + return False + return True + fn __ne__(self, other: Self) -> Bool: return not self == other diff --git a/tests/lightbug_http/io/test_byte_reader.mojo b/tests/lightbug_http/io/test_byte_reader.mojo index 401eee35..8f96cbc9 100644 --- a/tests/lightbug_http/io/test_byte_reader.mojo +++ b/tests/lightbug_http/io/test_byte_reader.mojo @@ -1,5 +1,6 @@ import testing from lightbug_http.io.bytes import Bytes, ByteReader, EndOfReaderError +from lightbug_http.strings import to_string alias example = "Hello, World!" @@ -15,28 +16,26 @@ def test_peek(): r.read_pos = 1 with testing.assert_raises(contains="No more bytes to read."): _ = r.peek() - - def test_read_until(): var r = ByteReader(example.as_bytes()) testing.assert_equal(r.read_pos, 0) - testing.assert_equal(r.read_until(ord(",")).to_bytes(), Bytes(72, 101, 108, 108, 111)) + testing.assert_equal(to_string(r.read_until(ord(",")).to_bytes()), to_string(Bytes(72, 101, 108, 108, 111))) testing.assert_equal(r.read_pos, 5) def test_read_bytes(): var r = ByteReader(example.as_bytes()) - testing.assert_equal(r.read_bytes().to_bytes(), Bytes(72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33)) + testing.assert_equal(to_string(r.read_bytes().to_bytes()), to_string(Bytes(72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33))) r = ByteReader(example.as_bytes()) - testing.assert_equal(r.read_bytes(7).to_bytes(), Bytes(72, 101, 108, 108, 111, 44, 32)) - testing.assert_equal(r.read_bytes().to_bytes(), Bytes(87, 111, 114, 108, 100, 33)) + testing.assert_equal(to_string(r.read_bytes(7).to_bytes()), to_string(Bytes(72, 101, 108, 108, 111, 44, 32))) + testing.assert_equal(to_string(r.read_bytes().to_bytes()), to_string(Bytes(87, 111, 114, 108, 100, 33))) def test_read_word(): var r = ByteReader(example.as_bytes()) testing.assert_equal(r.read_pos, 0) - testing.assert_equal(r.read_word().to_bytes(), Bytes(72, 101, 108, 108, 111, 44)) + testing.assert_equal(to_string(r.read_word().to_bytes()), to_string(Bytes(72, 101, 108, 108, 111, 44))) testing.assert_equal(r.read_pos, 6) @@ -44,15 +43,15 @@ def test_read_line(): # No newline, go to end of line var r = ByteReader(example.as_bytes()) testing.assert_equal(r.read_pos, 0) - testing.assert_equal(r.read_line().to_bytes(), Bytes(72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33)) + testing.assert_equal(to_string(r.read_line().to_bytes()), to_string(Bytes(72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33))) testing.assert_equal(r.read_pos, 13) # Newline, go to end of line. Should cover carriage return and newline var r2 = ByteReader("Hello\r\nWorld\n!".as_bytes()) testing.assert_equal(r2.read_pos, 0) - testing.assert_equal(r2.read_line().to_bytes(), Bytes(72, 101, 108, 108, 111)) + testing.assert_equal(to_string(r2.read_line().to_bytes()), to_string(Bytes(72, 101, 108, 108, 111))) testing.assert_equal(r2.read_pos, 7) - testing.assert_equal(r2.read_line().to_bytes(), Bytes(87, 111, 114, 108, 100)) + testing.assert_equal(to_string(r2.read_line().to_bytes()), to_string(Bytes(87, 111, 114, 108, 100))) testing.assert_equal(r2.read_pos, 13) @@ -60,16 +59,16 @@ def test_skip_whitespace(): var r = ByteReader(" Hola".as_bytes()) r.skip_whitespace() testing.assert_equal(r.read_pos, 1) - testing.assert_equal(r.read_word().to_bytes(), Bytes(72, 111, 108, 97)) + testing.assert_equal(to_string(r.read_word().to_bytes()), to_string(Bytes(72, 111, 108, 97))) def test_skip_carriage_return(): var r = ByteReader("\r\nHola".as_bytes()) r.skip_carriage_return() testing.assert_equal(r.read_pos, 2) - testing.assert_equal(r.read_bytes(4).to_bytes(), Bytes(72, 111, 108, 97)) + testing.assert_equal(to_string(r.read_bytes(4).to_bytes()), to_string(Bytes(72, 111, 108, 97))) def test_consume(): var r = ByteReader(example.as_bytes()) - testing.assert_equal(r^.consume(), Bytes(72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33)) + testing.assert_equal(to_string(r^.consume()), to_string(Bytes(72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33))) diff --git a/tests/lightbug_http/io/test_byte_writer.mojo b/tests/lightbug_http/io/test_byte_writer.mojo index 50b2a441..be566266 100644 --- a/tests/lightbug_http/io/test_byte_writer.mojo +++ b/tests/lightbug_http/io/test_byte_writer.mojo @@ -1,15 +1,15 @@ import testing from lightbug_http.io.bytes import Bytes, ByteWriter - +from lightbug_http.strings import to_string def test_write_byte(): var w = ByteWriter() w.write_byte(0x01) - testing.assert_equal(w^.consume(), Bytes(0x01)) + testing.assert_equal(to_string(w^.consume()), to_string(Bytes(0x01))) w = ByteWriter() w.write_byte(2) - testing.assert_equal(w^.consume(), Bytes(2)) + testing.assert_equal(to_string(w^.consume()), to_string(Bytes(2))) def test_consuming_write(): @@ -17,17 +17,14 @@ def test_consuming_write(): var my_string: String = "World" w.consuming_write("Hello ") w.consuming_write(my_string^) - testing.assert_equal(w^.consume(), Bytes(72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100)) - - w = ByteWriter() - var my_bytes = Bytes(72, 101, 108, 108, 111, 32) - w.consuming_write(my_bytes^) - w.consuming_write(Bytes(87, 111, 114, 108, 10)) - testing.assert_equal(w^.consume(), Bytes(72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100)) + + var result = w^.consume() + + testing.assert_equal(to_string(result), "Hello World") def test_write(): var w = ByteWriter() w.write("Hello", ", ") w.write_bytes("World!".as_bytes()) - testing.assert_equal(w^.consume(), Bytes(72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33)) + testing.assert_equal(to_string(w^.consume()), to_string(Bytes(72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33))) diff --git a/tests/lightbug_http/io/test_bytes.mojo b/tests/lightbug_http/io/test_bytes.mojo index 8affbc89..cbb466be 100644 --- a/tests/lightbug_http/io/test_bytes.mojo +++ b/tests/lightbug_http/io/test_bytes.mojo @@ -1,6 +1,7 @@ import testing from collections import Dict, List from lightbug_http.io.bytes import Bytes, ByteView, bytes +from lightbug_http.strings import to_string fn test_string_literal_to_bytes() raises: @@ -17,7 +18,7 @@ fn test_string_literal_to_bytes() raises: ) for c in cases.items(): - testing.assert_equal(Bytes(c[].key.as_bytes()), c[].value) + testing.assert_equal(to_string(Bytes(c[].key.as_bytes())), to_string(c[].value)) fn test_string_to_bytes() raises: @@ -34,5 +35,5 @@ fn test_string_to_bytes() raises: ) for c in cases.items(): - testing.assert_equal(Bytes(c[].key.as_bytes()), c[].value) + testing.assert_equal(to_string(Bytes(c[].key.as_bytes())), to_string(c[].value))