Skip to content

Commit 845ce1e

Browse files
committed
add Unit test
1 parent 37f6939 commit 845ce1e

2 files changed

Lines changed: 291 additions & 0 deletions

File tree

Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
import {charToSeparatorType, SeparatorType, separatorTypeToString} from "../state/SeparatorType";
2+
3+
describe('SeparatorType Enum', () => {
4+
describe('Enum Values', () => {
5+
it('should have correct string values for each separator type', () => {
6+
expect(SeparatorType.COMMA).toBe(',');
7+
expect(SeparatorType.SEMICOLON).toBe(';');
8+
expect(SeparatorType.SPACE).toBe(' ');
9+
expect(SeparatorType.PIPE).toBe('|');
10+
});
11+
12+
it('should have exactly 4 separator types', () => {
13+
const separatorCount = Object.keys(SeparatorType).length;
14+
expect(separatorCount).toBe(4);
15+
});
16+
17+
it('should contain all expected separator types', () => {
18+
const expectedTypes = ['COMMA', 'SEMICOLON', 'SPACE', 'PIPE'];
19+
const actualTypes = Object.keys(SeparatorType);
20+
expect(actualTypes).toEqual(expect.arrayContaining(expectedTypes));
21+
});
22+
});
23+
});
24+
25+
describe('separatorTypeToString', () => {
26+
describe('Valid Separator Types', () => {
27+
it('should convert COMMA to "Comma"', () => {
28+
const result = separatorTypeToString(SeparatorType.COMMA);
29+
expect(result).toBe('Comma');
30+
});
31+
32+
it('should convert SEMICOLON to "Semicolon"', () => {
33+
const result = separatorTypeToString(SeparatorType.SEMICOLON);
34+
expect(result).toBe('Semicolon');
35+
});
36+
37+
it('should convert SPACE to "Space"', () => {
38+
const result = separatorTypeToString(SeparatorType.SPACE);
39+
expect(result).toBe('Space');
40+
});
41+
42+
it('should convert PIPE to "Pipe"', () => {
43+
const result = separatorTypeToString(SeparatorType.PIPE);
44+
expect(result).toBe('Pipe');
45+
});
46+
});
47+
48+
describe('Invalid Separator Types', () => {
49+
it('should return "Unknown" for invalid separator type', () => {
50+
// Force TypeScript to accept an invalid value for testing
51+
const invalidSeparator = 'invalid' as SeparatorType;
52+
const result = separatorTypeToString(invalidSeparator);
53+
expect(result).toBe('Unknown');
54+
});
55+
56+
it('should return "Unknown" for null value', () => {
57+
const result = separatorTypeToString(null as any);
58+
expect(result).toBe('Unknown');
59+
});
60+
61+
it('should return "Unknown" for undefined value', () => {
62+
const result = separatorTypeToString(undefined as any);
63+
expect(result).toBe('Unknown');
64+
});
65+
});
66+
67+
describe('Type Safety', () => {
68+
it('should return string type', () => {
69+
const result = separatorTypeToString(SeparatorType.COMMA);
70+
expect(typeof result).toBe('string');
71+
});
72+
73+
it('should handle all enum values without compilation errors', () => {
74+
// This test ensures all enum values are handled in the switch statement
75+
Object.values(SeparatorType).forEach(separator => {
76+
expect(() => separatorTypeToString(separator)).not.toThrow();
77+
});
78+
});
79+
});
80+
});
81+
82+
describe('charToSeparatorType', () => {
83+
describe('Valid Characters', () => {
84+
it('should convert "," to SeparatorType.COMMA', () => {
85+
const result = charToSeparatorType(',');
86+
expect(result).toBe(SeparatorType.COMMA);
87+
});
88+
89+
it('should convert ";" to SeparatorType.SEMICOLON', () => {
90+
const result = charToSeparatorType(';');
91+
expect(result).toBe(SeparatorType.SEMICOLON);
92+
});
93+
94+
it('should convert " " to SeparatorType.SPACE', () => {
95+
const result = charToSeparatorType(' ');
96+
expect(result).toBe(SeparatorType.SPACE);
97+
});
98+
99+
it('should convert "|" to SeparatorType.PIPE', () => {
100+
const result = charToSeparatorType('|');
101+
expect(result).toBe(SeparatorType.PIPE);
102+
});
103+
});
104+
105+
describe('Invalid Characters', () => {
106+
it('should throw error for unsupported character "."', () => {
107+
expect(() => charToSeparatorType('.')).toThrow('Unsupported separator: .');
108+
});
109+
110+
it('should throw error for unsupported character "/"', () => {
111+
expect(() => charToSeparatorType('/')).toThrow('Unsupported separator: /');
112+
});
113+
114+
it('should throw error for unsupported character "\t"', () => {
115+
expect(() => charToSeparatorType('\t')).toThrow('Unsupported separator: \t');
116+
});
117+
118+
it('should throw error for empty string', () => {
119+
expect(() => charToSeparatorType('')).toThrow('Unsupported separator: ');
120+
});
121+
122+
it('should throw error for multi-character string', () => {
123+
expect(() => charToSeparatorType(',;')).toThrow('Unsupported separator: ,;');
124+
});
125+
126+
it('should throw error for null value', () => {
127+
expect(() => charToSeparatorType(null as any)).toThrow('Unsupported separator: null');
128+
});
129+
130+
it('should throw error for undefined value', () => {
131+
expect(() => charToSeparatorType(undefined as any)).toThrow('Unsupported separator: undefined');
132+
});
133+
134+
it('should throw error for numeric input', () => {
135+
expect(() => charToSeparatorType('1')).toThrow('Unsupported separator: 1');
136+
});
137+
138+
it('should throw error for alphabetic character', () => {
139+
expect(() => charToSeparatorType('a')).toThrow('Unsupported separator: a');
140+
});
141+
});
142+
143+
describe('Error Messages', () => {
144+
it('should include the invalid character in error message', () => {
145+
const invalidChar = '#';
146+
expect(() => charToSeparatorType(invalidChar))
147+
.toThrow(`Unsupported separator: ${invalidChar}`);
148+
});
149+
150+
it('should handle special characters in error message', () => {
151+
const specialChars = ['\n', '\r', '\t', '\\'];
152+
specialChars.forEach(char => {
153+
expect(() => charToSeparatorType(char))
154+
.toThrow(`Unsupported separator: ${char}`);
155+
});
156+
});
157+
});
158+
159+
describe('Type Safety', () => {
160+
it('should return SeparatorType enum value', () => {
161+
const result = charToSeparatorType(',');
162+
expect(Object.values(SeparatorType)).toContain(result);
163+
});
164+
165+
it('should handle all valid separator characters', () => {
166+
const validChars = [',', ';', ' ', '|'];
167+
validChars.forEach(char => {
168+
expect(() => charToSeparatorType(char)).not.toThrow();
169+
expect(Object.values(SeparatorType)).toContain(charToSeparatorType(char));
170+
});
171+
});
172+
});
173+
});
174+
175+
describe('Round-trip Conversion Tests', () => {
176+
it('should maintain consistency between enum and string conversion', () => {
177+
// Test that converting enum to string and back to enum works correctly
178+
Object.values(SeparatorType).forEach(separator => {
179+
const stringRepresentation = separatorTypeToString(separator);
180+
expect(stringRepresentation).not.toBe('Unknown');
181+
182+
// Convert back using the actual separator character
183+
const backToEnum = charToSeparatorType(separator);
184+
expect(backToEnum).toBe(separator);
185+
});
186+
});
187+
188+
it('should handle all separator characters bidirectionally', () => {
189+
const testCases = [
190+
{char: ',', enum: SeparatorType.COMMA, name: 'Comma'},
191+
{char: ';', enum: SeparatorType.SEMICOLON, name: 'Semicolon'},
192+
{char: ' ', enum: SeparatorType.SPACE, name: 'Space'},
193+
{char: '|', enum: SeparatorType.PIPE, name: 'Pipe'}
194+
];
195+
196+
testCases.forEach(({char, enum: separatorEnum, name}) => {
197+
// Character to enum
198+
expect(charToSeparatorType(char)).toBe(separatorEnum);
199+
200+
// Enum to string name
201+
expect(separatorTypeToString(separatorEnum)).toBe(name);
202+
203+
// Enum value should match character
204+
expect(separatorEnum).toBe(char);
205+
});
206+
});
207+
});
208+
209+
describe('Edge Cases and Performance', () => {
210+
it('should handle repeated calls efficiently', () => {
211+
// Performance test - should not degrade with repeated calls
212+
const iterations = 1000;
213+
const startTime = performance.now();
214+
215+
for (let i = 0; i < iterations; i++) {
216+
separatorTypeToString(SeparatorType.COMMA);
217+
charToSeparatorType(',');
218+
}
219+
220+
const endTime = performance.now();
221+
const executionTime = endTime - startTime;
222+
223+
// Should complete within reasonable time (adjust threshold as needed)
224+
expect(executionTime).toBeLessThan(100); // 100ms for 1000 iterations
225+
});
226+
227+
it('should be case-sensitive for character conversion', () => {
228+
// Uppercase versions should throw errors
229+
expect(() => charToSeparatorType('A')).toThrow();
230+
expect(() => charToSeparatorType('COMMA')).toThrow();
231+
});
232+
233+
it('should handle Unicode characters appropriately', () => {
234+
const unicodeChars = [',', ';', '|', ' ']; // Similar Unicode characters
235+
unicodeChars.forEach(char => {
236+
expect(() => charToSeparatorType(char)).toThrow();
237+
});
238+
});
239+
});

