我正在通过FParsec的教程,但调整代码以在xUnit下运行。运行此测试时:
open FParsec
open Xunit
exception ParseError of string
let parse parser input =
match run parser input with
| Success (result, _, _) -> result
| Failure (error, _, _) -> raise (ParseError error)
type UserState = unit
type Parser<'t> = Parser<'t, UserState>
let str s = pstring s
let floatBetweenBrackets:Parser<_> = str "[" >>. pfloat .>> str "]"
[<Fact>]
let ``Parsing [1.0] works`` () =
let r = parse floatBetweenBrackets "[1.0]"
Assert.Equal(1.0,r)
我失败了:
[23/10/17 13:31:53 Informational] ------ Run test started ------
[23/10/17 13:31:53 Informational] [xUnit.net 00:00:00.3299056] Received 1 results from 1 requests
[23/10/17 13:31:53 Informational] [xUnit.net 00:00:00.3306170] Starting: Experiments
[23/10/17 13:31:54 Error] [xUnit.net 00:00:00.5309115] fSharpExperiments.FParsecTest.Parsing [1.0] works [FAIL]
[23/10/17 13:31:54 Informational] [xUnit.net 00:00:00.5325011] System.NullReferenceException : Object reference not set to an instance of an object.
[23/10/17 13:31:54 Informational] [xUnit.net 00:00:00.5356227] Stack Trace:
[23/10/17 13:31:54 Informational] [xUnit.net 00:00:00.5360697] at FParsec.CharParsers.applyParser[Result,UserState](FSharpFunc`2 parser, CharStream`1 stream)
[23/10/17 13:31:54 Informational] [xUnit.net 00:00:00.5364446] at FParsec.CharParsers.run@97.Invoke(CharStream`1 stream)
[23/10/17 13:31:54 Informational] [xUnit.net 00:00:00.5367866] at FParsec.CharStream.ParseString[T,TUserState](String chars, Int32 index, Int32 length, FSharpFunc`2 parser, TUserState userState, String streamName)
[23/10/17 13:31:54 Informational] [xUnit.net 00:00:00.5371211] at FParsec.CharParsers.run[Result](FSharpFunc`2 parser, String string)
[23/10/17 13:31:54 Informational] [xUnit.net 00:00:00.5374329] C:\Users\jackc\Documents\Git\fsharpExperiments\Experiments\FParsec.fs(28,0): at fSharpExperiments.FParsecTest.parse[a](FSharpFunc`2 parser, String input)
[23/10/17 13:31:54 Informational] [xUnit.net 00:00:00.5377692] C:\Users\jackc\Documents\Git\fsharpExperiments\Experiments\FParsec.fs(54,0): at fSharpExperiments.FParsecTest.Parsing [1.0] works()
[23/10/17 13:31:54 Informational] [xUnit.net 00:00:00.5479057] Finished: Experiments
[23/10/17 13:31:54 Informational] ========== Run test finished: 1 run (0:00:00.604968) ==========
这很奇怪,因为如果我在测试函数之外执行此操作它会运行正常。
let r = parse floatBetweenBrackets "[1.0]"
Assert.Equal(1.0,r)
r为float = 1.0
且r = 1.0
为真。
我错过了什么?