cadence icon indicating copy to clipboard operation
cadence copied to clipboard

[Test Framework] Add initial support for Matchers

Open SupunS opened this issue 3 years ago • 0 comments

Work towards https://github.com/onflow/cadence/issues/331

Description

Supports:

  • Equal matcher

    // matcher with primitive type
    let matcher = Test.equal(1)
    
    // matcher with struct type
    let matcher = Test.equal(Foo())
    
    // matcher with resource type
    let f <- create Foo()
    let matcher = Test.equal(<-f)
    
  • Custom matchers: Supports defining custom matchers that deal with AnyStruct type.

    e.g.(1): Customer matcher for a primitive type

    let matcher = Test.NewMatcher(fun (_ value: Int): Bool {
        return value == 7
    })
    
    assert(matcher.test(7))
    

    e.g.(2): Customer matcher for a struct type

    let matcher = Test.NewMatcher(fun (_ value: AnyStruct): Bool {
        if !value.isInstance(Type<Int>()) {
            return false
        }
    
        return (value as! Int) > 5
    })
    
    assert(matcher.test(8))
    

    e.g.(3): Customer matcher for handling resource type. Even though resource types are not supported, it can be made to work with a resource reference

    let matcher = Test.NewMatcher(fun (_ value: &Foo): Bool {
        return value.a == 4
    })
    
    let f <-create Foo(4)
    assert(matcher.test(&f as &Foo))
    
  • Expect function

    let matcher = Test.equal("other string")
    Test.expect("this string", matcher)
    

    *This API was slightly changed compared to the proposal, to support resources. This API is not final and can be adjusted based on requirements/feedback.


  • [ ] Targeted PR against master branch
  • [x] Linked to Github issue with discussion and accepted design OR link to spec that describes this work
  • [x] Code follows the standards mentioned here
  • [x] Updated relevant documentation
  • [x] Re-reviewed Files changed in the Github PR explorer
  • [x] Added appropriate labels

SupunS avatar Aug 04 '22 18:08 SupunS