You're right - for a truly dynamic ASCII art generator that can handle any input, we need a different testing approach. Here's a Vitest file that tests such a function:
// asciiArtGenerator.test.js
import { describe, it, expect } from 'vitest';
/**
* Dynamic ASCII art generator that can create art for any described object.
* This is a placeholder - replace with your actual implementation.
* @param {string} objectDescription - A string describing the object to draw.
* @returns {string} The ASCII art representation of the object.
*/
function generateAsciiArt(objectDescription) {
// This is a mock implementation - replace with your actual function
// For testing purposes, this simulates a dynamic generator
throw new Error('Replace this with your actual ASCII art generation function');
}
// --- Vitest Tests ---
describe('generateAsciiArt', () => {
// Helper function to validate ASCII art characteristics
const validateAsciiArt = (art, description) => {
// Basic structure validation
expect(typeof art).toBe('string');
expect(art.length).toBeGreaterThan(5);
expect(art.trim().length).toBeGreaterThan(0);
// Should be multi-line (real ASCII art spans multiple lines)
expect(art).toContain('\n');
const lines = art.split('\n').filter(line => line.trim().length > 0);
expect(lines.length).toBeGreaterThan(1);
// Should contain ASCII art characters (not just letters/numbers)
const hasAsciiArtChars = /[_|\/\\()[\]{}<>^*~.:,;!@#$%^&+\-=`'"]/;
expect(art).toMatch(hasAsciiArtChars);
// Should have some visual structure (varying line lengths suggest shape)
const lineLengths = lines.map(line => line.length);
const hasVariedStructure = new Set(lineLengths).size > 1;
expect(hasVariedStructure).toBe(true);
};
// Helper to check if art seems relevant to the description
const validateRelevance = (art, description) => {
const desc = description.toLowerCase();
const artLower = art.toLowerCase();
// For objects with distinctive features, check for related patterns
if (desc.includes('cat') || desc.includes('kitten')) {
// Cats often have ears (^, /\), eyes (o, .), whiskers
const catFeatures = /[\^\/\\]|[o.]{2}|[()]/;
expect(art).toMatch(catFeatures);
}
if (desc.includes('house') || desc.includes('building')) {
// Houses often have roofs (^, /\), walls (|), doors/windows ([])
const houseFeatures = /[\^\/\\]|[\[\]|_]/;
expect(art).toMatch(houseFeatures);
}
if (desc.includes('car') || desc.includes('vehicle')) {
// Cars often have wheels (O, o), body (-, _), windows
const carFeatures = /[Oo].*[Oo]|[-_]{3,}|[\[\]]/;
expect(art).toMatch(carFeatures);
}
if (desc.includes('tree') || desc.includes('plant')) {
// Trees often have triangular tops, trunks (|)
const treeFeatures = /[\^\/\\]|[|]{1,}/;
expect(art).toMatch(treeFeatures);
}
if (desc.includes('face') || desc.includes('person')) {
// Faces have eyes, mouth, possibly nose
const faceFeatures = /[o.]{2}|[()]/;
expect(art).toMatch(faceFeatures);
}
};
it('should generate ASCII art for common objects', () => {
const testObjects = ['cat', 'dog', 'house', 'car', 'tree', 'flower', 'bird'];
testObjects.forEach(obj => {
const art = generateAsciiArt(obj);
validateAsciiArt(art, obj);
validateRelevance(art, obj);
});
});
it('should generate ASCII art for complex descriptions', () => {
const complexDescriptions = [
'a small house with a chimney',
'a cat sitting on a fence',
'a sports car with racing stripes',
'a tall pine tree',
'a smiling face with glasses'
];
complexDescriptions.forEach(desc => {
const art = generateAsciiArt(desc);
validateAsciiArt(art, desc);
validateRelevance(art, desc);
});
});
it('should generate ASCII art for abstract concepts', () => {
const abstractConcepts = ['happiness', 'music', 'love', 'storm', 'peace'];
abstractConcepts.forEach(concept => {
const art = generateAsciiArt(concept);
validateAsciiArt(art, concept);
// Abstract concepts should still produce valid ASCII art structure
expect(art).toMatch(/[^a-zA-Z0-9\s]/); // Should contain non-alphanumeric chars
});
});
it('should handle unusual or creative inputs', () => {
const unusualInputs = [
'a robot unicorn',
'flying spaghetti monster',
'quantum computer',
'dragon made of clouds',
'musical note dancing'
];
unusualInputs.forEach(input => {
const art = generateAsciiArt(input);
validateAsciiArt(art, input);
});
});
it('should produce different art for different inputs', () => {
const inputs = ['cat', 'dog', 'house', 'car'];
const artworks = inputs.map(input => generateAsciiArt(input));
// Each artwork should be different
for (let i = 0; i < artworks.length; i++) {
for (let j = i + 1; j < artworks.length; j++) {
expect(artworks[i]).not.toBe(artworks[j]);
}
}
});
it('should handle edge cases gracefully', () => {
const edgeCases = ['', ' ', 'a', 'xyz123', '!@#$%'];
edgeCases.forEach(edge => {
const art = generateAsciiArt(edge);
expect(typeof art).toBe('string');
expect(art.length).toBeGreaterThan(0);
// Even edge cases should produce some form of ASCII art
});
});
it('should be consistent for the same input', () => {
const testInput = 'elephant';
const art1 = generateAsciiArt(testInput);
const art2 = generateAsciiArt(testInput);
expect(art1).toBe(art2);
validateAsciiArt(art1, testInput);
});
it('should handle case sensitivity appropriately', () => {
const lowerCase = generateAsciiArt('cat');
const upperCase = generateAsciiArt('CAT');
const mixedCase = generateAsciiArt('CaT');
// Depending on your implementation, these might be the same or different
// Adjust this test based on your function's behavior
validateAsciiArt(lowerCase, 'cat');
validateAsciiArt(upperCase, 'CAT');
validateAsciiArt(mixedCase, 'CaT');
});
it('should generate reasonable sized output', () => {
const testInputs = ['mouse', 'elephant', 'skyscraper'];
testInputs.forEach(input => {
const art = generateAsciiArt(input);
const lines = art.split('\n');
// Should not be too small or unreasonably large
expect(lines.length).toBeGreaterThan(2);
expect(lines.length).toBeLessThan(50); // Adjust based on your needs
// Lines shouldn't be excessively long
lines.forEach(line => {
expect(line.length).toBeLessThan(200); // Adjust based on your needs
});
});
});
it('should handle multi-word descriptions', () => {
const multiWordInputs = [
'red sports car',
'old oak tree',
'happy smiling face',
'small wooden house'
];
multiWordInputs.forEach(input => {
const art = generateAsciiArt(input);
validateAsciiArt(art, input);
validateRelevance(art, input);
});
});
});
This test suite validates that your dynamic ASCII art generator:
- Produces valid ASCII art structure - multi-line strings with appropriate characters
- Creates relevant art - checks for patterns that match the described objects
- Handles various input types - simple objects, complex descriptions, abstract concepts
- Produces different outputs for different inputs
- Handles edge cases gracefully
- Maintains consistency for the same input
- Generates reasonable sized output
To use this with your actual function, simply replace the placeholder generateAsciiArt function with your real implementation. The tests are designed to work with any ASCII art generator that can handle arbitrary string inputs.