Skip to content

Commit

Permalink
Add tests for show with MIME"text/plain"
Browse files Browse the repository at this point in the history
  • Loading branch information
Seelengrab committed May 19, 2024
1 parent 3905abd commit f5a6362
Show file tree
Hide file tree
Showing 2 changed files with 186 additions and 28 deletions.
28 changes: 22 additions & 6 deletions src/data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Fallback definitions:
* `postype(::Possibility{T}) -> Type{T}`
* `example(::Possibility{T}) -> T`
* `==(::Possibility, ::Possibility) = false`
"""
abstract type Possibility{T} end
Base.:(==)(::Possibility, ::Possibility) = false
Expand Down Expand Up @@ -285,7 +286,7 @@ function Base.show(io::IO, ::MIME"text/plain", b::Bind)
print(io, styled"""
{code,underline:$Bind}:
Binds the output of {code:$(b.source)} through {code:$(b.map)} to a new {code:Possibility}.
""")
end

Expand Down Expand Up @@ -832,7 +833,7 @@ function Base.show(io::IO, ::MIME"text/plain", r::Recursive)
print(io, styled"""
{code,underline:$Recursive}:
Wrap {code:$(r.base)} up to {code:$(length(r.inner.strats))} with {code:}
Wrap {code:$(r.base)} up to {code:$(length(r.inner.strats)-1)} times with {code:$(r.extend)}
""")
end

Expand Down Expand Up @@ -1067,7 +1068,8 @@ Base.:(==)(t1::Text, t2::Text) = t1.vectors == t2.vectors
function Base.show(io::IO, t::Text)
print(io, Text, "(")
show(io, t.vectors.elements)
print(io, "; min_len=", t.vectors.min_size, ", max_len=", t.vectors.max_size, ")")
print(io, "; min_len=", string(t.vectors.min_size; base=10))
print(io, ", max_len=", string(t.vectors.max_size; base=10), ")")
nothing
end

Expand All @@ -1080,12 +1082,13 @@ function Base.show(io::IO, ::MIME"text/plain", t::Text)
rem_len = textwidth(str) - available_space + 3
str = str[begin:div(rem_len, 2)] * " _ " * str[(div(rem_len, 2)+3):end]
end
lower = sprint(show, t.vectors.min_size % Int)
upper = sprint(show, (t.vectors.min_size + t.vectors.max_size) % Int)
lower = string(t.vectors.min_size; base=10)
upper = string(t.vectors.max_size; base=10)
print(io, styled"""
{code,underline:$Text}:
Produce a {code:String} with length in the interval {code:[$lower, $upper]}
The {code:Char} of the {code:String} are produced by {code:$(t.vectors.elements)}.
E.g. {code:$str} (may be abbreviated here)
""")
Expand Down Expand Up @@ -1139,8 +1142,21 @@ function Base.show(io::IO, d::Dicts)
end

function Base.show(io::IO, ::MIME"text/plain", d::Dicts)
lengths = styled"{code:[$(Int128(d.min_size)), $(Int128(d.max_size))]}"
print(io, styled"""
""")
{code,underline:$Dicts}:
Produce a {code:Dict\{$(postype(d.keys)), $(postype(d.values))\}} with length in the interval $lengths.
The keys are produced by {code:$(d.keys)}.
The values are produced by {code:$(d.values)}.""")
obj = styled"""
Keys of the {code:Dict} may look like {code:$(sprint(show, example(d.keys)))}
Values of the {code:Dict} may look like {code:$(sprint(show, example(d.values)))}"""
if length(obj) < 100
print(io, obj)
end
end

function produce!(tc::TestCase, d::Dicts{K,V}) where {K,V}
Expand Down
186 changes: 164 additions & 22 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1031,28 +1031,170 @@ const verb = VERSION.major == 1 && VERSION.minor < 11
end
end

