|
| 1 | +using Test |
| 2 | +using StateSignals |
| 3 | + |
| 4 | +@testset "Basic Resource Operations" begin |
| 5 | + # Test resource creation |
| 6 | + s = Signal(0) |
| 7 | + r = Resource([s]) do |
| 8 | + s() * 2 |
| 9 | + end |
| 10 | + |
| 11 | + @test r() === nothing # Initial value should be nothing |
| 12 | + @test r.isloading() == false # Should not be loading initially |
| 13 | + |
| 14 | + # Test resource loading |
| 15 | + s(5) |
| 16 | + sleep(0.1) |
| 17 | + @test r() == 10 # Should compute s() * 2 |
| 18 | + @test r.isloading() == false # Should be done loading |
| 19 | +end |
| 20 | + |
| 21 | +@testset "Resource Loading State" begin |
| 22 | + s = Signal(0) |
| 23 | + loading_states = [] |
| 24 | + |
| 25 | + r = Resource([s]) do |
| 26 | + # Track loading state changes |
| 27 | + push!(loading_states, r.isloading()) |
| 28 | + sleep(0.1) # Simulate async work |
| 29 | + s() * 2 |
| 30 | + end |
| 31 | + |
| 32 | + s(5) |
| 33 | + sleep(0.2) # Wait for async operation |
| 34 | + |
| 35 | + @test loading_states[1] == true # Should be loading during computation |
| 36 | + @test r.isloading() == false # Should be done loading after computation |
| 37 | + @test r() == 10 # Should have correct final value |
| 38 | +end |
| 39 | + |
| 40 | +@testset "Multiple Dependencies" begin |
| 41 | + a = Signal(2) |
| 42 | + b = Signal(3) |
| 43 | + |
| 44 | + r = Resource([a, b]) do |
| 45 | + a() * b() |
| 46 | + end |
| 47 | + |
| 48 | + @test r() === nothing # Initial value |
| 49 | + |
| 50 | + a(4) # Should trigger resource update |
| 51 | + sleep(0.1) |
| 52 | + @test r() == 12 # 4 * 3 |
| 53 | + |
| 54 | + b(5) # Should trigger another update |
| 55 | + sleep(0.1) |
| 56 | + @test r() == 20 # 4 * 5 |
| 57 | +end |
| 58 | + |
| 59 | +@testset "Error Handling" begin |
| 60 | + s = Signal(0) |
| 61 | + r = Resource([s]) do |
| 62 | + if s() < 0 |
| 63 | + error("Value cannot be negative") |
| 64 | + end |
| 65 | + s() * 2 |
| 66 | + end |
| 67 | + |
| 68 | + # Initial state |
| 69 | + @test r() === nothing |
| 70 | + @test r.error() === nothing |
| 71 | + @test r.isloading() == false |
| 72 | + |
| 73 | + # Test error condition |
| 74 | + s(-1) |
| 75 | + sleep(0.1) |
| 76 | + @test r() === nothing # Value should be reset |
| 77 | + @test r.error() isa ErrorException # Should have captured the error |
| 78 | + @test r.error().msg == "Value cannot be negative" |
| 79 | + @test r.isloading() == false # Should not be loading after error |
| 80 | +end |
0 commit comments