Skip to content

Commit

Permalink
Merge pull request #17455 from JuliaLang/yyc/tests/random-fixes
Browse files Browse the repository at this point in the history
Random fixes
  • Loading branch information
tkelman authored Jul 17, 2016
2 parents 622d0c7 + ae0a18a commit 39b9d7d
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 66 deletions.
22 changes: 14 additions & 8 deletions base/REPLCompletions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -397,14 +397,20 @@ function dict_identifier_key(str,tag)

frange, end_of_indentifier = find_start_brace(str_close, c_start='[', c_end=']')
isempty(frange) && return (nothing, nothing, nothing)
identifier = Symbol(str[frange[1]:end_of_indentifier])
isdefined(Main,identifier) || return (nothing, nothing, nothing)
obj = Main
for name in split(str[frange[1]:end_of_indentifier], '.')
Base.isidentifier(name) || return (nothing, nothing, nothing)
sym = Symbol(name)
isdefined(obj, sym) || return (nothing, nothing, nothing)
obj = getfield(obj, sym)
# Avoid `isdefined(::Array, ::Symbol)`
isa(obj, Array) && return (nothing, nothing, nothing)
end
begin_of_key = findnext(x->!in(x,whitespace_chars), str, end_of_indentifier+2)
begin_of_key==0 && return (identifier, nothing, nothing)
begin_of_key==0 && return (true, nothing, nothing)
partial_key = str[begin_of_key:end]
main_id = getfield(Main,identifier)
typeof(main_id) <: Associative && length(main_id)<1e6 || return (identifier, nothing, nothing)
main_id, partial_key, begin_of_key
(isa(obj, Associative) && length(obj) < 1e6) || return (true, nothing, nothing)
return (obj, partial_key, begin_of_key)
end

function completions(string, pos)
Expand All @@ -416,8 +422,8 @@ function completions(string, pos)

# if completing a key in a Dict
identifier, partial_key, loc = dict_identifier_key(partial,inc_tag)
if identifier != nothing
if partial_key != nothing
if identifier !== nothing
if partial_key !== nothing
matches = []
for key in keys(identifier)
rkey = repr(key)
Expand Down
2 changes: 1 addition & 1 deletion base/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ function _subtypes(m::Module, x::DataType, sts=Set(), visited=Set())
push!(visited, m)
for s in names(m,true)
if isdefined(m,s)
t = eval(m,s)
t = getfield(m, s)
if isa(t, DataType) && t.name.name == s && supertype(t).name == x.name
ti = typeintersect(t, x)
ti != Bottom && push!(sts, ti)
Expand Down
2 changes: 1 addition & 1 deletion base/replutil.jl
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ function showerror(io::IO, ex::MethodError)
name = ft.name.mt.name
f_is_function = false
kwargs = Any[]
if startswith(string(ft.name), "#kw#")
if startswith(string(ft.name.name), "#kw#")
f = ex.args[2]
ft = typeof(f)
name = ft.name.mt.name
Expand Down
118 changes: 62 additions & 56 deletions test/replcompletions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ module CompletionFoo
const tuple = (1, 2)

test_y_array=[CompletionFoo.Test_y(rand()) for i in 1:10]
test_dict = Dict("abc"=>1, "abcd"=>10, :bar=>2, :bar2=>9, Base=>3,
contains=>4, `ls`=>5, 66=>7, 67=>8, ("q",3)=>11,
"α"=>12, =>13)
end
test_repl_comp_dict = CompletionFoo.test_dict

function temp_pkg_dir(fn::Function)
# Used in tests below to setup and teardown a sandboxed package directory
Expand Down Expand Up @@ -663,59 +667,61 @@ c, r, res = test_complete(s)
@test isempty(c)

