haxetink / tink_unittest

Tinkerbell Unit Testing
https://haxetink.github.io/tink_unittest
16 stars 4 forks source link

Autogenerated asserts should use @:to String on abstracts, if present. #22

Closed back2dos closed 4 years ago

back2dos commented 4 years ago

Currently, abstracts are piped to Assert.stringify, which loses the type and just shows the runtime representation.

kevinresol commented 4 years ago

iirc I did that because i want strings to be wrapped in quotes, so we know it is string.

back2dos commented 4 years ago

Yeah, sure. We could still do Assert.stringify((value:String)) for all abstracts that unify with string. But I think that overshoots the goal What I'd propose would be:.

For example "john.doe@example.com" does make sense for an abstract Email(String) to String but having some 2d point abstract having quotes in "Point(1, 2)" is noisy at best, misleading at worst.

kevinresol commented 4 years ago
@:asserts
class StringificationTest {
    public function new() {}
    public function eq() {
        var i = 2;
        asserts.assert(i == 2);
        var b = true;
        asserts.assert(b);
        var u = new UnderlyingString('foo');
        asserts.assert(u == 'foo');
        var c = new CastableToString(1);
        asserts.assert(c == 'Value=1');
        asserts.assert(c == 1);
        return asserts.done();
    }
}

abstract Abs(Int) {
    public inline function new(v) this = v;
    @:op(A==B) static function eq(a:Abs, b:Abs):Bool;
}

abstract UnderlyingString(String) to String {
    public inline function new(v) this = v;
}
abstract CastableToString(Int) to Int {
    public inline function new(v) this = v;
    @:to public inline function stringify():String return 'Value=$this';
}

output this:

    - [OK] [tests/RunTests.hx:364] i == 2 (2 == 2)
    - [OK] [tests/RunTests.hx:366] b
    - [OK] [tests/RunTests.hx:368] u == 'foo' ("foo" == "foo")
    - [OK] [tests/RunTests.hx:370] c == 'Value=1' ("Value=1" == "Value=1")
    - [OK] [tests/RunTests.hx:371] c == 1 (Value=1 == 1)
back2dos commented 4 years ago

Cool, thanks ;)