-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
316 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
use crate::runners::{setup_npm_script, JavaScriptRuntime}; | ||
|
||
use super::execute_command; | ||
|
||
#[inline] | ||
fn set_blade_formatter_args(cmd: &mut std::process::Command, snippet_path: &std::path::Path) { | ||
cmd.arg("--write").arg(snippet_path); | ||
} | ||
|
||
#[inline] | ||
fn invote_blade_formatter( | ||
mut cmd: std::process::Command, | ||
snippet_path: &std::path::Path, | ||
) -> std::io::Result<(bool, Option<String>)> { | ||
set_blade_formatter_args(&mut cmd, snippet_path); | ||
|
||
execute_command(&mut cmd, snippet_path) | ||
} | ||
|
||
#[inline] | ||
pub fn format_using_blade_formatter( | ||
snippet_path: &std::path::Path, | ||
) -> std::io::Result<(bool, Option<String>)> { | ||
invote_blade_formatter( | ||
setup_npm_script(JavaScriptRuntime::default(), "blade-formatter"), | ||
snippet_path, | ||
) | ||
} | ||
|
||
#[cfg(test)] | ||
mod test_blade_formatter { | ||
use crate::{formatters::setup_snippet, languages::Language}; | ||
|
||
#[test] | ||
fn it_should_format_blade() { | ||
let input = r#"@extends('frontend.layouts.app') | ||
@section('title') foo | ||
@endsection | ||
@section('content') | ||
<section id="content"> | ||
<div class="container mod-users-pd-h"> | ||
<div class="pf-user-header"> | ||
<div></div> | ||
<p>@lang('users.index')</p> | ||
</div> | ||
<div class="pf-users-branch"> | ||
<ul class="pf-users-branch__list"> | ||
@foreach($users as $user) | ||
<li> | ||
<img src="{{ asset('img/frontend/icon/branch-arrow.svg') }}" alt="branch_arrow"> | ||
{{ link_to_route("frontend.users.user.show",$users["name"],$users['_id']) }} | ||
</li> | ||
@endforeach | ||
</ul> | ||
<div class="pf-users-branch__btn"> | ||
@can('create', App\Models\User::class) | ||
{!! link_to_route("frontend.users.user.create",__('users.create'),[1,2,3],['class' => 'btn']) !!} | ||
@endcan | ||
</div> | ||
</div> | ||
</div> | ||
</section> | ||
@endsection | ||
@section('footer') | ||
@stop"#; | ||
|
||
let expected_output = r#"@extends('frontend.layouts.app') | ||
@section('title') foo | ||
@endsection | ||
@section('content') | ||
<section id="content"> | ||
<div class="container mod-users-pd-h"> | ||
<div class="pf-user-header"> | ||
<div></div> | ||
<p>@lang('users.index')</p> | ||
</div> | ||
<div class="pf-users-branch"> | ||
<ul class="pf-users-branch__list"> | ||
@foreach ($users as $user) | ||
<li> | ||
<img src="{{ asset('img/frontend/icon/branch-arrow.svg') }}" alt="branch_arrow"> | ||
{{ link_to_route('frontend.users.user.show', $users['name'], $users['_id']) }} | ||
</li> | ||
@endforeach | ||
</ul> | ||
<div class="pf-users-branch__btn"> | ||
@can('create', App\Models\User::class) | ||
{!! link_to_route('frontend.users.user.create', __('users.create'), [1, 2, 3], ['class' => 'btn']) !!} | ||
@endcan | ||
</div> | ||
</div> | ||
</div> | ||
</section> | ||
@endsection | ||
@section('footer') | ||
@stop | ||
"#; | ||
|
||
let snippet = | ||
setup_snippet(input, Language::Sql.to_file_ext()).expect("it to create a snippet file"); | ||
|
||
let output = super::format_using_blade_formatter(snippet.path()) | ||
.expect("it to be successful") | ||
.1 | ||
.expect("it to be some"); | ||
|
||
assert_eq!(expected_output, output); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
use schemars::JsonSchema; | ||
|
||
use crate::formatters::{blade_formatter::format_using_blade_formatter, MdsfFormatter}; | ||
|
||
use super::{Lang, LanguageFormatter}; | ||
|
||
#[derive(Debug, Default, serde::Serialize, serde::Deserialize, JsonSchema)] | ||
#[cfg_attr(test, derive(PartialEq, Eq))] | ||
pub enum Blade { | ||
#[default] | ||
#[serde(rename = "blade-formatter")] | ||
BladeFormatter, | ||
} | ||
|
||
impl Default for Lang<Blade> { | ||
#[inline] | ||
fn default() -> Self { | ||
Self { | ||
enabled: true, | ||
formatter: MdsfFormatter::<Blade>::default(), | ||
} | ||
} | ||
} | ||
|
||
impl Default for MdsfFormatter<Blade> { | ||
#[inline] | ||
fn default() -> Self { | ||
Self::Single(Blade::BladeFormatter) | ||
} | ||
} | ||
|
||
impl LanguageFormatter for Blade { | ||
#[inline] | ||
fn format_snippet( | ||
&self, | ||
snippet_path: &std::path::Path, | ||
) -> std::io::Result<(bool, Option<String>)> { | ||
match self { | ||
Self::BladeFormatter => format_using_blade_formatter(snippet_path), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test_blade { | ||
use crate::{ | ||
formatters::{setup_snippet, MdsfFormatter}, | ||
languages::Lang, | ||
}; | ||
|
||
use super::Blade; | ||
|
||
const INPUT: &str = r#"@extends('frontend.layouts.app') | ||
@section('title') foo | ||
@endsection | ||
@section('content') | ||
<section id="content"> | ||
<div class="container mod-users-pd-h"> | ||
<div class="pf-user-header"> | ||
<div></div> | ||
<p>@lang('users.index')</p> | ||
</div> | ||
<div class="pf-users-branch"> | ||
<ul class="pf-users-branch__list"> | ||
@foreach($users as $user) | ||
<li> | ||
<img src="{{ asset('img/frontend/icon/branch-arrow.svg') }}" alt="branch_arrow"> | ||
{{ link_to_route("frontend.users.user.show",$users["name"],$users['_id']) }} | ||
</li> | ||
@endforeach | ||
</ul> | ||
<div class="pf-users-branch__btn"> | ||
@can('create', App\Models\User::class) | ||
{!! link_to_route("frontend.users.user.create",__('users.create'),[1,2,3],['class' => 'btn']) !!} | ||
@endcan | ||
</div> | ||
</div> | ||
</div> | ||
</section> | ||
@endsection | ||
@section('footer') | ||
@stop"#; | ||
|
||
const EXTENSION: &str = crate::languages::Language::Blade.to_file_ext(); | ||
|
||
#[test] | ||
fn it_should_be_enabled_by_default() { | ||
assert!(Lang::<Blade>::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!(Lang::<Blade> { | ||
enabled: false, | ||
formatter: MdsfFormatter::Single(Blade::BladeFormatter), | ||
} | ||
.format(snippet_path) | ||
.expect("it to not fail") | ||
.is_none()); | ||
} | ||
|
||
#[test] | ||
fn test_blade_formatter() { | ||
let expected_output = r#"@extends('frontend.layouts.app') | ||
@section('title') foo | ||
@endsection | ||
@section('content') | ||
<section id="content"> | ||
<div class="container mod-users-pd-h"> | ||
<div class="pf-user-header"> | ||
<div></div> | ||
<p>@lang('users.index')</p> | ||
</div> | ||
<div class="pf-users-branch"> | ||
<ul class="pf-users-branch__list"> | ||
@foreach ($users as $user) | ||
<li> | ||
<img src="{{ asset('img/frontend/icon/branch-arrow.svg') }}" alt="branch_arrow"> | ||
{{ link_to_route('frontend.users.user.show', $users['name'], $users['_id']) }} | ||
</li> | ||
@endforeach | ||
</ul> | ||
<div class="pf-users-branch__btn"> | ||
@can('create', App\Models\User::class) | ||
{!! link_to_route('frontend.users.user.create', __('users.create'), [1, 2, 3], ['class' => 'btn']) !!} | ||
@endcan | ||
</div> | ||
</div> | ||
</div> | ||
</section> | ||
@endsection | ||
@section('footer') | ||
@stop | ||
"#; | ||
|
||
let l = Lang::<Blade> { | ||
enabled: true, | ||
formatter: MdsfFormatter::Single(Blade::BladeFormatter), | ||
}; | ||
|
||
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"); | ||
|
||
assert_eq!(output, expected_output); | ||
} | ||
} |
Oops, something went wrong.