Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: all language formatters #58

Merged
merged 33 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
3face0f
refactor: match specific events
hougesen Mar 11, 2024
986682a
test: validate zig
hougesen Mar 11, 2024
4c59325
test: validate zig
hougesen Mar 11, 2024
4c90e35
test: validate yaml
hougesen Mar 11, 2024
bd01d64
test: validate prettier vue
hougesen Mar 11, 2024
da975d2
test: typescript formatters
hougesen Mar 11, 2024
0c8e476
test: validate javascript formatters
hougesen Mar 11, 2024
ac90f21
test: validate enabled works
hougesen Mar 11, 2024
451e88d
test: validate c
hougesen Mar 11, 2024
de47a53
test: validate cpp
hougesen Mar 11, 2024
1676e39
test: csharp
hougesen Mar 11, 2024
9f79b34
test: css
hougesen Mar 11, 2024
27baba2
test: dart
hougesen Mar 11, 2024
0efc620
test: elixir
hougesen Mar 11, 2024
c95c872
test: gleam
hougesen Mar 11, 2024
ea4e3ba
test(language): go
hougesen Mar 11, 2024
b6b71cf
test(language): html
hougesen Mar 11, 2024
5193cd9
test: log stdout stderr
hougesen Mar 11, 2024
71fdf1a
test(language): java
hougesen Mar 11, 2024
f8c66c9
test: validate schema can be turned to json
hougesen Mar 11, 2024
f1cc000
test: validate schema can be loaded
hougesen Mar 11, 2024
3a0a46d
test(json): biome, prettier and clang-format
hougesen Mar 11, 2024
943bb78
test(lua): stylua
hougesen Mar 11, 2024
e5961d2
test(sql): sqlfluff and sql-formatter
hougesen Mar 11, 2024
d82aaab
test(nim): validate nimpretty
hougesen Mar 11, 2024
72e8ff0
test: it_should_be_enabled_by_default should check bool
hougesen Mar 11, 2024
8f4d649
test(objective-c): validate clang-format
hougesen Mar 11, 2024
efb64da
test(ruby): rubocop
hougesen Mar 11, 2024
2dabe51
test(profo): validate clang-format
hougesen Mar 11, 2024
dd3847b
test(rust): rustfmt
hougesen Mar 11, 2024
905ca21
test(shell): validate shfmt
hougesen Mar 11, 2024
c339358
test(python): ruff, black, blue, yapf and autopep8
hougesen Mar 11, 2024
cf47939
test(markdown): prettier
hougesen Mar 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading