Skip to content

Commit 9fd3784

Browse files
committed
Adapt format ::full to show parseable code
1 parent 35f98a5 commit 9fd3784

File tree

2 files changed

+26
-26
lines changed

2 files changed

+26
-26
lines changed

src/display.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,12 @@ end
212212

213213
# `String` representation of an `Interval`
214214

215-
function basic_representation(a::Interval, format::Symbol)
215+
function basic_representation(a::Interval{T}, format::Symbol) where {T}
216216
isempty(a) && return ""
217217
sigdigits = display_params.sigdigits
218218
if format === :full
219219
# Do not use `inf(a)` to avoid -0.0
220-
return string("Interval(", a.lo, ", ", sup(a), ")")
220+
return string("Interval{", T, "}(", a.lo, ", ", sup(a), ")")
221221
elseif format === :midpoint
222222
m = round_string(mid(a), sigdigits, RoundNearest)
223223
r = round_string(radius(a), sigdigits, RoundUp)
@@ -237,8 +237,8 @@ function basic_representation(a::Interval{Float32}, format::Symbol)
237237
sigdigits = display_params.sigdigits
238238
if format === :full
239239
# Do not use `inf(a)` to avoid -0.0
240-
output = string("Interval(", a.lo, "f0, ", sup(a), "f0)")
241-
return replace(replace(output, "NaNf0" => "NaN32"), "Inff0" => '')
240+
output = string("Interval{Float32}(", a.lo, "f0, ", sup(a), "f0)")
241+
return replace(replace(output, "NaNf0" => "NaN32"), "Inff0" => "Inf32")
242242
elseif format === :midpoint
243243
m = round_string(mid(a), sigdigits, RoundNearest)
244244
r = round_string(radius(a), sigdigits, RoundUp)
@@ -253,11 +253,11 @@ function basic_representation(a::Interval{Float32}, format::Symbol)
253253
end
254254
end
255255

256-
function basic_representation(a::Interval{<:Rational}, format::Symbol)
256+
function basic_representation(a::Interval{T}, format::Symbol) where {T<:Rational}
257257
isempty(a) && return ""
258258
if format === :full
259259
# Do not use `inf(a)` to avoid -0.0
260-
return string("Interval(", a.lo, ", ", sup(a), ")")
260+
return string("Interval{", T, "}(", a.lo, ", ", sup(a), ")")
261261
elseif format === :midpoint
262262
return string(mid(a), " ± ", radius(a))
263263
else # format === :standard

test/display_tests/display.jl

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ let x, b
4545
@testset "Full" begin
4646
setformat(:full)
4747

48-
@test sprint(show, MIME("text/plain"), a) == "Interval(1.0, 2.0)"
49-
@test sprint(show, MIME("text/plain"), b) == "Interval(-1.1, 1.3)"
50-
@test sprint(show, MIME("text/plain"), c) == "Interval(3.141592653589793, 3.1415926535897936)"
48+
@test sprint(show, MIME("text/plain"), a) == "Interval{Float64}(1.0, 2.0)"
49+
@test sprint(show, MIME("text/plain"), b) == "Interval{Float64}(-1.1, 1.3)"
50+
@test sprint(show, MIME("text/plain"), c) == "Interval{Float64}(3.141592653589793, 3.1415926535897936)"
5151
@test sprint(show, MIME("text/plain"), large_expo) ==
52-
"Interval(0.0, 1.000000000000000000000000000000000000000000000000000000000000000000000000000004e+123456789)"
52+
"Interval{BigFloat}(0.0, 1.000000000000000000000000000000000000000000000000000000000000000000000000000004e+123456789)"
5353
end
5454

5555
@testset "Midpoint" begin
@@ -73,7 +73,7 @@ let x, b
7373
@test_broken sprint(show, MIME("text/plain"), a) == "[1//3, 5//4]"
7474

7575
setformat(:full)
76-
@test_broken sprint(show, MIME("text/plain"), a) == "Interval(1//3, 5//4)"
76+
@test_broken sprint(show, MIME("text/plain"), a) == "Interval{Rational{Int}}(1//3, 5//4)"
7777

7878
setformat(:midpoint)
7979
@test_broken sprint(show, MIME("text/plain"), a) == "19//24 ± 11//24"
@@ -88,8 +88,8 @@ let x, b
8888
@test sprint(show, MIME("text/plain"), b) == "[-1.0f0, ∞]"
8989

9090
setformat(:full)
91-
@test sprint(show, MIME("text/plain"), a) == "Interval(1.0f0, 2.0f0)"
92-
@test sprint(show, MIME("text/plain"), b) == "Interval(-1.0f0, )"
91+
@test sprint(show, MIME("text/plain"), a) == "Interval{Float32}(1.0f0, 2.0f0)"
92+
@test sprint(show, MIME("text/plain"), b) == "Interval{Float32}(-1.0f0, Inf32)"
9393

9494
setformat(:midpoint)
9595
@test sprint(show, MIME("text/plain"), a) == "1.5f0 ± 0.5f0"
@@ -131,7 +131,7 @@ let x, b
131131
@test sprint(show, MIME("text/plain"), a) == "[2.0, 3.0]₂₅₆_com"
132132

133133
setformat(:full)
134-
@test sprint(show, MIME("text/plain"), a) == "DecoratedInterval(Interval(2.0, 3.0), com)"
134+
@test sprint(show, MIME("text/plain"), a) == "DecoratedInterval(Interval{BigFloat}(2.0, 3.0), com)"
135135