src/state/SeparatorType.tsx

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* Enum representing different types of separators used in data processing.
3+
* Each enum value corresponds to a specific character used to separate values.
4+
*/
5+
export enum SeparatorType {
6+
COMMA = ',',
7+
SEMICOLON = ';',
8+
SPACE = ' ',
9+
PIPE = '|'
10+
}
11+
12+
13+
/**
14+
* Converts a SeparatorType enum value to its string representation.
15+
* @param separator - The SeparatorType enum value to convert.
16+
* @returns The string representation of the separator type.
17+
*/
18+
export function separatorTypeToString(separator: SeparatorType): string {
19+
switch (separator) {
20+
case SeparatorType.COMMA:
21+
return 'Comma';
22+
case SeparatorType.SEMICOLON:
23+
return 'Semicolon';
24+
case SeparatorType.SPACE:
25+
return 'Space';
26+
case SeparatorType.PIPE:
27+
return 'Pipe';
28+
default:
29+
return 'Unknown';
30+
}
31+
}
32+
33+
/**
34+
* Converts a character to its corresponding SeparatorType enum value.
35+
* @param separator - The character representing the separator.
36+
* @returns The corresponding SeparatorType enum value.
37+
* @throws Error if the character is not a valid separator.
38+
*/
39+
export function charToSeparatorType(separator: string): SeparatorType {
40+
switch (separator) {
41+
case ',':
42+
return SeparatorType.COMMA;
43+
case ';':
44+
return SeparatorType.SEMICOLON;
45+
case ' ':
46+
return SeparatorType.SPACE;
47+
case '|':
48+
return SeparatorType.PIPE;
49+
default:
50+
throw new Error(`Unsupported separator: ${separator}`);
51+
}
52+
}

0 commit comments

Comments
 (0)