From 00e93be33bb3915b4f49817881f04e38fa3547b6 Mon Sep 17 00:00:00 2001 From: Emmanuel Raviart Date: Mon, 23 May 2016 17:06:07 +0200 Subject: [PATCH] Update to Julia 0.4. --- src/Biryani.jl | 2 - src/base.jl | 56 ++++++++--------- src/dates.jl | 10 +++- test/REQUIRE | 1 - test/test_base.jl | 146 ++++++++++++++++++++++----------------------- test/test_dates.jl | 2 - test/test_json.jl | 4 +- 7 files changed, 111 insertions(+), 110 deletions(-) diff --git a/src/Biryani.jl b/src/Biryani.jl index 842c430..bf75e3e 100644 --- a/src/Biryani.jl +++ b/src/Biryani.jl @@ -45,8 +45,6 @@ module DatesConverters export date_to_iso8601_string, iso8601_input_to_date, iso8601_string_to_date, to_date - import Dates: Date - import ..Biryani: call, Context, Convertible, N_, pipe, strip, test diff --git a/src/base.jl b/src/base.jl index f3ae289..fdfa543 100644 --- a/src/base.jl +++ b/src/base.jl @@ -36,7 +36,7 @@ type EmptyContext <: Context end -_(context::EmptyContext, message::String) = message +_(context::EmptyContext, message::AbstractString) = message N_(message) = message @@ -120,7 +120,7 @@ function embed_error(value, error) for (child_key, child_error) in error child_error = embed_error(get(value, child_key, nothing), child_error) if child_error !== nothing - errors = get!(value, "errors", (typeof(child_key) => Any)[]) + errors = get!(value, "errors", Dict{typeof(child_key), Any}()) errors[child_key] = child_error end end @@ -140,9 +140,9 @@ function embed_error(value, error) end return nothing end - if all(key -> isa(key, String) && isdigit(key) && 1 <= int(key) <= size(value, 1), error) + if all(key -> isa(key, AbstractString) && isdigit(key) && 1 <= parse(Int, key) <= size(value, 1), error) for (child_key, child_error) in error - child_error = embed_error(value[int(child_key)], child_error) + child_error = embed_error(value[parse(Int, child_key)], child_error) @assert(child_error === nothing, child_error) # if child_error !== nothing # return error @@ -179,9 +179,9 @@ end eval_error(context::Context, func::Function) = func(context) -eval_error(context::Context, ::Nothing) = nothing +eval_error(context::Context, ::Void) = nothing -eval_error(context::Context, message::String) = _(context, message) +eval_error(context::Context, message::AbstractString) = _(context, message) extract_when_singleton(convertible::Convertible) = condition( @@ -203,9 +203,9 @@ function fail(convertible::Convertible; error = nothing) ) end -fail(error::String) = convertible::Convertible -> fail(convertible, error = error) +fail(error::AbstractString) = convertible::Convertible -> fail(convertible, error = error) -fail(; error::String = nothing) = convertible::Convertible -> fail(convertible, error = error) +fail(; error::AbstractString = nothing) = convertible::Convertible -> fail(convertible, error = error) function first_match(converters::Function...; error = nothing) @@ -254,7 +254,7 @@ function guess_bool(convertible::Convertible) if convertible.error !== nothing || convertible.value === nothing return convertible end - if !isa(convertible.value, String) + if !isa(convertible.value, AbstractString) return to_bool(convertible) end lower_value = strip(convertible.value) |> lowercase @@ -421,7 +421,7 @@ function string_to_email(convertible::Convertible; accept_ip_address = false) return convertible end value = lowercase(convertible.value) - if beginswith(value, "mailto:") + if startswith(value, "mailto:") value = replace(value, "mailto:", "", 1) end split_value = split(value, '@') @@ -458,7 +458,7 @@ function string_to_ipv4(convertible::Convertible) if !ismatch(r"^\d+\.\d+\.\d+\.\d+$", convertible.value) return Convertible(convertible.value, convertible.context, N_("Invalid IPv4 address.")) end - bytes = map(int, split(convertible.value, '.')) + bytes = map(value -> parse(Int, value), split(convertible.value, '.')) if !all(byte -> 0 <= byte <= 255, bytes) return Convertible(convertible.value, convertible.context, N_("Invalid IPv4 address.")) end @@ -525,7 +525,7 @@ function struct(converters::Tuple; default = nothing) values = tuple(values..., nothing) end converted_values = cell(length(values_converter)) - error_by_index = (Int => Any)[] + error_by_index = Dict{Int,Any}() for (index, (converter, value)) in enumerate(zip(values_converter, values)) converted = converter(Convertible(value, convertible.context)) converted_values[index] = converted.value @@ -658,7 +658,7 @@ function test_in(values; error = nothing, handle_nothing = false) end -function test_isa(data_type::Union(DataType, UnionType); error = nothing, handle_nothing = false) +function test_isa(data_type::Union{DataType, Union}; error = nothing, handle_nothing = false) """Return a converter that accepts only an instance of given type.""" return test( value -> isa(value, data_type), @@ -703,15 +703,15 @@ end function to_bool(value, context::Context) try - return Convertible(bool(convert(Int, value)), context) + return Convertible(convert(Int, value) != 0, context) catch return Convertible(value, context, N_("Value must be a boolean.")) end end -function to_bool(value::String, context::Context) +function to_bool(value::AbstractString, context::Context) try - return Convertible(bool(int(value)), context) + return Convertible(parse(Int, value) != 0, context) catch return Convertible(value, context, N_("Value must be a boolean.")) end @@ -727,7 +727,7 @@ function to_float(convertible::Convertible; accept_expression = false) return convertible end value = convertible.value - if accept_expression && isa(value, String) + if accept_expression && isa(value, AbstractString) value = strip(value) if !ismatch(r"^[ \t\n\r\d.+\-*/()]+$", value) return Convertible(value, convertible.context, N_("Value must be a valid floating point expression.")) @@ -745,7 +745,7 @@ function to_float(; accept_expression = false) return convertible::Convertible -> to_float(convertible; accept_expression = accept_expression) end -function to_float(value::String, context::Context) +function to_float(value::AbstractString, context::Context) try return Convertible(float(value), context) catch @@ -771,7 +771,7 @@ function to_int(convertible::Convertible; accept_expression = false) return convertible end value = convertible.value - if accept_expression && isa(value, String) + if accept_expression && isa(value, AbstractString) value = strip(value) if !ismatch(r"^[ \t\n\r\d.+\-*/()]+$", value) return Convertible(value, convertible.context, N_("Value must be a valid integer expression.")) @@ -789,9 +789,9 @@ function to_int(; accept_expression = false) return convertible::Convertible -> to_int(convertible; accept_expression = accept_expression) end -function to_int(value::String, context::Context) +function to_int(value::AbstractString, context::Context) try - return Convertible(int(value), context) + return Convertible(parse(Int, value), context) catch return Convertible(value, context, N_("Value must be an integer number.")) end @@ -838,7 +838,7 @@ function uniform_mapping(key_converter::Function, value_converters::Function...; if convertible.error !== nothing || convertible.value === nothing return convertible end - error_by_key = (Any => Any)[] + error_by_key = Dict{Any, Any}() value_by_key = OrderedDict{Any, Any}() for (key, value) in convertible.value key_converted = key_converter(Convertible(key, convertible.context)) @@ -859,10 +859,10 @@ function uniform_mapping(key_converter::Function, value_converters::Function...; typed_value_by_key = OrderedDict{ key_type === nothing ? - (isempty(value_by_key) ? Any : mapreduce(typeof, promote_type, Nothing, keys(value_by_key))) : + (isempty(value_by_key) ? Any : mapreduce(typeof, promote_type, Void, keys(value_by_key))) : key_type, value_type === nothing ? - (isempty(value_by_key) ? Any : mapreduce(typeof, promote_type, Nothing, values(value_by_key))) : + (isempty(value_by_key) ? Any : mapreduce(typeof, promote_type, Void, values(value_by_key))) : value_type, }() for (key, value) in value_by_key @@ -873,8 +873,8 @@ function uniform_mapping(key_converter::Function, value_converters::Function...; return Convertible(typed_value_by_key, convertible.context) else typed_error_by_key = OrderedDict{ - key_type === nothing ? mapreduce(typeof, promote_type, Nothing, keys(error_by_key)) : key_type, - value_type === nothing ? mapreduce(typeof, promote_type, Nothing, values(error_by_key)) : value_type, + key_type === nothing ? mapreduce(typeof, promote_type, Void, keys(error_by_key)) : key_type, + value_type === nothing ? mapreduce(typeof, promote_type, Void, values(error_by_key)) : value_type, }() for (key, error) in error_by_key typed_error_by_key[key] = error @@ -892,7 +892,7 @@ function uniform_sequence(converters::Function...; drop_nothing = false, item_ty if convertible.error !== nothing || convertible.value === nothing return convertible end - error_by_index = (Int => Any)[] + error_by_index = Dict{Any, Any}() values = Any[] for (index, value) in enumerate(convertible.value) converted = pipe(converters...)(Convertible(value, convertible.context)) @@ -905,7 +905,7 @@ function uniform_sequence(converters::Function...; drop_nothing = false, item_ty end return Convertible( collect(item_type === nothing ? - (isempty(values) ? Any : mapreduce(typeof, promote_type, Nothing, values)) : + (isempty(values) ? Any : mapreduce(typeof, promote_type, Void, values)) : item_type, values), convertible.context, isempty(error_by_index) ? nothing : error_by_index, diff --git a/src/dates.jl b/src/dates.jl index 968f743..2d9967e 100644 --- a/src/dates.jl +++ b/src/dates.jl @@ -52,9 +52,15 @@ function to_date(convertible::Convertible) # return Convertible(Date(convertible.value), convertible.context) end -to_date(value::(Int...), context::Context) = Convertible(Date(value...), context) +to_date(year::Int, context::Context) = Convertible(Date(year), context) +to_date(year::Int, month::Int, context::Context) = Convertible(Date(year, month), context) +to_date(year::Int, month::Int, day::Int, context::Context) = Convertible(Date(year, month, day), context) -function to_date(value::String, context::Context) +to_date(value::Tuple{Int}, context::Context) = Convertible(Date(value[1]), context) +to_date(value::Tuple{Int, Int}, context::Context) = Convertible(Date(value[1], value[2]), context) +to_date(value::Tuple{Int, Int, Int}, context::Context) = Convertible(Date(value[1], value[2], value[3]), context) + +function to_date(value::AbstractString, context::Context) if !('-' in value) # Work around bug in Julia 0.3: Date("2013") throws: # ERROR: ArgumentError("Delimiter mismatch. Couldn't find first delimiter, \"-\", in date string") diff --git a/test/REQUIRE b/test/REQUIRE index a65ecb3..306d5e7 100644 --- a/test/REQUIRE +++ b/test/REQUIRE @@ -1,3 +1,2 @@ -Dates JSON Slugify diff --git a/test/test_base.jl b/test/test_base.jl index 92a3922..784191e 100644 --- a/test/test_base.jl +++ b/test/test_base.jl @@ -23,7 +23,7 @@ # call -@test Convertible("42") |> call(int) |> to_value == 42 +@test Convertible("42") |> call(value -> parse(Int, value)) |> to_value == 42 @test Convertible([3, 2, 1]) |> call(sort) |> to_value == [1, 2, 3] @test Convertible(42) |> call(value -> value + 1) |> to_value == 43 @test Convertible(nothing) |> call(value -> value + 1) |> to_value === nothing @@ -57,8 +57,8 @@ detect_unknown_values = condition( @test Convertible(" ") |> strip |> empty_to_nothing |> to_value === nothing @test Convertible([]) |> empty_to_nothing |> to_value === nothing @test Convertible([42, 43]) |> empty_to_nothing |> to_value == [42, 43] -@test Convertible({}) |> empty_to_nothing |> to_value === nothing -@test Convertible(["answer" => 42]) |> empty_to_nothing |> to_value == ["answer" => 42] +@test Convertible(Dict()) |> empty_to_nothing |> to_value === nothing +@test Convertible(Dict("answer" => 42)) |> empty_to_nothing |> to_value == Dict("answer" => 42) @test Convertible(nothing) |> empty_to_nothing |> to_value === nothing @test Convertible("") |> fail("Initial error.") |> empty_to_nothing |> to_value_error == ("", "Initial error.") @@ -213,7 +213,7 @@ detect_unknown_values = condition( @test Convertible(["42"]) |> item_or_sequence(input_to_int) |> to_value == 42 @test Convertible(["42", "43"]) |> item_or_sequence(input_to_int) |> to_value == [42, 43] @test Convertible(["42", "43", "Hello world!"]) |> item_or_sequence(input_to_int) |> to_value_error == ( - [42, 43, "Hello world!"], [3 => "Value must be an integer number."]) + [42, 43, "Hello world!"], Dict(3 => "Value must be an integer number.")) @test Convertible(nothing) |> item_or_sequence(input_to_int) |> to_value === nothing @test Convertible([nothing]) |> item_or_sequence(input_to_int, drop_nothing = true) |> to_value == [] @test Convertible([nothing, nothing]) |> item_or_sequence(input_to_int) |> to_value == [nothing, nothing] @@ -236,8 +236,8 @@ detect_unknown_values = condition( # pipe @test_throws MethodError Convertible(42) |> input_to_bool @test_throws MethodError Convertible(42) |> pipe(input_to_bool) -@test Convertible(42) |> pipe(test_isa(String), input_to_bool) |> to_value_error == (42, - "Value must be an instance of String.") +@test Convertible(42) |> pipe(test_isa(AbstractString), input_to_bool) |> to_value_error == (42, + "Value must be an instance of AbstractString.") @test Convertible(42) |> pipe(to_string, input_to_bool) |> to_value === true @test Convertible(42) |> pipe() |> to_value == 42 @test Convertible(42) |> fail("Initial error.") |> pipe(to_string, input_to_bool) |> to_value_error == (42, @@ -259,90 +259,90 @@ detect_unknown_values = condition( # struct dict_strict_converter = struct( - [ + Dict( "name" => pipe(strip, require), "age" => input_to_int, "email" => input_to_email, - ], + ), ) -@test Convertible([ +@test Convertible(Dict( "name" => "John Doe", "age" => "72", "email" => "john@doe.name", -]) |> dict_strict_converter |> to_value == [ +)) |> dict_strict_converter |> to_value == Dict( "name" => "John Doe", "age" => 72, "email" => "john@doe.name", -] -@test Convertible([ +) +@test Convertible(Dict( "name" => "John Doe", "email" => "john@doe.name", -]) |> dict_strict_converter |> to_value == [ +)) |> dict_strict_converter |> to_value == Dict( "name" => "John Doe", "age" => nothing, "email" => "john@doe.name", -] -@test Convertible([ +) +@test Convertible(Dict( "name" => "John Doe", "age" => nothing, "email" => "john@doe.name", -]) |> dict_strict_converter |> to_value == [ +)) |> dict_strict_converter |> to_value == Dict( "name" => "John Doe", "age" => nothing, "email" => "john@doe.name", -] -@test Convertible([ +) +@test Convertible(Dict( "name" => "John Doe", "age" => "72", "email" => "john@doe.name", "phone" => " +33 9 12 34 56 78 ", -]) |> dict_strict_converter |> to_value_error == ( - [ +)) |> dict_strict_converter |> to_value_error == ( + Dict( "name" => "John Doe", "age" => 72, "email" => "john@doe.name", "phone" => " +33 9 12 34 56 78 ", - ], - [ + ), + Dict( "phone" => "Unexpected item.", - ], + ), ) dict_non_strict_converter = struct( - [ + Dict( "name" => pipe(strip, require), "age" => input_to_int, "email" => input_to_email, - ], + ), default = strip, ) -@test Convertible([ +@test Convertible(Dict( "name" => "John Doe", "age" => "72", "email" => "john@doe.name", -]) |> dict_non_strict_converter |> to_value == [ +)) |> dict_non_strict_converter |> to_value == Dict( "name" => "John Doe", "age" => 72, "email" => "john@doe.name", -] -@test Convertible([ +) +@test Convertible(Dict( "name" => "John Doe", "email" => "john@doe.name", -]) |> dict_non_strict_converter |> to_value == [ +)) |> dict_non_strict_converter |> to_value == Dict( "name" => "John Doe", "age" => nothing, "email" => "john@doe.name", -] -@test Convertible([ +) +@test Convertible(Dict( "name" => "John Doe", "age" => "72", "email" => "john@doe.name", "phone" => " +33 9 12 34 56 78 ", -]) |> dict_non_strict_converter |> to_value == [ +)) |> dict_non_strict_converter |> to_value == Dict( "name" => "John Doe", "age" => 72, "email" => "john@doe.name", "phone" => "+33 9 12 34 56 78", -] +) tuple_strict_converter = struct( ( pipe(strip, require), @@ -380,9 +380,9 @@ tuple_strict_converter = struct( "john@doe.name", " +33 9 12 34 56 78 ", ), - [ + Dict( 4 => "Unexpected item.", - ], + ), ) tuple_non_strict_converter = struct( ( @@ -422,27 +422,27 @@ tuple_non_strict_converter = struct( "+33 9 12 34 56 78", ) @test Convertible(nothing) |> dict_strict_converter |> to_value === nothing -@test Convertible([ +@test Convertible(Dict( "name" => "John Doe", "age" => "72", "email" => "john@doe.name", -]) |> fail("Initial error.") |> dict_strict_converter |> to_value_error == ( - [ +)) |> fail("Initial error.") |> dict_strict_converter |> to_value_error == ( + Dict( "name" => "John Doe", "age" => "72", "email" => "john@doe.name", - ], + ), "Initial error.") # test -@test Convertible("hello") |> test(value -> isa(value, String)) |> to_value == "hello" -@test Convertible(1) |> test(value -> isa(value, String)) |> to_value_error == (1, "Test failed.") -@test Convertible(1) |> test(value -> isa(value, String), error = "Value is not a string.") |> to_value_error == (1, +@test Convertible("hello") |> test(value -> isa(value, AbstractString)) |> to_value == "hello" +@test Convertible(1) |> test(value -> isa(value, AbstractString)) |> to_value_error == (1, "Test failed.") +@test Convertible(1) |> test(value -> isa(value, AbstractString), error = "Value is not a string.") |> to_value_error == (1, "Value is not a string.") -@test Convertible(nothing) |> test(value -> isa(value, String)) |> to_value === nothing -@test Convertible(nothing) |> test(value -> isa(value, String), handle_nothing = true) |> to_value_error == (nothing, +@test Convertible(nothing) |> test(value -> isa(value, AbstractString)) |> to_value === nothing +@test Convertible(nothing) |> test(value -> isa(value, AbstractString), handle_nothing = true) |> to_value_error == (nothing, "Test failed.") -@test Convertible("hello") |> fail("Initial error.") |> test(value -> isa(value, String)) |> to_value_error == ("hello", +@test Convertible("hello") |> fail("Initial error.") |> test(value -> isa(value, AbstractString)) |> to_value_error == ("hello", "Initial error.") # test_between @@ -456,7 +456,7 @@ tuple_non_strict_converter = struct( # test_equal @test Convertible(42) |> test_equal(42) |> to_value == 42 -@test Convertible(["a" => 1, "b" => 2]) |> test_equal(["a" => 1, "b" => 2]) |> to_value == ["a" => 1, "b" => 2] +@test Convertible(Dict("a" => 1, "b" => 2)) |> test_equal(Dict("a" => 1, "b" => 2)) |> to_value == Dict("a" => 1, "b" => 2) @test Convertible(41) |> test_equal(42) |> to_value_error == (41, "Value must be equal to 42.") @test Convertible(41) |> test_equal(42, error = "Value is not the answer.") |> to_value_error == (41, "Value is not the answer.") @@ -481,12 +481,12 @@ tuple_non_strict_converter = struct( @test Convertible('a') |> test_in("abcd") |> to_value == 'a' @test Convertible('a') |> test_in(['a', 'b', 'c', 'd']) |> to_value == 'a' @test Convertible('z') |> test_in(['a', 'b', 'c', 'd']) |> to_value_error == ('z', - "Value must belong to Char[a,b,c,d].") -@test Convertible('z') |> test_in(Set('a', 'b', 'c', 'd')) |> to_value_error == ('z', - "Value must belong to Char[d,b,c,a].") + "Value must belong to ['a','b','c','d'].") +@test Convertible('z') |> test_in(Set(['a', 'b', 'c', 'd'])) |> to_value_error == ('z', + "Value must belong to ['d','b','c','a'].") @test Convertible('z') |> test_in(['a', 'b', 'c', 'd'], error = """Value must be a letter less than "e".""") |> to_value_error == ('z', """Value must be a letter less than "e".""") -@test Convertible('z') |> test_in([]) |> to_value_error == ('z', "Value must belong to None[].") +@test Convertible('z') |> test_in([]) |> to_value_error == ('z', "Value must belong to Any[].") @test Convertible(nothing) |> test_in("abcd") |> to_value == nothing @test Convertible(nothing) |> test_in("abcd", handle_nothing = true) |> to_value_error == (nothing, "Value must belong to abcd.") @@ -494,14 +494,14 @@ tuple_non_strict_converter = struct( @test Convertible('a') |> fail("Initial error.") |> test_in("abcd") |> to_value_error == ('a', "Initial error.") # test_isa -@test Convertible("This is a string.") |> test_isa(String) |> to_value == "This is a string." -@test Convertible(42) |> test_isa(String) |> to_value_error == (42, "Value must be an instance of String.") -@test Convertible(42) |> test_isa(String, error = "Value is not a string.") |> to_value_error == (42, +@test Convertible("This is a string.") |> test_isa(AbstractString) |> to_value == "This is a string." +@test Convertible(42) |> test_isa(AbstractString) |> to_value_error == (42, "Value must be an instance of AbstractString.") +@test Convertible(42) |> test_isa(AbstractString, error = "Value is not a string.") |> to_value_error == (42, "Value is not a string.") -@test Convertible(nothing) |> test_isa(String) |> to_value === nothing -@test Convertible(nothing) |> test_isa(String, handle_nothing = true) |> to_value_error == (nothing, - "Value must be an instance of String.") -@test Convertible("This is a string.") |> fail("Initial error.") |> test_isa(String) |> to_value_error == ( +@test Convertible(nothing) |> test_isa(AbstractString) |> to_value === nothing +@test Convertible(nothing) |> test_isa(AbstractString, handle_nothing = true) |> to_value_error == (nothing, + "Value must be an instance of AbstractString.") +@test Convertible("This is a string.") |> fail("Initial error.") |> test_isa(AbstractString) |> to_value_error == ( "This is a string.", "Initial error.") # test_nothing @@ -561,29 +561,29 @@ tuple_non_strict_converter = struct( @test Convertible(42) |> fail("Initial error.") |> to_string |> to_value_error == (42, "Initial error.") # uniform_mapping -@test Convertible(["a" => "1", "b" => "2"]) |> uniform_mapping(strip, input_to_int) |> to_value == ["a" => 1, "b" => 2] -@test Convertible([" answer\n " => "42"]) |> uniform_mapping(strip, input_to_int) |> to_value == ["answer" => 42] -@test Convertible(["a" => "1", "b" => "2", "c" => 3]) |> uniform_mapping(strip, test_isa(String), input_to_int) |> - to_value_error == (["a" => 1, "b" => 2, "c" => 3], ["c" => "Value must be an instance of String."]) -@test Convertible(["a" => "1", "b" => "2", "c" => 3]) |> - uniform_mapping(strip, condition(test_isa(String), input_to_int)) |> - to_value == ["a" => 1, "b" => 2, "c" => 3] -@test Convertible({}) |> uniform_mapping(strip, input_to_int) |> to_value == (Nothing => Nothing)[] -@test Convertible([nothing => "42"]) |> uniform_mapping(strip, input_to_int) |> to_value == [nothing => 42] -@test Convertible([" \n " => "42"]) |> uniform_mapping(strip, input_to_int) |> to_value == [nothing => 42] -@test Convertible([nothing => "42"]) |> uniform_mapping(strip, input_to_int, drop_nothing_keys = true) |> - to_value == (Nothing => Nothing)[] +@test Convertible(Dict("a" => "1", "b" => "2")) |> uniform_mapping(strip, input_to_int) |> to_value == Dict("a" => 1, "b" => 2) +@test Convertible(Dict(" answer\n " => "42")) |> uniform_mapping(strip, input_to_int) |> to_value == Dict("answer" => 42) +@test Convertible(Dict("a" => "1", "b" => "2", "c" => 3)) |> uniform_mapping(strip, test_isa(AbstractString), input_to_int) |> + to_value_error == (Dict("a" => 1, "b" => 2, "c" => 3), Dict("c" => "Value must be an instance of AbstractString.")) +@test Convertible(Dict("a" => "1", "b" => "2", "c" => 3)) |> + uniform_mapping(strip, condition(test_isa(AbstractString), input_to_int)) |> + to_value == Dict("a" => 1, "b" => 2, "c" => 3) +@test Convertible(Dict()) |> uniform_mapping(strip, input_to_int) |> to_value == Dict{Void,Void}() +@test Convertible(Dict(nothing => "42")) |> uniform_mapping(strip, input_to_int) |> to_value == Dict(nothing => 42) +@test Convertible(Dict(" \n " => "42")) |> uniform_mapping(strip, input_to_int) |> to_value == Dict(nothing => 42) +@test Convertible(Dict(nothing => "42")) |> uniform_mapping(strip, input_to_int, drop_nothing_keys = true) |> + to_value == Dict{Void,Void}() # >>> uniform_mapping(cleanup_line, input_to_int)(None) # (None, None) @test Convertible(nothing) |> uniform_mapping(strip, input_to_int) |> to_value === nothing -@test Convertible(["a" => "1", "b" => "2"]) |> fail("Initial error.") |> uniform_mapping(strip, input_to_int) |> - to_value_error == (["a" => "1", "b" => "2"], "Initial error.") +@test Convertible(Dict("a" => "1", "b" => "2")) |> fail("Initial error.") |> uniform_mapping(strip, input_to_int) |> + to_value_error == (Dict("a" => "1", "b" => "2"), "Initial error.") # uniform_sequence @test Convertible(["42"]) |> uniform_sequence(input_to_int) |> to_value == [42] @test Convertible(["42", "43"]) |> uniform_sequence(input_to_int) |> to_value == [42, 43] @test Convertible(["42", "43", "Hello world!"]) |> uniform_sequence(input_to_int) |> to_value_error == ( - [42, 43, "Hello world!"], [3 => "Value must be an integer number."]) + [42, 43, "Hello world!"], Dict(3 => "Value must be an integer number.")) @test Convertible([nothing, nothing]) |> uniform_sequence(input_to_int) |> to_value == [nothing, nothing] @test Convertible([nothing, nothing]) |> uniform_sequence(input_to_int, drop_nothing = true) |> to_value == [] @test Convertible(["42", " \n ", "43"]) |> uniform_sequence(input_to_int) |> to_value == [42, nothing, 43] diff --git a/test/test_dates.jl b/test/test_dates.jl index 3488135..2306eb4 100644 --- a/test/test_dates.jl +++ b/test/test_dates.jl @@ -19,8 +19,6 @@ # limitations under the License. -import Dates: Date - importall Biryani.DatesConverters diff --git a/test/test_json.jl b/test/test_json.jl index 5d9b989..3957f18 100644 --- a/test/test_json.jl +++ b/test/test_json.jl @@ -23,9 +23,9 @@ importall Biryani.JsonConverters # input_to_json -@test Convertible("""{"a": 1, "b": [2, "three"]}""") |> input_to_json |> to_value == {"a" => 1, "b" => [2, "three"]} +@test Convertible("""{"a": 1, "b": [2, "three"]}""") |> input_to_json |> to_value == Dict("a" => 1, "b" => [2, "three"]) @test Convertible(""" {"a": 1, "b": [2, "three"]} """) |> input_to_json |> to_value == - {"a" => 1, "b" => [2, "three"]} + Dict("a" => 1, "b" => [2, "three"]) @test Convertible("null") |> input_to_json |> to_value == nothing @test Convertible(" null ") |> input_to_json |> to_value == nothing @test Convertible("Hello world!") |> input_to_json |> to_value_error == ("Hello world!", "Invalid JSON")