Skip to content

Commit

Permalink
Moving Accuracy static member to module – workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
haf committed Jan 25, 2017
1 parent 7c4b1ec commit 0d6a5e3
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .semver
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
:major: 3
:minor: 2
:patch: 0
:patch: 1
:special: ''
:metadata: ''
12 changes: 11 additions & 1 deletion Expecto.Sample/Expecto.Sample.fs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,18 @@ let tests =
Expect.equal 1 1 "1=1"
do! async.Zero ()
}

test "Should be close" {
let actual, expected = 41.5621, 41.5620
Expect.floatClose Accuracy.medium actual expected "Should be close within 5 sig figs (approx)"
}

test "Should not be close enough (should fail)" {
let actual, expected = 41.562, 41.563
Expect.floatClose Accuracy.medium actual expected "Should be close within 5 sig figs (approx)"
}
]

[<EntryPoint>]
let main argv =
Tests.runTestsInAssembly defaultConfig argv
Tests.runTestsInAssembly defaultConfig argv
42 changes: 32 additions & 10 deletions Expecto/Expecto.fs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ open System.Reflection
open System.Diagnostics
open System.Threading

// TODO: move to internal
type SourceLocation =
{ sourcePath : string
lineNumber : int }
Expand Down Expand Up @@ -54,9 +55,13 @@ type Test =
/// Require sequenced for a Test (list or test code).
| Sequenced of Test

// TODO: move to internal namespace
type ExpectoException(msg) = inherit Exception(msg)
// TODO: move to internal namespace
type AssertException(msg) = inherit ExpectoException(msg)
// TODO: move to internal namespace
type FailedException(msg) = inherit ExpectoException(msg)
// TODO: move to internal namespace
type IgnoreException(msg) = inherit ExpectoException(msg)

/// Marks a top-level test for scanning
Expand All @@ -77,13 +82,16 @@ type PTestsAttribute() = inherit Attribute()
[<AttributeUsage(AttributeTargets.Method ||| AttributeTargets.Property ||| AttributeTargets.Field)>]
type FTestsAttribute() = inherit Attribute()

// TODO: make internal
module Async =
// TODO: make internal
let map fn a =
async {
let! v = a
return fn v
}

// TODO: make internal
let bind fn a =
async.Bind(a, fn)

Expand Down Expand Up @@ -135,6 +143,7 @@ module Async =
Async.AwaitWaitHandle handle.WaitHandle
|> map (fun _ -> results)

// TODO: make internal
module Helpers =

let inline fst3 (a,_,_) = a
Expand Down Expand Up @@ -208,6 +217,7 @@ module Helpers =
|> List.map snd
|> List.tryFind (fun _ -> true)

// TODO: move to internal
type FlatTest =
{ name : string
test : TestCode
Expand Down Expand Up @@ -310,6 +320,7 @@ module Test =
raise <| AssertException(sprintf "Timeout (%A)" ts)
} |> Async

// TODO: make internal?
module Impl =
open Expecto.Logging
open Expecto.Logging.Message
Expand Down Expand Up @@ -896,6 +907,7 @@ module Impl =
| [] -> None
| x -> Some (TestList (x, Normal))

// TODO: make internal
let testFromType =
let asMembers x = Seq.map (fun m -> m :> MemberInfo) x
let bindingFlags = BindingFlags.Public ||| BindingFlags.Static
Expand Down Expand Up @@ -1079,6 +1091,7 @@ module Tests =
Seq.map (fun (name, partialTest) ->
testCase name (partialTest param))

// TODO: docs
type TestCaseBuilder(name, focusState) =
member x.TryFinally(f, compensation) =
try
Expand Down Expand Up @@ -1107,10 +1120,13 @@ module Tests =
| Focused -> ftestCase name f
| Pending -> ptestCase name f

// TODO: docs
let inline test name =
TestCaseBuilder (name, Normal)
// TODO: docs
let inline ftest name =
TestCaseBuilder (name, Focused)
// TODO: docs
let inline ptest name =
TestCaseBuilder (name, Pending)

Expand All @@ -1132,6 +1148,7 @@ module Tests =
verbosity = LogLevel.Info
locate = fun _ -> SourceLocation.empty }

// TODO: docs
type CLIArguments =
| Sequenced
| Parallel
Expand Down Expand Up @@ -1164,6 +1181,7 @@ module Tests =
| Version -> "Prints out version information."
| Summary_Location -> "Prints out summary after all tests are finished including their source code location"

// TODO: docs
[<CompilationRepresentation (CompilationRepresentationFlags.ModuleSuffix)>]
module ExpectoConfig =

Expand Down Expand Up @@ -1260,19 +1278,23 @@ module Tests =
|> Option.orDefault (TestList ([], Normal))
|> runTestsWithArgs config args


// TODO: docs
type Accuracy =
{
absolute: float
relative: float
}
static member low = {absolute=1e-6; relative=1e-3}
static member medium = {absolute=1e-8; relative=1e-5}
static member high = {absolute=1e-10; relative=1e-7}
static member veryHigh = {absolute=1e-12; relative=1e-9}
{ absolute: float
relative: float }

// TODO: docs
[<CompilationRepresentation (CompilationRepresentationFlags.ModuleSuffix)>]
module Accuracy =
let inline areCloseLhs a b = abs(a-b)
let inline areCloseRhs m a b = m.absolute + m.relative * max (abs a) (abs b)
let inline areClose m a b = areCloseLhs a b <= areCloseRhs m a b
let inline areClose m a b = areCloseLhs a b <= areCloseRhs m a b

let low =
{absolute=1e-6; relative=1e-3}
let medium =
{absolute=1e-8; relative=1e-5}
let high =
{absolute=1e-10; relative=1e-7}
let veryHigh =
{absolute=1e-12; relative=1e-9}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,8 @@ let properties =
testPropertyWithConfig config "Product is distributive over addition" <|
fun a b c ->
a * (b + c) = a * b + a * c
// you can also focus on a StdGen seed value (failing test will give you this)
// you can also focus on a StdGen seed value (failing test will give you this)
ftestProperty (12345,67890) "Focused on seed" <| fun a b ->
a + b = b + a
]
Expand Down Expand Up @@ -449,7 +449,7 @@ This module is your main entry-point when asserting.
available are: `Accuracy.low = {absolute=1e-6; relative=1e-3}`,
`Accuracy.medium = {absolute=1e-8; relative=1e-5}`,
`Accuracy.high = {absolute=1e-10; relative=1e-7}`,
`Accuracy.veryHigh = {absolute=1e-12; relative=1e-9}`.
`Accuracy.veryHigh = {absolute=1e-12; relative=1e-9}`.
- `sequenceStarts` - Expect the sequence `subject` to start with `prefix`. If
it does not then fail with `format` as an error message together with a
description of `subject` and `prefix`.
Expand Down

0 comments on commit 0d6a5e3

Please sign in to comment.