Path: blob/main/extensions/copilot/src/extension/renameSuggestions/test/common/namingConvention.spec.ts
13405 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { expect, suite, test } from 'vitest';6import { chunkUpIdentByConvention, enforceNamingConvention, guessNamingConvention, NamingConvention } from '../../common/namingConvention';78suite('guessNamingConvention', () => {9const testCases = [10{ input: 'camelCaseExample', expected: NamingConvention.CamelCase },11{ input: 'PascalCaseExample', expected: NamingConvention.PascalCase },12{ input: 'snake_case_example', expected: NamingConvention.SnakeCase },13{ input: 'SCREAMING_SNAKE_CASE_EXAMPLE', expected: NamingConvention.ScreamingSnakeCase },14{ input: 'Capital_snake_case', expected: NamingConvention.CapitalSnakeCase },15{ input: 'kebab-case-example', expected: NamingConvention.KebabCase },16{ input: 'Uppercase', expected: NamingConvention.Capitalized },17{ input: 'lowercase', expected: NamingConvention.LowerCase },18{ input: 'Unknown_Example', expected: NamingConvention.Unknown },19];2021testCases.forEach(({ input, expected }) => {22test(`should return ${expected} for input "${input}"`, () => {23expect(guessNamingConvention(input)).toBe(expected);24});25});2627// Additional tests for edge cases28const edgeCases = [29{ input: 'foo', expected: NamingConvention.LowerCase },30{ input: 'FOO', expected: NamingConvention.Uppercase },31{ input: 'Foo', expected: NamingConvention.Capitalized },32{ input: 'foo_bar', expected: NamingConvention.SnakeCase },33{ input: 'foo-bar', expected: NamingConvention.KebabCase },34{ input: 'FOO_BAR', expected: NamingConvention.ScreamingSnakeCase },35{ input: 'Foo_Bar', expected: NamingConvention.Unknown },36{ input: '', expected: NamingConvention.Unknown },37{ input: '123', expected: NamingConvention.Unknown },38{ input: 'foo123', expected: NamingConvention.LowerCase },39{ input: 'foo_123', expected: NamingConvention.SnakeCase },40{ input: 'foo-bar-123', expected: NamingConvention.KebabCase },41];4243edgeCases.forEach(({ input, expected }) => {44test(`should return ${expected} for edge case input "${input}"`, () => {45expect(guessNamingConvention(input)).toBe(expected);46});47});48});4950suite('chunkByNamingConvention', () => {51const testCases = [52{ input: 'camelCaseExample', convention: NamingConvention.CamelCase, expected: ['camel', 'Case', 'Example'] },53{ input: 'PascalCaseExample', convention: NamingConvention.PascalCase, expected: ['Pascal', 'Case', 'Example'] },54{ input: 'snake_case_example', convention: NamingConvention.SnakeCase, expected: ['snake', 'case', 'example'] },55{ input: 'SCREAMING_SNAKE_CASE_EXAMPLE', convention: NamingConvention.ScreamingSnakeCase, expected: ['screaming', 'snake', 'case', 'example'] },56{ input: 'Capital_snake_case', convention: NamingConvention.CapitalSnakeCase, expected: ['capital', 'snake', 'case'] },57{ input: 'kebab-case-example', convention: NamingConvention.KebabCase, expected: ['kebab', 'case', 'example'] },58{ input: 'Uppercase', convention: NamingConvention.Uppercase, expected: ['Uppercase'] },59{ input: 'lowercase', convention: NamingConvention.LowerCase, expected: ['lowercase'] },60{ input: 'Unknown_Example', convention: NamingConvention.Unknown, expected: ['Unknown_Example'] },61];6263testCases.forEach(({ input, convention, expected }) => {64test(`should return ${expected} for input "${input}" with convention "${convention}"`, () => {65expect(chunkUpIdentByConvention(input, convention)).toEqual(expected);66});67});68});6970suite('enforceNamingConvention', () => {71const testCases = [72{ givenIdent: 'snake_case_example', targetConvention: NamingConvention.CamelCase, expected: 'snakeCaseExample' },73{ givenIdent: 'camelCaseExample', targetConvention: NamingConvention.SnakeCase, expected: 'camel_case_example' },74{ givenIdent: 'PascalCaseExample', targetConvention: NamingConvention.SnakeCase, expected: 'pascal_case_example' },75{ givenIdent: 'camelCaseExample', targetConvention: NamingConvention.PascalCase, expected: 'CamelCaseExample' },76{ givenIdent: 'snake_case_example', targetConvention: NamingConvention.ScreamingSnakeCase, expected: 'SNAKE_CASE_EXAMPLE' },77{ givenIdent: 'SCREAMING_SNAKE_CASE_EXAMPLE', targetConvention: NamingConvention.CapitalSnakeCase, expected: 'Screaming_snake_case_example' },78{ givenIdent: 'Capital_snake_case', targetConvention: NamingConvention.KebabCase, expected: 'capital-snake-case' },79{ givenIdent: 'kebab-case-example', targetConvention: NamingConvention.Uppercase, expected: 'KEBAB-CASE-EXAMPLE' },80{ givenIdent: 'Uppercase', targetConvention: NamingConvention.LowerCase, expected: 'uppercase' },81{ givenIdent: 'lowercase', targetConvention: NamingConvention.Unknown, expected: 'lowercase' },82{ givenIdent: 'Unknown_Example', targetConvention: NamingConvention.CamelCase, expected: 'unknown_example' /* TODO@ulugbekna: improve unknown convention handling */ },83];8485testCases.forEach(({ givenIdent, targetConvention, expected }) => {86test(`should enforce ${targetConvention} convention for "${givenIdent}"`, () => {87expect(enforceNamingConvention(givenIdent, targetConvention)).toBe(expected);88});89});90});919293