-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bbbd552
commit ed602a4
Showing
3 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}); |