Skip to content
This repository was archived by the owner on Mar 11, 2022. It is now read-only.

Commit 020bf5f

Browse files
committed
Improve methods of Base.show
1 parent b291ff1 commit 020bf5f

File tree

8 files changed

+86
-44
lines changed

8 files changed

+86
-44
lines changed

src/DiffinDiffsBase.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module DiffinDiffsBase
22

3-
using CSV
3+
using CSV: File
44
using Reexport
55
using StatsBase
66
@reexport using StatsModels
@@ -14,6 +14,7 @@ export @fieldequal,
1414
kwarg,
1515
@unpack,
1616
exampledata,
17+
sprintcompact,
1718

1819
EleOrVec,
1920
TreatmentSharpness,
@@ -44,8 +45,10 @@ export @fieldequal,
4445
parse_treat,
4546

4647
AbstractDiffinDiffs,
48+
DefaultDID,
4749
did,
4850
DIDSpec,
51+
isnamed,
4952
spec,
5053
@spec,
5154
@did,

src/did.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,20 @@ end
134134
==(a::DIDSpec{T}, b::DIDSpec{T}) where {T<:AbstractDiffinDiffs} =
135135
a.args == b.args && a.kwargs == b.kwargs
136136

137+
isnamed(sp::DIDSpec) = sp.name != ""
138+
139+
function show(io::IO, sp::DIDSpec{T}) where {T}
140+
print(io, "DIDSpec{", sprintcompact(T), "}")
141+
isnamed(sp) && print(io, ": ", sp.name)
142+
if get(io, :compact, false) || !haskey(sp.args, :tr) && !haskey(sp.args, :pr)
143+
return
144+
elseif !isnamed(sp)
145+
print(io, ":")
146+
end
147+
haskey(sp.args, :tr) && print(io, "\n ", sprintcompact(sp.args[:tr]))
148+
haskey(sp.args, :pr) && print(io, "\n ", sprintcompact(sp.args[:pr]))
149+
end
150+
137151
"""
138152
spec(args...; kwargs...)
139153

src/parallels.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ function show(io::IO, pr::NeverTreatedParallel)
111111
print(io, "NeverTreated{", pr.c, ",", pr.s, "}(", pr.e,")")
112112
else
113113
println(io, pr.s, " trends with any never-treated group:")
114-
println(io, " Never-treated groups: ", pr.e)
115-
pr.c isa Unconditional ? nothing : println(io, " ", pr.c)
114+
print(io, " Never-treated groups: ", pr.e)
115+
pr.c isa Unconditional || print(io, "\n ", pr.c)
116116
end
117117
end
118118

@@ -193,8 +193,8 @@ function show(io::IO, pr::NotYetTreatedParallel)
193193
else
194194
println(io, pr.s, " trends with any not-yet-treated group:")
195195
println(io, " Not-yet-treated groups: ", pr.e)
196-
println(io, " Treated since: ", pr.emin isa Nothing ? "not specified" : pr.emin)
197-
pr.c isa Unconditional ? nothing : println(io, " ", pr.c)
196+
print(io, " Treated since: ", pr.emin isa Nothing ? "not specified" : pr.emin)
197+
pr.c isa Unconditional || print(io, "\n ", pr.c)
198198
end
199199
end
200200

src/treatments.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ function show(io::IO, tr::DynamicTreatment)
7272
else
7373
println(io, tr.s, " dynamic treatment:")
7474
println(io, " column name of time variable: ", tr.time)
75-
println(io, " excluded relative time: ", tr.exc)
75+
print(io, " excluded relative time: ", tr.exc)
7676
end
7777
end
7878

src/utils.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,5 +110,7 @@ Return a `CSV.File` by loading the example dataset with the specified name.
110110
function exampledata(name::Union{Symbol,String})
111111
"$(name)" in exampledata() ||
112112
throw(ArgumentError("example dataset $(name) is not found"))
113-
return CSV.File((@__DIR__)*"/../data/$(name).csv")
113+
return File((@__DIR__)*"/../data/$(name).csv")
114114
end
115+
116+
sprintcompact(x) = sprint(show, x; context=:compact=>true)

test/did.jl

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
using DiffinDiffsBase: parse_didargs
22
import DiffinDiffsBase: did
33

4+
const testterm = treat(:g, TR, PR)
5+
46
did(::Type{TestDID}, ::AbstractTreatment, ::AbstractParallel;
57
yterm=:unknown, treatname=:unknown, treatintterms=nothing, xterms=nothing) =
68
(yterm, treatname, treatintterms, xterms)
79

8-
const testterm = treat(:g, TR, PR)
9-
1010
@testset "did wrapper" begin
1111
@testset "DefaultDID" begin
1212
@test_throws ErrorException did(TR, PR)
@@ -86,6 +86,45 @@ end
8686
@test_throws ArgumentError parse_didargs('a', :a, 1)
8787
end
8888

89+
@testset "DIDSpec" begin
90+
@testset "show" begin
91+
sp = DIDSpec("", Dict{Symbol,Any}(), Dict{Symbol,Any}())
92+
@test sprint(show, sp) == "DIDSpec{DefaultDID}"
93+
@test sprintcompact(sp) == "DIDSpec{DefaultDID}"
94+
95+
sp = DIDSpec("name", Dict{Symbol,Any}(), Dict{Symbol,Any}())
96+
@test sprint(show, sp) == "DIDSpec{DefaultDID}: name"
97+
@test sprintcompact(sp) == "DIDSpec{DefaultDID}: name"
98+
99+
sp = DIDSpec("name", Dict(:d=>TestDID, :tr=>TR, :pr=>PR), Dict(:a=>1, :b=>2))
100+
@test sprint(show, sp) == """
101+
DIDSpec{TestDID}: name
102+
TestTreatment(:t, 0)
103+
TestParallel{ParallelCondition,ParallelStrength}(0)"""
104+
@test sprintcompact(sp) == "DIDSpec{TestDID}: name"
105+
106+
sp = DIDSpec("", Dict(:d=>TestDID, :tr=>dynamic(:time,-1), :pr=>nevertreated(-1)),
107+
Dict{Symbol,Any}())
108+
@test sprint(show, sp) == """
109+
DIDSpec{TestDID}:
110+
Dynamic{S}(-1)
111+
NeverTreated{U,P}([-1])"""
112+
@test sprintcompact(sp) == "DIDSpec{TestDID}"
113+
114+
sp = DIDSpec("", Dict(:d=>TestDID, :tr=>dynamic(:time,-1)), Dict{Symbol,Any}())
115+
@test sprint(show, sp) == """
116+
DIDSpec{TestDID}:
117+
Dynamic{S}(-1)"""
118+
@test sprintcompact(sp) == "DIDSpec{TestDID}"
119+
120+
sp = DIDSpec("", Dict(:d=>TestDID, :pr=>nevertreated(-1)), Dict{Symbol,Any}())
121+
@test sprint(show, sp) == """
122+
DIDSpec{TestDID}:
123+
NeverTreated{U,P}([-1])"""
124+
@test sprintcompact(sp) == "DIDSpec{TestDID}"
125+
end
126+
end
127+
89128
@testset "spec @spec" begin
90129
testname = "name"
91130

test/parallels.jl

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
@testset "show" begin
88
@test sprint(show, Unconditional()) == "Unconditional"
9-
@test sprint(show, Unconditional(); context=:compact => true) == "U"
9+
@test sprintcompact(Unconditional()) == "U"
1010

1111
@test sprint(show, Exact()) == "Parallel"
12-
@test sprint(show, Exact(); context=:compact => true) == "P"
12+
@test sprintcompact(Exact()) == "P"
1313
end
1414
end
1515

@@ -68,17 +68,13 @@ end
6868
@testset "show" begin
6969
@test sprint(show, nt0) == """
7070
Parallel trends with any never-treated group:
71-
Never-treated groups: [0]
72-
"""
73-
@test sprint(show, nt0; context=:compact => true) ==
74-
"NeverTreated{U,P}([0])"
71+
Never-treated groups: [0]"""
72+
@test sprintcompact(nt0) == "NeverTreated{U,P}([0])"
7573

