|
| 1 | +@testset "Configuration" begin |
| 2 | + |
| 3 | + # parse_port_file |
| 4 | + @testset "parse_port_file" begin |
| 5 | + |
| 6 | + @testset "single entry" begin |
| 7 | + mktempdir() do dir |
| 8 | + path = joinpath(dir, "test.port") |
| 9 | + write(path, "{'e1': 1}") |
| 10 | + result = Concore.parse_port_file(path) |
| 11 | + @test result == Dict("e1" => 1) |
| 12 | + end |
| 13 | + end |
| 14 | + |
| 15 | + @testset "multiple entries" begin |
| 16 | + mktempdir() do dir |
| 17 | + path = joinpath(dir, "test.port") |
| 18 | + write(path, "{'e1': 1, 'e2': 2, 'e3': 3}") |
| 19 | + result = Concore.parse_port_file(path) |
| 20 | + @test result == Dict("e1" => 1, "e2" => 2, "e3" => 3) |
| 21 | + end |
| 22 | + end |
| 23 | + |
| 24 | + @testset "missing file returns empty dict" begin |
| 25 | + result = Concore.parse_port_file("/nonexistent/path/file.port") |
| 26 | + @test result == Dict{String,Int}() |
| 27 | + @test isempty(result) |
| 28 | + end |
| 29 | + |
| 30 | + @testset "empty file returns empty dict" begin |
| 31 | + mktempdir() do dir |
| 32 | + path = joinpath(dir, "test.port") |
| 33 | + write(path, "") |
| 34 | + result = Concore.parse_port_file(path) |
| 35 | + @test result == Dict{String,Int}() |
| 36 | + end |
| 37 | + end |
| 38 | + |
| 39 | + @testset "whitespace-only file returns empty dict" begin |
| 40 | + mktempdir() do dir |
| 41 | + path = joinpath(dir, "test.port") |
| 42 | + write(path, " \n ") |
| 43 | + result = Concore.parse_port_file(path) |
| 44 | + @test result == Dict{String,Int}() |
| 45 | + end |
| 46 | + end |
| 47 | + |
| 48 | + @testset "single port with name containing underscore" begin |
| 49 | + mktempdir() do dir |
| 50 | + path = joinpath(dir, "test.port") |
| 51 | + write(path, "{'my_edge': 5}") |
| 52 | + result = Concore.parse_port_file(path) |
| 53 | + @test result == Dict("my_edge" => 5) |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + @testset "port with negative value" begin |
| 58 | + mktempdir() do dir |
| 59 | + path = joinpath(dir, "test.port") |
| 60 | + write(path, "{'e1': -1}") |
| 61 | + result = Concore.parse_port_file(path) |
| 62 | + @test result == Dict("e1" => -1) |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + @testset "content without braces matches nothing" begin |
| 67 | + mktempdir() do dir |
| 68 | + path = joinpath(dir, "test.port") |
| 69 | + write(path, "random text here") |
| 70 | + result = Concore.parse_port_file(path) |
| 71 | + @test result == Dict{String,Int}() |
| 72 | + end |
| 73 | + end |
| 74 | + |
| 75 | + @testset "return type is Dict{String,Int}" begin |
| 76 | + mktempdir() do dir |
| 77 | + path = joinpath(dir, "test.port") |
| 78 | + write(path, "{'e1': 1}") |
| 79 | + result = Concore.parse_port_file(path) |
| 80 | + @test result isa Dict{String,Int} |
| 81 | + end |
| 82 | + end |
| 83 | + |
| 84 | + @testset "port numbers with spaces" begin |
| 85 | + mktempdir() do dir |
| 86 | + path = joinpath(dir, "test.port") |
| 87 | + write(path, "{'e1' : 1 , 'e2' : 2}") |
| 88 | + result = Concore.parse_port_file(path) |
| 89 | + @test result == Dict("e1" => 1, "e2" => 2) |
| 90 | + end |
| 91 | + end |
| 92 | + |
| 93 | + end |
| 94 | + |
| 95 | + # load_iport / load_oport |
| 96 | + @testset "load_iport" begin |
| 97 | + |
| 98 | + @testset "loads from concore.iport file" begin |
| 99 | + mktempdir() do dir |
| 100 | + cd(dir) do |
| 101 | + write("concore.iport", "{'sensor': 1, 'command': 2}") |
| 102 | + Concore.iport = Dict{String,Int}() |
| 103 | + result = Concore.load_iport() |
| 104 | + @test result == Dict("sensor" => 1, "command" => 2) |
| 105 | + @test Concore.iport == Dict("sensor" => 1, "command" => 2) |
| 106 | + end |
| 107 | + end |
| 108 | + end |
| 109 | + |
| 110 | + @testset "returns empty dict when file missing" begin |
| 111 | + mktempdir() do dir |
| 112 | + cd(dir) do |
| 113 | + Concore.iport = Dict{String,Int}() |
| 114 | + result = Concore.load_iport() |
| 115 | + @test isempty(result) |
| 116 | + end |
| 117 | + end |
| 118 | + end |
| 119 | + |
| 120 | + end |
| 121 | + |
| 122 | + @testset "load_oport" begin |
| 123 | + |
| 124 | + @testset "loads from concore.oport file" begin |
| 125 | + mktempdir() do dir |
| 126 | + cd(dir) do |
| 127 | + write("concore.oport", "{'output': 1}") |
| 128 | + Concore.oport = Dict{String,Int}() |
| 129 | + result = Concore.load_oport() |
| 130 | + @test result == Dict("output" => 1) |
| 131 | + @test Concore.oport == Dict("output" => 1) |
| 132 | + end |
| 133 | + end |
| 134 | + end |
| 135 | + |
| 136 | + end |
| 137 | + |
| 138 | + # load_params |
| 139 | + @testset "load_params" begin |
| 140 | + |
| 141 | + @testset "Python dict format" begin |
| 142 | + mktempdir() do dir |
| 143 | + old_inpath = Concore.inpath |
| 144 | + Concore.inpath = joinpath(dir, "in") |
| 145 | + mkpath(Concore.inpath * "1") |
| 146 | + write(joinpath(Concore.inpath * "1", "concore.params"), |
| 147 | + "{'gain': 2.5, 'mode': 'pid'}") |
| 148 | + Concore.params = Dict{String,Any}() |
| 149 | + Concore.load_params() |
| 150 | + @test Concore.params["gain"] == 2.5 |
| 151 | + @test Concore.params["mode"] == "pid" |
| 152 | + Concore.inpath = old_inpath |
| 153 | + end |
| 154 | + end |
| 155 | + |
| 156 | + @testset "key=value format" begin |
| 157 | + mktempdir() do dir |
| 158 | + old_inpath = Concore.inpath |
| 159 | + Concore.inpath = joinpath(dir, "in") |
| 160 | + mkpath(Concore.inpath * "1") |
| 161 | + write(joinpath(Concore.inpath * "1", "concore.params"), |
| 162 | + "gain=2.5;mode=pid") |
| 163 | + Concore.params = Dict{String,Any}() |
| 164 | + Concore.load_params() |
| 165 | + @test Concore.params["gain"] == 2.5 |
| 166 | + @test Concore.params["mode"] == "pid" |
| 167 | + Concore.inpath = old_inpath |
| 168 | + end |
| 169 | + end |
| 170 | + |
| 171 | + @testset "key=value with spaces" begin |
| 172 | + mktempdir() do dir |
| 173 | + old_inpath = Concore.inpath |
| 174 | + Concore.inpath = joinpath(dir, "in") |
| 175 | + mkpath(Concore.inpath * "1") |
| 176 | + write(joinpath(Concore.inpath * "1", "concore.params"), |
| 177 | + "gain = 3.0 ; mode = adaptive") |
| 178 | + Concore.params = Dict{String,Any}() |
| 179 | + Concore.load_params() |
| 180 | + @test Concore.params["gain"] == 3.0 |
| 181 | + @test Concore.params["mode"] == "adaptive" |
| 182 | + Concore.inpath = old_inpath |
| 183 | + end |
| 184 | + end |
| 185 | + |
| 186 | + @testset "Windows quoted params" begin |
| 187 | + mktempdir() do dir |
| 188 | + old_inpath = Concore.inpath |
| 189 | + Concore.inpath = joinpath(dir, "in") |
| 190 | + mkpath(Concore.inpath * "1") |
| 191 | + write(joinpath(Concore.inpath * "1", "concore.params"), |
| 192 | + "\"{'gain': 1.0}\"") |
| 193 | + Concore.params = Dict{String,Any}() |
| 194 | + Concore.load_params() |
| 195 | + @test Concore.params["gain"] == 1.0 |
| 196 | + Concore.inpath = old_inpath |
| 197 | + end |
| 198 | + end |
| 199 | + |
| 200 | + @testset "missing params file does nothing" begin |
| 201 | + mktempdir() do dir |
| 202 | + old_inpath = Concore.inpath |
| 203 | + Concore.inpath = joinpath(dir, "in") |
| 204 | + Concore.params = Dict{String,Any}("existing" => 1.0) |
| 205 | + Concore.load_params() |
| 206 | + @test Concore.params == Dict{String,Any}("existing" => 1.0) |
| 207 | + Concore.inpath = old_inpath |
| 208 | + end |
| 209 | + end |
| 210 | + |
| 211 | + @testset "empty params file does nothing" begin |
| 212 | + mktempdir() do dir |
| 213 | + old_inpath = Concore.inpath |
| 214 | + Concore.inpath = joinpath(dir, "in") |
| 215 | + mkpath(Concore.inpath * "1") |
| 216 | + write(joinpath(Concore.inpath * "1", "concore.params"), "") |
| 217 | + Concore.params = Dict{String,Any}("existing" => 1.0) |
| 218 | + Concore.load_params() |
| 219 | + @test Concore.params == Dict{String,Any}("existing" => 1.0) |
| 220 | + Concore.inpath = old_inpath |
| 221 | + end |
| 222 | + end |
| 223 | + |
| 224 | + @testset "numeric values are Float64" begin |
| 225 | + mktempdir() do dir |
| 226 | + old_inpath = Concore.inpath |
| 227 | + Concore.inpath = joinpath(dir, "in") |
| 228 | + mkpath(Concore.inpath * "1") |
| 229 | + write(joinpath(Concore.inpath * "1", "concore.params"), |
| 230 | + "{'x': 42}") |
| 231 | + Concore.params = Dict{String,Any}() |
| 232 | + Concore.load_params() |
| 233 | + @test Concore.params["x"] isa Float64 |
| 234 | + @test Concore.params["x"] == 42.0 |
| 235 | + Concore.inpath = old_inpath |
| 236 | + end |
| 237 | + end |
| 238 | + |
| 239 | + @testset "string values are strings" begin |
| 240 | + mktempdir() do dir |
| 241 | + old_inpath = Concore.inpath |
| 242 | + Concore.inpath = joinpath(dir, "in") |
| 243 | + mkpath(Concore.inpath * "1") |
| 244 | + write(joinpath(Concore.inpath * "1", "concore.params"), |
| 245 | + "{'mode': 'auto'}") |
| 246 | + Concore.params = Dict{String,Any}() |
| 247 | + Concore.load_params() |
| 248 | + @test Concore.params["mode"] isa AbstractString |
| 249 | + Concore.inpath = old_inpath |
| 250 | + end |
| 251 | + end |
| 252 | + |
| 253 | + end |
| 254 | + |
| 255 | + # tryparam |
| 256 | + @testset "tryparam" begin |
| 257 | + |
| 258 | + @testset "returns value when key exists" begin |
| 259 | + Concore.params = Dict{String,Any}("gain" => 2.5) |
| 260 | + @test Concore.tryparam("gain", 0.0) == 2.5 |
| 261 | + end |
| 262 | + |
| 263 | + @testset "returns default when key missing" begin |
| 264 | + Concore.params = Dict{String,Any}() |
| 265 | + @test Concore.tryparam("missing_key", 99.0) == 99.0 |
| 266 | + end |
| 267 | + |
| 268 | + @testset "returns default of correct type" begin |
| 269 | + Concore.params = Dict{String,Any}() |
| 270 | + @test Concore.tryparam("x", "fallback") == "fallback" |
| 271 | + end |
| 272 | + |
| 273 | + @testset "works with string values" begin |
| 274 | + Concore.params = Dict{String,Any}("mode" => "pid") |
| 275 | + @test Concore.tryparam("mode", "none") == "pid" |
| 276 | + end |
| 277 | + |
| 278 | + @testset "does not modify params" begin |
| 279 | + Concore.params = Dict{String,Any}("a" => 1.0) |
| 280 | + Concore.tryparam("b", 2.0) |
| 281 | + @test !haskey(Concore.params, "b") |
| 282 | + end |
| 283 | + |
| 284 | + end |
| 285 | + |
| 286 | + # default_maxtime |
| 287 | + @testset "default_maxtime" begin |
| 288 | + |
| 289 | + @testset "reads from file" begin |
| 290 | + mktempdir() do dir |
| 291 | + old_inpath = Concore.inpath |
| 292 | + Concore.inpath = joinpath(dir, "in") |
| 293 | + mkpath(Concore.inpath * "1") |
| 294 | + write(joinpath(Concore.inpath * "1", "concore.maxtime"), "500") |
| 295 | + result = Concore.default_maxtime(100) |
| 296 | + @test result == 500 |
| 297 | + @test Concore.maxtime == 500 |
| 298 | + Concore.inpath = old_inpath |
| 299 | + end |
| 300 | + end |
| 301 | + |
| 302 | + @testset "uses default when file missing" begin |
| 303 | + mktempdir() do dir |
| 304 | + old_inpath = Concore.inpath |
| 305 | + Concore.inpath = joinpath(dir, "in") |
| 306 | + result = Concore.default_maxtime(200) |
| 307 | + @test result == 200 |
| 308 | + @test Concore.maxtime == 200 |
| 309 | + Concore.inpath = old_inpath |
| 310 | + end |
| 311 | + end |
| 312 | + |
| 313 | + @testset "uses default when file has bad content" begin |
| 314 | + mktempdir() do dir |
| 315 | + old_inpath = Concore.inpath |
| 316 | + Concore.inpath = joinpath(dir, "in") |
| 317 | + mkpath(Concore.inpath * "1") |
| 318 | + write(joinpath(Concore.inpath * "1", "concore.maxtime"), "not_a_number") |
| 319 | + result = Concore.default_maxtime(300) |
| 320 | + @test result == 300 |
| 321 | + @test Concore.maxtime == 300 |
| 322 | + Concore.inpath = old_inpath |
| 323 | + end |
| 324 | + end |
| 325 | + |
| 326 | + @testset "handles whitespace in file" begin |
| 327 | + mktempdir() do dir |
| 328 | + old_inpath = Concore.inpath |
| 329 | + Concore.inpath = joinpath(dir, "in") |
| 330 | + mkpath(Concore.inpath * "1") |
| 331 | + write(joinpath(Concore.inpath * "1", "concore.maxtime"), " 750 \n") |
| 332 | + result = Concore.default_maxtime(100) |
| 333 | + @test result == 750 |
| 334 | + Concore.inpath = old_inpath |
| 335 | + end |
| 336 | + end |
| 337 | + |
| 338 | + @testset "sets module global" begin |
| 339 | + mktempdir() do dir |
| 340 | + old_inpath = Concore.inpath |
| 341 | + Concore.inpath = joinpath(dir, "in") |
| 342 | + Concore.default_maxtime(42) |
| 343 | + @test Concore.maxtime == 42 |
| 344 | + Concore.inpath = old_inpath |
| 345 | + end |
| 346 | + end |
| 347 | + |
| 348 | + end |
| 349 | + |
| 350 | +end |
0 commit comments