@testset "show Report: $pos" for pos in (
Data.Integers{UInt8}(),
Data.Integers(0x1,0xfe),
Data.Floats{Float16}(),
Data.Floats(),
Data.Booleans(),
Data.Pairs(Data.Booleans(), Data.Booleans()),
Data.Vectors(Data.Booleans();max_size=1),
Data.Dicts(Data.Booleans(),Data.Booleans();max_size=1),
Data.AsciiCharacters(),
Data.Characters(),
Data.UnicodeCharacters(),
Data.Text(Data.AsciiCharacters();max_len=1),
Data.SampledFrom(0:10),
Data.filter(iseven, Data.Just(0:10)),
Data.map(sqrt, Data.Just(0:10)),
Data.Just(1),
Data.Floats() | Data.Booleans(),
Data.WeightedNumbers([.1, .2, .7]),
Data.WeightedSample(1:3, [.1, .2, .7]),
)
@test eval(Meta.parse(repr(pos))) == pos
@testset "show" begin
@testset "2-arg show" begin
@testset "repr is evalable: $pos" for pos in (
Data.Integers{UInt8}(),
Data.Integers(0x1,0xfe),
Data.Floats{Float16}(),
Data.Floats(),
Data.Booleans(),
Data.Pairs(Data.Booleans(), Data.Booleans()),
Data.Vectors(Data.Booleans();max_size=1),
Data.Dicts(Data.Booleans(),Data.Booleans();max_size=1),
Data.AsciiCharacters(),
Data.Characters(),
Data.UnicodeCharacters(),
Data.Text(Data.AsciiCharacters();max_len=1),
Data.SampledFrom(0:10),
Data.filter(iseven, Data.Just(0:10)),
Data.map(sqrt, Data.Just(0:10)),
Data.Just(1),
Data.Floats() | Data.Booleans(),
Data.WeightedNumbers([.1, .2, .7]),
Data.WeightedSample(1:3, [.1, .2, .7]),
)
@test eval(Meta.parse(repr(pos))) == pos
end
end
@testset "3-arg show" begin
@testset "Integers" begin
@test occursin("Produce an integer of type $Int", repr("text/plain", Data.Integers{Int}()))
limited_repr = repr("text/plain", Data.Integers(5, 10))
@test occursin("Integers", limited_repr)
@test occursin("Int64", limited_repr)
@test occursin("[5, 10]", limited_repr)
bitint_repr = repr("text/plain", Data.BitIntegers())
@testset "BitIntegers: $T" for T in (
UInt8, Int8, UInt16, Int16, UInt32, Int32, UInt64, Int64, UInt128, Int128
)
@test occursin(string(T), bitint_repr)
end
end
@testset "Floats" begin
@test occursin("of type Float16", repr("text/plain", Data.Floats{Float16}()))
@test occursin("isinf: never", repr("text/plain", Data.Floats{Float16}(;infs=false)))
@test occursin("isnan: never", repr("text/plain", Data.Floats{Float16}(;nans=false)))
@test occursin("AllFloats", repr("text/plain", Data.Floats()))
@test occursin("AllFloats", repr("text/plain", Data.AllFloats()))
@test occursin("true and false have a probability of 50%", repr("text/plain", Data.Booleans()))
end
@testset "Data.Pairs" begin
pair_repr = repr("text/plain", Data.Pairs(Data.Booleans(), Data.Characters()))
@test occursin("Pairs", pair_repr)
@test occursin(r"From[\w\s\.]+Booleans", pair_repr)
@test occursin(r"From[\w\s\.]+Characters", pair_repr)
@test occursin("Pair{Bool, Char}", pair_repr)
end
@testset "Data.Vectors" begin
vec_repr = repr("text/plain", Data.Vectors(Data.Booleans(); min_size=10, max_size=50))
@test occursin("Vectors", vec_repr)
# show the interval of the length
@test occursin("[10, 50]", vec_repr)
# show the `Possibility` for elements
@test occursin("Booleans()", vec_repr)
# show the target vector type
@test occursin("Vector{Bool}", vec_repr)
end
@testset "Data.Dicts" begin
dict_repr = repr("text/plain", Data.Dicts(Data.Booleans(), Data.Characters(); min_size=10, max_size=50))
@test occursin("Dicts", dict_repr)
# show the interval of the length
@test occursin("[10, 50]", dict_repr)
# show the `Possibility` for keys
@test occursin("Booleans()", dict_repr)
# show the `Possibility` for values
@test occursin("Characters", dict_repr)
# show the target Dict type
@test occursin("Dict{Bool, Char}", dict_repr)
end
@testset "`Char`" begin
ascii_char_repr = repr("text/plain", Data.AsciiCharacters())
@test occursin("AsciiCharacters", ascii_char_repr)
@test occursin("isascii returns true", ascii_char_repr)
char_repr = repr("text/plain", Data.Characters())
@test occursin("Characters", char_repr)
@test occursin("well-formed", char_repr)
uni_char_repr = repr("text/plain", Data.UnicodeCharacters())
@test occursin("UnicodeCharacters", uni_char_repr)
@test occursin("ismalformed", uni_char_repr)
end
@testset "Text" begin
text_repr = repr("text/plain", Data.Text(Data.AsciiCharacters(); min_len=10, max_len=50))
@test occursin("Text", text_repr)
# show the interval of the length
@test occursin("[10, 50]", text_repr)
# show the `Possibility` for individual characters
@test occursin("AsciiCharacters()", text_repr)
@test occursin("String", text_repr)
end
@testset "SampledFrom" begin
sf_repr = repr("text/plain", Data.SampledFrom((sin, cos)))
@test occursin("SampledFrom", sf_repr)
@test occursin("(sin, cos)", sf_repr)
@test occursin("equal probability", sf_repr)
end
@testset "filter" begin
satis_repr = repr("text/plain", filter(isinf, Data.Integers{Int8}()))
@test occursin("Satisfying", satis_repr)
@test occursin("Integers{Int8}", satis_repr)
@test occursin("isinf", satis_repr)
end
@testset "map" begin
map_repr = repr("text/plain", map(sqrt, Data.Integers{UInt8}()))
@test occursin("Map", map_repr)
@test occursin("Integers{UInt8}", map_repr)
@test occursin("Float64", map_repr)
@test occursin("sqrt", map_repr)
@check max_examples=100 description="map" (f=map(abs, Data.Floats{Float64}())) -> begin
str = repr("text/plain", map(sqrt, Data.Just(f)))
exp = "sqrt($f)"
occursin(exp, str)
end
end
@testset "Just" begin
# a totally random number for testing
just_repr = repr("text/plain", Data.Just(4))
@test occursin("Just", just_repr)
@test occursin("4", just_repr)
end
@testset "OneOf" begin
of_repr = repr("text/plain", Data.Just(4) | Data.Integers{UInt8}())
@test occursin("OneOf", of_repr)
@test occursin("Just(4)", of_repr)
@test occursin("Integers{UInt8}", of_repr)
end
@testset "Bind" begin
intbind(o) = Data.Integers(o,o)
bind_repr = repr("text/plain", Data.Bind(Data.Just(4), intbind))
@test occursin("Bind", bind_repr)
@test occursin("Just(4)", bind_repr)
@test occursin("intbind", bind_repr)
end
@testset "Recursive" begin
recwrap(pos) = map(tuple, pos)
recr_repr = repr("text/plain", Data.recursive(recwrap, Data.Characters(); max_layers=3))
@test occursin("Recursive", recr_repr)
@test occursin("Characters", recr_repr)
@test occursin("recwrap", recr_repr)
@test occursin("2", recr_repr)
end
@testset "WeightedNumbers" begin
wn_repr = repr("text/plain", Data.WeightedNumbers([.1, .2, .7]))
@test occursin("WeightedNumbers", wn_repr)
@test occursin("1:3", wn_repr)
@test occursin("10.00% : 1", wn_repr)
@test occursin("20.00% : 2", wn_repr)
@test occursin("70.00% : 3", wn_repr)
end
@testset "WeightedSample" begin
wn_repr = repr("text/plain", Data.WeightedSample(("foo", "bar", "baz"), [.1, .2, .7]))
@test occursin("WeightedSample", wn_repr)
@test occursin("10.00% : foo", wn_repr)
@test occursin("20.00% : bar", wn_repr)
@test occursin("70.00% : baz", wn_repr)
end
end
end

@testset "Utility" begin
Expand Down

0 comments on commit f5a6362

Please sign in to comment.