测试代码高亮功能

代码高亮测试

测试不同编程语言的代码高亮效果。

C# 代码测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using System;
using System.Collections.Generic;
using System.Linq;

namespace TestNamespace
{
public class TestClass
{
private string _name;
private int _value;

public TestClass(string name, int value)
{
_name = name;
_value = value;
}

public void PrintInfo()
{
Console.WriteLine($"Name: {_name}, Value: {_value}");
}

public async Task<string> GetDataAsync()
{
await Task.Delay(1000);
return "Data loaded";
}

// LINQ 示例
public List<int> GetEvenNumbers(List<int> numbers)
{
return numbers.Where(n => n % 2 == 0).ToList();
}
}
}

C# Unity 代码测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour
{
[SerializeField] private float speed = 5.0f;
[SerializeField] private Rigidbody rb;

private Vector3 movement;

void Start()
{
rb = GetComponent<Rigidbody>();
}

void Update()
{
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");

movement = new Vector3(horizontal, 0, vertical);
}

void FixedUpdate()
{
rb.MovePosition(transform.position + movement * speed * Time.fixedDeltaTime);
}
}

JavaScript 代码测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class GameManager {
constructor() {
this.score = 0;
this.players = [];
}

addPlayer(name) {
const player = {
id: Date.now(),
name: name,
score: 0
};
this.players.push(player);
return player;
}

async loadData() {
try {
const response = await fetch('/api/gamedata');
const data = await response.json();
this.processData(data);
} catch (error) {
console.error('Failed to load data:', error);
}
}

updateScore(playerId, points) {
const player = this.players.find(p => p.id === playerId);
if (player) {
player.score += points;
this.score += points;
}
}
}

Python 代码测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import asyncio
import json
from typing import List, Dict, Optional

class DataProcessor:
def __init__(self, config: Dict):
self.config = config
self.data = []

async def process_data(self, items: List[Dict]) -> List[Dict]:
"""处理数据的异步方法"""
processed = []

for item in items:
# 数据清洗
cleaned_item = self._clean_item(item)

# 数据验证
if self._validate_item(cleaned_item):
processed.append(cleaned_item)

return processed

def _clean_item(self, item: Dict) -> Dict:
"""清理单个数据项"""
return {k: v.strip() if isinstance(v, str) else v
for k, v in item.items()}

def _validate_item(self, item: Dict) -> bool:
"""验证数据项"""
required_fields = self.config.get('required_fields', [])
return all(field in item for field in required_fields)

# 使用示例
async def main():
config = {"required_fields": ["name", "email"]}
processor = DataProcessor(config)

data = [
{"name": "Alice", "email": "alice@example.com"},
{"name": "Bob", "email": "bob@example.com"}
]

result = await processor.process_data(data)
print(json.dumps(result, indent=2))

if __name__ == "__main__":
asyncio.run(main())

Java 代码测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.util.*;
import java.util.stream.Collectors;

public class GameEngine {
private Map<String, Player> players;
private GameState currentState;

public GameEngine() {
this.players = new HashMap<>();
this.currentState = GameState.WAITING;
}

public void addPlayer(String id, String name) {
Player player = new Player(id, name);
players.put(id, player);

if (players.size() >= 2) {
currentState = GameState.READY;
}
}

public List<Player> getTopPlayers(int count) {
return players.values().stream()
.sorted(Comparator.comparingInt(Player::getScore).reversed())
.limit(count)
.collect(Collectors.toList());
}

public Optional<Player> findPlayer(String id) {
return Optional.ofNullable(players.get(id));
}

private enum GameState {
WAITING, READY, PLAYING, FINISHED
}

private static class Player {
private final String id;
private final String name;
private int score;

public Player(String id, String name) {
this.id = id;
this.name = name;
this.score = 0;
}

public int getScore() { return score; }
public void addScore(int points) { this.score += points; }
}
}

测试结果

如果你看到:

  • 正确高亮:语法关键字有颜色,字符串、注释等有不同颜色
  • 无高亮:所有代码都是同一种颜色

请告诉我哪些语言有高亮问题,我会进行修复。


测试代码高亮功能
https://lshgame.com/2025/08/01/Test_Code_Highlight_Functionality/
作者
SuHang
发布于
2025年8月1日
许可协议