| 1 | import { detectLanguage } from "./SyntaxHighlighter.js"; |
| 2 | |
| 3 | describe("detectLanguage", () => { |
| 4 | it("should detect JavaScript from import statements", () => { |
| 5 | const code = `import React from 'react';`; |
| 6 | expect(detectLanguage(code)).toBe("javascript"); |
| 7 | }); |
| 8 | |
| 9 | it("should detect JavaScript from require statements", () => { |
| 10 | const code = `const fs = require('fs');`; |
| 11 | expect(detectLanguage(code)).toBe("javascript"); |
| 12 | }); |
| 13 | |
| 14 | it("should detect JavaScript from function declarations", () => { |
| 15 | const code = `function myFunction() { return true; }`; |
| 16 | expect(detectLanguage(code)).toBe("javascript"); |
| 17 | }); |
| 18 | |
| 19 | it("should detect TypeScript from interface declarations", () => { |
| 20 | const code = `interface User { name: string; }`; |
| 21 | expect(detectLanguage(code)).toBe("typescript"); |
| 22 | }); |
| 23 | |
| 24 | it("should detect TypeScript from type declarations", () => { |
| 25 | const code = `type UserType = { name: string; };`; |
| 26 | expect(detectLanguage(code)).toBe("typescript"); |
| 27 | }); |
| 28 | |
| 29 | it("should detect Python from function definitions", () => { |
| 30 | const code = `def my_function(): |
| 31 | return True`; |
| 32 | expect(detectLanguage(code)).toBe("python"); |
| 33 | }); |
| 34 | |
| 35 | it("should detect Python from class definitions", () => { |
| 36 | const code = `class MyClass: |
| 37 | def __init__(self): |
| 38 | pass`; |
| 39 | expect(detectLanguage(code)).toBe("python"); |
| 40 | }); |
| 41 | |
| 42 | it("should detect Java from public class declarations", () => { |
| 43 | const code = `public class MyClass { |
| 44 | public static void main(String[] args) { |
| 45 | } |
| 46 | }`; |
| 47 | expect(detectLanguage(code)).toBe("java"); |
| 48 | }); |
| 49 | |
| 50 | it("should detect C from include statements", () => { |
| 51 | const code = `#include <stdio.h> |
| 52 | int main() { |
| 53 | return 0; |
| 54 | }`; |
| 55 | expect(detectLanguage(code)).toBe("c"); |
| 56 | }); |
| 57 | |
| 58 | it("should detect C++ from using namespace", () => { |
| 59 | const code = `using namespace std; |
| 60 | int main() { |
| 61 | return 0; |
| 62 | }`; |
| 63 | expect(detectLanguage(code)).toBe("cpp"); |
| 64 | }); |
| 65 | |
| 66 | it("should detect C# from using System", () => { |
| 67 | const code = `using System; |
| 68 | public class Program { |
| 69 | public static void Main() { |
| 70 | } |
| 71 | }`; |
| 72 | expect(detectLanguage(code)).toBe("csharp"); |
| 73 | }); |
| 74 | |
| 75 | it("should detect Go from package main", () => { |
| 76 | const code = `package main |
| 77 | import "fmt" |
| 78 | func main() { |
| 79 | fmt.Println("Hello, World!") |
| 80 | }`; |
| 81 | expect(detectLanguage(code)).toBe("go"); |
| 82 | }); |
| 83 | |
| 84 | it("should detect Rust from function syntax", () => { |
| 85 | const code = `fn main() { |
| 86 | println!("Hello, World!"); |
| 87 | }`; |
| 88 | expect(detectLanguage(code)).toBe("rust"); |
| 89 | }); |
| 90 | |
| 91 | it("should detect PHP from opening tag", () => { |
| 92 | const code = `<?php |
| 93 | echo "Hello, World!"; |
| 94 | ?>`; |
| 95 | expect(detectLanguage(code)).toBe("php"); |
| 96 | }); |
| 97 | |
| 98 | it("should detect SQL from SELECT statements", () => { |
| 99 | const code = `SELECT * FROM users WHERE id = 1;`; |
| 100 | expect(detectLanguage(code)).toBe("sql"); |
| 101 | }); |
| 102 | |
| 103 | it("should detect SQL with case insensitive matching", () => { |
| 104 | const code = `select name from users where active = true;`; |
| 105 | expect(detectLanguage(code)).toBe("sql"); |
| 106 | }); |
| 107 | |
| 108 | it("should detect JSON from object syntax", () => { |
| 109 | const code = `{ |
| 110 | "name": "John", |
| 111 | "age": 30 |
| 112 | }`; |
| 113 | expect(detectLanguage(code)).toBe("json"); |
| 114 | }); |
| 115 | |
| 116 | it("should detect bash from shebang", () => { |
| 117 | const code = `#!/bin/bash |
| 118 | echo "Hello, World!"`; |
| 119 | expect(detectLanguage(code)).toBe("bash"); |
| 120 | }); |
| 121 | |
| 122 | it("should detect HTML from DOCTYPE", () => { |
| 123 | const code = `<!DOCTYPE html> |
| 124 | <html> |
| 125 | <head> |
| 126 | <title>Test</title> |
| 127 | </head> |
| 128 | </html>`; |
| 129 | expect(detectLanguage(code)).toBe("html"); |
| 130 | }); |
| 131 | |
| 132 | it("should detect HTML from html tag", () => { |
| 133 | const code = `<html> |
| 134 | <body> |
| 135 | <h1>Hello</h1> |
| 136 | </body> |
| 137 | </html>`; |
| 138 | expect(detectLanguage(code)).toBe("html"); |
| 139 | }); |
| 140 | |
| 141 | it("should detect CSS from media queries", () => { |
| 142 | const code = `@media screen and (max-width: 600px) { |
| 143 | body { |
| 144 | font-size: 14px; |
| 145 | } |
| 146 | }`; |
| 147 | expect(detectLanguage(code)).toBe("css"); |
| 148 | }); |
| 149 | |
| 150 | it("should detect Markdown from headers", () => { |
| 151 | const code = `# Main Title |
| 152 | ## Subtitle |
| 153 | This is some content.`; |
| 154 | expect(detectLanguage(code)).toBe("markdown"); |
| 155 | }); |
| 156 | |
| 157 | it("should default to javascript for unknown patterns", () => { |
| 158 | const code = `some random code that doesn't match any pattern`; |
| 159 | expect(detectLanguage(code)).toBe("javascript"); |
| 160 | }); |
| 161 | |
| 162 | it("should handle empty string", () => { |
| 163 | const code = ``; |
| 164 | expect(detectLanguage(code)).toBe("javascript"); |
| 165 | }); |
| 166 | |
| 167 | it("should handle whitespace-only code", () => { |
| 168 | const code = ` |
| 169 | |
| 170 | `; |
| 171 | expect(detectLanguage(code)).toBe("javascript"); |
| 172 | }); |
| 173 | |
| 174 | it("should match first pattern when multiple patterns could match", () => { |
| 175 | // This code has both import (JavaScript) and interface (TypeScript) patterns |
| 176 | const code = `import { Component } from 'react'; |
| 177 | interface Props { |
| 178 | name: string; |
| 179 | }`; |
| 180 | expect(detectLanguage(code)).toBe("javascript"); // JavaScript pattern comes first |
| 181 | }); |
| 182 | |
| 183 | it("should handle multiline code correctly", () => { |
| 184 | const code = ` |
| 185 | import React from 'react'; |
| 186 | |
| 187 | const component = () => { |
| 188 | return <div>Hello</div>; |
| 189 | }; |
| 190 | `; |
| 191 | expect(detectLanguage(code)).toBe("javascript"); |
| 192 | }); |
| 193 | |
| 194 | it("should detect patterns with leading whitespace", () => { |
| 195 | const code = ` def my_function(): |
| 196 | return True`; |
| 197 | expect(detectLanguage(code)).toBe("python"); |
| 198 | }); |
| 199 | |
| 200 | it("should handle YAML-like content but default to javascript", () => { |
| 201 | // The actual implementation doesn't detect YAML from document separator |
| 202 | const code = `--- |
| 203 | name: John |
| 204 | age: 30`; |
| 205 | expect(detectLanguage(code)).toBe("yaml"); |
| 206 | }); |
| 207 | }); |