apisurf
Version:
Analyze API surface changes between npm package versions to catch breaking changes
178 lines (177 loc) • 6.27 kB
JavaScript
import { describe, expect, it } from '@jest/globals';
import { parseEnumDefinition } from './parseEnumDefinition.js';
describe('parseEnumDefinition', () => {
it('should parse a simple enum', () => {
const lines = [
'export enum Status {',
' PENDING = "pending",',
' APPROVED = "approved",',
' REJECTED = "rejected"',
'}'
];
const result = parseEnumDefinition(lines, 0, 'Status');
expect(result).toEqual({
name: 'Status',
kind: 'enum',
members: ['PENDING', 'APPROVED', 'REJECTED'],
signature: 'export enum Status { PENDING, APPROVED, REJECTED }'
});
});
it('should parse an enum with numeric values', () => {
const lines = [
'export enum Priority {',
' LOW = 0,',
' MEDIUM = 1,',
' HIGH = 2',
'}'
];
const result = parseEnumDefinition(lines, 0, 'Priority');
expect(result).toEqual({
name: 'Priority',
kind: 'enum',
members: ['LOW', 'MEDIUM', 'HIGH'],
signature: 'export enum Priority { LOW, MEDIUM, HIGH }'
});
});
it('should parse an enum with mixed values', () => {
const lines = [
'export enum Mixed {',
' FIRST = 1,',
' SECOND = "two",',
' THIRD = 3',
'}'
];
const result = parseEnumDefinition(lines, 0, 'Mixed');
expect(result).toEqual({
name: 'Mixed',
kind: 'enum',
members: ['FIRST', 'SECOND', 'THIRD'],
signature: 'export enum Mixed { FIRST, SECOND, THIRD }'
});
});
it('should handle multi-line enum members', () => {
const lines = [
'export enum LongEnum {',
' VERY_LONG_MEMBER_NAME_ONE = ',
' "very-long-value-one",',
' VERY_LONG_MEMBER_NAME_TWO = ',
' "very-long-value-two"',
'}'
];
const result = parseEnumDefinition(lines, 0, 'LongEnum');
expect(result).toEqual({
name: 'LongEnum',
kind: 'enum',
members: ['VERY_LONG_MEMBER_NAME_ONE', 'VERY_LONG_MEMBER_NAME_TWO'],
signature: 'export enum LongEnum { VERY_LONG_MEMBER_NAME_ONE, VERY_LONG_MEMBER_NAME_TWO }'
});
});
it('should handle empty enum', () => {
const lines = [
'export enum Empty {',
'}'
];
const result = parseEnumDefinition(lines, 0, 'Empty');
expect(result).toEqual({
name: 'Empty',
kind: 'enum',
members: [],
signature: 'export enum Empty { }'
});
});
it('should handle nested braces in enum values', () => {
const lines = [
'export enum WithBraces {',
' VALUE1 = "{}",',
' VALUE2 = "{nested}"',
'}'
];
const result = parseEnumDefinition(lines, 0, 'WithBraces');
expect(result).toEqual({
name: 'WithBraces',
kind: 'enum',
members: ['VALUE1', 'VALUE2'],
signature: 'export enum WithBraces { VALUE1, VALUE2 }'
});
});
it('should start parsing from the specified index', () => {
const lines = [
'some other code',
'more code',
'export enum Status {',
' ACTIVE = "active",',
' INACTIVE = "inactive"',
'}'
];
const result = parseEnumDefinition(lines, 2, 'Status');
expect(result).toEqual({
name: 'Status',
kind: 'enum',
members: ['ACTIVE', 'INACTIVE'],
signature: 'export enum Status { ACTIVE, INACTIVE }'
});
});
it('should handle enums with single-line comments', () => {
const lines = [
'export enum DocumentState {',
' // Initial state when document is created',
' DRAFT = "draft",',
' // Document is being reviewed',
' REVIEW = "review",',
' // Document has been published',
' PUBLISHED = "published", // This is the final state',
'}'
];
const result = parseEnumDefinition(lines, 0, 'DocumentState');
expect(result).toEqual({
name: 'DocumentState',
kind: 'enum',
members: ['DRAFT', 'REVIEW', 'PUBLISHED'],
signature: 'export enum DocumentState { DRAFT, REVIEW, PUBLISHED }'
});
});
it('should handle enums with block comments', () => {
const lines = [
'export enum ErrorCode {',
' /* Network related errors */',
' NETWORK_ERROR = 500,',
' /*',
' * Authentication errors',
' * These occur when user credentials are invalid',
' */',
' AUTH_ERROR = 401,',
' /**',
' * Permission denied',
' */',
' FORBIDDEN = 403',
'}'
];
const result = parseEnumDefinition(lines, 0, 'ErrorCode');
expect(result).toEqual({
name: 'ErrorCode',
kind: 'enum',
members: ['NETWORK_ERROR', 'AUTH_ERROR', 'FORBIDDEN'],
signature: 'export enum ErrorCode { NETWORK_ERROR, AUTH_ERROR, FORBIDDEN }'
});
});
it('should handle enums with mixed comment styles', () => {
const lines = [
'export enum LogLevel {',
' // Debugging information',
' DEBUG = 0, // Lowest priority',
' /* Informational messages */',
' INFO = 1,',
' /** Warning messages */',
' WARN = 2, /* Medium priority */',
' ERROR = 3 // Highest priority',
'}'
];
const result = parseEnumDefinition(lines, 0, 'LogLevel');
expect(result).toEqual({
name: 'LogLevel',
kind: 'enum',
members: ['DEBUG', 'INFO', 'WARN', 'ERROR'],
signature: 'export enum LogLevel { DEBUG, INFO, WARN, ERROR }'
});
});
});