Skip to content

Commit

Permalink
Local to Cloud
Browse files Browse the repository at this point in the history
  • Loading branch information
james-beans authored Oct 13, 2024
1 parent bbbd552 commit ed602a4
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
8 changes: 8 additions & 0 deletions deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"tasks": {
"dev": "deno run --watch main.ts"
},
"imports": {
"@std/assert": "jsr:@std/assert@1"
}
}
23 changes: 23 additions & 0 deletions main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// rndhex - main.ts
import { bold, rgb24 } from "https://deno.land/[email protected]/fmt/colors.ts";

// Function to generate a random hex color code
export function getRandomHexCode(): string {
const randomInt = Math.floor(Math.random() * 0xffffff);
return `#${randomInt.toString(16).padStart(6, '0')}`;
}

// Function to print a square of hashtags with the background color
export function printColoredSquare(hex: string) {
const r = parseInt(hex.substring(1, 3), 16);
const g = parseInt(hex.substring(3, 5), 16);
const b = parseInt(hex.substring(5, 7), 16);

// Print a 3x3 square with the background color
const square = `###\n###\n###`;
console.log(rgb24(bold(square), { r, g, b }));
}

const hexCode = getRandomHexCode();
console.log(hexCode); // Print the random hex code
printColoredSquare(hexCode); // Print the square with the hex color
36 changes: 36 additions & 0 deletions main_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// rndhex - main_test.ts
import { assertMatch } from "https://deno.land/[email protected]/assert/mod.ts";
import { getRandomHexCode, printColoredSquare } from "./main.ts";

// Test for generating a random hex code
Deno.test("Random Hex Code Generation", () => {
const hexCode = getRandomHexCode();

// Check that the hex code matches the pattern for a valid hex color code
const hexPattern = /^#[0-9a-fA-F]{6}$/;
assertMatch(hexCode, hexPattern, `Generated hex code (${hexCode}) is not valid`);
});

// Mock the console output to test the square printing
Deno.test("Print Colored Square Format", () => {
const hexCode = "#ffffff"; // Example hex code to test

// Capture the console output
const originalConsoleLog = console.log;
let consoleOutput = "";
console.log = (output: string) => {
consoleOutput += output + "\n";
};

try {
// Call the function to print the colored square
printColoredSquare(hexCode);

// Check that the output contains the square format
const squarePattern = /###\n###\n###/;
assertMatch(consoleOutput, squarePattern, "Square format not found in output");
} finally {
// Restore the original console.log
console.log = originalConsoleLog;
}
});

0 comments on commit ed602a4

Please sign in to comment.