7674
@test sprint(show, nt1) == """
7775
Parallel trends with any never-treated group:
78-
Never-treated groups: [0, 1]
79-
"""
80-
@test sprint(show, nt1; context=:compact => true) ==
81-
"NeverTreated{U,P}([0, 1])"
76+
Never-treated groups: [0, 1]"""
77+
@test sprintcompact(nt1) == "NeverTreated{U,P}([0, 1])"
8278
end
8379
end
8480

@@ -152,33 +148,25 @@ end
152148
@test sprint(show, ny0) == """
153149
Parallel trends with any not-yet-treated group:
154150
Not-yet-treated groups: [0]
155-
Treated since: not specified
156-
"""
157-
@test sprint(show, ny0; context=:compact => true) ==
158-
"NotYetTreated{U,P}([0], NA)"
151+
Treated since: not specified"""
152+
@test sprintcompact(ny0) == "NotYetTreated{U,P}([0], NA)"
159153

160154
@test sprint(show, ny1) == """
161155
Parallel trends with any not-yet-treated group:
162156
Not-yet-treated groups: [0, 1]
163-
Treated since: not specified
164-
"""
165-
@test sprint(show, ny1; context=:compact => true) ==
166-
"NotYetTreated{U,P}([0, 1], NA)"
157+
Treated since: not specified"""
158+
@test sprintcompact(ny1) == "NotYetTreated{U,P}([0, 1], NA)"
167159

168160
@test sprint(show, ny2) == """
169161
Parallel trends with any not-yet-treated group:
170162
Not-yet-treated groups: [0, 1]
171-
Treated since: [0]
172-
"""
173-
@test sprint(show, ny2; context=:compact => true) ==
174-
"NotYetTreated{U,P}([0, 1], [0])"
163+
Treated since: [0]"""
164+
@test sprintcompact(ny2) == "NotYetTreated{U,P}([0, 1], [0])"
175165

