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