5

I have some xunit tests I would like to layout as follows for readability:

[<Fact>] let ``Has Hash Code 2``()          = target.GetHashCode().ShouldBe 2
[<Fact>] let ``ToString yields two``()      = target.ToString().ShouldBe "two"
[<Fact>] let ``Has underlysing type int``() = target.GetUnderlyingType().ShouldBe typeof<int>

I'm getting a compiler warning on the let statements: "Possible incorrect indentation: this token is offside of context started at position %s. Try indenting this token further or using standard formatting conventions."

I tried #nowarn "lexfltTokenIsOffsideOfContextStartedEarlier" but that just generated another compiler warning "Invalid warning number".

There isn't a warning number listed for this error in https://github.com/fsharp/fsharp/blob/057dbf77df7355321c3c18137c2d552bdfa1272b/src/fsharp/FSComp.txt

Is there a way to suppress this warning?

Craig
  • 55
  • 7

2 Answers2

6

I apologize for answering in a style, "you don't need it", but this seems to be the case. Also, this question apparently becomes a FAQ for those who write Unit Tests in F#. Yet another case is for [<Literal>]'s.

Anyway, disabling warnings on a project- or even file-level seems to be a bad idea, unless you have a good reason to do that.

As far as I understood, you'd like to have the statements to be one-liners (e.g., [<Fact>] did not occupy an entire line). There are two ways to accomplish that in VS2013:

  1. Place the attribute inside let:

    let [<Fact>] ``Has Hash Code 2``()          = target.GetHashCode().ShouldBe 2
    
  2. Write a double-semicolon after the declaration:

    [<Fact>] let ``Has Hash Code 2``()          = target.GetHashCode().ShouldBe 2;;
    
Be Brave Be Like Ukraine
  • 7,596
  • 3
  • 42
  • 66
  • 1
    Thank you. I've gone with your option 1. Glad to hear there is a way to do it without disabling warnings. – Craig May 17 '15 at 01:07
  • I am just starting writing unit tests in F# on a C# code base as a way to learn F# and write more succinct and readable tests. Could you explain what you mean by "Yet another case is for []'s"? Could you also recommend any posts / articles with tips for newbies to F# unit tests? – Craig May 17 '15 at 01:12
  • @Craig, for example, libraries working with Web API often keep many **hard-coded constants**, e.g. partial URL's, numeric values, etc. They are usually marked with `[]` attribute as [described](https://msdn.microsoft.com/en-us/library/dd233193.aspx) in MSDN. If there are many of those, a developer may also like them to be one-liners, just likewise in your question, and they would have exactly the same problem *(note: MSDN article has the attribute in a separate line!)*. I mentioned it in my answer so that someone who searches the Web for a solution would find this answer. – Be Brave Be Like Ukraine May 17 '15 at 01:28
5

Try this:

#nowarn "0058"

To find out what the correct warning number is, just build the thing in command line (or in VS and then go to View -> Output -> Build), it will tell you. Here is what I see:

Program.fs(7,3): warning FS0058: Possible incorrect indentation: this token is offside of context started at position (5:25). Try indenting this token further or using standard formatting conventions.
Fyodor Soikin
  • 78,590
  • 9
  • 125
  • 172
  • Thank you. For some reason I don't get the portion of text "warning FS0058:" shown in my build output. – Craig May 17 '15 at 01:17
  • That's because you're looking in the Errors window, not build output. To get to the real build output, go to View -> Output, and then select "Build" in the dropdown. – Fyodor Soikin May 17 '15 at 02:25
  • Great. Thanks. I can see it there. – Craig May 17 '15 at 02:41
  • Looking in the build output, I can see a line in red ``4> C:\Program Files (x86)\Microsoft SDKs\F#\3.1\Framework\v4.0\fsc.exe -o:obj\Debug\TestFastLibValueObj.dll -g --debug:full --noframework --define:DEBUG --define:TRACE --doc:bin\Debug\TestFastLibValueObj.XML --optimize- --tailcalls- -r:"C:\Program Files (x86)\Reference Assemblies\Microsoft\FSharp\.NETFramework\v4.0\4.3.1.0\FSharp.Core.dll" -r:C:\Users\azuretools\Documents\GitHub\TaPrivate\CoreComponents\bin\Debug\LibBase.dll -`` etc. However it doesn't seem to give a specific error and the build complete's successfully. Can I ignore it? – Craig May 17 '15 at 02:43