From 6887b437778944a1ee2b8a04fb5304b1aeb21bf1 Mon Sep 17 00:00:00 2001 From: diegomrsantos Date: Mon, 22 May 2023 11:07:22 +0200 Subject: [PATCH] Improve utility tests (#893) --- tests/testutility.nim | 58 +++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 30 deletions(-) diff --git a/tests/testutility.nim b/tests/testutility.nim index 18c8ce141..e6a499fa3 100644 --- a/tests/testutility.nim +++ b/tests/testutility.nim @@ -16,61 +16,59 @@ import ../libp2p/utility suite "Utility": test "successful safeConvert from int8 to int16": - var result: int16 = safeConvert[int16, int8](-128) - assert result == -128, fmt"Expected -128 but got {result}" + let res = safeConvert[int16, int8]((-128).int8) + check res == -128'i16 test "unsuccessful safeConvert from int16 to int8": - doAssert not (compiles do: - var result: int8 = safeConvert[int8, int16](32767)) + check not (compiles do: + result: int8 = safeConvert[int8, int16](32767'i16)) test "successful safeConvert from uint8 to uint16": - var result: uint16 = safeConvert[uint16, uint8](255) - assert result == 255, fmt"Expected 255 but got {result}" + let res: uint16 = safeConvert[uint16, uint8](255'u8) + check res == 255'u16 test "unsuccessful safeConvert from uint16 to uint8": - doAssert not (compiles do: - var result: uint8 = safeConvert[uint8, uint16](256)) + check not (compiles do: + result: uint8 = safeConvert[uint8, uint16](256'u16)) test "successful safeConvert from char to int": - var result: int = safeConvert[int, char]('A') - assert result == 65, fmt"Expected 65 but got {result}" + let res: int = safeConvert[int, char]('A') + check res == 65 test "unsuccessful safeConvert from int to char": - doAssert not (compiles do: - var result: char = safeConvert[char, int](128)) + check not (compiles do: + result: char = safeConvert[char, int](128)) test "successful safeConvert from bool to int": - var result: int = safeConvert[int, bool](true) - assert result == 1, fmt"Expected 1 but got {result}" + let res: int = safeConvert[int, bool](true) + check res == 1 test "unsuccessful safeConvert from int to bool": - doAssert not (compiles do: - var result: bool = safeConvert[bool, int](2)) + check not (compiles do: + result: bool = safeConvert[bool, int](2)) test "successful safeConvert from enum to int": type Color = enum red, green, blue - var result: int = safeConvert[int, Color](green) - assert result == 1, fmt"Expected 1 but got {result}" + let res: int = safeConvert[int, Color](green) + check res == 1 test "unsuccessful safeConvert from int to enum": type Color = enum red, green, blue - doAssert not (compiles do: - var result: Color = safeConvert[Color, int](3)) + check not (compiles do: + result: Color = safeConvert[Color, int](3)) test "successful safeConvert from range to int": - var result: int = safeConvert[int, range[1..10]](5) - assert result == 5, fmt"Expected 5 but got {result}" + let res: int = safeConvert[int, range[1..10]](5) + check res == 5 test "unsuccessful safeConvert from int to range": - doAssert not (compiles do: - var result: range[1..10] = safeConvert[range[1..10], int](11)) + check not (compiles do: + result: range[1..10] = safeConvert[range[1..10], int](11)) test "unsuccessful safeConvert from int to uint": - doAssert not (compiles do: - var result: uint = safeConvert[uint, int](11)) + check not (compiles do: + result: uint = safeConvert[uint, int](11)) test "unsuccessful safeConvert from uint to int": - doAssert not (compiles do: - var result: uint = safeConvert[int, uint](11)) - - + check not (compiles do: + result: uint = safeConvert[int, uint](11.uint))