Skip to content

Commit 501b47b

Browse files
authored
Add support for SmartAsserts.jl (#152)
Add support for SmartCheck.jl (closes #34) Update minimum Julia version to 1.5
1 parent e55b025 commit 501b47b

File tree

7 files changed

+45
-8
lines changed

7 files changed

+45
-8
lines changed

.appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
environment:
22
matrix:
3-
- julia_version: 1.0
3+
- julia_version: 1.5
44
- julia_version: latest
55

66
platform:

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
fail-fast: false
1111
matrix:
1212
version:
13-
- '1.0'
13+
- '1.5'
1414
- 'nightly'
1515
os:
1616
- ubuntu-latest

NEWS.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 2023-06-20
2+
3+
Dropped Julia 1.0-1.4 support.
4+
5+
Added support for `@smart_assert` from [SmartAsserts.jl](https://github.com/MrVPlusOne/SmartAsserts.jl) in type-body.
6+
17
# 2021-01-22
28
Added `@consts` macro to define a block of constants.
39

Project.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ UnPack = "3a884ed6-31ef-47d7-9d2a-63182c4928ed"
1010
[compat]
1111
OrderedCollections = "1"
1212
UnPack = "0.1, 1.0"
13-
julia = "1"
13+
julia = "1.5"
1414

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

2021
[targets]
21-
test = ["REPL", "Test", "Markdown"]
22+
test = ["REPL", "Test", "Markdown", "SmartAsserts"]

docs/src/manual.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ pp3 = PhysicalPara(pp2; cw=.11e-7, rw=100.)
3636
pp4 = PhysicalPara(1,2,3,4,5,6)
3737
```
3838

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

43+
[^1]: `@smart_assert` is defined in [SmartAsserts.jl](https://github.com/MrVPlusOne/SmartAsserts.jl).
44+
4345
```julia
4446
@with_kw struct PhysicalPara2{R}
4547
rw::R = 1000.; @assert rw>0

src/Parameters.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ function with_kw(typedef, mod::Module, withshow=true)
390390
push!(lns2, l)
391391
continue
392392
end
393-
if l.head==:macrocall && l.args[1]!=Symbol("@assert")
393+
if l.head==:macrocall && l.args[1] != Symbol("@assert") && l.args[1] != Symbol("@smart_assert")
394394
tmp = macroexpand(mod, l)
395395
if tmp.head==:block
396396
llns = Lines(tmp)
@@ -456,7 +456,7 @@ function with_kw(typedef, mod::Module, withshow=true)
456456
# unwrap-macro
457457
push!(unpack_vars, decolon2(fld))
458458
end
459-
elseif l.head==:macrocall && l.args[1]==Symbol("@assert")
459+
elseif l.head==:macrocall && (l.args[1]==Symbol("@assert") || l.args[1]==Symbol("@smart_assert"))
460460
# store all asserts
461461
push!(asserts, l)
462462
elseif l.head==:function # inner constructor

test/runtests.jl

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Parameters, Test, Markdown, REPL
1+
using Parameters, Test, Markdown, REPL, SmartAsserts
22

33
# reconstruct
44
a8679 = (a=1, b=2)
@@ -378,6 +378,34 @@ end
378378
@test_throws AssertionError MT12a([1,2])
379379
@test MT12a([1]).a==MT12a(a=[1]).a
380380

381+
### Smart Assertions
382+
383+
@with_kw struct MT12Smart
384+
a=5; @smart_assert a>=5
385+
b
386+
@smart_assert b>a
387+
end
388+
389+
@test_throws AssertionError MT12Smart(b=2)
390+
@test_throws AssertionError MT12Smart(a=1,b=2)
391+
@test MT12Smart(b=6)==MT12Smart(5,6)
392+
393+
# only asserts allowed if no inner constructors
394+
@test_throws ErrorException Parameters.with_kw(:(struct MT13Smart
395+
a=5;
396+
@smart_assert a>=5
397+
MT13Smart(a) = new(8)
398+
end),
399+
@__MODULE__)
400+
401+
# issue #29: assertions with parameterized types
402+
@with_kw struct MT12aSmart{R}
403+
a::Array{R,1}
404+
@smart_assert 1 == length(a)
405+
end
406+
@test_throws AssertionError MT12aSmart([1,2])
407+
@test MT12aSmart([1]).a==MT12aSmart(a=[1]).a
408+
381409
####
382410
# issue 10: infer type parameters from kw-args
383411
@with_kw struct I10{T} @deftype Int

0 commit comments

Comments
 (0)