2

FsCheck allows a custom Arbitrary in its NUnit integration:

[<Property(Verbose = true, Arbitrary= [typeof<Test.Arithmetic.MyArb>])>]
static member  MultiplyIdentity (x: int64) = x * 1 = x

This syntax doesn't work. I feel a bit embarrassed to ask, but apparently I never needed this before: how do you specify the type in F# as an attribute parameter? Microsoft says nothing about it, nor does the Wikibooks project and I have some trouble googling this (the word type is omnipresent).

Note 1: the Arbitrary parameter is of type Type [].

Ivan Chaer
  • 6,980
  • 1
  • 38
  • 48
Abel
  • 56,041
  • 24
  • 146
  • 247

1 Answers1

4

I think you're close, but [1;2;3] creates a list<int>, you want an array literal using [| 1;2;3 |]:

[<Property(Verbose = true, Arbitrary= [| typeof<Test.Arithmetic.MyArb> |])>]
static member  MultiplyIdentity (x: int64) = x * 1 = x
Kurt Schelfthout
  • 8,880
  • 1
  • 30
  • 48
  • Yes, I wasn't showing attention to my list-syntax when writing my question. You are correct, of course, but I was actually focusing on getting the `Type`, and copying your syntax still throws with a _"This is not a valid constant expression or custom attribute value"_ error. Can you repro or does this syntax work for you? – Abel Dec 06 '16 at 14:52
  • 1
    Works here: https://github.com/fscheck/FsCheck/blob/master/tests/FsCheck.Test/Runner.fs#L21 – Kurt Schelfthout Dec 06 '16 at 15:11
  • My bad. The first error was hidden by the tooltip error in F#, and was _"The type 'MyArb' is not defined"_ I had `Tests.Arithmetic.MyArb`, which exists, but apparently when using `typeof`, you cannot use the FQN??? Either way, when I simplified it to `typeof` it "just worked". Fooled by my own foolishness ;). – Abel Dec 06 '16 at 15:16
  • 1
    Indeed the FQN does not work generally to refer to a type that lives in the same namespace. That's an interesting find. I think that's because the compiler tries to resolve the name from within its own namespace (i.e. it tries `Test.Test.Arithmetic.MyArb`). But then I'd expect that prefixing `global` would work (it doesn't). I have opened an issue: https://github.com/Microsoft/visualfsharp/issues/1957 – Kurt Schelfthout Dec 07 '16 at 09:37