diff --git a/.appveyor.yml b/.appveyor.yml index 5b05644..fe9c091 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -1,6 +1,6 @@ environment: matrix: - - julia_version: 1.0 + - julia_version: 1.5 - julia_version: latest platform: diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f23752d..f12f693 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,7 +10,7 @@ jobs: fail-fast: false matrix: version: - - '1.0' + - '1.5' - 'nightly' os: - ubuntu-latest diff --git a/NEWS.md b/NEWS.md index 1c7da35..ca35a5b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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. diff --git a/Project.toml b/Project.toml index 86a250c..1c3a138 100644 --- a/Project.toml +++ b/Project.toml @@ -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"] diff --git a/docs/src/manual.md b/docs/src/manual.md index 5983c58..b04c3af 100644 --- a/docs/src/manual.md +++ b/docs/src/manual.md @@ -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 diff --git a/src/Parameters.jl b/src/Parameters.jl index da6663d..47945ff 100644 --- a/src/Parameters.jl +++ b/src/Parameters.jl @@ -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) @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index 79e9286..eb7a3a2 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,4 +1,4 @@ -using Parameters, Test, Markdown, REPL +using Parameters, Test, Markdown, REPL, SmartAsserts # reconstruct a8679 = (a=1, b=2) @@ -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