Skip to content

Commit

Permalink
test: all language formatters (#58)
Browse files Browse the repository at this point in the history
* refactor: match specific events

* test: validate zig

* test: validate zig

* test: validate yaml

* test: validate prettier vue

* test: typescript formatters

* test: validate javascript formatters

* test: validate enabled works

* test: validate c

* test: validate cpp

* test: csharp

* test: css

* test: dart

* test: elixir

* test: gleam

* test(language): go

* test(language): html

* test: log stdout stderr

* test(language): java

* test: validate schema can be turned to json

* test: validate schema can be loaded

* test(json): biome, prettier and clang-format

* test(lua): stylua

* test(sql): sqlfluff and sql-formatter

* test(nim): validate nimpretty

* test: it_should_be_enabled_by_default should check bool

* test(objective-c): validate clang-format

* test(ruby): rubocop

* test(profo): validate clang-format

* test(rust): rustfmt

* test(shell): validate shfmt

* test(python): ruff, black, blue, yapf and autopep8

* test(markdown): prettier
  • Loading branch information
hougesen authored Mar 11, 2024
1 parent 38bca07 commit 35c6eec
Show file tree
Hide file tree
Showing 34 changed files with 2,078 additions and 14 deletions.
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ test:

test-coverage:
cargo llvm-cov clean
cargo llvm-cov --all-features --lcov --output-path lcov.info
cargo llvm-cov --open
cargo llvm-cov --all-features --open

precommit:
cargo clean
Expand Down
23 changes: 23 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::languages::{
};

#[derive(Debug, serde::Serialize, serde::Deserialize, JsonSchema)]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct MdsfConfig {
#[schemars(skip)]
#[serde(rename = "$schema", default = "default_schema_location")]
Expand Down Expand Up @@ -158,3 +159,25 @@ fn default_schema_location() -> String {
"https://raw.githubusercontent.com/hougesen/mdsf/main/schemas/v{package_version}/mdsf.schema.json"
)
}

#[cfg(test)]
mod test_config {
use super::MdsfConfig;

#[test]
fn schema_should_be_serializable() {
let config = MdsfConfig::default();

let json = serde_json::to_string_pretty(&config).expect("it to be serializable");

let loaded = serde_json::from_str::<MdsfConfig>(&json).expect("it to be parsed");

assert_eq!(config, loaded);
}

#[test]
fn json_schema_should_be_serializable() {
serde_json::to_string_pretty(&schemars::schema_for!(MdsfConfig))
.expect("it to be serializable");
}
}
27 changes: 27 additions & 0 deletions src/formatters/clang_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,31 @@ mod test_clang_format {

assert_eq!(expected_output, output);
}

#[test]
fn it_should_format_java() {
let input = "class HelloWorld {
public static void main(String[] args) {
System.out.println(\"Hello\");
System.out.println(\"World!\");
}
}";

let expected_output = "class HelloWorld {
public static void main(String[] args) {
System.out.println(\"Hello\");
System.out.println(\"World!\");
}
}";

let snippet = setup_snippet(input, Language::Java.to_file_ext())
.expect("it to create a snippet file");

let output = format_using_clang_format(snippet.path())
.expect("it to be successful")
.1
.expect("it to be some");

assert_eq!(expected_output, output);
}
}
4 changes: 2 additions & 2 deletions src/formatters/gofmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ mod test_gofmt {

#[test]
fn it_should_format_go() {
let input = "package main
let input = "package main
func add(a int , b int ) int {
return a + b
return a + b
}
";
Expand Down
17 changes: 7 additions & 10 deletions src/formatters/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use std::{
io::Write,
process::{Command, Stdio},
};
use std::{io::Write, process::Command};

use tempfile::NamedTempFile;

Expand Down Expand Up @@ -72,12 +69,12 @@ fn handle_post_execution(
}

fn spawn_command(cmd: &mut Command) -> std::io::Result<bool> {
Ok(cmd
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()?
.wait()?
.success())
#[cfg(not(test))]
cmd.stdout(std::process::Stdio::null());
#[cfg(not(test))]
cmd.stderr(std::process::Stdio::null());

Ok(cmd.spawn()?.wait()?.success())
}

#[inline]
Expand Down
46 changes: 46 additions & 0 deletions src/formatters/prettier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,4 +313,50 @@ updates:

assert_eq!(expected_output, output);
}

#[test]
fn it_should_format_vue() {
let input = "<script lang=\"ts\" setup >
import {
ref
} from \"vue\"
const count = ref(1)
function add (a:number,b:number):number {
return a +b
} </script>
<template>
<button @click=\"()=> count = add(count,count )\">Increment </button>
</template>
";

let expected_output = "<script lang=\"ts\" setup>
import { ref } from \"vue\";
const count = ref(1);
function add(a: number, b: number): number {
return a + b;
}
</script>
<template>
<button @click=\"() => (count = add(count, count))\">Increment</button>
</template>
";

let snippet =
setup_snippet(input, Language::Vue.to_file_ext()).expect("it to create a snippet file");

let output = format_using_prettier(snippet.path(), true)
.expect("it to be successful")
.1
.expect("it to be some");

assert_eq!(expected_output, output);
}
}
58 changes: 58 additions & 0 deletions src/languages/c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ use crate::{config::default_enabled, formatters::clang_format::format_using_clan
use super::LanguageFormatter;

#[derive(Debug, Default, serde::Serialize, serde::Deserialize, JsonSchema)]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub enum CFormatter {
#[default]
#[serde(rename = "clang-format")]
ClangFormat,
}

#[derive(Debug, serde::Serialize, serde::Deserialize, JsonSchema)]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct C {
#[serde(default = "default_enabled")]
pub enabled: bool,
Expand Down Expand Up @@ -41,3 +43,59 @@ impl LanguageFormatter for C {
}
}
}

