Skip to content

Commit

Permalink
Add within and within_inclusive assertion helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
jbn committed Oct 14, 2015
1 parent a020ad4 commit f3166ea
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,17 @@ B = (1 + 1e-6)*A
#### `less_than`/
#### `less_than_or_equal`/
#### `less_than_or_equal`/
#### `greater_than_or_equal`
#### `greater_than_or_equal`/
#### `within`/
#### `within_inclusive`
Test inequality relationships between numbers.
```julia
@fact 1 --> less_than(2)
@fact 1 --> less_than_or_equal(1)
@fact 2 --> greater_than(1)
@fact 2 --> greater_than_or_equal(2)
@fact 2 --> within(1,3)
@fact 1 --> within_inclusive(1,2)
```

#### `anyof`
Expand Down
6 changes: 4 additions & 2 deletions src/FactCheck.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export @fact, @fact_throws, @pending,
roughly,
anyof,
less_than, less_than_or_equal,
greater_than, greater_than_or_equal
greater_than, greater_than_or_equal,
within, within_inclusive

const INDENT = " "

Expand Down Expand Up @@ -182,7 +183,8 @@ print_compact(s::Pending) = print_with_color(:yellow, "P")

const SPECIAL_FACTCHECK_FUNCTIONS =
Set([:not, :exactly, :roughly, :anyof,
:less_than, :less_than_or_equal, :greater_than, :greater_than_or_equal])
:less_than, :less_than_or_equal, :greater_than, :greater_than_or_equal,
:within, :within_inclusive])

@compat const FACTCHECK_FUN_NAMES =
Dict{Symbol,AbstractString}(
Expand Down
5 changes: 5 additions & 0 deletions src/helpers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,8 @@ greater_than(compared) = (compare) -> compare > compared

# greater_than_or_equal: Comparing two numbers
greater_than_or_equal(compared) = (compare) -> compare >= compared

within(lb, ub) = (compare) -> lb < compare < ub

within_inclusive(lb, ub) = (compare) -> lb <= compare <= ub

8 changes: 8 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,14 @@ facts("FactCheck assertion helper functions") do
@fact 2 --> greater_than_or_equal(1)
@fact 2 --> greater_than_or_equal(2)
end

context("within") do
@fact 2 --> within(1, 3)
end

context("within_inclusive") do
@fact 10 --> within_inclusive(10, 11)
end
end

exitstatus()
Expand Down

0 comments on commit f3166ea

Please sign in to comment.