176166
@test sprint(show, ny3) == """
177167
Parallel trends with any not-yet-treated group:
178168
Not-yet-treated groups: [0, 1]
179-
Treated since: [0, 1]
180-
"""
181-
@test sprint(show, ny3; context=:compact => true) ==
182-
"NotYetTreated{U,P}([0, 1], [0, 1])"
169+
Treated since: [0, 1]"""
170+
@test sprintcompact(ny3) == "NotYetTreated{U,P}([0, 1], [0, 1])"
183171
end
184172
end

test/treatments.jl

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
@testset "show" begin
77
@test sprint(show, SharpDesign()) == "Sharp"
8-
@test sprint(show, SharpDesign(); context=:compact => true) == "S"
8+
@test sprintcompact(SharpDesign()) == "S"
99
end
1010
end
1111

@@ -46,16 +46,12 @@ end
4646
@test sprint(show, dt0) == """
4747
Sharp dynamic treatment:
4848
column name of time variable: month
49-
excluded relative time: -1
50-
"""
51-
@test sprint(show, dt0; context=:compact => true) ==
52-
"Dynamic{S}(-1)"
49+
excluded relative time: -1"""
50+
@test sprintcompact(dt0) == "Dynamic{S}(-1)"
5351
@test sprint(show, dt1) == """
5452
Sharp dynamic treatment:
5553
column name of time variable: month
56-
excluded relative time: [-2, -1]
57-
"""
58-
@test sprint(show, dt1; context=:compact => true) ==
59-
"Dynamic{S}([-2, -1])"
54+
excluded relative time: [-2, -1]"""
55+
@test sprintcompact(dt1) == "Dynamic{S}([-2, -1])"
6056
end
6157
end

0 commit comments

Comments
 (0)