#[cfg(test)]
mod test_c {
use crate::{formatters::setup_snippet, languages::LanguageFormatter};

use super::{CFormatter, C};

const INPUT: &str = "int add(int a,int b){
a-b;
return a + b;
}";

const EXTENSION: &str = crate::languages::Language::C.to_file_ext();

#[test]
fn it_should_be_enabled_by_default() {
assert!(C::default().enabled);
}

#[test]
fn it_should_not_format_when_enabled_is_false() {
let snippet = setup_snippet(INPUT, EXTENSION).expect("it to save the file");
let snippet_path = snippet.path();

assert!(C {
enabled: false,
formatter: CFormatter::default(),
}
.format(snippet_path)
.expect("it to not fail")
.is_none());
}

#[test]
fn test_clang_format() {
let l = C {
enabled: true,
formatter: CFormatter::ClangFormat,
};

let snippet = setup_snippet(INPUT, EXTENSION).expect("it to save the file");
let snippet_path = snippet.path();

let output = l
.format(snippet_path)
.expect("it to not fail")
.expect("it to be a snippet");

let expected_output = "int add(int a, int b) {
a - b;
return a + b;
}";

assert_eq!(output, expected_output);
}
}
58 changes: 58 additions & 0 deletions src/languages/cpp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ use crate::{config::default_enabled, formatters::clang_format::format_using_clan
use super::LanguageFormatter;

#[derive(Debug, Default, serde::Serialize, serde::Deserialize, JsonSchema)]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub enum CppFormatter {
#[default]
#[serde(rename = "clang-format")]
ClangFormat,
}

#[derive(Debug, serde::Serialize, serde::Deserialize, JsonSchema)]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct Cpp {
#[serde(default = "default_enabled")]
pub enabled: bool,
Expand Down Expand Up @@ -41,3 +43,59 @@ impl LanguageFormatter for Cpp {
}
}
}

#[cfg(test)]
mod test_cpp {
use crate::{formatters::setup_snippet, languages::LanguageFormatter};

use super::{Cpp, CppFormatter};

const INPUT: &str = "int add(int a,int b){
a-b;
return a + b;
}";

const EXTENSION: &str = crate::languages::Language::Cpp.to_file_ext();

#[test]
fn it_should_be_enabled_by_default() {
assert!(Cpp::default().enabled);
}

#[test]
fn it_should_not_format_when_enabled_is_false() {
let snippet = setup_snippet(INPUT, EXTENSION).expect("it to save the file");
let snippet_path = snippet.path();

assert!(Cpp {
enabled: false,
formatter: CppFormatter::default(),
}
.format(snippet_path)
.expect("it to not fail")
.is_none());
}

#[test]
fn test_clang_format() {
let l = Cpp {
enabled: true,
formatter: CppFormatter::ClangFormat,
};

let snippet = setup_snippet(INPUT, EXTENSION).expect("it to save the file");
let snippet_path = snippet.path();

let output = l
.format(snippet_path)
.expect("it to not fail")
.expect("it to be a snippet");

let expected_output = "int add(int a, int b) {
a - b;
return a + b;
}";

assert_eq!(output, expected_output);
}
}
Loading

0 comments on commit 35c6eec

Please sign in to comment.