From dc8807eb2224453cf4e96c43b7906e6fc98453d5 Mon Sep 17 00:00:00 2001 From: Alex Kulcsar <38046796+alex-kulcsar@users.noreply.github.com> Date: Mon, 28 Oct 2024 18:12:47 -0400 Subject: [PATCH] Add Base64 tests. --- base64.ts | 43 +++++++++++++++++++++++++++++++++++++++++++ main.ts | 5 +++++ pxt.json | 5 +++-- 3 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 base64.ts diff --git a/base64.ts b/base64.ts new file mode 100644 index 0000000..ae446b1 --- /dev/null +++ b/base64.ts @@ -0,0 +1,43 @@ +namespace B64Tests { + export function run(): boolean { + let allPassed: boolean = true + + let bytes: Uint8Array = new Uint8Array(5) + bytes.setFromArray([1, 2, 3, 4, 5,]) + let b64: string = Base64.encodeBuffer(bytes.buffer) + if (b64 != 'AQIDBAU=') { + game.splash("Base64 test 1a failed.") + allPassed = false + } + let bytesOut: Uint8Array = Base64.decodeBuffer(b64) + if (!areEquivalent2(bytesOut, bytes)) { + game.splash("Base64 test 1b failed.") + allPassed = false + } + + b64 = 'foocat==' + let b64Standard: string = Base64.encodeBuffer(Base64.decodeBuffer(b64).buffer) + if (b64Standard != 'foocag==') { + game.splash("Base64 test 2 failed.") + allPassed = false + } + + try { + Base64.decodeBuffer('abc') + game.splash("Base64 test 3a failed.") + allPassed = false + } catch { + + } + + try { + Base64.decodeBuffer('123%') + game.splash("Base64 test 3b failed.") + allPassed = false + } catch { + + } + + return allPassed + } +} \ No newline at end of file diff --git a/main.ts b/main.ts index 2777328..4796b9c 100644 --- a/main.ts +++ b/main.ts @@ -14,6 +14,11 @@ if (!DataViewTests.run()) { allPassed = false } +// Bas64 tests. +if (!B64Tests.run()) { + allPassed = false +} + if (allPassed) { game.splash("All tests passed!") } else { diff --git a/pxt.json b/pxt.json index 142faa0..6bc8ff4 100644 --- a/pxt.json +++ b/pxt.json @@ -4,7 +4,7 @@ "description": "", "dependencies": { "device": "*", - "pxt-typed-arrays": "github:robo-technical-group/pxt-typed-arrays#v1.1.0" + "pxt-typed-arrays": "github:robo-technical-group/pxt-typed-arrays#v1.2.0" }, "files": [ "README.md", @@ -13,7 +13,8 @@ "arraybuffer.ts", "dataview.ts", "typedarrays.ts", - "common.ts" + "common.ts", + "base64.ts" ], "testFiles": [ "test.ts"