Skip to content

Commit

Permalink
Add support for SmartAsserts.jl (#152)
Browse files Browse the repository at this point in the history
Add support for SmartCheck.jl (closes #34)

Update minimum Julia version to 1.5
  • Loading branch information
Zentrik authored Jun 20, 2023
1 parent e55b025 commit 501b47b
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
environment:
matrix:
- julia_version: 1.0
- julia_version: 1.5
- julia_version: latest

platform:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
fail-fast: false
matrix:
version:
- '1.0'
- '1.5'
- 'nightly'
os:
- ubuntu-latest
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 2023-06-20

Dropped Julia 1.0-1.4 support.

Added support for `@smart_assert` from [SmartAsserts.jl](https://github.com/MrVPlusOne/SmartAsserts.jl) in type-body.

# 2021-01-22
Added `@consts` macro to define a block of constants.

Expand Down
5 changes: 3 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ UnPack = "3a884ed6-31ef-47d7-9d2a-63182c4928ed"
[compat]
OrderedCollections = "1"
UnPack = "0.1, 1.0"
julia = "1"
julia = "1.5"

[extras]
Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"
REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
SmartAsserts = "56560af0-ab70-43fe-a531-155d81972b00"

[targets]
test = ["REPL", "Test", "Markdown"]
test = ["REPL", "Test", "Markdown", "SmartAsserts"]
4 changes: 3 additions & 1 deletion docs/src/manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ pp3 = PhysicalPara(pp2; cw=.11e-7, rw=100.)
pp4 = PhysicalPara(1,2,3,4,5,6)
```

To enforce constraints on the values, it's possible to use `@assert`s
To enforce constraints on the values, it's possible to use `@assert`s or `@smart_assert`[^1]
straight inside the type-def. (As usual, for mutables these
asserts can be violated by updating the fields after type construction.)

[^1]: `@smart_assert` is defined in [SmartAsserts.jl](https://github.com/MrVPlusOne/SmartAsserts.jl).

```julia
@with_kw struct PhysicalPara2{R}
rw::R = 1000.; @assert rw>0
Expand Down
4 changes: 2 additions & 2 deletions src/Parameters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ function with_kw(typedef, mod::Module, withshow=true)
push!(lns2, l)
continue
end
if l.head==:macrocall && l.args[1]!=Symbol("@assert")
if l.head==:macrocall && l.args[1] != Symbol("@assert") && l.args[1] != Symbol("@smart_assert")
tmp = macroexpand(mod, l)
if tmp.head==:block
llns = Lines(tmp)
Expand Down Expand Up @@ -456,7 +456,7 @@ function with_kw(typedef, mod::Module, withshow=true)
# unwrap-macro
push!(unpack_vars, decolon2(fld))
end
elseif l.head==:macrocall && l.args[1]==Symbol("@assert")
elseif l.head==:macrocall && (l.args[1]==Symbol("@assert") || l.args[1]==Symbol("@smart_assert"))
# store all asserts
push!(asserts, l)
elseif l.head==:function # inner constructor
Expand Down
30 changes: 29 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Parameters, Test, Markdown, REPL
using Parameters, Test, Markdown, REPL, SmartAsserts

# reconstruct
a8679 = (a=1, b=2)
Expand Down Expand Up @@ -378,6 +378,34 @@ end
@test_throws AssertionError MT12a([1,2])
@test MT12a([1]).a==MT12a(a=[1]).a

### Smart Assertions

@with_kw struct MT12Smart
a=5; @smart_assert a>=5
b
@smart_assert b>a
end

@test_throws AssertionError MT12Smart(b=2)
@test_throws AssertionError MT12Smart(a=1,b=2)
@test MT12Smart(b=6)==MT12Smart(5,6)

# only asserts allowed if no inner constructors
@test_throws ErrorException Parameters.with_kw(:(struct MT13Smart
a=5;
@smart_assert a>=5
MT13Smart(a) = new(8)
end),
@__MODULE__)

# issue #29: assertions with parameterized types
@with_kw struct MT12aSmart{R}
a::Array{R,1}
@smart_assert 1 == length(a)
end
@test_throws AssertionError MT12aSmart([1,2])
@test MT12aSmart([1]).a==MT12aSmart(a=[1]).a

####
# issue 10: infer type parameters from kw-args
@with_kw struct I10{T} @deftype Int
Expand Down

0 comments on commit 501b47b

Please sign in to comment.