Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ the string copy/pasted from the show of the custom type.
## The `parse_show` function:

```julia
parse_show(x; vector_simplify=true, repl=Dict())
parse_show(x; vector_simplify=true, repl=())
```

Parse the output of `show` to a `ParsedShow` object, which can be compared with `isapprox` (`≈`),
Expand All @@ -30,7 +30,9 @@ to other `ParsedShow` objects or to strings.

- `x`: object to parse
- `vector_simplify`: if `true`, only the first and last elements of arrays are kept
- `repl`: dictionary with custom replacements to be made before parsing
- `repl`: container with custom replacements to be made before parsing. The replacements must
be defined as a list of Pair(s), and are applied from left to right in the case of ordered
collections. (example: `repl=["new" => "old", "bad" => "good"]`).

### Comparison arguments for `isapprox`

Expand Down
20 changes: 14 additions & 6 deletions src/ShowMethodTesting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ end
Base.show(io::IO, ::MIME"text/plain", x::ParsedShow) = print(io, x.parsed_show)

"""
parse_show(x; vector_simplify=true, repl=Dict())
parse_show(x::String; vector_simplify=true, repl=Dict())
parse_show(x; vector_simplify=true, repl=())
parse_show(x::String; vector_simplify=true, repl)

Parse the output of `show` to a `ParsedShow` object, which can be compared with `isapprox` (`≈`).

# Arguments

- `x`: object to parse
- `vector_simplify`: if `true`, only the first and last elements of arrays are kept
- `repl`: dictionary with custom replacements to be made before parsing
- `repl`: container of Pair(s) with custom replacements to be made before parsing. The replacements
will be applied from left to right for ordered collections.

A `ParsedShow` object can be compared to another `ParsedShow` object or a string with `isapprox`.
See the `isapprox` function for more details.
Expand Down Expand Up @@ -49,12 +50,19 @@ Note that in the last line we have set the comparison function for floats to be
`assertion_error` is set to `false`, so the function returns `false` instead of throwing an error.

"""
parse_show(x; vector_simplify=true, repl=Dict()) = parse_show(repr("text/plain", x); vector_simplify, repl)
parse_show(x; vector_simplify=true, repl=()) = parse_show(repr("text/plain", x); vector_simplify, repl)

function parse_show(x::String;
vector_simplify=true,
repl=Dict(),
)
repl=(),
)
if length(repl) > 0 && !(eltype(repl) <: Pair)
throw(ArgumentError("""\n

The `repl` argument must be a container of Pair(s), e.g. Dict("old" => "new") or [ "old" => "new" ].

"""))
end
# Custom replacements
s = x
for (k, v) in repl
Expand Down
3 changes: 2 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ end
@test !isapprox(parse_show(a), "Object with Int(2), /usr/bin/bash and [1.0, 3.141592653589793, 7.5, 1.4142135623730951]"; assertion_error=false)
# Test show method
@test contains(repr("text/plain", parse_show(a)), "Object with Int( 1 )")
@test parse_show([a, a, a, a]; repl=Dict("/usr/bin/bash" => "", r"^((?:[^\n]*\n){3}).*"s => s"\1")) ≈ """
@test parse_show([a, a, a, a]; repl=["/usr/bin/bash" => "", r"^((?:[^\n]*\n){3}).*"s => s"\1"]) ≈ """
4 -element Vector{A}:
Object with Int( 1 ), and [ 1.0 1.4142135623730951 ]
Object with Int( 1 ), and [ 1.0 1.4142135623730951 ]
"""
@test_throws ArgumentError parse_show([a, a, a, a]; repl=["a", "b"])
end
Loading