import { test } from 'node:test'; import assert from 'node:assert/strict'; import { formatCode, formatXml } from '../app/static/js/tools/code-format.js'; import { tokenize, toLines, toHtml, toRtf, detect } from '../app/static/js/tools/highlight-core.js'; import { THEMES } from '../app/static/js/tools/highlight-themes.js'; const squash = (s) => s.replace(/\s+/g, ''); const SAMPLES = { kotlin: `package demo class Greeter(val name:String) : Base(){ // say hi fun greet(times:Int=1):String { val s="hi, \${name} x" if(times>1) return s.repeat(times) val list=listOf(1,2,3).map{it*2} .filter { it>2 } when(times){ 1->println("one") else->{ println("many") } } return s } }`, java: `public class A{ @Override public int f( int a,int b ){ int x=a+b ; for(int i=0;i<10;i++){ x+=i; } switch(x){ case 1: x=2; break; default: x=3; } /** * Doc comment * second line */ if(x==3){return -1;} else{return x;} } }`, csharp: `public class Foo { public int Bar(int x) { if (x > 0) { return x; } else { return -x; } } }`, go: `package main func main(){ x:=map[string]int{"a":1} switch x["a"]{ case 1: fmt.Println("one") } }`, c: `#include int main(int argc,char **argv){ int *p=&argc; if(argc>1){printf("%d\\n",*p);} return 0; }`, python: `import os def f(a,b = 2,*args,c:int=3,**kw): x=a+b # comment d={'k' : 1,'j':[1,2]} if x>1 : return d[ 'k' ][1:-1] y = f(a , b=1) return -x `, rust: `fn main(){ let v=vec![1,2,3]; let s:i32=v.iter().map(|x| x*2).sum(); for i in 0..=5 { println!("{}",i); } }`, }; for (const [lang, src] of Object.entries(SAMPLES)) { test(`${lang}: only whitespace changes, and formatting is stable`, async () => { const once = await formatCode(src, lang); assert.equal(squash(once), squash(src), `${lang} changed non-whitespace:\n${once}`); const twice = await formatCode(once, lang); assert.equal(twice, once, `${lang} not idempotent:\n--- once\n${once}\n--- twice\n${twice}`); }); } test('kotlin output', async () => { const out = await formatCode(SAMPLES.kotlin, 'kotlin'); assert.equal(out, `package demo class Greeter(val name: String) : Base() { // say hi fun greet(times: Int = 1): String { val s = "hi, \${name} x" if (times > 1) return s.repeat(times) val list = listOf(1, 2, 3).map { it * 2 } .filter { it > 2 } when (times) { 1 -> println("one") else -> { println("many") } } return s } } `); }); test('java output', async () => { const out = await formatCode(SAMPLES.java, 'java'); assert.equal(out, `public class A { @Override public int f(int a, int b) { int x = a + b; for (int i = 0; i < 10; i++) { x += i; } switch (x) { case 1: x = 2; break; default: x = 3; } /** * Doc comment * second line */ if (x == 3) {return -1;} else {return x;} } } `); }); test('c# uses Allman braces', async () => { const out = await formatCode(SAMPLES.csharp, 'csharp'); assert.equal(out, `public class Foo { public int Bar(int x) { if (x > 0) { return x; } else { return -x; } } } `); }); test('go: tabs, cases at switch level', async () => { const out = await formatCode(SAMPLES.go, 'go'); assert.equal(out, 'package main\nfunc main() {\n\tx := map[string]int{"a": 1}\n\tswitch x["a"] {\n\tcase 1:\n\t\tfmt.Println("one")\n\t}\n}\n'); }); test('c: pointers untouched, preprocessor at column 0', async () => { const out = await formatCode(SAMPLES.c, 'c'); assert.equal(out, '#include \nint main(int argc, char **argv) {\n int *p = &argc;\n if (argc > 1) {printf("%d\\n", *p);}\n return 0;\n}\n'); }); test('python output', async () => { const out = await formatCode(SAMPLES.python, 'python'); assert.equal(out, `import os def f(a, b=2, *args, c: int = 3, **kw): x = a + b # comment d = {'k': 1, 'j': [1, 2]} if x > 1: return d['k'][1:-1] y = f(a, b=1) return -x `); }); test('xml: Android Studio attribute wrapping', () => { const out = formatXml('text'); assert.equal(out, ` text `); }); test('json and sql and prettier', async () => { assert.equal(await formatCode('{"a":[1,2]}', 'json'), '{\n "a": [\n 1,\n 2\n ]\n}\n'); assert.match(await formatCode('select a,b from t where x=1', 'sql'), /^select\n a,\n b\nfrom\n t\nwhere\n x = 1\n$/); assert.equal(await formatCode('const a={b:1}', 'javascript'), 'const a = { b: 1 };\n'); assert.equal(await formatCode('let x:number=1', 'typescript'), 'let x: number = 1;\n'); assert.equal(await formatCode('a{color:red}', 'css'), 'a {\n color: red;\n}\n'); await assert.rejects(formatCode('const = ;', 'javascript')); }); test('tokens, html and rtf', () => { const lines = toLines(tokenize('val x = "a" // c\nfun f() {}', 'kotlin')); assert.equal(lines.length, 2); const theme = THEMES.find((t) => t.id === 'darcula'); const html = toHtml(lines, theme, { lineNumbers: true }); assert.match(html, /val<\/span>/); assert.match(html, /color:#6A8759;">"a"|color:#6A8759;">"a"/); assert.match(html, /color:#606366;">1 t.id === 'vscode-dark'); const h2 = toHtml(toLines(tokenize('if (a) return 1; const b = 2;', 'javascript')), vs); assert.match(h2, /#C586C0;">ifconst