Git Product home page Git Product logo

Comments (3)

frondeus avatar frondeus commented on August 24, 2024

Right now you might do this:

#[test_case(2, 2 => 2 + 2)]
fn test(a: usize, b: usize) -> usize { a + b }

which is pretty nice, but it means we need have a way to distinguish between expression and pattern.

Two ideas:

  • Breaking change: Let change => token into -> token inside attribute macro for expressions and use => for patterns.
  • Use match keyword to indicate pattern
#[test_case(MyEnum::Foo('a', 5) => match MyEnum::Foo(_, 5))]

But this is ambiguous - you don't know if match is a match expression or only pattern indication.

  • Use pat or pattern new keyword to indicate pattern
#[test_case(MyEnum::Foo('a', 5) => pattern MyEnum::Foo(_, 5))]

from test-case.

frondeus avatar frondeus commented on August 24, 2024

I have been working on parser combinators library and pattern matching was necessary.
So I created on my local branch solution:

#[test_case("abcd" => matches (Foo("a", "bc"), _) )]
#[test_case("abcd" => (Foo("a", "bc"), "d") )] // Simple assert_eq! comparison when no keyword used.
#[test_case("ab"   => panics "[Bar]" )]
#[test_case(""     => panics "[Baz, Bar(Bar)]" )]
fn test<'a, T: Debug>(input: &'a str) -> (T, &'a str) {
    parse(input).unwrap()
}

This solution uses two new keywords: matches for pattern matching and panics for adding #[should_panic(expected = "[Bar]") attribute.

It is backward compatible so I don't have to sed few thousands of tests, and it solves two issues at the same time.

from test-case.

frondeus avatar frondeus commented on August 24, 2024

Created v1.0.0-alpha-1 with this feature because it's really useful for me :)

from test-case.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.