From 27c433f750edb475f6c8c56e992516c038f28fa2 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Fri, 12 Mar 2021 10:44:47 +1100 Subject: [PATCH] Test hex conversion functions --- src/test_utils/nim_waku.spec.ts | 43 ++++++++++++++++++++++++++++++++- src/test_utils/nim_waku.ts | 17 +++++++------ 2 files changed, 51 insertions(+), 9 deletions(-) diff --git a/src/test_utils/nim_waku.spec.ts b/src/test_utils/nim_waku.spec.ts index 87167a7204..c97c4539ec 100644 --- a/src/test_utils/nim_waku.spec.ts +++ b/src/test_utils/nim_waku.spec.ts @@ -1,6 +1,6 @@ import test from 'ava'; -import { argsToArray, mergeArguments } from './nim_waku'; +import { argsToArray, bufToHex, mergeArguments, strToHex } from './nim_waku'; test('Default arguments are correct', (t) => { const args = mergeArguments({}); @@ -36,3 +36,44 @@ test('Passing staticnode argument return default + static node', (t) => { t.deepEqual(actual, expected); }); + +test('Convert utf-8 string to hex', (t) => { + const str = 'This is an utf-8 string.'; + const expected = '0x5468697320697320616e207574662d3820737472696e672e'; + + const actual = strToHex(str); + t.deepEqual(actual, expected); +}); + +test('Convert buffer to hex', (t) => { + const buf = Uint8Array.from([ + 0x54, + 0x68, + 0x69, + 0x73, + 0x20, + 0x69, + 0x73, + 0x20, + 0x61, + 0x6e, + 0x20, + 0x75, + 0x74, + 0x66, + 0x2d, + 0x38, + 0x20, + 0x73, + 0x74, + 0x72, + 0x69, + 0x6e, + 0x67, + 0x2e, + ]); + const expected = '0x5468697320697320616e207574662d3820737472696e672e'; + + const actual = bufToHex(buf); + t.deepEqual(actual, expected); +}); diff --git a/src/test_utils/nim_waku.ts b/src/test_utils/nim_waku.ts index c7c3ee1e8a..90ad38aaf8 100644 --- a/src/test_utils/nim_waku.ts +++ b/src/test_utils/nim_waku.ts @@ -178,14 +178,13 @@ export function mergeArguments(args: Args): Args { return res; } -// TODO: Test this -function strToHex(str: string): string { +export function strToHex(str: string): string { let hex: string; try { hex = unescape(encodeURIComponent(str)) .split('') .map(function (v) { - return v.charCodeAt(0).toString(16).toUpperCase(); + return v.charCodeAt(0).toString(16); }) .join(''); } catch (e) { @@ -195,9 +194,11 @@ function strToHex(str: string): string { return '0x' + hex; } -// TODO: Test this -function bufToHex(buffer: Uint8Array) { - return Array.prototype.map - .call(buffer, (x) => ('00' + x.toString(16)).slice(-2)) - .join(''); +export function bufToHex(buffer: Uint8Array) { + return ( + '0x' + + Array.prototype.map + .call(buffer, (x) => ('00' + x.toString(16)).slice(-2)) + .join('') + ); }