| 
1 | 1 | module TestProgressMacro  | 
2 | 2 | 
 
  | 
3 |  | -using ProgressLogging: @progress  | 
 | 3 | +using ProgressLogging: @progress, ProgressLevel  | 
4 | 4 | using Test  | 
 | 5 | +using Test: collect_test_logs  | 
 | 6 | +using OffsetArrays  | 
5 | 7 | 
 
  | 
6 |  | -let i = 0, x  | 
7 |  | -    x = @progress for _ = 1:100  | 
8 |  | -        i += 1  | 
9 |  | -    end  | 
10 |  | -    @test i == 100  | 
11 |  | -    @test x == nothing  | 
12 |  | -end  | 
 | 8 | +@testset "@progress" begin  | 
 | 9 | +  let i = 0, x  | 
 | 10 | +      x = @progress for _ = 1:100  | 
 | 11 | +          i += 1  | 
 | 12 | +      end  | 
 | 13 | +      @test i == 100  | 
 | 14 | +      @test x == nothing  | 
 | 15 | +  end  | 
13 | 16 | 
 
  | 
14 |  | -let i = 0, r = -50:10:50, x  | 
15 |  | -    x = @progress for _ in r  | 
16 |  | -        i += 1  | 
17 |  | -    end  | 
18 |  | -    @test i == 11  | 
19 |  | -    @test x == nothing  | 
20 |  | -end  | 
 | 17 | +  let i = 0, r = -50:10:50, x  | 
 | 18 | +      x = @progress for _ in r  | 
 | 19 | +          i += 1  | 
 | 20 | +      end  | 
 | 21 | +      @test i == 11  | 
 | 22 | +      @test x == nothing  | 
 | 23 | +  end  | 
21 | 24 | 
 
  | 
22 |  | -let i = 0, x  | 
23 |  | -    x = @progress "named" for _ = 1:100  | 
24 |  | -        i += 1  | 
25 |  | -    end  | 
26 |  | -    @test i == 100  | 
27 |  | -    @test x == nothing  | 
28 |  | -end  | 
 | 25 | +  let i = 0, x  | 
 | 26 | +      x = @progress "named" for _ = 1:100  | 
 | 27 | +          i += 1  | 
 | 28 | +      end  | 
 | 29 | +      @test i == 100  | 
 | 30 | +      @test x == nothing  | 
 | 31 | +  end  | 
29 | 32 | 
 
  | 
30 |  | -let i = 0, j = 0, x  | 
31 |  | -    x = @progress for _ = 1:10, __ = 1:20  | 
32 |  | -        i += 1  | 
33 |  | -    end  | 
34 |  | -    @test i == 200  | 
35 |  | -    @test x == nothing  | 
36 |  | -end  | 
 | 33 | +  let i = 0, j = 0, x  | 
 | 34 | +      x = @progress for _ = 1:10, __ = 1:20  | 
 | 35 | +          i += 1  | 
 | 36 | +      end  | 
 | 37 | +      @test i == 200  | 
 | 38 | +      @test x == nothing  | 
 | 39 | +  end  | 
 | 40 | + | 
 | 41 | +  let i = 0, j = 0, x  | 
 | 42 | +      bar = "bar"  | 
 | 43 | +      x = @progress "foo $bar" for _ = 1:10  | 
 | 44 | +          i += 1  | 
 | 45 | +      end  | 
 | 46 | +      @test i == 10  | 
 | 47 | +      @test x == nothing  | 
 | 48 | +  end  | 
 | 49 | + | 
 | 50 | +  let x, y  | 
 | 51 | +      x = @progress y = [i + 3j for i = 1:3, j = 1:4]  | 
 | 52 | +      @test y == reshape(4:15, 3, 4)  | 
 | 53 | +      @test x == y  | 
 | 54 | +  end  | 
37 | 55 | 
 
  | 
