Skip to content

Commit

Permalink
iter
Browse files Browse the repository at this point in the history
  • Loading branch information
wookay committed Nov 3, 2016
1 parent 0e4bf52 commit c9a4547
Show file tree
Hide file tree
Showing 9 changed files with 219 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .codecov.yml
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
comment: false
coverage:
status:
project: false
patch: false
changes: false
11 changes: 4 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@ julia:
notifications:
email: false
# uncomment the following lines to override the default test script
#script:
# - if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
# - julia -e 'Pkg.clone(pwd()); Pkg.build("Duiz"); Pkg.test("Duiz"; coverage=true)'
script:
- if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
- julia --color=yes -e 'Pkg.clone(pwd()); Pkg.build("Duiz"); Pkg.test("Duiz"; coverage=true)'
after_success:
# push coverage results to Coveralls
- julia -e 'cd(Pkg.dir("Duiz")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder())'
# push coverage results to Codecov
- julia -e 'cd(Pkg.dir("Duiz")); Pkg.add("Coverage"); using Coverage; Codecov.submit(Codecov.process_folder())'
- julia --color=yes -e 'cd(Pkg.dir("Duiz")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder()); Codecov.submit(Codecov.process_folder())'
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
# Duiz

[![Travis CI](https://api.travis-ci.org/wookay/Duiz.jl.svg?branch=master)](https://travis-ci.org/wookay/Duiz.jl)
[![AppVeyor](https://ci.appveyor.com/api/projects/status/8149pgwicks5ssvs?svg=true)](https://ci.appveyor.com/project/wookay/duiz-jl)
[![Codecov](https://codecov.io/gh/wookay/Duiz.jl/branch/master/graph/badge.svg)](https://codecov.io/gh/wookay/Duiz.jl)
[![Coveralls](https://coveralls.io/repos/github/wookay/Duiz.jl/badge.svg?branch=master)](https://coveralls.io/github/wookay/Duiz.jl?branch=master)


```julia
julia> using Duiz

julia> Duiz.iter([(1,2),(5,6)])
e = Tuple{Int64,Int64}[(1,2),(5,6)]
1 == start(e)
false == done(e, 1)
((1,2), 2) == next(e, 1)
false == done(e, 2)
((5,6), 3) == next(e, 2)
true == done(e, 3)
```
4 changes: 2 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ install:
build_script:
# Need to convert from shallow to complete for Pkg.clone to work
- IF EXIST .git\shallow (git fetch --unshallow)
- C:\projects\julia\bin\julia -e "versioninfo();
- C:\projects\julia\bin\julia --color=yes -e "versioninfo();
Pkg.clone(pwd(), \"Duiz\"); Pkg.build(\"Duiz\")"

test_script:
- C:\projects\julia\bin\julia -e "Pkg.test(\"Duiz\")"
- C:\projects\julia\bin\julia --color=yes -e "Pkg.test(\"Duiz\")"
5 changes: 4 additions & 1 deletion src/Duiz.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
__precompile__(true)

module Duiz

# package code goes here
include("util.jl")
include("iter.jl")

end # module
39 changes: 39 additions & 0 deletions src/iter.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# module Duiz

function iter(e)::Union{Void,String}
iter_trace(e, settings[:output])
end

function iter_trace(g::Base.Generator, output::Function)::Union{Void,String}
iter_trace(g.iter, output)
end

function iter_trace(e, output::Function)::Union{Void,String}
out = String[]
function proc(x)
if string == output
push!(out, x)
else
println(x)
end
end
padding = 11
proc(string(lpad("e", padding), " = $e"))
state = start(e)
proc(string(lpad(state, padding), " == start(e)"))
while true
fin = done(e, state)
proc(string(lpad(fin, padding) , " == $done(e, $state)"))
fin && break
(value, next_state) = next(e, state)
val = string == output ? value : with_color(:green, value)
proc(string(repeat(" ", max(0, padding - length(string(value,next_state)) - 4)),
"(", val, ", $next_state) == next(e, $state)"))
state = next_state
end
if string == output
string(join(out, "\n"), "\n")
else
nothing
end
end
13 changes: 13 additions & 0 deletions src/util.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# module Duiz

settings = Dict{Symbol,Any}(
:output => println
)

function set_output(f::Function)
settings[:output] = f
end

function with_color(name::Symbol, text)::String
string(Base.text_colors[name], text, Base.color_normal)
end
116 changes: 116 additions & 0 deletions test/iter.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
using Base.Test
using Duiz

Duiz.set_output(string)

@test Duiz.iter([(1,2),(5,6)]) == """
e = Tuple{Int64,Int64}[(1,2),(5,6)]
1 == start(e)
false == done(e, 1)
((1,2), 2) == next(e, 1)
false == done(e, 2)
((5,6), 3) == next(e, 2)
true == done(e, 3)
"""

@test Duiz.iter(([1,2],[5,6])) == """
e = ([1,2],[5,6])
1 == start(e)
false == done(e, 1)
([1,2], 2) == next(e, 1)
false == done(e, 2)
([5,6], 3) == next(e, 2)
true == done(e, 3)
"""

@test Duiz.iter(Dict(:a=>5,:b=>6,:c=>7)) == """
e = Dict(:c=>7,:a=>5,:b=>6)
1 == start(e)
false == done(e, 1)
(:c=>7, 10) == next(e, 1)
false == done(e, 10)
(:a=>5, 11) == next(e, 10)
false == done(e, 11)
(:b=>6, 17) == next(e, 11)
true == done(e, 17)
"""

@test Duiz.iter((5,6,7,8)) == """
e = (5,6,7,8)
1 == start(e)
false == done(e, 1)
(5, 2) == next(e, 1)
false == done(e, 2)
(6, 3) == next(e, 2)
false == done(e, 3)
(7, 4) == next(e, 3)
false == done(e, 4)
(8, 5) == next(e, 4)
true == done(e, 5)
"""

@test Duiz.iter([5,6,7,8]) == """
e = [5,6,7,8]
1 == start(e)
false == done(e, 1)
(5, 2) == next(e, 1)
false == done(e, 2)
(6, 3) == next(e, 2)
false == done(e, 3)
(7, 4) == next(e, 3)
false == done(e, 4)
(8, 5) == next(e, 4)
true == done(e, 5)
"""

@test Duiz.iter(7:10) == """
e = 7:10
7 == start(e)
false == done(e, 7)
(7, 8) == next(e, 7)
false == done(e, 8)
(8, 9) == next(e, 8)
false == done(e, 9)
(9, 10) == next(e, 9)
false == done(e, 10)
(10, 11) == next(e, 10)
true == done(e, 11)
"""

@test Duiz.iter(1:5:11) == """
e = 1:5:11
1 == start(e)
false == done(e, 1)
(1, 6) == next(e, 1)
false == done(e, 6)
(6, 11) == next(e, 6)
false == done(e, 11)
(11, 16) == next(e, 11)
true == done(e, 16)
"""

@test Duiz.iter(12.0:-5:0.0) == """
e = 12.0:-5.0:2.0
0 == start(e)
false == done(e, 0)
(12.0, 1) == next(e, 0)
false == done(e, 1)
(7.0, 2) == next(e, 1)
false == done(e, 2)
(2.0, 3) == next(e, 2)
true == done(e, 3)
"""

@test Duiz.iter((2x for x in [5,6,7,90])) == """
e = [5,6,7,90]
1 == start(e)
false == done(e, 1)
(5, 2) == next(e, 1)
false == done(e, 2)
(6, 3) == next(e, 2)
false == done(e, 3)
(7, 4) == next(e, 3)
false == done(e, 4)
(90, 5) == next(e, 4)
true == done(e, 5)
"""
21 changes: 17 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
using Duiz
using Base.Test
all_tests = []
for (root, dirs, files) in walkdir(".")
for filename in files
!endswith(filename, ".jl") && continue
"runtests.jl" == filename && continue
filepath = joinpath(root, filename)[3:end]
!isempty(ARGS) && !any(x->startswith(filepath, x), ARGS) && continue
push!(all_tests, filepath)
end
end

# write your own tests here
@test 1 == 2
for (idx, filepath) in enumerate(all_tests)
numbering = string(idx, /, length(all_tests))
ts = Base.Test.@testset "$numbering $filepath" begin
include(filepath)
end
isdefined(Base.Test, :print_test_results) && Base.Test.print_test_results(ts)
end

0 comments on commit c9a4547

Please sign in to comment.