|
4 | 4 | x = 4 |
5 | 5 | """ |
6 | 6 |
|
7 | | - filename = tempname() |
8 | | - try |
9 | | - open(filename, "w") do file |
10 | | - write(file, example) |
11 | | - end |
| 7 | + mktemp() do path, io |
| 8 | + write(io, example) |
| 9 | + close(io) |
12 | 10 |
|
13 | 11 | # Use `@trixi_testset`, which wraps code in a temporary module, and call |
14 | 12 | # `trixi_include` with `@__MODULE__` in order to isolate this test. |
15 | | - @test_nowarn_mod trixi_include(@__MODULE__, filename) |
| 13 | + @test_nowarn_mod trixi_include(@__MODULE__, path) |
16 | 14 | @test @isdefined x |
17 | 15 | @test x == 4 |
18 | 16 |
|
19 | | - @test_nowarn_mod trixi_include(@__MODULE__, filename, x = 7) |
| 17 | + @test_nowarn_mod trixi_include(@__MODULE__, path, x = 7) |
20 | 18 |
|
21 | 19 | @test x == 7 |
22 | 20 |
|
23 | 21 | # Verify default version (that includes in `Main`) |
24 | | - @test_nowarn_mod trixi_include(filename, x = 11) |
| 22 | + @test_nowarn_mod trixi_include(path, x = 11) |
25 | 23 | @test Main.x == 11 |
26 | 24 |
|
27 | 25 | @test_throws "assignment `y` not found in expression" trixi_include(@__MODULE__, |
28 | | - filename, |
| 26 | + path, |
29 | 27 | y = 3) |
30 | | - finally |
31 | | - rm(filename, force = true) |
32 | 28 | end |
33 | 29 | end |
34 | 30 |
|
|
40 | 36 | x = solve() |
41 | 37 | """ |
42 | 38 |
|
43 | | - filename = tempname() |
44 | | - try |
45 | | - open(filename, "w") do file |
46 | | - write(file, example) |
47 | | - end |
| 39 | + mktemp() do path, io |
| 40 | + write(io, example) |
| 41 | + close(io) |
48 | 42 |
|
49 | 43 | # Use `@trixi_testset`, which wraps code in a temporary module, and call |
50 | 44 | # `trixi_include` with `@__MODULE__` in order to isolate this test. |
51 | 45 | @test_throws "no method matching solve(; maxiters" trixi_include(@__MODULE__, |
52 | | - filename) |
| 46 | + path) |
53 | 47 |
|
54 | 48 | @test_throws "no method matching solve(; maxiters" trixi_include(@__MODULE__, |
55 | | - filename, |
| 49 | + path, |
56 | 50 | maxiters = 3) |
57 | | - finally |
58 | | - rm(filename, force = true) |
59 | 51 | end |
60 | 52 | end |
61 | 53 |
|
|
81 | 73 | y = solve(; maxiters=0) |
82 | 74 | """ |
83 | 75 |
|
84 | | - filename1 = tempname() |
85 | | - filename2 = tempname() |
86 | | - filename3 = tempname() |
87 | | - filename4 = tempname() |
88 | | - try |
89 | | - open(filename1, "w") do file |
90 | | - write(file, example1) |
91 | | - end |
92 | | - open(filename2, "w") do file |
93 | | - write(file, example2) |
94 | | - end |
95 | | - open(filename3, "w") do file |
96 | | - write(file, example3) |
97 | | - end |
98 | | - open(filename4, "w") do file |
99 | | - write(file, example4) |
| 76 | + mktemp() do path1, io1 |
| 77 | + write(io1, example1) |
| 78 | + close(io1) |
| 79 | + |
| 80 | + mktemp() do path2, io2 |
| 81 | + write(io2, example2) |
| 82 | + close(io2) |
| 83 | + |
| 84 | + mktemp() do path3, io3 |
| 85 | + write(io3, example3) |
| 86 | + close(io3) |
| 87 | + |
| 88 | + mktemp() do path4, io4 |
| 89 | + write(io4, example4) |
| 90 | + close(io4) |
| 91 | + |
| 92 | + # Use `@trixi_testset`, which wraps code in a temporary module, |
| 93 | + # and call `Base.include` and `trixi_include` with `@__MODULE__` |
| 94 | + # in order to isolate this test. |
| 95 | + Base.include(@__MODULE__, path1) |
| 96 | + @test_nowarn_mod trixi_include(@__MODULE__, path2) |
| 97 | + @test @isdefined x |
| 98 | + # This is the default `maxiters` inserted by `trixi_include` |
| 99 | + @test x == 10^5 |
| 100 | + |
| 101 | + @test_nowarn_mod trixi_include(@__MODULE__, path2, maxiters = 7) |
| 102 | + # Test that `maxiters` got overwritten |
| 103 | + @test x == 7 |
| 104 | + |
| 105 | + # Verify that existing `maxiters` is added exactly once in the |
| 106 | + # following cases: |
| 107 | + # case 1) `maxiters` is *before* semicolon in included file |
| 108 | + @test_nowarn_mod trixi_include(@__MODULE__, path3, maxiters = 11) |
| 109 | + @test y == 11 |
| 110 | + # case 2) `maxiters` is *after* semicolon in included file |
| 111 | + @test_nowarn_mod trixi_include(@__MODULE__, path3, maxiters = 14) |
| 112 | + @test y == 14 |
| 113 | + end |
| 114 | + end |
100 | 115 | end |
| 116 | + end |
| 117 | + end |
| 118 | +end |
| 119 | + |
| 120 | +@trixi_testset "`trixi_include_changeprecision`" begin |
| 121 | + @trixi_testset "Basic" begin |
| 122 | + example = """ |
| 123 | + x = 4.0 |
| 124 | + y = zeros(3) |
| 125 | + """ |
| 126 | + |
| 127 | + mktemp() do path, io |
| 128 | + write(io, example) |
| 129 | + close(io) |
101 | 130 |
|
102 | 131 | # Use `@trixi_testset`, which wraps code in a temporary module, and call |
103 | | - # `Base.include` and `trixi_include` with `@__MODULE__` in order to isolate this test. |
104 | | - Base.include(@__MODULE__, filename1) |
105 | | - @test_nowarn_mod trixi_include(@__MODULE__, filename2) |
| 132 | + # `trixi_include_changeprecision` with `@__MODULE__` in order to isolate this test. |
| 133 | + @test_nowarn_mod trixi_include_changeprecision(Float32, @__MODULE__, path) |
106 | 134 | @test @isdefined x |
107 | | - # This is the default `maxiters` inserted by `trixi_include` |
108 | | - @test x == 10^5 |
| 135 | + @test x == 4 |
| 136 | + @test typeof(x) == Float32 |
| 137 | + @test @isdefined y |
| 138 | + @test eltype(y) == Float32 |
| 139 | + |
| 140 | + # Manually overwritten assignments are also changed |
| 141 | + @test_nowarn_mod trixi_include_changeprecision(Float32, @__MODULE__, path, |
| 142 | + x = 7.0) |
109 | 143 |
|
110 | | - @test_nowarn_mod trixi_include(@__MODULE__, filename2, |
111 | | - maxiters = 7) |
112 | | - # Test that `maxiters` got overwritten |
113 | 144 | @test x == 7 |
| 145 | + @test typeof(x) == Float32 |
| 146 | + |
| 147 | + # Verify default version (that includes in `Main`) |
| 148 | + @test_nowarn_mod trixi_include_changeprecision(Float32, path, x = 11.0) |
| 149 | + @test Main.x == 11 |
| 150 | + @test typeof(Main.x) == Float32 |
| 151 | + end |
| 152 | + end |
| 153 | + |
| 154 | + @trixi_testset "Recursive" begin |
| 155 | + example1 = """ |
| 156 | + x = 4.0 |
| 157 | + y = zeros(3) |
| 158 | + """ |
114 | 159 |
|
115 | | - # Verify that adding `maxiters` to `maxiters` results in exactly one of them |
116 | | - # case 1) `maxiters` is *before* semicolon in included file |
117 | | - @test_nowarn_mod trixi_include(@__MODULE__, filename3, maxiters = 11) |
118 | | - @test y == 11 |
119 | | - # case 2) `maxiters` is *after* semicolon in included file |
120 | | - @test_nowarn_mod trixi_include(@__MODULE__, filename3, maxiters = 14) |
121 | | - @test y == 14 |
122 | | - finally |
123 | | - rm(filename1, force = true) |
124 | | - rm(filename2, force = true) |
125 | | - rm(filename3, force = true) |
126 | | - rm(filename4, force = true) |
| 160 | + mktemp() do path1, io1 |
| 161 | + write(io1, example1) |
| 162 | + close(io1) |
| 163 | + |
| 164 | + # Use raw string to allow backslashes in Windows paths |
| 165 | + example2 = """ |
| 166 | + trixi_include(@__MODULE__, raw"$path1", x = 7.0) |
| 167 | + """ |
| 168 | + |
| 169 | + mktemp() do path2, io2 |
| 170 | + write(io2, example2) |
| 171 | + close(io2) |
| 172 | + |
| 173 | + # Use `@trixi_testset`, which wraps code in a temporary module, and call |
| 174 | + # `trixi_include_changeprecision` with `@__MODULE__` in order to isolate this test. |
| 175 | + @test_nowarn_mod trixi_include_changeprecision(Float32, @__MODULE__, path2) |
| 176 | + @test @isdefined x |
| 177 | + @test x == 7 |
| 178 | + @test typeof(x) == Float32 |
| 179 | + @test @isdefined y |
| 180 | + @test eltype(y) == Float32 |
| 181 | + end |
127 | 182 | end |
128 | 183 | end |
129 | 184 | end |
0 commit comments