mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
add test for [json:fieldname]
Added test also tests the output from json.encode to make sure the json:fieldname attribute is used.
This commit is contained in:
parent
d362f73af8
commit
817f5bf0a6
@ -1,24 +1,33 @@
|
|||||||
import json
|
import json
|
||||||
|
|
||||||
struct User {
|
struct User {
|
||||||
age int
|
age int
|
||||||
nums []int
|
nums []int
|
||||||
last_name string [json:lastName]
|
last_name string [json:lastName]
|
||||||
|
is_registered bool [json:IsRegistered]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_parse_user() {
|
fn test_parse_user() {
|
||||||
s := '{"age": 10, "nums": [1,2,3], "lastName": "Johnson"}'
|
s := '{"age": 10, "nums": [1,2,3], "lastName": "Johnson", "IsRegistered": true}'
|
||||||
u := json.decode(User, s) or {
|
u := json.decode(User, s) or {
|
||||||
exit(1)
|
exit(1)
|
||||||
}
|
}
|
||||||
assert u.age == 10
|
assert u.age == 10
|
||||||
assert u.last_name == 'Johnson'
|
assert u.last_name == 'Johnson'
|
||||||
|
assert u.is_registered == true
|
||||||
assert u.nums.len == 3
|
assert u.nums.len == 3
|
||||||
assert u.nums[0] == 1
|
assert u.nums[0] == 1
|
||||||
assert u.nums[1] == 2
|
assert u.nums[1] == 2
|
||||||
assert u.nums[2] == 3
|
assert u.nums[2] == 3
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn test_encode_user(){
|
||||||
|
usr := User{ age: 10, nums: [1,2,3], last_name: 'Johnson', is_registered: true}
|
||||||
|
expected := '{"age":10,"nums":[1,2,3],"lastName":"Johnson","IsRegistered":true}'
|
||||||
|
out := json.encode(usr)
|
||||||
|
assert out == expected
|
||||||
|
}
|
||||||
|
|
||||||
struct Color {
|
struct Color {
|
||||||
space string
|
space string
|
||||||
point string [raw]
|
point string [raw]
|
||||||
@ -32,4 +41,3 @@ fn test_raw_json_field() {
|
|||||||
assert color.point == '{"Y":123}'
|
assert color.point == '{"Y":123}'
|
||||||
assert color.space == 'YCbCr'
|
assert color.space == 'YCbCr'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user