136136
setformat(:midpoint)
137137
@test sprint(show, MIME("text/plain"), a) == "(2.5 ± 0.5)₂₅₆_com"
@@ -150,7 +150,7 @@ let x, b
150150
@test sprint(show, MIME("text/plain"), a) == "[1.0, 1.0]₁₂₈"
151151

152152
setformat(:full)
153-
@test sprint(show, MIME("text/plain"), a) == "Interval(1.0, 1.0)"
153+
@test sprint(show, MIME("text/plain"), a) == "Interval{BigFloat}(1.0, 1.0)"
154154

155155
a = DecoratedInterval(big(2), big(3), com)
156156
@test typeof(a) == DecoratedInterval{BigFloat}
@@ -162,7 +162,7 @@ let x, b
162162
@test sprint(show, MIME("text/plain"), a) == "[2.0, 3.0]₁₂₈_com"
163163

164164
setformat(:full)
165-
@test sprint(show, MIME("text/plain"), a) == "DecoratedInterval(Interval(2.0, 3.0), com)"
165+
@test sprint(show, MIME("text/plain"), a) == "DecoratedInterval(Interval{BigFloat}(2.0, 3.0), com)"
166166
end
167167

168168
@testset "IntervalBox" begin
@@ -177,7 +177,7 @@ let x, b
177177
@test sprint(show, MIME("text/plain"), X) == "[-∞, ∞]²"
178178

179179
setformat(:full)
180-
@test sprint(show, MIME("text/plain"), X) == "IntervalBox(Interval(-Inf, Inf), 2)"
180+
@test sprint(show, MIME("text/plain"), X) == "IntervalBox(Interval{Float64}(-Inf, Inf), 2)"
181181

182182
setformat(:standard)
183183
a = IntervalBox(1..2, 2..3)
@@ -188,9 +188,9 @@ let x, b
188188
@test sprint(show, MIME("text/plain"), c) == "[1.0, 2.0]¹"
189189

190190
setformat(:full)
191-
@test sprint(show, MIME("text/plain"), a) == "IntervalBox(Interval(1.0, 2.0), Interval(2.0, 3.0))"
191+
@test sprint(show, MIME("text/plain"), a) == "IntervalBox(Interval{Float64}(1.0, 2.0), Interval{Float64}(2.0, 3.0))"
192192
@test sprint(show, MIME("text/plain"), b) == "IntervalBox(∅, 2)"
193-
@test sprint(show, MIME("text/plain"), c) == "IntervalBox(Interval(1.0, 2.0), 1)"
193+
@test sprint(show, MIME("text/plain"), c) == "IntervalBox(Interval{Float64}(1.0, 2.0), 1)"
194194

195195
setformat(:midpoint)
196196
@test sprint(show, MIME("text/plain"), a) == "(1.5 ± 0.5) × (2.5 ± 0.5)"
@@ -205,19 +205,19 @@ end
205205

206206
x = 0..1
207207
@test sprint(show, MIME("text/plain"), x) == "[0.0, 1.0]"
208-
@test sprint(show, x) == "Interval(0.0, 1.0)"
208+
@test sprint(show, x) == "Interval{Float64}(0.0, 1.0)"
209209

210210
x = @biginterval(0, 1)
211211
@test sprint(show, MIME("text/plain"), x) == "[0.0, 1.0]₁₂₈"
212-
@test sprint(show, x) == "Interval(0.0, 1.0)"
212+
@test sprint(show, x) == "Interval{BigFloat}(0.0, 1.0)"
213213

214214
x = DecoratedInterval(0, 1, dac)
215215
@test sprint(show, MIME("text/plain"), x) == "[0.0, 1.0]"
216-
@test sprint(show, x) == "DecoratedInterval(Interval(0.0, 1.0), dac)"
216+
@test sprint(show, x) == "DecoratedInterval(Interval{Float64}(0.0, 1.0), dac)"
217217

218218
x = DecoratedInterval(big(0), big(1), def)
219219
@test sprint(show, MIME("text/plain"), x) == "[0.0, 1.0]₁₂₈"
220-
@test sprint(show, x) == "DecoratedInterval(Interval(0.0, 1.0), def)"
220+
@test sprint(show, x) == "DecoratedInterval(Interval{BigFloat}(0.0, 1.0), def)"
221221

222222
setformat(; decorations = true)
223223
@test sprint(show, MIME("text/plain"), x) == "[0.0, 1.0]₁₂₈_def"
@@ -226,17 +226,17 @@ end
226226
b = IntervalBox(emptyinterval(), 2)
227227
c = IntervalBox(1..2, 1)
228228

229-
@test sprint(show, a) == "IntervalBox(Interval(1.0, 2.0), Interval(2.0, 3.0))"
229+
@test sprint(show, a) == "IntervalBox(Interval{Float64}(1.0, 2.0), Interval{Float64}(2.0, 3.0))"
230230
@test sprint(show, b) == "IntervalBox(∅, 2)"
231-
@test sprint(show, c) == "IntervalBox(Interval(1.0, 2.0), 1)"
231+
@test sprint(show, c) == "IntervalBox(Interval{Float64}(1.0, 2.0), 1)"
232232

233233
end
234234

235235
@testset "@format tests" begin
236236
x = prevfloat(0.1)..nextfloat(0.3)
237237

238238
@format full
239-
@test sprint(show, MIME("text/plain"), x) == "Interval(0.09999999999999999, 0.30000000000000004)"
239+
@test sprint(show, MIME("text/plain"), x) == "Interval{Float64}(0.09999999999999999, 0.30000000000000004)"
240240

241241
@format standard 3
242242
@test sprint(show, MIME("text/plain"), x) == "[0.0999, 0.301]"

0 commit comments

Comments
 (0)