38 |  | -let i = 0, j = 0, x  | 
39 |  | -    bar = "bar"  | 
40 |  | -    x = @progress "foo $bar" for _ = 1:10  | 
41 |  | -        i += 1  | 
 | 56 | +  let a = [], x  | 
 | 57 | +      x = @progress for i = 1:3, j in [-5, -2, -1, 8]  | 
 | 58 | +          j > 0 && continue  | 
 | 59 | +          push!(a, (i, j))  | 
 | 60 | +          i > 1 && break  | 
 | 61 | +      end  | 
 | 62 | +      @test a == [(1, -5), (1, -2), (1, -1), (2, -5)]  | 
 | 63 | +      @test x == nothing  | 
 | 64 | +  end  | 
 | 65 | + | 
 | 66 | +  # Multi-dimensional arrays in comprehension and offset axes  | 
 | 67 | +  let off1 = -2, off2 = 21  | 
 | 68 | +    v1 = OffsetArray(2:3, off1)  | 
 | 69 | +    v2 = OffsetArray(-1:2, off2)  | 
 | 70 | +    logs, _ = collect_test_logs(min_level = ProgressLevel) do  | 
 | 71 | +      x = @progress y = [i*j for i in v1, j in v2]  | 
 | 72 | +      @test x == y == OffsetArray([-2 0 2 4; -3 0 3 6], off1, off2)  | 
42 | 73 |     end  | 
43 |  | -    @test i == 10  | 
44 |  | -    @test x == nothing  | 
45 |  | -end  | 
 | 74 | +    @test isequal(  | 
 | 75 | +        [l.kwargs[:progress] for l in logs],  | 
 | 76 | +        [nothing; (1:8)./8; "done"],  | 
 | 77 | +    )  | 
 | 78 | +    m = OffsetArray(reshape(2:7,2,3), off1, off2)  | 
 | 79 | +    x = @progress y = [i*j for i in v1, j in m]  | 
 | 80 | +    @test x == y == [i*j for i in v1, j in m]  | 
 | 81 | +  end  | 
46 | 82 | 
 
  | 
47 |  | -let x, y  | 
48 |  | -    x = @progress y = [i + 3j for i = 1:3, j = 1:4]  | 
49 |  | -    @test y == reshape(4:15, 3, 4)  | 
50 |  | -    @test x == y  | 
51 |  | -end  | 
 | 83 | +  # non-indexable iterables with axes  | 
 | 84 | +  @testset "non-indexable" for off in (0,10)  | 
 | 85 | +    let r = OffsetVector(1:5, off)  | 
 | 86 | +      x1 = @progress y1 = [i for i in (x^2 for x in r)]  | 
 | 87 | +      x2 = @progress y2 = [i for i in zip(r,r)]  | 
 | 88 | +      @test x1 == y1 == r.^2  | 
 | 89 | +      @test x2 == y2 == collect(zip(r,r))  | 
52 | 90 | 
 
  | 
53 |  | -let a = [], x  | 
54 |  | -    x = @progress for i = 1:3, j in [-5, -2, -1, 8]  | 
55 |  | -        j > 0 && continue  | 
56 |  | -        push!(a, (i, j))  | 
57 |  | -        i > 1 && break  | 
 | 91 | +      y1, y2 = [], []  | 
 | 92 | +      x1 = @progress for i in (x^2 for x in r)  | 
 | 93 | +        push!(y1, i)  | 
 | 94 | +      end  | 
 | 95 | +      x2 = @progress for i in zip(r,r)  | 
 | 96 | +        push!(y2, i)  | 
 | 97 | +      end  | 
 | 98 | +      @test x1 == x2 == nothing  | 
 | 99 | +      @test OffsetVector(y1,off) == r.^2  | 
 | 100 | +      @test OffsetVector(y2,off) == collect(zip(r,r))  | 
58 | 101 |     end  | 
59 |  | -    @test a == [(1, -5), (1, -2), (1, -1), (2, -5)]  | 
60 |  | -    @test x == nothing  | 
 | 102 | +  end  | 
61 | 103 | end  | 
62 | 104 | 
 
  | 
63 | 105 | end  # module  | 
0 commit comments