|
## Common test patterns for Nimcheck tests.
|
|
## This module provides code snippets used across test suites.
|
|
## Import it, then reference the const values directly.
|
|
|
|
import std/[strutils, json]
|
|
|
|
const
|
|
NimGoodCode* = """proc add*(a, b: int): int =
|
|
## Add two numbers.
|
|
result = a + b
|
|
"""
|
|
|
|
NimBadCode* = """proc add*(a, b: int): int =
|
|
## Add two numbers.
|
|
result = a + b
|
|
let x = "
|
|
|
|
"""
|
|
|
|
BashGoodCode* = """#!/usr/bin/env bash
|
|
echo "Hello, world!"
|
|
for i in 1 2 3; do
|
|
echo $i
|
|
done
|
|
"""
|
|
|
|
BashBadCode* = """#!/usr/bin/env bash
|
|
echo ${unclosed
|
|
"""
|
|
|
|
PythonGoodCode* = """def hello(name: str) -> str:
|
|
return f"Hello, {name}!"
|
|
|
|
class Greeter:
|
|
def __init__(self, prefix: str):
|
|
self.prefix = prefix
|
|
"""
|
|
|
|
PythonBadCode* = """def broken():
|
|
x = "
|
|
"""
|
|
|
|
JsGoodCode* = """function greet(name) {
|
|
return `Hello, ${name}!`;
|
|
}
|
|
|
|
const nums = [1, 2, 3];
|
|
"""
|
|
|
|
JsBadCode* = """function broken() {
|
|
const x = `
|
|
"""
|
|
|
|
PhpGoodCode* = """<?php
|
|
function greet(string $name): string {
|
|
return "Hello, $name!";
|
|
}
|
|
"""
|
|
|
|
PhpBadCode* = """<?php
|
|
$a = "unclosed_end
|
|
"""
|
|
|
|
HtmlGoodCode* = """<!DOCTYPE html>
|
|
<html>
|
|
<head><title>Test</title></head>
|
|
<body><p>Hello</p></body>
|
|
</html>
|
|
"""
|
|
|
|
HtmlBadCode* = """<!DOCTYPE html>
|
|
<html>
|
|
<body><p>Unclosed tag
|
|
"""
|
|
|
|
JinjaGoodCode* = """{% extends "base.html" %}
|
|
{% block content %}
|
|
<h1>{{ title }}</h1>
|
|
<p>{{ content }}</p>
|
|
{% endblock %}
|
|
"""
|
|
|
|
JsonGoodCode* = """{"name": "test", "value": 42}"""
|
|
JsonBadCode* = """{"name": "test", "value": }"""
|
|
|
|
YamlGoodCode* = """name: test
|
|
version: 1.0
|
|
dependencies:
|
|
- lib1
|
|
- lib2
|
|
"""
|
|
|
|
YamlBadCode* = """name: test
|
|
: invalid
|
|
"""
|
|
|
|
TomlGoodCode* = """[package]
|
|
name = "test"
|
|
version = "1.0.0"
|
|
|
|
[[dependencies]]
|
|
name = "lib"
|
|
version = "2.0"
|
|
"""
|
|
|
|
TomlBadCode* = """[package]
|
|
name = "test"
|
|
invalid line
|
|
"""
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# C
|
|
# ---------------------------------------------------------------------------
|
|
CGoodCode* = """#include <stdio.h>
|
|
|
|
int main(void) {
|
|
printf("Hello, world!\\n");
|
|
return 0;
|
|
}
|
|
"""
|
|
|
|
CBadCode* = """#include <stdio.h>
|
|
|
|
int main(void) {
|
|
printf("Hello, world!\\n")
|
|
return 0
|
|
"""
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# C++
|
|
# ---------------------------------------------------------------------------
|
|
CppGoodCode* = """#include <iostream>
|
|
#include <vector>
|
|
|
|
int main() {
|
|
std::vector<int> nums = {1, 2, 3};
|
|
for (auto n : nums) {
|
|
std::cout << n << std::endl;
|
|
}
|
|
return 0;
|
|
}
|
|
"""
|
|
|
|
CppBadCode* = """#include <iostream>
|
|
|
|
int main() {
|
|
std::cout << "unclosed
|
|
return 0;
|
|
"""
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# C#
|
|
# ---------------------------------------------------------------------------
|
|
CSharpGoodCode* = """using System;
|
|
|
|
class Program {
|
|
static void Main(string[] args) {
|
|
Console.WriteLine("Hello, world!");
|
|
}
|
|
}
|
|
"""
|
|
|
|
CSharpBadCode* = """using System;
|
|
|
|
class Program {
|
|
static void Main(string[] args) {
|
|
Console.WriteLine("unclosed
|
|
"""
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Java
|
|
# ---------------------------------------------------------------------------
|
|
JavaGoodCode* = """public class Hello {
|
|
public static void main(String[] args) {
|
|
System.out.println("Hello, world!");
|
|
}
|
|
}
|
|
"""
|
|
|
|
JavaBadCode* = """public class Hello {
|
|
public static void main(String[] args) {
|
|
System.out.println("unclosed
|
|
"""
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Kotlin
|
|
# ---------------------------------------------------------------------------
|
|
KotlinGoodCode* = """fun main() {
|
|
println("Hello, world!")
|
|
}
|
|
"""
|
|
|
|
KotlinBadCode* = """fun main() {
|
|
println("unclosed
|
|
"""
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Lua
|
|
# ---------------------------------------------------------------------------
|
|
LuaGoodCode* = """function hello(name)
|
|
print("Hello, " .. name .. "!")
|
|
end
|
|
"""
|
|
|
|
LuaBadCode* = """function hello(name)
|
|
print("unclosed
|
|
"""
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Swift
|
|
# ---------------------------------------------------------------------------
|
|
SwiftGoodCode* = """import Foundation
|
|
|
|
func hello(name: String) -> String {
|
|
return "Hello, \\(name)!"
|
|
}
|
|
print(hello(name: "world"))
|
|
"""
|
|
|
|
SwiftBadCode* = """import Foundation
|
|
|
|
func hello(name: String) -> String {
|
|
return "unclosed
|
|
"""
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# TypeScript
|
|
# ---------------------------------------------------------------------------
|
|
TsGoodCode* = """function greet(name: string): string {
|
|
return `Hello, ${name}!`;
|
|
}
|
|
"""
|
|
|
|
TsBadCode* = """function greet(name: string): string {
|
|
return `unclosed
|
|
"""
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# XML
|
|
# ---------------------------------------------------------------------------
|
|
XmlGoodCode* = """<?xml version="1.0" encoding="UTF-8"?>
|
|
<root>
|
|
<element attr="value">Content</element>
|
|
</root>
|
|
"""
|
|
|
|
XmlBadCode* = """<?xml version="1.0" encoding="UTF-8"?>
|
|
<root>
|
|
<element>Unclosed
|
|
"""
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Go
|
|
# ---------------------------------------------------------------------------
|
|
GoGoodCode* = """package main
|
|
|
|
import "fmt"
|
|
|
|
func main() {
|
|
fmt.Println("Hello, world!")
|
|
}
|
|
"""
|
|
|
|
GoBadCode* = """package main
|
|
|
|
import "fmt"
|
|
|
|
func main() {
|
|
fmt.Println("unclosed
|
|
"""
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Rust
|
|
# ---------------------------------------------------------------------------
|
|
RustGoodCode* = """fn main() {
|
|
println!("Hello, world!");
|
|
}
|
|
"""
|
|
|
|
RustBadCode* = """fn main() {
|
|
println!("unclosed
|
|
"""
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Ruby
|
|
# ---------------------------------------------------------------------------
|
|
RubyGoodCode* = """def hello
|
|
puts "Hello, world!"
|
|
end
|
|
"""
|
|
|
|
RubyBadCode* = """def hello
|
|
puts "unclosed
|
|
"""
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# CSS
|
|
# ---------------------------------------------------------------------------
|
|
CssGoodCode* = """body {
|
|
color: red;
|
|
background: blue;
|
|
}
|
|
h1 {
|
|
font-size: 2em;
|
|
}
|
|
"""
|
|
|
|
CssBadCode* = """body {
|
|
color: red;
|
|
background: "unclosed
|
|
"""
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# SQL
|
|
# ---------------------------------------------------------------------------
|
|
SqlGoodCode* = """SELECT * FROM users
|
|
WHERE id = 1
|
|
ORDER BY name;
|
|
"""
|
|
|
|
SqlBadCode* = """SELECT * FROM users
|
|
WHERE name = 'unclosed
|
|
"""
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Markdown
|
|
# ---------------------------------------------------------------------------
|
|
MdGoodCode* = """# Hello World
|
|
|
|
This is a **markdown** file.
|
|
|
|
- Item 1
|
|
- Item 2
|
|
|
|
```python
|
|
print("hi")
|
|
```
|
|
"""
|
|
|
|
MdBadCode* = """# Hello World
|
|
|
|
```python
|
|
print("unclosed
|
|
"""
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Dockerfile
|
|
# ---------------------------------------------------------------------------
|
|
DockerGoodCode* = """FROM ubuntu:latest
|
|
RUN apt-get update
|
|
CMD ["echo", "hello"]
|
|
"""
|
|
|
|
DockerBadCode* = """FROM ubuntu:latest
|
|
RUN echo "
|
|
"""
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Makefile
|
|
# ---------------------------------------------------------------------------
|
|
MakefileGoodCode* = """CC = gcc
|
|
CFLAGS = -Wall -O2
|
|
|
|
all: hello
|
|
|
|
hello: hello.c
|
|
""" & "\t" & """$(CC) $(CFLAGS) -o hello hello.c
|
|
|
|
.PHONY: clean
|
|
clean:
|
|
""" & "\t" & "rm -f hello\n"
|
|
|
|
MakefileBadCode* = """CC = gcc
|
|
all:
|
|
""" & "\t" & "echo \"\n"
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Garbage/false positive check patterns
|
|
# ---------------------------------------------------------------------------
|
|
FalsePositiveDetectionSamples* = """abc def ghi
|
|
hello
|
|
42 123 456
|
|
... --- !!!
|
|
"""
|