Skip to content

Commit 7457f08

Browse files
authored
Merge pull request #8 from JuliaGraphics/aa/tests
Add tests
2 parents 1bfbd51 + d6c75e0 commit 7457f08

File tree

6 files changed

+142
-107
lines changed

6 files changed

+142
-107
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.jl.cov
2+
*.jl.*.cov
3+
*.jl.mem

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ julia:
88
- nightly
99
notifications:
1010
email: false
11+
after_success:
12+
- julia -e 'cd(Pkg.dir("Showoff")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder())';

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# Showoff
22

3-
[![Build
4-
Status](https://travis-ci.org/JuliaGraphics/Showoff.jl.svg?branch=master)](https://travis-ci.org/JuliaGraphics/Showoff.jl)
3+
[![Showoff](http://pkg.julialang.org/badges/Showoff_0.5.svg)](http://pkg.julialang.org/?pkg=Showoff)
4+
[![Showoff](http://pkg.julialang.org/badges/Showoff_0.6.svg)](http://pkg.julialang.org/?pkg=Showoff)
5+
[![Build Status](https://travis-ci.org/JuliaGraphics/Showoff.jl.svg?branch=master)](https://travis-ci.org/JuliaGraphics/Showoff.jl)
6+
[![Coverage Status](https://coveralls.io/repos/github/JuliaGraphics/Showoff.jl/badge.svg?branch=master)](https://coveralls.io/github/JuliaGraphics/Showoff.jl?branch=master)
57

68
Showoff provides an interface for consistently formatting an array of n things,
79
e.g. numbers, dates, unitful values. It's used in Gadfly to
@@ -58,4 +60,4 @@ trailing the `.`, and look nice when right-aligned.
5860

5961
When no specialized `showoff` is defined, it falls back on the `show` function.
6062

61-
63+
This package was originally written by [Daniel C. Jones](https://github.com/dcjones).

REQUIRE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
julia 0.5
2-
Compat 0.9.5
2+
Compat 0.18.0

src/Showoff.jl

Lines changed: 86 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
VERSION >= v"0.4.0-dev+6641" && __precompile__()
1+
__precompile__()
22

33
module Showoff
44

55
using Compat
6-
if VERSION >= v"0.6.0-dev.1015"
7-
import Base.Iterators: drop
8-
end
6+
import Compat.Iterators: drop
97

108
export showoff
119

@@ -17,23 +15,13 @@ end
1715

1816

1917
function grisu(v::AbstractFloat, mode, requested_digits)
20-
if VERSION < v"0.4-dev"
21-
if isa(v, Float32) && mode == Base.Grisu.SHORTEST
22-
mode = Base.Grisu.SHORTEST_SINGLE
23-
end
24-
@grisu_ccall v mode requested_digits
25-
return Base.Grisu.LEN[1], Base.Grisu.POINT[1], Base.Grisu.NEG, Base.Grisu.DIGITS
26-
elseif VERSION < v"0.5.0-dev+2094"
27-
return Base.Grisu.grisu(v, mode, requested_digits)
28-
else
29-
return tuple(Base.Grisu.grisu(v, mode, requested_digits)..., Base.Grisu.DIGITS)
30-
end
18+
return tuple(Base.Grisu.grisu(v, mode, requested_digits)..., Base.Grisu.DIGITS)
3119
end
3220

3321

3422
# Fallback
3523
function showoff(xs::AbstractArray, style=:none)
36-
result = Array(AbstractString, length(xs))
24+
result = Vector{String}(length(xs))
3725
buf = IOBuffer()
3826
for (i, x) in enumerate(xs)
3927
show(buf, x)
@@ -48,7 +36,7 @@ end
4836

4937
function concrete_minimum(xs)
5038
if done(xs, start(xs))
51-
error("argument must not be empty")
39+
throw(ArgumentError("argument must not be empty"))
5240
end
5341

5442
x_min = first(xs)
@@ -70,7 +58,7 @@ end
7058

7159
function concrete_maximum(xs)
7260
if done(xs, start(xs))
73-
error("argument must not be empty")
61+
throw(ArgumentError("argument must not be empty"))
7462
end
7563

7664
x_max = first(xs)
@@ -103,7 +91,7 @@ end
10391

10492
function scientific_precision_heuristic{T <: AbstractFloat}(xs::AbstractArray{T})
10593
ys = [x == 0.0 ? 0.0 : x / 10.0^floor(log10(abs(x)))
106-
for x in filter(isfinite, xs)]
94+
for x in xs if isfinite(x)]
10795
return plain_precision_heuristic(ys) + 1
10896
end
10997

@@ -115,7 +103,7 @@ function showoff{T <: AbstractFloat}(xs::AbstractArray{T}, style=:auto)
115103
x_max = Float64(Float32(x_max))
116104

117105
if !isfinite(x_min) || !isfinite(x_max)
118-
error("At least one finite value must be provided to formatter.")
106+
throw(ArgumentError("At least one finite value must be provided to formatter."))
119107
end
120108

121109
if style == :auto
@@ -128,17 +116,17 @@ function showoff{T <: AbstractFloat}(xs::AbstractArray{T}, style=:auto)
128116

129117
if style == :plain
130118
precision = plain_precision_heuristic(xs)
131-
return AbstractString[format_fixed(x, precision) for x in xs]
119+
return String[format_fixed(x, precision) for x in xs]
132120
elseif style == :scientific
133121
precision = scientific_precision_heuristic(xs)
134-
return AbstractString[format_fixed_scientific(x, precision, false)
122+
return String[format_fixed_scientific(x, precision, false)
135123
for x in xs]
136124
elseif style == :engineering
137125
precision = scientific_precision_heuristic(xs)
138-
return AbstractString[format_fixed_scientific(x, precision, true)
126+
return String[format_fixed_scientific(x, precision, true)
139127
for x in xs]
140128
else
141-
error("$(style) is not a recongnized number format")
129+
throw(ArgumentError("$(style) is not a recongnized number format"))
142130
end
143131
end
144132

@@ -274,91 +262,88 @@ function format_fixed_scientific(x::AbstractFloat, precision::Integer,
274262
end
275263

276264

277-
278-
if VERSION >= v"0.4-dev"
279-
function showoff{T <: (Union{Date, DateTime})}(ds::AbstractArray{T}, style=:none)
280-
years = Set()
281-
months = Set()
282-
days = Set()
283-
hours = Set()
284-
minutes = Set()
285-
seconds = Set()
286-
for d in ds
287-
push!(years, Dates.year(d))
288-
push!(months, Dates.month(d))
289-
push!(days, Dates.day(d))
290-
push!(hours, Dates.hour(d))
291-
push!(minutes, Dates.minute(d))
292-
push!(seconds, Dates.second(d))
293-
end
294-
all_same_year = length(years) == 1
295-
all_one_month = length(months) == 1 && 1 in months
296-
all_one_day = length(days) == 1 && 1 in days
297-
all_zero_hour = length(hours) == 1 && 0 in hours
298-
all_zero_minute = length(minutes) == 1 && 0 in minutes
299-
all_zero_seconds = length(minutes) == 1 && 0 in minutes
300-
all_zero_milliseconds = length(minutes) == 1 && 0 in minutes
301-
302-
# first label format
303-
label_months = false
304-
label_days = false
305-
f1 = "u d, yyyy"
306-
f2 = ""
307-
if !all_zero_seconds
308-
f2 = "HH:MM:SS.sss"
309-
elseif !all_zero_seconds
310-
f2 = "HH:MM:SS"
311-
elseif !all_zero_hour || !all_zero_minute
312-
f2 = "HH:MM"
313-
else
314-
if !all_one_day
315-
first_label_format = "u d yyyy"
316-
elseif !all_one_month
317-
first_label_format = "u yyyy"
318-
elseif !all_one_day
319-
first_label_format = "yyyy"
320-
end
321-
end
322-
if f2 != ""
323-
first_label_format = string(f1, " ", f2)
324-
else
325-
first_label_format = f1
265+
function showoff{T <: (Union{Date, DateTime})}(ds::AbstractArray{T}, style=:none)
266+
years = Set()
267+
months = Set()
268+
days = Set()
269+
hours = Set()
270+
minutes = Set()
271+
seconds = Set()
272+
for d in ds
273+
push!(years, Dates.year(d))
274+
push!(months, Dates.month(d))
275+
push!(days, Dates.day(d))
276+
push!(hours, Dates.hour(d))
277+
push!(minutes, Dates.minute(d))
278+
push!(seconds, Dates.second(d))
279+
end
280+
all_same_year = length(years) == 1
281+
all_one_month = length(months) == 1 && 1 in months
282+
all_one_day = length(days) == 1 && 1 in days
283+
all_zero_hour = length(hours) == 1 && 0 in hours
284+
all_zero_minute = length(minutes) == 1 && 0 in minutes
285+
all_zero_seconds = length(minutes) == 1 && 0 in minutes
286+
all_zero_milliseconds = length(minutes) == 1 && 0 in minutes
287+
288+
# first label format
289+
label_months = false
290+
label_days = false
291+
f1 = "u d, yyyy"
292+
f2 = ""
293+
if !all_zero_seconds
294+
f2 = "HH:MM:SS.sss"
295+
elseif !all_zero_seconds
296+
f2 = "HH:MM:SS"
297+
elseif !all_zero_hour || !all_zero_minute
298+
f2 = "HH:MM"
299+
else
300+
if !all_one_day
301+
first_label_format = "u d yyyy"
302+
elseif !all_one_month
303+
first_label_format = "u yyyy"
304+
elseif !all_one_day
305+
first_label_format = "yyyy"
326306
end
307+
end
308+
if f2 != ""
309+
first_label_format = string(f1, " ", f2)
310+
else
311+
first_label_format = f1
312+
end
327313

328-
labels = Array(AbstractString, length(ds))
329-
labels[1] = Dates.format(ds[1], first_label_format)
330-
d_last = ds[1]
331-
for (i, d) in enumerate(ds[2:end])
332-
if Dates.year(d) != Dates.year(d_last)
333-
if all_one_day && all_one_month
334-
f1 = "yyyy"
335-
elseif all_one_day && !all_one_month
336-
f1 = "u yyyy"
337-
else
338-
f1 = "u d, yyyy"
339-
end
340-
elseif Dates.month(d) != Dates.month(d_last)
341-
f1 = all_one_day ? "u" : "u d"
342-
elseif Dates.day(d) != Dates.day(d_last)
343-
f1 = "d"
344-
else
345-
f1 = ""
346-
end
347-
348-
if f2 != ""
349-
f = string(f1, " ", f2)
350-
elseif f1 != ""
351-
f = f1
314+
labels = Vector{String}(length(ds))
315+
labels[1] = Dates.format(ds[1], first_label_format)
316+
d_last = ds[1]
317+
for (i, d) in enumerate(ds[2:end])
318+
if Dates.year(d) != Dates.year(d_last)
319+
if all_one_day && all_one_month
320+
f1 = "yyyy"
321+
elseif all_one_day && !all_one_month
322+
f1 = "u yyyy"
352323
else
353-
f = first_label_format
324+
f1 = "u d, yyyy"
354325
end
326+
elseif Dates.month(d) != Dates.month(d_last)
327+
f1 = all_one_day ? "u" : "u d"
328+
elseif Dates.day(d) != Dates.day(d_last)
329+
f1 = "d"
330+
else
331+
f1 = ""
332+
end
355333

356-
labels[i+1] = Dates.format(d, f)
357-
d_last = d
334+
if f2 != ""
335+
f = string(f1, " ", f2)
336+
elseif f1 != ""
337+
f = f1
338+
else
339+
f = first_label_format
358340
end
359341

360-
return labels
342+
labels[i+1] = Dates.format(d, f)
343+
d_last = d
361344
end
345+
346+
return labels
362347
end
363348

364349

test/runtests.jl

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,48 @@
11
using Showoff
22
using Base.Test
33

4-
# write your own tests here
5-
@test 1 == 1
4+
@testset "Internals" begin
5+
@test Showoff.@grisu_ccall(1, 2, 3) === nothing
6+
@test Showoff.grisu(1.0, Base.Grisu.SHORTEST, 2) == (1, 1, false, Base.Grisu.DIGITS)
7+
8+
let x = [1.0, Inf, 2.0, NaN]
9+
@test Showoff.concrete_minimum(x) == 1.0
10+
@test Showoff.concrete_maximum(x) == 2.0
11+
end
12+
13+
@test_throws ArgumentError Showoff.concrete_minimum([])
14+
@test_throws ArgumentError Showoff.concrete_maximum([])
15+
16+
let x = [1.12345, 4.5678]
17+
@test Showoff.plain_precision_heuristic(x) == 5
18+
@test Showoff.scientific_precision_heuristic(x) == 6
19+
end
20+
end
21+
22+
@testset "Formatting" begin
23+
@test Showoff.format_fixed(-10.0, 0) == "-10"
24+
@test Showoff.format_fixed(0.012345, 3) == "0.012"
25+
@test Showoff.format_fixed(Inf, 1) == ""
26+
@test Showoff.format_fixed(-Inf, 1) == "-∞"
27+
@test Showoff.format_fixed(NaN, 1) == "NaN"
28+
@test Showoff.format_fixed_scientific(0.0, 1, false) == "0"
29+
@test Showoff.format_fixed_scientific(Inf, 1, false) == ""
30+
@test Showoff.format_fixed_scientific(-Inf, 1, false) == "-∞"
31+
@test Showoff.format_fixed_scientific(NaN, 1, false) == "NaN"
32+
@test Showoff.format_fixed_scientific(0.012345678, 4, true) == "12.34568×10⁻³"
33+
@test Showoff.format_fixed_scientific(0.012345678, 4, false) == "1.234568×10⁻²"
34+
@test Showoff.format_fixed_scientific(-10.0, 4, false) == "-1.000×10¹"
35+
end
36+
37+
@testset "Showoff" begin
38+
x = [1.12345, 4.5678]
39+
@test showoff(x) == ["1.12345", "4.56780"]
40+
@test showoff([0.0, 50000.0]) == ["0", "5×10⁴"]
41+
@test showoff(x, :plain) == ["1.12345", "4.56780"]
42+
@test showoff(x, :scientific) == ["1.12345×10⁰", "4.56780×10⁰"]
43+
@test showoff(x, :engineering) == ["1.12345×10⁰", "4.56780×10⁰"]
44+
@test showoff([Dates.DateTime("2017-04-11", "yyyy-mm-dd")]) == ["Apr 11, 2017"]
45+
@test showoff(["a", "b"]) == ["\"a\"", "\"b\""]
46+
@test_throws ArgumentError showoff(x, :nevergonnagiveyouup)
47+
@test_throws ArgumentError showoff([Inf, Inf, NaN])
48+
end

0 commit comments

Comments
 (0)