Skip to content

Commit

Permalink
Explicitly test eachregion
Browse files Browse the repository at this point in the history
While this is implicitly tested later on, I think it's nice to test for
it explicitly. If nothing else, should any potentially buggy
modifications be made in the future it will make it easier to pin down
the root misbehaviour.
  • Loading branch information
tecosaur committed Aug 4, 2024
1 parent c417262 commit fc686f3
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Test

using StyledStrings: StyledStrings, Legacy, SimpleColor, FACES, Face,
@styled_str, styled, StyledMarkup, getface, addface!, loadface!, resetfaces!
@styled_str, styled, StyledMarkup, eachregion, getface, addface!, loadface!, resetfaces!
using .StyledMarkup: MalformedStylingMacro
using Base: AnnotatedString, AnnotatedChar, AnnotatedIOBuffer, annotations

Expand Down Expand Up @@ -35,6 +35,50 @@ end
# When tested as part of the stdlib, the package prefix can start appearing in show methods.
choppkg(s::String) = chopprefix(s, "StyledStrings.")

@testset "Eachregion" begin
annregions(str::String, annots::Vector{<:Tuple{UnitRange{Int}, <:Pair{Symbol, <:Any}}}) =
collect(eachregion(AnnotatedString(str, annots)))
# Regions that do/don't extend to the left/right edges
@test annregions(" abc ", [(2:4, :face => :bold)]) ==
[(" ", []),
("abc", [:face => :bold]),
(" ", [])]
@test annregions(" x ", [(2:2, :face => :bold)]) ==
[(" ", []),
("x", [:face => :bold]),
(" ", [])]
@test annregions(" x", [(2:2, :face => :bold)]) ==
[(" ", []),
("x", [:face => :bold])]
@test annregions("x ", [(1:1, :face => :bold)]) ==
[("x", [:face => :bold]),
(" ", [])]
@test annregions("x", [(1:1, :face => :bold)]) ==
[("x", [:face => :bold])]
# Overlapping/nested regions
@test annregions(" abc ", [(2:4, :face => :bold), (3:3, :face => :italic)]) ==
[(" ", []),
("a", [:face => :bold]),
("b", [:face => :bold, :face => :italic]),
("c", [:face => :bold]),
(" ", [])]
@test annregions("abc-xyz", [(1:7, :face => :bold), (1:3, :face => :green), (4:4, :face => :yellow), (4:7, :face => :italic)]) ==
[("abc", [:face => :bold, :face => :green]),
("-", [:face => :bold, :face => :yellow, :face => :italic]),
("xyz", [:face => :bold, :face => :italic])]
# Preserving annotation order
@test annregions("abcd", [(1:3, :face => :red), (2:2, :face => :yellow), (2:3, :face => :green), (2:4, :face => :blue)]) ==
[("a", [:face => :red]),
("b", [:face => :red, :face => :yellow, :face => :green, :face => :blue]),
("c", [:face => :red, :face => :green, :face => :blue]),
("d", [:face => :blue])]
@test annregions("abcd", [(2:4, :face => :blue), (1:3, :face => :red), (2:3, :face => :green), (2:2, :face => :yellow)]) ==
[("a", [:face => :red]),
("b", [:face => :blue, :face => :red, :face => :green, :face => :yellow]),
("c", [:face => :blue, :face => :red, :face => :green]),
("d", [:face => :blue])]
end

@testset "SimpleColor" begin
@test SimpleColor(:hey).value == :hey # no error
@test SimpleColor(0x01, 0x02, 0x03).value == (r=0x01, g=0x02, b=0x03)
Expand Down

0 comments on commit fc686f3

Please sign in to comment.