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

Replace asserts with shoulds #433

Open
jmh530 opened this issue Jul 14, 2022 · 0 comments
Open

Replace asserts with shoulds #433

jmh530 opened this issue Jul 14, 2022 · 0 comments

Comments

@jmh530
Copy link
Contributor

jmh530 commented Jul 14, 2022

I've been using shouldApprox recently and thought it was a nice addition, but realized that changing all my functions to use shouldApprox instead of approxEqual would be pretty annoying manually.

Would it be worth it to write a script that can replace assert with == and approxEqual with should and shouldApprox, respectively? If so, should said script be located as part of libmir?

It would be pretty easy to do with regex for the simple cases, as in below (does not handle the imports). It would also be possible to only replace within unittest blocks. Probably also would be good to replace in pre- and post-conditions.

import std.regex;
import std.stdio: writeln;

string replaceAssertWithShould(Captures!(string) m)
{
    auto rEqual = regex(r" == ");
    auto x = m.hit[7..($ - 2)].split(rEqual);
    return x[0] ~ ".should == " ~ x[1] ~ ";";
}

string replaceAssertWithShouldApprox(Captures!(string) m)
{
    auto rApproxEqual = regex(r"\.approxEqual\(");
    auto x = m.hit[7..($ - 3)].split(rApproxEqual);
    return x[0] ~ ".shouldApprox == " ~ x[1] ~ ";";
}

void main()
{
    string x = "assert(x == y); \nassert(x.approxEqual(y));";

    auto rAssertEqual = regex(r"assert\(.* \== .*\);");
    auto rAssertApproxEqual = regex(r"assert\(.*\.approxEqual\(.*\);");

    auto a = x.replaceAll!replaceAssertWithShould(rAssertEqual);
    auto b = a.replaceAll!replaceAssertWithShouldApprox(rAssertApproxEqual);
    writeln(x);
    writeln(b);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant