Skip to content

Commit

Permalink
Add extra tests to ; fix not working cases; cleanup scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
luke-biel committed Mar 25, 2022
1 parent 75bd091 commit ff59e5e
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 46 deletions.
4 changes: 3 additions & 1 deletion acceptance_tests/basic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ mod test_cases {
}

#[test_case(SimpleEnum::Var1 => matches Ok(e) if e == SimpleEnum::Var1)]
#[test_case(SimpleEnum::Var1 => matches Ok(e) if e == SimpleEnum::Var2; "ok should fail")]
#[test_case(SimpleEnum::Var2 => matches Err(e) if e == "var2")]
#[test_case(SimpleEnum::Var2 => matches Err(e) if e == "var1"; "err should fail")]
fn extended_pattern_matching_result(e: SimpleEnum) -> Result<SimpleEnum, &'static str> {
if e == SimpleEnum::Var1 {
Ok(e)
Expand All @@ -160,7 +162,7 @@ mod test_cases {
}
}

#[should_panic(expected = "Expected SimpleEnum :: Var2 found Var1")]
#[should_panic(expected = "Expected `SimpleEnum :: Var2` found Var1")]
#[test_case(SimpleEnum::Var1 => matches SimpleEnum::Var2)]
fn pattern_matching_result_fails(e: SimpleEnum) -> SimpleEnum {
e
Expand Down
34 changes: 34 additions & 0 deletions scripts/publish.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env bash

SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
REPO_DIR="${SCRIPT_DIR}/.."
CURRENT_DIR=$(pwd)

cd "${REPO_DIR}"

set -eo xtrace

./scripts/test_all.sh

nvim Cargo.toml
cargo build

nvim CHANGELOG.md
nvim src/lib.rs

cargo readme > README.md

cargo publish --dry-run --allow-dirty

git add .
git commit
git push origin

set +o xtrace

echo "Next step: Wait for CI\n"
echo "Next step: \`git tag vX.Y.Z; git push --tags\`\n"
echo "Next step: Create release in Github\n"
echo "Next step: \`cargo publish\`"

cd "${CURRENT_DIR}"
25 changes: 2 additions & 23 deletions publish.sh → scripts/test_all.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash
#!/usr/bin/env bash

set -eo xtrace
set -e

cargo clean

Expand All @@ -12,24 +12,3 @@ find . -name 'target' | xargs rm -rf
SNAPSHOT_DIR=rust-nightly cargo +nightly test --workspace --all-features
find . -name 'target' | xargs rm -rf
SNAPSHOT_DIR=rust-1.49.0 cargo +1.49 test --workspace --all-features

nvim Cargo.toml
cargo build

nvim CHANGELOG.md
nvim src/lib.rs

cargo readme > README.md

cargo publish --dry-run --allow-dirty

git add .
git commit
git push origin

set +o xtrace

echo "Next step: Wait for CI\n"
echo "Next step: \`git tag vX.Y.Z; git push --tags\`\n"
echo "Next step: Create release in Github\n"
echo "Next step: \`cargo publish\`"
36 changes: 29 additions & 7 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use quote::ToTokens;
use std::collections::HashSet;
use std::fmt::{Display, Formatter};
use syn::parse::{Parse, ParseStream};
use syn::token::If;
use syn::{parse_quote, Attribute, Expr, Pat, Token};

pub mod kw {
Expand All @@ -29,7 +30,7 @@ pub enum TestCaseResult {
// test_case(a, b, c => result)
Simple(Expr),
// test_case(a, b, c => matches Ok(_) if true)
Matching(Pat, Option<Expr>),
Matching(Pat, Option<Box<Expr>>),
// test_case(a, b, c => panics "abcd")
Panicking(Option<Expr>),
// test_case(a, b, c => with |v: T| assert!(v.is_nan()))
Expand All @@ -46,10 +47,19 @@ impl Parse for TestCaseExpression {
let extra_keywords = parse_kws(input);

if input.parse::<kw::matches>().is_ok() {
let pattern = input.parse()?;
let guard = if input.peek(If) {
let _if_kw: If = input.parse()?;
let guard: Box<Expr> = input.parse()?;
Some(guard)
} else {
None
};

Ok(TestCaseExpression {
_token: token,
extra_keywords,
result: TestCaseResult::Matching(input.parse()?, input.parse::<Expr>().ok()),
result: TestCaseResult::Matching(pattern, guard),
})
} else if input.parse::<kw::it>().is_ok() || input.parse::<kw::is>().is_ok() {
parse_with_keyword::<_, _>(input, token, extra_keywords, TestCaseResult::Complex)
Expand Down Expand Up @@ -101,12 +111,24 @@ impl TestCaseExpression {
pub fn assertion(&self) -> TokenStream2 {
match &self.result {
TestCaseResult::Simple(expr) => parse_quote! { assert_eq!(#expr, _result) },
TestCaseResult::Matching(pat, expr) => {
TestCaseResult::Matching(pat, guard) => {
let pat_str = pat.to_token_stream().to_string();
parse_quote! {
match _result {
#pat #expr => (),
e => panic!("Expected {} found {:?}", #pat_str, e)

if let Some(guard) = guard {
let guard_str = guard.to_token_stream().to_string();

parse_quote! {
match _result {
#pat if #guard => (),
e => panic!("Expected `{} if {}` found {:?}", #pat_str, #guard_str, e)
}
}
} else {
parse_quote! {
match _result {
#pat => (),
e => panic!("Expected `{}` found {:?}", #pat_str, e)
}
}
}
}
Expand Down
18 changes: 13 additions & 5 deletions tests/snapshots/rust-1.49.0/acceptance__acceptance__basic.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ expression: lines

---
panic message: `"It has to panic"`,
test_cases::extended_pattern_matching_result::err_should_fail
test_cases::extended_pattern_matching_result::ok_should_fail
test_cases::panicing::_expects_panicking_some_this_should_fail_
expected substring: `"This should fail"`
---- test_cases::extended_pattern_matching_result::err_should_fail stdout ----
---- test_cases::extended_pattern_matching_result::ok_should_fail stdout ----
---- test_cases::panicing::_expects_panicking_some_this_should_fail_ stdout ----
failures:
failures:
running 96 tests
test result: FAILED. 91 passed; 1 failed; 4 ignored; 0 measured; 0 filtered out
running 98 tests
test result: FAILED. 91 passed; 3 failed; 4 ignored; 0 measured; 0 filtered out
test test_cases::abs_tests::returns_0_for_0 ... ok
test test_cases::abs_tests::returns_given_number_for_positive_input ... ok
test test_cases::abs_tests::returns_opposite_number_for_non_positive_input ... ok
Expand Down Expand Up @@ -52,8 +56,10 @@ test test_cases::create_path::long_dir ... ok
test test_cases::create_path::short_dir ... ok
test test_cases::divide_by_zero_f64_with_lambda::_0_0_expects_with_v_f64_assert_v_is_nan_ ... ok
test test_cases::divide_by_zero_f64_with_lambda::_1_0_expects_with_v_f64_assert_v_is_infinite_ ... ok
test test_cases::extended_pattern_matching_result::simpleenum_var1_expects_matching_ok_e_ ... ok
test test_cases::extended_pattern_matching_result::simpleenum_var2_expects_matching_err_e_ ... ok
test test_cases::extended_pattern_matching_result::err_should_fail ... FAILED
test test_cases::extended_pattern_matching_result::ok_should_fail ... FAILED
test test_cases::extended_pattern_matching_result::simpleenum_var1_expects_matching_ok_e_e_simpleenum_var1 ... ok
test test_cases::extended_pattern_matching_result::simpleenum_var2_expects_matching_err_e_e_var2_ ... ok
test test_cases::fancy_addition::some_2_3_some_4_expects_2_3_4 ... ok
test test_cases::fancy_addition::some_2_some_3_expects_5 ... ok
test test_cases::fancy_addition::treats_none_as_0 ... ok
Expand Down Expand Up @@ -108,4 +114,6 @@ test test_cases::test_generics::source1_expects_1 ... ok
test test_cases::test_generics::source2_expects_2 ... ok
test test_cases::test_impl::source1_expects_1 ... ok
test test_cases::test_impl::source2_expects_2 ... ok
thread 'test_cases::panicing::_expects_panicking_some_this_should_fail_' panicked at 'It has to panic', src/lib.rs:172:9
thread 'test_cases::extended_pattern_matching_result::err_should_fail' panicked at 'Expected `Err(e) if e == "var1"` found Err("var2")', src/lib.rs:153:5
thread 'test_cases::extended_pattern_matching_result::ok_should_fail' panicked at 'Expected `Ok(e) if e == SimpleEnum :: Var2` found Ok(Var1)', src/lib.rs:153:5
thread 'test_cases::panicing::_expects_panicking_some_this_should_fail_' panicked at 'It has to panic', src/lib.rs:174:9
18 changes: 13 additions & 5 deletions tests/snapshots/rust-nightly/acceptance__acceptance__basic.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ expression: lines

---
panic message: `"It has to panic"`,
test_cases::extended_pattern_matching_result::err_should_fail
test_cases::extended_pattern_matching_result::ok_should_fail
test_cases::panicing::_expects_panicking_some_this_should_fail_
expected substring: `"This should fail"`
---- test_cases::extended_pattern_matching_result::err_should_fail stdout ----
---- test_cases::extended_pattern_matching_result::ok_should_fail stdout ----
---- test_cases::panicing::_expects_panicking_some_this_should_fail_ stdout ----
failures:
failures:
running 96 tests
test result: FAILED. 91 passed; 1 failed; 4 ignored; 0 measured; 0 filtered out
running 98 tests
test result: FAILED. 91 passed; 3 failed; 4 ignored; 0 measured; 0 filtered out
test test_cases::abs_tests::returns_0_for_0 ... ok
test test_cases::abs_tests::returns_given_number_for_positive_input ... ok
test test_cases::abs_tests::returns_opposite_number_for_non_positive_input ... ok
Expand Down Expand Up @@ -52,8 +56,10 @@ test test_cases::create_path::long_dir ... ok
test test_cases::create_path::short_dir ... ok
test test_cases::divide_by_zero_f64_with_lambda::_0_0_expects_with_v_f64_assert_v_is_nan_ ... ok
test test_cases::divide_by_zero_f64_with_lambda::_1_0_expects_with_v_f64_assert_v_is_infinite_ ... ok
test test_cases::extended_pattern_matching_result::simpleenum_var1_expects_matching_ok_e_ ... ok
test test_cases::extended_pattern_matching_result::simpleenum_var2_expects_matching_err_e_ ... ok
test test_cases::extended_pattern_matching_result::err_should_fail ... FAILED
test test_cases::extended_pattern_matching_result::ok_should_fail ... FAILED
test test_cases::extended_pattern_matching_result::simpleenum_var1_expects_matching_ok_e_e_simpleenum_var1 ... ok
test test_cases::extended_pattern_matching_result::simpleenum_var2_expects_matching_err_e_e_var2_ ... ok
test test_cases::fancy_addition::some_2_3_some_4_expects_2_3_4 ... ok
test test_cases::fancy_addition::some_2_some_3_expects_5 ... ok
test test_cases::fancy_addition::treats_none_as_0 ... ok
Expand Down Expand Up @@ -108,4 +114,6 @@ test test_cases::test_generics::source1_expects_1 ... ok
test test_cases::test_generics::source2_expects_2 ... ok
test test_cases::test_impl::source1_expects_1 ... ok
test test_cases::test_impl::source2_expects_2 ... ok
thread 'test_cases::panicing::_expects_panicking_some_this_should_fail_' panicked at 'It has to panic', src/lib.rs:172:9
thread 'test_cases::extended_pattern_matching_result::err_should_fail' panicked at 'Expected `Err(e) if e == "var1"` found Err("var2")', src/lib.rs:153:5
thread 'test_cases::extended_pattern_matching_result::ok_should_fail' panicked at 'Expected `Ok(e) if e == SimpleEnum :: Var2` found Ok(Var1)', src/lib.rs:153:5
thread 'test_cases::panicing::_expects_panicking_some_this_should_fail_' panicked at 'It has to panic', src/lib.rs:174:9
18 changes: 13 additions & 5 deletions tests/snapshots/rust-stable/acceptance__acceptance__basic.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ expression: lines

---
panic message: `"It has to panic"`,
test_cases::extended_pattern_matching_result::err_should_fail
test_cases::extended_pattern_matching_result::ok_should_fail
test_cases::panicing::_expects_panicking_some_this_should_fail_
expected substring: `"This should fail"`
---- test_cases::extended_pattern_matching_result::err_should_fail stdout ----
---- test_cases::extended_pattern_matching_result::ok_should_fail stdout ----
---- test_cases::panicing::_expects_panicking_some_this_should_fail_ stdout ----
failures:
failures:
running 96 tests
test result: FAILED. 91 passed; 1 failed; 4 ignored; 0 measured; 0 filtered out
running 98 tests
test result: FAILED. 91 passed; 3 failed; 4 ignored; 0 measured; 0 filtered out
test test_cases::abs_tests::returns_0_for_0 ... ok
test test_cases::abs_tests::returns_given_number_for_positive_input ... ok
test test_cases::abs_tests::returns_opposite_number_for_non_positive_input ... ok
Expand Down Expand Up @@ -52,8 +56,10 @@ test test_cases::create_path::long_dir ... ok
test test_cases::create_path::short_dir ... ok
test test_cases::divide_by_zero_f64_with_lambda::_0_0_expects_with_v_f64_assert_v_is_nan_ ... ok
test test_cases::divide_by_zero_f64_with_lambda::_1_0_expects_with_v_f64_assert_v_is_infinite_ ... ok
test test_cases::extended_pattern_matching_result::simpleenum_var1_expects_matching_ok_e_ ... ok
test test_cases::extended_pattern_matching_result::simpleenum_var2_expects_matching_err_e_ ... ok
test test_cases::extended_pattern_matching_result::err_should_fail ... FAILED
test test_cases::extended_pattern_matching_result::ok_should_fail ... FAILED
test test_cases::extended_pattern_matching_result::simpleenum_var1_expects_matching_ok_e_e_simpleenum_var1 ... ok
test test_cases::extended_pattern_matching_result::simpleenum_var2_expects_matching_err_e_e_var2_ ... ok
test test_cases::fancy_addition::some_2_3_some_4_expects_2_3_4 ... ok
test test_cases::fancy_addition::some_2_some_3_expects_5 ... ok
test test_cases::fancy_addition::treats_none_as_0 ... ok
Expand Down Expand Up @@ -108,4 +114,6 @@ test test_cases::test_generics::source1_expects_1 ... ok
test test_cases::test_generics::source2_expects_2 ... ok
test test_cases::test_impl::source1_expects_1 ... ok
test test_cases::test_impl::source2_expects_2 ... ok
thread 'test_cases::panicing::_expects_panicking_some_this_should_fail_' panicked at 'It has to panic', src/lib.rs:172:9
thread 'test_cases::extended_pattern_matching_result::err_should_fail' panicked at 'Expected `Err(e) if e == "var1"` found Err("var2")', src/lib.rs:153:5
thread 'test_cases::extended_pattern_matching_result::ok_should_fail' panicked at 'Expected `Ok(e) if e == SimpleEnum :: Var2` found Ok(Var1)', src/lib.rs:153:5
thread 'test_cases::panicing::_expects_panicking_some_this_should_fail_' panicked at 'It has to panic', src/lib.rs:174:9

0 comments on commit ff59e5e

Please sign in to comment.