microsoft/vscode-languagedetection

Public

mirrored fromhttps://github.com/microsoft/vscode-languagedetectionAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
4b130f80ed16fb3ccb072fc6bb7a9b2b26ef7a9b

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

test/index.test.ts

221lines · modecode

1import { expect } from "chai";
2import { readFileSync } from "fs";
3import path from "path";
4import { ModelOperations, ModelResult } from "../lib";
5
6const expectedRelativeConfidence = 0.2;
7
8async function arrayify(generator: Generator<string, any, unknown>) {
9 const langs: string[] = [];
10 for await (const lang of generator) {
11 langs.push(lang);
12 }
13 return langs;
14}
15
16// Similar to the heuristic used in VS Code:
17function * runVSCodeHeuristic(modelResults: ModelResult[]) {
18 if (!modelResults) {
19 return;
20 }
21
22 if (modelResults[0].confidence < expectedRelativeConfidence) {
23 return;
24 }
25
26 const possibleLanguages: ModelResult[] = [modelResults[0]];
27
28 for (let current of modelResults) {
29
30 if (current === modelResults[0]) {
31 continue;
32 }
33
34 const currentHighest = possibleLanguages[possibleLanguages.length - 1];
35
36 if (currentHighest.confidence - current.confidence >= expectedRelativeConfidence) {
37 while (possibleLanguages.length) {
38 yield possibleLanguages.shift()!.languageId;
39 }
40 if (current.confidence > expectedRelativeConfidence) {
41 possibleLanguages.push(current);
42 continue;
43 }
44 return;
45 } else {
46 if (current.confidence > expectedRelativeConfidence) {
47 possibleLanguages.push(current);
48 continue;
49 }
50
51 // Handle languages that are close enough to each other
52 if ((currentHighest.languageId === 'ts' && current.languageId === 'js')
53 || (currentHighest.languageId === 'js' && current.languageId === 'ts')
54 || (currentHighest.languageId === 'c' && current.languageId === 'cpp')
55 || (currentHighest.languageId === 'cpp' && current.languageId === 'c')) {
56 continue;
57 }
58
59 return;
60 }
61 }
62}
63
64describe('describe', () => {
65 const modulOperations = new ModelOperations();
66
67 it('test TypeScript', async () => {
68 const result = await modulOperations.runModel(`
69import { ValidationPipe } from '@nestjs/common';
70import { NestFactory } from '@nestjs/core';
71import { AppModule } from './app.module';
72
73async function bootstrap() {
74 const app = await NestFactory.create(AppModule);
75 app.useGlobalPipes(new ValidationPipe());
76
77 await app.listen(3000);
78 console.log(\`Application is running on: \${await app.getUrl()}\`);
79}
80bootstrap();
81`);
82
83 expect(result[0].languageId).to.equal('ts');
84 expect(result[0].confidence).to.greaterThan(expectedRelativeConfidence);
85
86 const langs: string[] = await arrayify(runVSCodeHeuristic(result));
87 expect(langs.length).to.equal(1);
88 expect(langs[0]).to.equal('ts');
89 });
90
91 it('test Python', async () => {
92 const result = await modulOperations.runModel(`
93 # Python program to check if the input number is odd or even.
94 # A number is even if division by 2 gives a remainder of 0.
95 # If the remainder is 1, it is an odd number.
96
97 num = int(input("Enter a number: "))
98 if (num % 2) == 0:
99 print("{0} is Even".format(num))
100 else:
101 print("{0} is Odd".format(num))
102`);
103 expect(result[0].languageId).to.equal('py');
104 expect(result[0].confidence).to.greaterThan(expectedRelativeConfidence);
105 const langs: string[] = await arrayify(runVSCodeHeuristic(result));
106 expect(langs.length).to.equal(1);
107 expect(langs[0]).to.equal('py');
108 });
109
110 it('test Java', async () => {
111 const result = await modulOperations.runModel(`
112public class Main {
113
114 public static void main(String[] args) {
115 int num = 29;
116 boolean flag = false;
117 for (int i = 2; i <= num / 2; ++i) {
118 // condition for nonprime number
119 if (num % i == 0) {
120 flag = true;
121 break;
122 }
123 }
124
125 if (!flag)
126 System.out.println(num + " is a prime number.");
127 else
128 System.out.println(num + " is not a prime number.");
129 }
130}
131`);
132 expect(result[0].languageId).to.equal('java');
133 expect(result[0].confidence).to.greaterThan(expectedRelativeConfidence);
134 const langs: string[] = await arrayify(runVSCodeHeuristic(result));
135 expect(langs.length).to.equal(1);
136 expect(langs[0]).to.equal('java');
137 });
138
139 it('test PowerShell', async () => {
140 const result = await modulOperations.runModel(`
141$msedgedriverPath = 'C:\\Users\\Tyler\\Downloads\\edgedriver_win64\\msedgedriver.exe'
142
143$Driver = Start-SeNewEdge -BinaryPath $msedgedriverPath -StartURL https://seattle.signetic.com/home
144
145Start-Sleep -Seconds 5
146
147while($true) {
148 $element = Find-SeElement -Driver $Driver -ClassName text-demibold
149
150 if (!$element -or $element.Text -ne 'No appointments available') {
151 # notify
152 New-BurntToastNotification -Text 'Check website'
153 break
154 } else {
155 Write-Host 'no appointments available'
156 }
157
158 Enter-SeUrl https://seattle.signetic.com/home -Driver $Driver
159 Start-Sleep -Seconds 2
160}
161`);
162 expect(result[0].languageId).to.equal('ps1');
163 expect(result[0].confidence).to.greaterThan(expectedRelativeConfidence);
164 const langs: string[] = await arrayify(runVSCodeHeuristic(result));
165 expect(langs.length).to.equal(1);
166 expect(langs[0]).to.equal('ps1');
167 });
168
169 it('test large file', async () => {
170 const result = await modulOperations.runModel(readFileSync(path.resolve(__dirname, '..', '..', 'test', 'large.ts.txt')).toString());
171
172 expect(result[0].languageId).to.equal('ts');
173 expect(result[0].confidence).to.greaterThan(expectedRelativeConfidence);
174
175 const langs: string[] = await arrayify(runVSCodeHeuristic(result));
176 expect(langs.length).to.equal(1);
177 expect(langs[0]).to.equal('ts');
178 });
179
180 it('test Visual Basic', async () => {
181 const result = await modulOperations.runModel(`
182Module Module1
183
184Sub Main()
185 Dim id As Integer
186 Dim name As String = "Suresh Dasari"
187 Dim percentage As Double = 10.23
188 Dim gender As Char = "M"c
189 Dim isVerified As Boolean
190
191 id = 10
192 isVerified = True
193 Console.WriteLine("Id:{0}", id)
194 Console.WriteLine("Name:{0}", name)
195 Console.WriteLine("Percentage:{0}", percentage)
196 Console.WriteLine("Gender:{0}", gender)
197 Console.WriteLine("Verfied:{0}", isVerified)
198 Console.ReadLine()
199End Sub
200
201End Module
202`);
203
204 expect(result[0].languageId).to.equal('vba');
205 expect(result[0].confidence).to.greaterThan(expectedRelativeConfidence);
206
207 for await (const lang of runVSCodeHeuristic(result)) {
208 expect(lang).to.equal('vba');
209 }
210 });
211
212 it('test results with carriage returns', async () => {
213 const result = await modulOperations.runModel(`public class Reverse {\r\n\r\n public static void main(String[] args) {\r\n String sentence = "Go work";\r\n String reversed = reverse(sentence);\r\n System.out.println("The reversed sentence is: " + reversed);\r\n }\r\n\r\n public static String reverse(String sentence) {\r\n if (sentence.isEmpty())\r\n return sentence;\r\n\r\n return reverse(sentence.substring(1)) + sentence.charAt(0);\r\n }\r\n}`);
214 expect(result[0].languageId).to.equal('java');
215 expect(result[0].confidence).to.greaterThan(expectedRelativeConfidence);
216 const langs: string[] = await arrayify(runVSCodeHeuristic(result));
217 expect(langs.length).to.equal(1);
218 expect(langs[0]).to.equal('java');
219 });
220
221});
222