Skip to content

Commit 41437aa

Browse files
committed
support comparison with numbers in scientific notation
1 parent dd7f3aa commit 41437aa

File tree

2 files changed

+42
-13
lines changed

2 files changed

+42
-13
lines changed

src/ShowMethodTesting.jl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,16 @@ function parse_show(x::String;
8787
for (k, v) in repl
8888
s = replace(s, k => v)
8989
end
90-
# add spaces between digits and other characters (except dots), to interpret them as numbers
91-
s = replace(s, r"(?<=\d)(?=[^\d.])|(?<=[^\d.])(?=\d)" => s" ")
90+
#
91+
# add spaces between digits and other characters (except dots and scientific notation),
92+
# to interpret them as numbers
93+
#
94+
NUMBER_PATTERN = r"([+-]?\d*\.?\d+[eEfFrR][+-]?\d+|[+-]?\d*\.?\d+)"
95+
# Replaces the matched number N with " N " (a space on each side).
96+
# Then, it cleans up multiple consecutive spaces.
97+
s = replace(s, NUMBER_PATTERN => s" \0 ")
98+
# Clean up any leftover multiple spaces and surrounding whitespace.
99+
s = replace(strip(s), r"\s+" => " ")
92100
if vector_simplify # keep only first and last array elements
93101
s = replace(s, r"(\[)([^,\]]+)(,.*,)([^,\]]+)(\])" => s"\1\2 \4\5")
94102
end

test/runtests.jl

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,41 @@ end
1212
x::Int
1313
path::String
1414
vec::Vector{Float64}
15+
a::Float32
16+
b::Float32
17+
c::Float64
18+
d::Float64
1519
end
16-
Base.show(io::IO, ::MIME"text/plain", a::A) = print(io, "Object with Int($(a.x)), $(a.path) and $(a.vec)")
17-
a = A(1, "/usr/bin/bash", [1.0, π, 7.5, 2])
18-
@test parse_show(a) "Object with Int(1), /usr/bin/bash and [1.0, 3.1415, 7.5, 1.4142]"
19-
@test "Object with Int(1), /usr/bin/bash and [1.0, 3.1415, 7.5, 1.4142]" parse_show(a)
20-
@test parse_show("Object with Int(1), /usr/bin/bash and [1.0, 3.1415, 7.5, 1.4142]") parse_show(a)
21-
@test_throws AssertionError parse_show(a) "Object with Int(2), /usr/bin/bash and [1.0, 3.141592653589793, 7.5, 1.4142135623730951]"
22-
@test !isapprox(parse_show(a), "Object with Int(2), /usr/bin/bash and [1.0, 3.141592653589793, 7.5, 1.4142135623730951]"; assertion_error=false)
20+
Base.show(io::IO, ::MIME"text/plain", a::A) =
21+
print(io, """
22+
Object with Int($(a.x)), $(a.path) and $(a.vec) vector.
23+
a, b, c, d = $(a.a), $(a.b), $(a.c), $(a.d)
24+
""")
25+
a = A(1, "/usr/bin/bash", [1.0, π, 7.5, 2], 1.3f-17, 2.6f17, 1.3e-17, 2.6f17)
26+
@test parse_show(a) """
27+
Object with Int(1), /usr/bin/bash and [1.0, 3.141592653589793, 7.5, 1.4142135623730951] vector.
28+
a, b, c, d = 1.3e-17, 2.6e17, 1.3e-17, 2.6000000279170253e17
29+
"""
30+
@test """
31+
Object with Int(1), /usr/bin/bash and [1.0, 3.141592653589793, 7.5, 1.4142135623730951] vector.
32+
a, b, c, d = 1.3e-17, 2.6e17, 1.3e-17, 2.6000000279170253e17
33+
""" parse_show(a)
34+
@test parse_show("""
35+
Object with Int(1), /usr/bin/bash and [1.0, 3.141592653589793, 7.5, 1.4142135623730951] vector.
36+
a, b, c, d = 1.3e-17, 2.6e17, 1.3e-17, 2.6000000279170253e17
37+
""") parse_show(a)
38+
@test_throws "comparison failed" parse_show(a) "Object with Int(2), /usr/bin/bash and [1.0, 3.141592653589793, 7.5, 1.4142135623730951]"
39+
# do not throw error with assertion_error == false
40+
@test !isapprox(parse_show(a), """
41+
Object with Int(2), /usr/bin/bash and [1.0, 3.141592653589793, 7.5, 1.4142135623730951]"
42+
"""; assertion_error=false)
2343
# Test show method
2444
@test contains(repr("text/plain", parse_show(a)), "Object with Int( 1 )")
25-
@test parse_show([a, a, a, a]; repl=["/usr/bin/bash" => "", r"^((?:[^\n]*\n){3}).*"s => s"\1"]) """
26-
4 -element Vector{A}:
27-
Object with Int( 1 ), and [ 1.0 1.4142135623730951 ]
28-
Object with Int( 1 ), and [ 1.0 1.4142135623730951 ]
45+
# Test custom multiple substitions (keep only first 3 lines)
46+
@test parse_show([a, a, a, a]; repl=["/usr/bin/bash" => "", r"^((?:[^\n]*\n){3}).*"s => s"\1"], vector_simplify=false) """
47+
4-element Vector{A}:
48+
A(1, "/usr/bin/bash", [1.0, 3.141592653589793, 7.5, 1.4142135623730951], 1.3f-17, 2.6f17, 1.3e-17, 2.6000000279170253e17)
49+
A(1, "/usr/bin/bash", [1.0, 3.141592653589793, 7.5, 1.4142135623730951], 1.3f-17, 2.6f17, 1.3e-17, 2.6000000279170253e17)
2950
"""
3051
@test_throws ArgumentError parse_show([a, a, a, a]; repl=["a", "b"])
3152
# compact printing

0 commit comments

Comments
 (0)