From b4f6a32f92e7788ded4b569d19b3422e2b326df9 Mon Sep 17 00:00:00 2001
From: Daniel Vedder <daniel.vedder@idiv.de>
Date: Thu, 8 Aug 2024 15:14:23 +0200
Subject: [PATCH] AnnualDates can be constructed from a string

---
 src/core/utils.jl      | 4 ++++
 src/world/landscape.jl | 2 +-
 test/io_tests.jl       | 7 ++++++-
 3 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/src/core/utils.jl b/src/core/utils.jl
index 358d9b8..ddffe48 100644
--- a/src/core/utils.jl
+++ b/src/core/utils.jl
@@ -47,6 +47,10 @@ end
 AnnualDate(ad::Tuple{Int64,Int64}) = AnnualDate(ad...)
 Base.convert(::Type{AnnualDate}, ad::Tuple{Int64,Int64}) = AnnualDate(ad)
 
+# allow creating AnnualDates from a string of the format "8 August"
+AnnualDate(ad::String) = AnnualDate(Date(ad, dateformat"d U"))
+Base.convert(::Type{AnnualDate}, ad::String) = AnnualDate(ad)
+
 # Interface with Dates
 AnnualDate(date::Date) = AnnualDate(month(date), day(date))
 Base.convert(::Type{AnnualDate}, ad::Date) = AnnualDate(ad)
diff --git a/src/world/landscape.jl b/src/world/landscape.jl
index 762ea8b..afbc6d7 100644
--- a/src/world/landscape.jl
+++ b/src/world/landscape.jl
@@ -22,7 +22,7 @@ mutable struct Pixel
     fieldid::Union{Missing,Int64} # ID of the farmplot (if any) at this position
     events::Vector{Management}    # management events that have been applied to this pixel
     animals::Vector{Int64}        # IDs of animals currently at this position
-    territories::Vector{String}    # IDs of animals that claim this pixel as part of their territory
+    territories::Vector{String}   # IDs of animals that claim this pixel as part of their territory
 end
 
 Pixel(landcover::LandCover, fieldid::Union{Missing,Int64}) =
diff --git a/test/io_tests.jl b/test/io_tests.jl
index 9a28b5d..deebec3 100644
--- a/test/io_tests.jl
+++ b/test/io_tests.jl
@@ -113,11 +113,16 @@ end
     @test 1km / 5 == 200m
     # AnnualDates
     birthday::AnnualDate = (August, 21)
-    christmas::AnnualDate = (December, 24)
+    christmas::AnnualDate = "24 December"
     heute = Date(2024, 8, 8)
     @test christmas - birthday == Day(125)
     @test birthday - christmas == Day(240)
     @test birthday > heute
     @test heute + Week(2) - Day(1) == birthday
     @test length(AnnualDate(heute):birthday) == 14
+    # bounds
+    @test Ps.bounds(3) == 3
+    @test Ps.bounds(-3) == 0
+    @test Ps.bounds(20, max=10) == 10
+    @test Ps.bounds(-3, min=-10) == -3
 end
-- 
GitLab