File: lowering.jl

package info (click to toggle)
julia 1.5.3%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 91,132 kB
  • sloc: lisp: 278,486; ansic: 60,186; cpp: 29,801; sh: 2,403; makefile: 1,998; pascal: 1,313; objc: 647; javascript: 516; asm: 226; python: 161; xml: 34
file content (37 lines) | stat: -rw-r--r-- 933 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
module TestLowering

using JSON
using Test
using Dates
using FixedPointNumbers: Fixed

@test JSON.json(Date(2016, 8, 3)) == "\"2016-08-03\""

@test JSON.json(:x) == "\"x\""
@test_throws ArgumentError JSON.json(Base)

struct Type151{T}
    x::T
end

@test JSON.parse(JSON.json(Type151)) == string(Type151)

JSON.lower(v::Type151{T}) where {T} = Dict(:type => T, :value => v.x)
@test JSON.parse(JSON.json(Type151(1.0))) == Dict(
    "type" => "Float64",
    "value" => 1.0)

fixednum = Fixed{Int16, 15}(0.1234)
@test JSON.parse(JSON.json(fixednum)) == convert(Float64, fixednum)

# test that the default string-serialization of enums can be overriden by
# `lower` if needed
@enum Fruit apple orange banana
JSON.lower(x::Fruit) = string("Fruit: ", x)
@test JSON.json(apple) == "\"Fruit: apple\""

@enum Vegetable carrot tomato potato
JSON.lower(x::Vegetable) = Dict(string(x) => Int(x))
@test JSON.json(potato) == "{\"potato\":2}"

end