# test Dicts
test_dict = Dict("abc"=>1, "abcd"=>10, :bar=>2, :bar2=>9, Base=>3, contains=>4, `ls`=>5,
66=>7, 67=>8, ("q",3)=>11, "α"=>12, =>13)
s="test_dict[\"ab"
c,r = test_complete(s)
@test c == Any["\"abc\"","\"abcd\""]
s="test_dict[\"abcd"
c,r = test_complete(s)
@test c == Any["\"abcd\"]"]
s="test_dict[ \"abcd" # leading whitespace
c,r = test_complete(s)
@test c == Any["\"abcd\"]"]
s="test_dict[\"abcd]" # trailing close bracket
c,r = completions(s,endof(s)-1)
@test c == Any["\"abcd\""]
s="test_dict[:b"
c,r = test_complete(s)
@test c == Any[":bar",":bar2"]
s="test_dict[:bar2"
c,r = test_complete(s)
@test c == Any[":bar2]"]
s="test_dict[Ba"
c,r = test_complete(s)
@test c == Any["Base]"]
s="test_dict[co"
c,r = test_complete(s)
@test c == Any["contains]"]
s="test_dict[`l"
c,r = test_complete(s)
@test c == Any["`ls`]"]
s="test_dict[6"
c,r = test_complete(s)
@test c == Any["66","67"]
s="test_dict[66"
c,r = test_complete(s)
@test c == Any["66]"]
s="test_dict[("
c,r = test_complete(s)
@test c == Any["(\"q\",3)]"]
s="test_dict[\"\\alp"
c,r = test_complete(s)
@test c == String["\\alpha"]
s="test_dict[\"\\alpha"
c,r = test_complete(s)
@test c == String["α"]
s="test_dict[\"α"
c,r = test_complete(s)
@test c == Any["\"α\"]"]
s="test_dict[:\\alp"
c,r = test_complete(s)
@test c == String["\\alpha"]
s="test_dict[:\\alpha"
c,r = test_complete(s)
@test c == String["α"]
s="test_dict[:α"
c,r = test_complete(s)
@test c == Any[":α]"]
function test_dict_completion(dict_name)
s = "$dict_name[\"ab"
c, r = test_complete(s)
@test c == Any["\"abc\"", "\"abcd\""]
s = "$dict_name[\"abcd"
c, r = test_complete(s)
@test c == Any["\"abcd\"]"]
s = "$dict_name[ \"abcd" # leading whitespace
c, r = test_complete(s)
@test c == Any["\"abcd\"]"]
s = "$dict_name[\"abcd]" # trailing close bracket
c, r = completions(s, endof(s) - 1)
@test c == Any["\"abcd\""]
s = "$dict_name[:b"
c, r = test_complete(s)
@test c == Any[":bar", ":bar2"]
s = "$dict_name[:bar2"
c, r = test_complete(s)
@test c == Any[":bar2]"]
s = "$dict_name[Ba"
c, r = test_complete(s)
@test c == Any["Base]"]
s = "$dict_name[co"
c, r = test_complete(s)
@test c == Any["contains]"]
s = "$dict_name[`l"
c, r = test_complete(s)
@test c == Any["`ls`]"]
s = "$dict_name[6"
c, r = test_complete(s)
@test c == Any["66", "67"]
s = "$dict_name[66"
c, r = test_complete(s)
@test c == Any["66]"]
s = "$dict_name[("
c, r = test_complete(s)
@test c == Any["(\"q\",3)]"]
s = "$dict_name[\"\\alp"
c, r = test_complete(s)
@test c == String["\\alpha"]
s = "$dict_name[\"\\alpha"
c, r = test_complete(s)
@test c == String["α"]
s = "$dict_name[\"α"
c, r = test_complete(s)
@test c == Any["\"α\"]"]
s = "$dict_name[:\\alp"
c, r = test_complete(s)
@test c == String["\\alpha"]
s = "$dict_name[:\\alpha"
c, r = test_complete(s)
@test c == String["α"]
s = "$dict_name[:α"
c, r = test_complete(s)
@test c == Any[":α]"]
end
test_dict_completion("CompletionFoo.test_dict")
test_dict_completion("test_repl_comp_dict")
17 changes: 17 additions & 0 deletions test/replutil.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,31 @@ m_error = try method_c6(1, x=1) catch e; e; end
showerror(buf, m_error)
error_out1 = takebuf_string(buf)

module TestKWError
method_c6_in_module(; x=1) = x
method_c6_in_module(a; y=1) = y
end
m_error = try TestKWError.method_c6_in_module(y=1) catch e; e; end
showerror(buf, m_error)
error_out2 = takebuf_string(buf)
m_error = try TestKWError.method_c6_in_module(1, x=1) catch e; e; end
showerror(buf, m_error)
error_out3 = takebuf_string(buf)

if Base.have_color
@test contains(error_out, "method_c6(; x)\e[1m\e[31m got an unsupported keyword argument \"y\"\e[0m")
@test contains(error_out, "method_c6(\e[1m\e[31m::Any\e[0m; y)")
@test contains(error_out1, "method_c6(::Any; y)\e[1m\e[31m got an unsupported keyword argument \"x\"\e[0m")
@test contains(error_out2, "method_c6_in_module(; x)\e[1m\e[31m got an unsupported keyword argument \"y\"\e[0m")
@test contains(error_out2, "method_c6_in_module(\e[1m\e[31m::Any\e[0m; y)")
@test contains(error_out3, "method_c6_in_module(::Any; y)\e[1m\e[31m got an unsupported keyword argument \"x\"\e[0m")
else
@test contains(error_out, "method_c6(; x) got an unsupported keyword argument \"y\"")
@test contains(error_out, "method_c6(!Matched::Any; y)")
@test contains(error_out1, "method_c6(::Any; y) got an unsupported keyword argument \"x\"")
@test contains(error_out2, "method_c6_in_module(; x) got an unsupported keyword argument \"y\"")
@test contains(error_out2, "method_c6_in_module(!Matched::Any; y)")
@test contains(error_out3, "method_c6_in_module(::Any; y) got an unsupported keyword argument \"x\"")
end

method_c7(a, b; kargs...) = a
Expand Down

0 comments on commit 39b9d7d

Please sign in to comment.