mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
x.json2: refactor json tests (#16638)
This commit is contained in:
parent
44a539f186
commit
30b39bebde
60
vlib/x/json2/decode_struct_test.v
Normal file
60
vlib/x/json2/decode_struct_test.v
Normal file
@ -0,0 +1,60 @@
|
||||
import x.json2 as json
|
||||
|
||||
type StringAlias = string
|
||||
type BoolAlias = bool
|
||||
type IntAlias = int
|
||||
|
||||
type SumTypes = bool | int | string
|
||||
|
||||
struct StructType[T] {
|
||||
mut:
|
||||
val T
|
||||
}
|
||||
|
||||
struct StructTypeOptional[T] {
|
||||
mut:
|
||||
val ?T
|
||||
}
|
||||
|
||||
struct StructTypePointer[T] {
|
||||
mut:
|
||||
val &T
|
||||
}
|
||||
|
||||
fn test_types() {
|
||||
assert json.decode[StructType[string]]('{"val": ""}')!.val == ''
|
||||
assert json.decode[StructType[string]]('{"val": "0"}')!.val == '0'
|
||||
assert json.decode[StructType[string]]('{"val": "1"}')!.val == '1'
|
||||
assert json.decode[StructType[string]]('{"val": "2"}')!.val == '2'
|
||||
assert json.decode[StructType[string]]('{"val": 0}')!.val == '0'
|
||||
assert json.decode[StructType[string]]('{"val": 1}')!.val == '1'
|
||||
assert json.decode[StructType[string]]('{"val": 2}')!.val == '2'
|
||||
assert json.decode[StructType[string]]('{"val": "true"}')!.val == 'true'
|
||||
assert json.decode[StructType[string]]('{"val": "false"}')!.val == 'false'
|
||||
assert json.decode[StructType[string]]('{"val": true}')!.val == 'true'
|
||||
assert json.decode[StructType[string]]('{"val": false}')!.val == 'false'
|
||||
|
||||
assert json.decode[StructType[bool]]('{"val": ""}')!.val == false
|
||||
assert json.decode[StructType[bool]]('{"val": "0"}')!.val == false
|
||||
assert json.decode[StructType[bool]]('{"val": "1"}')!.val == true
|
||||
assert json.decode[StructType[bool]]('{"val": "2"}')!.val == true
|
||||
assert json.decode[StructType[bool]]('{"val": 0}')!.val == false
|
||||
assert json.decode[StructType[bool]]('{"val": 1}')!.val == true
|
||||
assert json.decode[StructType[bool]]('{"val": 2}')!.val == true
|
||||
assert json.decode[StructType[bool]]('{"val": "true"}')!.val == true
|
||||
assert json.decode[StructType[bool]]('{"val": "false"}')!.val == false
|
||||
assert json.decode[StructType[bool]]('{"val": true}')!.val == true
|
||||
assert json.decode[StructType[bool]]('{"val": false}')!.val == false
|
||||
|
||||
assert json.decode[StructType[int]]('{"val": ""}')!.val == 0
|
||||
assert json.decode[StructType[int]]('{"val": "0"}')!.val == 0
|
||||
assert json.decode[StructType[int]]('{"val": "1"}')!.val == 1
|
||||
assert json.decode[StructType[int]]('{"val": "2"}')!.val == 2
|
||||
assert json.decode[StructType[int]]('{"val": 0}')!.val == 0
|
||||
assert json.decode[StructType[int]]('{"val": 1}')!.val == 1
|
||||
assert json.decode[StructType[int]]('{"val": 2}')!.val == 2
|
||||
assert json.decode[StructType[int]]('{"val": "true"}')!.val == 1
|
||||
assert json.decode[StructType[int]]('{"val": "false"}')!.val == 0
|
||||
assert json.decode[StructType[int]]('{"val": true}')!.val == 1
|
||||
assert json.decode[StructType[int]]('{"val": false}')!.val == 0
|
||||
}
|
77
vlib/x/json2/decode_struct_todo_test.vv
Normal file
77
vlib/x/json2/decode_struct_todo_test.vv
Normal file
@ -0,0 +1,77 @@
|
||||
|
||||
fn test_optional_types() {
|
||||
assert json.decode[StructTypeOptional[string]]('{}')!.val == none
|
||||
assert json.decode[StructTypeOptional[string]]('{"val": ""}')!.val == ''
|
||||
assert json.decode[StructTypeOptional[string]]('{"val": "0"}')!.val == '0'
|
||||
assert json.decode[StructTypeOptional[string]]('{"val": "1"}')!.val == '1'
|
||||
assert json.decode[StructTypeOptional[string]]('{"val": "2"}')!.val == '2'
|
||||
assert json.decode[StructTypeOptional[string]]('{"val": 0}')!.val == '0'
|
||||
assert json.decode[StructTypeOptional[string]]('{"val": 1}')!.val == '1'
|
||||
assert json.decode[StructTypeOptional[string]]('{"val": 2}')!.val == '2'
|
||||
assert json.decode[StructTypeOptional[string]]('{"val": "true"}')!.val == 'true'
|
||||
assert json.decode[StructTypeOptional[string]]('{"val": "false"}')!.val == 'false'
|
||||
assert json.decode[StructTypeOptional[string]]('{"val": true}')!.val == 'true'
|
||||
assert json.decode[StructTypeOptional[string]]('{"val": false}')!.val == 'false'
|
||||
|
||||
assert json.decode[StructTypeOptional[bool]]('{}')!.val == none
|
||||
assert json.decode[StructTypeOptional[bool]]('{"val": ""}')!.val == false
|
||||
assert json.decode[StructTypeOptional[bool]]('{"val": "0"}')!.val == false
|
||||
assert json.decode[StructTypeOptional[bool]]('{"val": "1"}')!.val == true
|
||||
assert json.decode[StructTypeOptional[bool]]('{"val": "2"}')!.val == true
|
||||
assert json.decode[StructTypeOptional[bool]]('{"val": 0}')!.val == false
|
||||
assert json.decode[StructTypeOptional[bool]]('{"val": 1}')!.val == true
|
||||
assert json.decode[StructTypeOptional[bool]]('{"val": 2}')!.val == true
|
||||
assert json.decode[StructTypeOptional[bool]]('{"val": "true"}')!.val == true
|
||||
assert json.decode[StructTypeOptional[bool]]('{"val": "false"}')!.val == false
|
||||
assert json.decode[StructTypeOptional[bool]]('{"val": true}')!.val == true
|
||||
assert json.decode[StructTypeOptional[bool]]('{"val": false}')!.val == false
|
||||
|
||||
assert json.decode[StructTypeOptional[int]]('{}')!.val == none
|
||||
assert json.decode[StructTypeOptional[int]]('{"val": ""}')!.val == 0
|
||||
assert json.decode[StructTypeOptional[int]]('{"val": "0"}')!.val == 0
|
||||
assert json.decode[StructTypeOptional[int]]('{"val": "1"}')!.val == 1
|
||||
assert json.decode[StructTypeOptional[int]]('{"val": "2"}')!.val == 2
|
||||
assert json.decode[StructTypeOptional[int]]('{"val": 0}')!.val == 0
|
||||
assert json.decode[StructTypeOptional[int]]('{"val": 1}')!.val == 1
|
||||
assert json.decode[StructTypeOptional[int]]('{"val": 2}')!.val == 2
|
||||
assert json.decode[StructTypeOptional[int]]('{"val": "true"}')!.val == 1
|
||||
assert json.decode[StructTypeOptional[int]]('{"val": "false"}')!.val == 0
|
||||
assert json.decode[StructTypeOptional[int]]('{"val": true}')!.val == 1
|
||||
assert json.decode[StructTypeOptional[int]]('{"val": false}')!.val == 0
|
||||
}
|
||||
|
||||
fn test_array() {
|
||||
// TODO
|
||||
}
|
||||
|
||||
fn test_optional_array() {
|
||||
// TODO
|
||||
}
|
||||
|
||||
fn test_alias() {
|
||||
// TODO
|
||||
}
|
||||
|
||||
fn test_optional_alias() {
|
||||
// TODO
|
||||
}
|
||||
|
||||
fn test_sumtypes() {
|
||||
// TODO
|
||||
}
|
||||
|
||||
fn test_optional_sumtypes() {
|
||||
// TODO
|
||||
}
|
||||
|
||||
fn test_pointer() {
|
||||
// TODO
|
||||
}
|
||||
|
||||
fn test_caos() {
|
||||
// TODO
|
||||
}
|
||||
|
||||
fn test_caos_array() {
|
||||
// TODO
|
||||
}
|
119
vlib/x/json2/encode_struct_test.v
Normal file
119
vlib/x/json2/encode_struct_test.v
Normal file
@ -0,0 +1,119 @@
|
||||
import x.json2 as json
|
||||
|
||||
type StringAlias = string
|
||||
type BoolAlias = bool
|
||||
type IntAlias = int
|
||||
|
||||
type SumTypes = bool | int | string
|
||||
|
||||
struct StructType[T] {
|
||||
mut:
|
||||
val T
|
||||
}
|
||||
|
||||
struct StructTypeOptional[T] {
|
||||
mut:
|
||||
val ?T
|
||||
}
|
||||
|
||||
struct StructTypePointer[T] {
|
||||
mut:
|
||||
val &T
|
||||
}
|
||||
|
||||
fn test_types() {
|
||||
assert json.encode(StructType[string]{}) == '{"val":""}'
|
||||
assert json.encode(StructType[string]{ val: '' }) == '{"val":""}'
|
||||
assert json.encode(StructType[string]{ val: 'a' }) == '{"val":"a"}'
|
||||
|
||||
assert json.encode(StructType[bool]{}) == '{"val":false}'
|
||||
assert json.encode(StructType[bool]{ val: false }) == '{"val":false}'
|
||||
assert json.encode(StructType[bool]{ val: true }) == '{"val":true}'
|
||||
|
||||
assert json.encode(StructType[int]{}) == '{"val":0}'
|
||||
assert json.encode(StructType[int]{ val: 0 }) == '{"val":0}'
|
||||
assert json.encode(StructType[int]{ val: 1 }) == '{"val":1}'
|
||||
}
|
||||
|
||||
fn test_optional_types() {
|
||||
assert json.encode(StructTypeOptional[string]{ val: none }) == '{}'
|
||||
assert json.encode(StructTypeOptional[string]{}) == '{"val":""}'
|
||||
assert json.encode(StructTypeOptional[string]{ val: '' }) == '{"val":""}'
|
||||
assert json.encode(StructTypeOptional[string]{ val: 'a' }) == '{"val":"a"}'
|
||||
|
||||
assert json.encode(StructTypeOptional[bool]{ val: none }) == '{}'
|
||||
assert json.encode(StructTypeOptional[bool]{}) == '{"val":false}'
|
||||
assert json.encode(StructTypeOptional[bool]{ val: false }) == '{"val":false}'
|
||||
assert json.encode(StructTypeOptional[bool]{ val: true }) == '{"val":true}'
|
||||
|
||||
assert json.encode(StructTypeOptional[int]{ val: none }) == '{}'
|
||||
assert json.encode(StructTypeOptional[int]{}) == '{"val":0}'
|
||||
assert json.encode(StructTypeOptional[int]{ val: 0 }) == '{"val":0}'
|
||||
assert json.encode(StructTypeOptional[int]{ val: 1 }) == '{"val":1}'
|
||||
}
|
||||
|
||||
fn test_array() {
|
||||
assert json.encode(StructType[[]string]{}) == '{"val":[]}'
|
||||
assert json.encode(StructType[[]string]{ val: [] }) == '{"val":[]}'
|
||||
assert json.encode(StructType[[]string]{ val: ['0'] }) == '{"val":["0"]}'
|
||||
assert json.encode(StructType[[]string]{ val: ['1'] }) == '{"val":["1"]}'
|
||||
|
||||
assert json.encode(StructType[[]int]{}) == '{"val":[]}'
|
||||
assert json.encode(StructType[[]int]{ val: [] }) == '{"val":[]}'
|
||||
assert json.encode(StructType[[]int]{ val: [0] }) == '{"val":[0]}'
|
||||
assert json.encode(StructType[[]int]{ val: [1] }) == '{"val":[1]}'
|
||||
assert json.encode(StructType[[]int]{ val: [0, 1, 0, 2, 3, 2, 5, 1] }) == '{"val":[0,1,0,2,3,2,5,1]}'
|
||||
|
||||
assert json.encode(StructType[[]byte]{}) == '{"val":[]}'
|
||||
assert json.encode(StructType[[]byte]{ val: [] }) == '{"val":[]}'
|
||||
assert json.encode(StructType[[]byte]{ val: [byte(0)] }) == '{"val":[0]}'
|
||||
assert json.encode(StructType[[]byte]{ val: [byte(1)] }) == '{"val":[1]}'
|
||||
assert json.encode(StructType[[]byte]{ val: [byte(0), 1, 0, 2, 3, 2, 5, 1] }) == '{"val":[0,1,0,2,3,2,5,1]}'
|
||||
|
||||
assert json.encode(StructType[[]i64]{}) == '{"val":[]}'
|
||||
assert json.encode(StructType[[]i64]{ val: [] }) == '{"val":[]}'
|
||||
assert json.encode(StructType[[]i64]{ val: [i64(0)] }) == '{"val":[0]}'
|
||||
assert json.encode(StructType[[]i64]{ val: [i64(1)] }) == '{"val":[1]}'
|
||||
assert json.encode(StructType[[]i64]{ val: [i64(0), 1, 0, 2, 3, 2, 5, 1] }) == '{"val":[0,1,0,2,3,2,5,1]}'
|
||||
|
||||
assert json.encode(StructType[[]u64]{}) == '{"val":[]}'
|
||||
assert json.encode(StructType[[]u64]{ val: [] }) == '{"val":[]}'
|
||||
assert json.encode(StructType[[]u64]{ val: [u64(0)] }) == '{"val":[0]}'
|
||||
assert json.encode(StructType[[]u64]{ val: [u64(1)] }) == '{"val":[1]}'
|
||||
assert json.encode(StructType[[]u64]{ val: [u64(0), 1, 0, 2, 3, 2, 5, 1] }) == '{"val":[0,1,0,2,3,2,5,1]}'
|
||||
|
||||
assert json.encode(StructType[[]f64]{}) == '{"val":[]}'
|
||||
assert json.encode(StructType[[]f64]{ val: [] }) == '{"val":[]}'
|
||||
assert json.encode(StructType[[]f64]{ val: [f64(0)] }) == '{"val":[0.0]}'
|
||||
assert json.encode(StructType[[]f64]{ val: [f64(1)] }) == '{"val":[1.0]}'
|
||||
assert json.encode(StructType[[]f64]{ val: [f64(0), 1, 0, 2, 3, 2, 5, 1] }) == '{"val":[0.0,1.0,0.0,2.0,3.0,2.0,5.0,1.0]}'
|
||||
|
||||
assert json.encode(StructType[[]bool]{}) == '{"val":[]}'
|
||||
assert json.encode(StructType[[]bool]{ val: [] }) == '{"val":[]}'
|
||||
assert json.encode(StructType[[]bool]{ val: [true] }) == '{"val":[true]}'
|
||||
assert json.encode(StructType[[]bool]{ val: [false] }) == '{"val":[false]}'
|
||||
assert json.encode(StructType[[]bool]{ val: [false, true, false] }) == '{"val":[false,true,false]}'
|
||||
}
|
||||
|
||||
fn test_optional_array() {
|
||||
assert json.encode(StructTypeOptional[[]int]{ val: none }) == '{}'
|
||||
assert json.encode(StructTypeOptional[[]int]{}) == '{"val":[]}'
|
||||
assert json.encode(StructTypeOptional[[]int]{ val: [] }) == '{"val":[]}'
|
||||
assert json.encode(StructTypeOptional[[]int]{ val: [0] }) == '{"val":[0]}'
|
||||
assert json.encode(StructTypeOptional[[]int]{ val: [1] }) == '{"val":[1]}'
|
||||
assert json.encode(StructTypeOptional[[]int]{ val: [0, 1, 0, 2, 3, 2, 5, 1] }) == '{"val":[0,1,0,2,3,2,5,1]}'
|
||||
}
|
||||
|
||||
fn test_alias() {
|
||||
assert json.encode(StructType[StringAlias]{}) == '{"val":""}'
|
||||
assert json.encode(StructType[StringAlias]{ val: '' }) == '{"val":""}'
|
||||
assert json.encode(StructType[StringAlias]{ val: 'a' }) == '{"val":"a"}'
|
||||
|
||||
assert json.encode(StructType[BoolAlias]{}) == '{"val":false}'
|
||||
assert json.encode(StructType[BoolAlias]{ val: false }) == '{"val":false}'
|
||||
assert json.encode(StructType[BoolAlias]{ val: true }) == '{"val":true}'
|
||||
|
||||
assert json.encode(StructType[IntAlias]{}) == '{"val":0}'
|
||||
assert json.encode(StructType[IntAlias]{ val: 0 }) == '{"val":0}'
|
||||
assert json.encode(StructType[IntAlias]{ val: 1 }) == '{"val":1}'
|
||||
}
|
188
vlib/x/json2/encode_struct_todo_test.vv
Normal file
188
vlib/x/json2/encode_struct_todo_test.vv
Normal file
@ -0,0 +1,188 @@
|
||||
import x.json2 as json
|
||||
|
||||
type StringAlias = string
|
||||
type BoolAlias = bool
|
||||
type IntAlias = int
|
||||
|
||||
type SumTypes = bool | int | string
|
||||
|
||||
struct StructType[T] {
|
||||
mut:
|
||||
val T
|
||||
}
|
||||
|
||||
struct StructTypeOptional[T] {
|
||||
mut:
|
||||
val ?T
|
||||
}
|
||||
|
||||
struct StructTypePointer[T] {
|
||||
mut:
|
||||
val &T
|
||||
}
|
||||
|
||||
fn test_optional_alias() {
|
||||
assert json.encode(StructTypeOptional[BoolAlias]{ val: none }) == '{}'
|
||||
assert json.encode(StructTypeOptional[BoolAlias]{}) == '{"val":false}'
|
||||
assert json.encode(StructTypeOptional[BoolAlias]{ val: false }) == '{"val":false}'
|
||||
assert json.encode(StructTypeOptional[BoolAlias]{ val: true }) == '{"val":true}'
|
||||
|
||||
assert json.encode(StructTypeOptional[IntAlias]{ val: none }) == '{}'
|
||||
assert json.encode(StructTypeOptional[IntAlias]{}) == '{"val":0}'
|
||||
assert json.encode(StructTypeOptional[IntAlias]{ val: 0 }) == '{"val":0}'
|
||||
assert json.encode(StructTypeOptional[IntAlias]{ val: 1 }) == '{"val":1}'
|
||||
}
|
||||
|
||||
fn test_sumtypes() {
|
||||
assert json.encode(StructType[SumTypes]{}) == '{}' // REVIEW
|
||||
|
||||
assert json.encode(StructType[SumTypes]{ val: '' }) == '{"val":""}'
|
||||
assert json.encode(StructType[SumTypes]{ val: 'a' }) == '{"val":"a"}'
|
||||
|
||||
assert json.encode(StructType[SumTypes]{ val: false }) == '{"val":false}'
|
||||
assert json.encode(StructType[SumTypes]{ val: true }) == '{"val":true}'
|
||||
|
||||
assert json.encode(StructType[SumTypes]{ val: 0 }) == '{"val":0}'
|
||||
assert json.encode(StructType[SumTypes]{ val: 1 }) == '{"val":1}'
|
||||
}
|
||||
|
||||
fn test_optional_sumtypes() {
|
||||
assert json.encode(StructTypeOptional[SumTypes]{}) == '{}' // REVIEW
|
||||
|
||||
assert json.encode(StructTypeOptional[SumTypes]{ val: '' }) == '{"val":""}'
|
||||
assert json.encode(StructTypeOptional[SumTypes]{ val: 'a' }) == '{"val":"a"}'
|
||||
|
||||
assert json.encode(StructTypeOptional[SumTypes]{ val: false }) == '{"val":false}'
|
||||
assert json.encode(StructTypeOptional[SumTypes]{ val: true }) == '{"val":true}'
|
||||
|
||||
assert json.encode(StructTypeOptional[SumTypes]{ val: 0 }) == '{"val":0}'
|
||||
assert json.encode(StructTypeOptional[SumTypes]{ val: 1 }) == '{"val":1}'
|
||||
}
|
||||
|
||||
fn test_pointer() {
|
||||
// mut string_initialized_with_reference := ""
|
||||
assert json.encode(StructTypePointer[string]{ val: 0 }) == '{"val":""}' // REVIEW
|
||||
assert json.encode(StructTypePointer[string]{ val: &string_initialized_with_reference }) == '{"val":""}'
|
||||
string_initialized_with_reference = 'a'
|
||||
assert json.encode(StructTypePointer[string]{ val: &string_initialized_with_reference }) == '{"val":"a"}'
|
||||
|
||||
mut bool_initialized_with_reference := false
|
||||
assert json.encode(StructTypePointer[bool]{ val: 0 }) == '{"val":false}' // REVIEW
|
||||
assert json.encode(StructTypePointer[bool]{ val: &bool_initialized_with_reference }) == '{"val":false}'
|
||||
bool_initialized_with_reference = true
|
||||
assert json.encode(StructTypePointer[bool]{ val: &bool_initialized_with_reference }) == '{"val":true}'
|
||||
|
||||
mut int_initialized_with_reference := 0
|
||||
assert json.encode(StructTypePointer[int]{ val: 0 }) == '{"val":0}' // REVIEW
|
||||
assert json.encode(StructTypePointer[int]{ val: &int_initialized_with_reference }) == '{"val":0}'
|
||||
int_initialized_with_reference = 1
|
||||
assert json.encode(StructTypePointer[int]{ val: &int_initialized_with_reference }) == '{"val":1}'
|
||||
}
|
||||
|
||||
fn test_caos() {
|
||||
typed_string_struct := StructType[string]{
|
||||
val: 'a'
|
||||
}
|
||||
|
||||
// StructType
|
||||
assert json.encode(StructType[StructType]{}) == 'TODO'
|
||||
assert json.encode(StructType[StructType[string]]{ val: typed_string_struct }) == 'TODO'
|
||||
assert json.encode(StructType[StructType[StringAlias]]{ val: typed_string_struct }) == 'TODO'
|
||||
assert json.encode(StructType[StructType[SumTypes]]{ val: typed_string_struct }) == 'TODO'
|
||||
|
||||
assert json.encode(StructType[StructTypeOptional]{typed_string_struct}) == 'TODO'
|
||||
assert json.encode(StructType[StructTypeOptional[string]]{ val: StructType{'a'} }) == 'TODO'
|
||||
assert json.encode(StructType[StructTypeOptional[StringAlias]]{typed_string_struct}) == 'TODO'
|
||||
assert json.encode(StructType[StructTypeOptional[SumTypes]]{typed_string_struct}) == 'TODO'
|
||||
|
||||
assert json.encode(StructType[StructTypePointer]{typed_string_struct}) == 'TODO'
|
||||
assert json.encode(StructType[StructTypePointer[string]]{typed_string_struct}) == 'TODO'
|
||||
assert json.encode(StructType[StructTypePointer[StringAlias]]{typed_string_struct}) == 'TODO'
|
||||
assert json.encode(StructType[StructTypePointer[SumTypes]]{typed_string_struct}) == 'TODO'
|
||||
|
||||
// StructTypeOptional
|
||||
assert json.encode(StructTypeOptional[StructType]{}) == 'TODO'
|
||||
assert json.encode(StructTypeOptional[StructType[string]]{ val: '' }) == 'TODO'
|
||||
assert json.encode(StructTypeOptional[StructType[StringAlias]]{ val: '' }) == 'TODO'
|
||||
assert json.encode(StructTypeOptional[StructType[SumTypes]]{ val: '' }) == 'TODO'
|
||||
|
||||
assert json.encode(StructTypeOptional[StructTypeOptional]{typed_string_struct}) == 'TODO'
|
||||
assert json.encode(StructTypeOptional[StructTypeOptional[string]]{ val: StructType{'a'} }) == 'TODO'
|
||||
assert json.encode(StructTypeOptional[StructTypeOptional[StringAlias]]{typed_string_struct}) == 'TODO'
|
||||
assert json.encode(StructTypeOptional[StructTypeOptional[SumTypes]]{typed_string_struct}) == 'TODO'
|
||||
|
||||
assert json.encode(StructTypeOptional[StructTypePointer]{typed_string_struct}) == 'TODO'
|
||||
assert json.encode(StructTypeOptional[StructTypePointer[string]]{typed_string_struct}) == 'TODO'
|
||||
assert json.encode(StructTypeOptional[StructTypePointer[StringAlias]]{typed_string_struct}) == 'TODO'
|
||||
assert json.encode(StructTypeOptional[StructTypePointer[SumTypes]]{typed_string_struct}) == 'TODO'
|
||||
|
||||
// StructTypePointer
|
||||
assert json.encode(StructTypePointer[StructType]{}) == 'TODO'
|
||||
assert json.encode(StructTypePointer[StructType[string]]{ val: '' }) == 'TODO'
|
||||
assert json.encode(StructTypePointer[StructType[StringAlias]]{ val: '' }) == 'TODO'
|
||||
assert json.encode(StructTypePointer[StructType[SumTypes]]{ val: '' }) == 'TODO'
|
||||
|
||||
assert json.encode(StructTypePointer[StructTypeOptional]{typed_string_struct}) == 'TODO'
|
||||
assert json.encode(StructTypePointer[StructTypeOptional[string]]{ val: StructType{'a'} }) == 'TODO'
|
||||
assert json.encode(StructTypePointer[StructTypeOptional[StringAlias]]{typed_string_struct}) == 'TODO'
|
||||
assert json.encode(StructTypePointer[StructTypeOptional[SumTypes]]{typed_string_struct}) == 'TODO'
|
||||
|
||||
assert json.encode(StructTypePointer[StructTypePointer]{typed_string_struct}) == 'TODO'
|
||||
assert json.encode(StructTypePointer[StructTypePointer[string]]{typed_string_struct}) == 'TODO'
|
||||
assert json.encode(StructTypePointer[StructTypePointer[StringAlias]]{typed_string_struct}) == 'TODO'
|
||||
assert json.encode(StructTypePointer[StructTypePointer[SumTypes]]{typed_string_struct}) == 'TODO'
|
||||
}
|
||||
|
||||
fn test_caos_array() {
|
||||
typed_string_struct := [StructType[string]{
|
||||
val: 'a'
|
||||
}]
|
||||
|
||||
// StructType
|
||||
assert json.encode(StructType[[]StructType]{}) == 'TODO'
|
||||
assert json.encode(StructType[[]StructType[string]]{ val: typed_string_struct }) == 'TODO'
|
||||
assert json.encode(StructType[[]StructType[StringAlias]]{ val: typed_string_struct }) == 'TODO'
|
||||
assert json.encode(StructType[[]StructType[SumTypes]]{ val: typed_string_struct }) == 'TODO'
|
||||
|
||||
assert json.encode(StructType[[]StructTypeOptional]{typed_string_struct}) == 'TODO'
|
||||
assert json.encode(StructType[[]StructTypeOptional[string]]{ val: StructType{'a'} }) == 'TODO'
|
||||
assert json.encode(StructType[[]StructTypeOptional[StringAlias]]{typed_string_struct}) == 'TODO'
|
||||
assert json.encode(StructType[[]StructTypeOptional[SumTypes]]{typed_string_struct}) == 'TODO'
|
||||
|
||||
assert json.encode(StructType[[]StructTypePointer]{typed_string_struct}) == 'TODO'
|
||||
assert json.encode(StructType[[]StructTypePointer[string]]{typed_string_struct}) == 'TODO'
|
||||
assert json.encode(StructType[[]StructTypePointer[StringAlias]]{typed_string_struct}) == 'TODO'
|
||||
assert json.encode(StructType[[]StructTypePointer[SumTypes]]{typed_string_struct}) == 'TODO'
|
||||
|
||||
// StructTypeOptional
|
||||
assert json.encode(StructTypeOptional[[]StructType]{}) == 'TODO'
|
||||
assert json.encode(StructTypeOptional[[]StructType[string]]{ val: '' }) == 'TODO'
|
||||
assert json.encode(StructTypeOptional[[]StructType[StringAlias]]{ val: '' }) == 'TODO'
|
||||
assert json.encode(StructTypeOptional[[]StructType[SumTypes]]{ val: '' }) == 'TODO'
|
||||
|
||||
assert json.encode(StructTypeOptional[[]StructTypeOptional]{typed_string_struct}) == 'TODO'
|
||||
assert json.encode(StructTypeOptional[[]StructTypeOptional[string]]{ val: StructType{'a'} }) == 'TODO'
|
||||
assert json.encode(StructTypeOptional[[]StructTypeOptional[StringAlias]]{typed_string_struct}) == 'TODO'
|
||||
assert json.encode(StructTypeOptional[[]StructTypeOptional[SumTypes]]{typed_string_struct}) == 'TODO'
|
||||
|
||||
assert json.encode(StructTypeOptional[[]StructTypePointer]{typed_string_struct}) == 'TODO'
|
||||
assert json.encode(StructTypeOptional[[]StructTypePointer[string]]{typed_string_struct}) == 'TODO'
|
||||
assert json.encode(StructTypeOptional[[]StructTypePointer[StringAlias]]{typed_string_struct}) == 'TODO'
|
||||
assert json.encode(StructTypeOptional[[]StructTypePointer[SumTypes]]{typed_string_struct}) == 'TODO'
|
||||
|
||||
// StructTypePointer
|
||||
assert json.encode(StructTypePointer[[]StructType]{}) == 'TODO'
|
||||
assert json.encode(StructTypePointer[[]StructType[string]]{ val: '' }) == 'TODO'
|
||||
assert json.encode(StructTypePointer[[]StructType[StringAlias]]{ val: '' }) == 'TODO'
|
||||
assert json.encode(StructTypePointer[[]StructType[SumTypes]]{ val: '' }) == 'TODO'
|
||||
|
||||
assert json.encode(StructTypePointer[[]StructTypeOptional]{typed_string_struct}) == 'TODO'
|
||||
assert json.encode(StructTypePointer[[]StructTypeOptional[string]]{ val: StructType{'a'} }) == 'TODO'
|
||||
assert json.encode(StructTypePointer[[]StructTypeOptional[StringAlias]]{typed_string_struct}) == 'TODO'
|
||||
assert json.encode(StructTypePointer[[]StructTypeOptional[SumTypes]]{typed_string_struct}) == 'TODO'
|
||||
|
||||
assert json.encode(StructTypePointer[[]StructTypePointer]{typed_string_struct}) == 'TODO'
|
||||
assert json.encode(StructTypePointer[[]StructTypePointer[string]]{typed_string_struct}) == 'TODO'
|
||||
assert json.encode(StructTypePointer[[]StructTypePointer[StringAlias]]{typed_string_struct}) == 'TODO'
|
||||
assert json.encode(StructTypePointer[[]StructTypePointer[SumTypes]]{typed_string_struct}) == 'TODO'
|
||||
}
|
@ -15,50 +15,6 @@ pub mut:
|
||||
title JobTitle
|
||||
}
|
||||
|
||||
struct OptionalStruct {
|
||||
pub mut:
|
||||
name string
|
||||
last_name ?string = none
|
||||
age ?int = none
|
||||
salary ?f32 = none
|
||||
}
|
||||
|
||||
fn test_simple_optional() {
|
||||
x := OptionalStruct{
|
||||
name: 'Peter'
|
||||
}
|
||||
s := json.encode[OptionalStruct](x)
|
||||
assert s == '{"name":"Peter"}'
|
||||
// y := json.decode<EmployeeOp>(s) or {
|
||||
// println(err)
|
||||
// assert false
|
||||
// return
|
||||
// }
|
||||
// assert y.name == 'Peter'
|
||||
// assert y.age == 28
|
||||
// assert y.salary == 95000.5
|
||||
// assert y.title == .worker
|
||||
}
|
||||
|
||||
// ! BUGFIX
|
||||
// fn test_simplegg() {
|
||||
// // x := EmployeeOp{'Peter', 28, 95000.5, .worker}
|
||||
// x := EmployeeOp{
|
||||
// name: 'vshfvhsd'
|
||||
// }
|
||||
// s := json.encode<EmployeeOp>(x)
|
||||
// assert s == '{"name":"vshfvhsd","age":0,"salary":0.0,"title":0.0}'
|
||||
// // y := json.decode<EmployeeOp>(s) or {
|
||||
// // println(err)
|
||||
// // assert false
|
||||
// // return
|
||||
// // }
|
||||
// // assert y.name == 'Peter'
|
||||
// // assert y.age == 28
|
||||
// // assert y.salary == 95000.5
|
||||
// // assert y.title == .worker
|
||||
// }
|
||||
|
||||
fn test_fast_raw_decode() {
|
||||
s := '{"name":"Peter","age":28,"salary":95000.5,"title":2}'
|
||||
o := json.fast_raw_decode(s) or {
|
||||
@ -89,134 +45,3 @@ fn test_character_unescape() {
|
||||
assert lines['quotes'] or { 0 }.str() == '"quotes"'
|
||||
assert lines['slash'] or { 0 }.str() == '/dev/null'
|
||||
}
|
||||
|
||||
struct MultTypeTest[T] {
|
||||
mut:
|
||||
val T
|
||||
}
|
||||
|
||||
struct MultTypeTestOptional[T] {
|
||||
mut:
|
||||
val ?T
|
||||
}
|
||||
|
||||
// NOTE - This can substitute a lot of others tests
|
||||
fn test_mult_decode() {
|
||||
assert json.decode[MultTypeTest[bool]]('{"val": ""}')!.val == false
|
||||
assert json.decode[MultTypeTest[bool]]('{"val": "0"}')!.val == false
|
||||
assert json.decode[MultTypeTest[bool]]('{"val": "1"}')!.val == true
|
||||
assert json.decode[MultTypeTest[bool]]('{"val": "2"}')!.val == true
|
||||
assert json.decode[MultTypeTest[bool]]('{"val": 0}')!.val == false
|
||||
assert json.decode[MultTypeTest[bool]]('{"val": 1}')!.val == true
|
||||
assert json.decode[MultTypeTest[bool]]('{"val": 2}')!.val == true
|
||||
assert json.decode[MultTypeTest[bool]]('{"val": "true"}')!.val == true
|
||||
assert json.decode[MultTypeTest[bool]]('{"val": "false"}')!.val == false
|
||||
assert json.decode[MultTypeTest[bool]]('{"val": true}')!.val == true
|
||||
assert json.decode[MultTypeTest[bool]]('{"val": false}')!.val == false
|
||||
|
||||
assert json.decode[MultTypeTest[int]]('{"val": ""}')!.val == 0
|
||||
assert json.decode[MultTypeTest[int]]('{"val": "0"}')!.val == 0
|
||||
assert json.decode[MultTypeTest[int]]('{"val": "1"}')!.val == 1
|
||||
assert json.decode[MultTypeTest[int]]('{"val": "2"}')!.val == 2
|
||||
assert json.decode[MultTypeTest[int]]('{"val": 0}')!.val == 0
|
||||
assert json.decode[MultTypeTest[int]]('{"val": 1}')!.val == 1
|
||||
assert json.decode[MultTypeTest[int]]('{"val": 2}')!.val == 2
|
||||
assert json.decode[MultTypeTest[int]]('{"val": "true"}')!.val == 1
|
||||
assert json.decode[MultTypeTest[int]]('{"val": "false"}')!.val == 0
|
||||
assert json.decode[MultTypeTest[int]]('{"val": true}')!.val == 1
|
||||
assert json.decode[MultTypeTest[int]]('{"val": false}')!.val == 0
|
||||
|
||||
assert json.decode[MultTypeTest[string]]('{"val": ""}')!.val == ''
|
||||
assert json.decode[MultTypeTest[string]]('{"val": "0"}')!.val == '0'
|
||||
assert json.decode[MultTypeTest[string]]('{"val": "1"}')!.val == '1'
|
||||
assert json.decode[MultTypeTest[string]]('{"val": "2"}')!.val == '2'
|
||||
assert json.decode[MultTypeTest[string]]('{"val": 0}')!.val == '0'
|
||||
assert json.decode[MultTypeTest[string]]('{"val": 1}')!.val == '1'
|
||||
assert json.decode[MultTypeTest[string]]('{"val": 2}')!.val == '2'
|
||||
assert json.decode[MultTypeTest[string]]('{"val": "true"}')!.val == 'true'
|
||||
assert json.decode[MultTypeTest[string]]('{"val": "false"}')!.val == 'false'
|
||||
assert json.decode[MultTypeTest[string]]('{"val": true}')!.val == 'true'
|
||||
assert json.decode[MultTypeTest[string]]('{"val": false}')!.val == 'false'
|
||||
|
||||
// assert json.decode[MultTypeTestOptional[string]]('{"val": ""}')! == MultTypeTestOptional[string]{val: ""}
|
||||
/*
|
||||
assert json.decode[MultTypeTestOptional[string]]('{"val": "0"}')!.val == "0"
|
||||
assert json.decode[MultTypeTestOptional[string]]('{"val": "1"}')!.val == "1"
|
||||
assert json.decode[MultTypeTestOptional[string]]('{"val": "2"}')!.val == "2"
|
||||
assert json.decode[MultTypeTestOptional[string]]('{"val": 0}')!.val == "0"
|
||||
assert json.decode[MultTypeTestOptional[string]]('{"val": 1}')!.val == "1"
|
||||
assert json.decode[MultTypeTestOptional[string]]('{"val": 2}')!.val == "2"
|
||||
assert json.decode[MultTypeTestOptional[string]]('{"val": "true"}')!.val == "true"
|
||||
assert json.decode[MultTypeTestOptional[string]]('{"val": "false"}')!.val == "false"
|
||||
assert json.decode[MultTypeTestOptional[string]]('{"val": true}')!.val == "true"
|
||||
assert json.decode[MultTypeTestOptional[string]]('{"val": false}')!.val == "false"
|
||||
*/
|
||||
}
|
||||
|
||||
fn test_mult_encode() {
|
||||
assert json.encode(MultTypeTest[[]string]{}) == '{"val":[]}'
|
||||
assert json.encode(MultTypeTest[[]string]{ val: [] }) == '{"val":[]}'
|
||||
assert json.encode(MultTypeTest[[]string]{ val: ['0'] }) == '{"val":["0"]}'
|
||||
assert json.encode(MultTypeTest[[]string]{ val: ['1'] }) == '{"val":["1"]}'
|
||||
|
||||
assert json.encode(MultTypeTest[bool]{}) == '{"val":false}'
|
||||
assert json.encode(MultTypeTest[bool]{ val: false }) == '{"val":false}'
|
||||
assert json.encode(MultTypeTest[bool]{ val: true }) == '{"val":true}'
|
||||
|
||||
assert json.encode(MultTypeTestOptional[bool]{ val: none }) == '{}'
|
||||
assert json.encode(MultTypeTestOptional[bool]{}) == '{"val":false}'
|
||||
assert json.encode(MultTypeTestOptional[bool]{ val: false }) == '{"val":false}'
|
||||
assert json.encode(MultTypeTestOptional[bool]{ val: true }) == '{"val":true}'
|
||||
|
||||
assert json.encode(MultTypeTest[int]{}) == '{"val":0}'
|
||||
assert json.encode(MultTypeTest[int]{ val: 0 }) == '{"val":0}'
|
||||
assert json.encode(MultTypeTest[int]{ val: 1 }) == '{"val":1}'
|
||||
|
||||
assert json.encode(MultTypeTestOptional[int]{ val: none }) == '{}'
|
||||
assert json.encode(MultTypeTestOptional[int]{}) == '{"val":0}'
|
||||
assert json.encode(MultTypeTestOptional[int]{ val: 0 }) == '{"val":0}'
|
||||
assert json.encode(MultTypeTestOptional[int]{ val: 1 }) == '{"val":1}'
|
||||
|
||||
assert json.encode(MultTypeTest[[]int]{}) == '{"val":[]}'
|
||||
assert json.encode(MultTypeTest[[]int]{ val: [] }) == '{"val":[]}'
|
||||
assert json.encode(MultTypeTest[[]int]{ val: [0] }) == '{"val":[0]}'
|
||||
assert json.encode(MultTypeTest[[]int]{ val: [1] }) == '{"val":[1]}'
|
||||
assert json.encode(MultTypeTest[[]int]{ val: [0, 1, 0, 2, 3, 2, 5, 1] }) == '{"val":[0,1,0,2,3,2,5,1]}'
|
||||
|
||||
assert json.encode(MultTypeTestOptional[[]int]{ val: none }) == '{}'
|
||||
assert json.encode(MultTypeTestOptional[[]int]{}) == '{"val":[]}'
|
||||
assert json.encode(MultTypeTestOptional[[]int]{ val: [] }) == '{"val":[]}'
|
||||
assert json.encode(MultTypeTestOptional[[]int]{ val: [0] }) == '{"val":[0]}'
|
||||
assert json.encode(MultTypeTestOptional[[]int]{ val: [1] }) == '{"val":[1]}'
|
||||
assert json.encode(MultTypeTestOptional[[]int]{ val: [0, 1, 0, 2, 3, 2, 5, 1] }) == '{"val":[0,1,0,2,3,2,5,1]}'
|
||||
|
||||
assert json.encode(MultTypeTest[[]byte]{}) == '{"val":[]}'
|
||||
assert json.encode(MultTypeTest[[]byte]{ val: [] }) == '{"val":[]}'
|
||||
assert json.encode(MultTypeTest[[]byte]{ val: [byte(0)] }) == '{"val":[0]}'
|
||||
assert json.encode(MultTypeTest[[]byte]{ val: [byte(1)] }) == '{"val":[1]}'
|
||||
assert json.encode(MultTypeTest[[]byte]{ val: [byte(0), 1, 0, 2, 3, 2, 5, 1] }) == '{"val":[0,1,0,2,3,2,5,1]}'
|
||||
|
||||
assert json.encode(MultTypeTest[[]i64]{}) == '{"val":[]}'
|
||||
assert json.encode(MultTypeTest[[]i64]{ val: [] }) == '{"val":[]}'
|
||||
assert json.encode(MultTypeTest[[]i64]{ val: [i64(0)] }) == '{"val":[0]}'
|
||||
assert json.encode(MultTypeTest[[]i64]{ val: [i64(1)] }) == '{"val":[1]}'
|
||||
assert json.encode(MultTypeTest[[]i64]{ val: [i64(0), 1, 0, 2, 3, 2, 5, 1] }) == '{"val":[0,1,0,2,3,2,5,1]}'
|
||||
|
||||
assert json.encode(MultTypeTest[[]u64]{}) == '{"val":[]}'
|
||||
assert json.encode(MultTypeTest[[]u64]{ val: [] }) == '{"val":[]}'
|
||||
assert json.encode(MultTypeTest[[]u64]{ val: [u64(0)] }) == '{"val":[0]}'
|
||||
assert json.encode(MultTypeTest[[]u64]{ val: [u64(1)] }) == '{"val":[1]}'
|
||||
assert json.encode(MultTypeTest[[]u64]{ val: [u64(0), 1, 0, 2, 3, 2, 5, 1] }) == '{"val":[0,1,0,2,3,2,5,1]}'
|
||||
|
||||
assert json.encode(MultTypeTest[[]f64]{}) == '{"val":[]}'
|
||||
assert json.encode(MultTypeTest[[]f64]{ val: [] }) == '{"val":[]}'
|
||||
assert json.encode(MultTypeTest[[]f64]{ val: [f64(0)] }) == '{"val":[0.0]}'
|
||||
assert json.encode(MultTypeTest[[]f64]{ val: [f64(1)] }) == '{"val":[1.0]}'
|
||||
assert json.encode(MultTypeTest[[]f64]{ val: [f64(0), 1, 0, 2, 3, 2, 5, 1] }) == '{"val":[0.0,1.0,0.0,2.0,3.0,2.0,5.0,1.0]}'
|
||||
|
||||
assert json.encode(MultTypeTest[[]bool]{}) == '{"val":[]}'
|
||||
assert json.encode(MultTypeTest[[]bool]{ val: [] }) == '{"val":[]}'
|
||||
assert json.encode(MultTypeTest[[]bool]{ val: [true] }) == '{"val":[true]}'
|
||||
assert json.encode(MultTypeTest[[]bool]{ val: [false] }) == '{"val":[false]}'
|
||||
assert json.encode(MultTypeTest[[]bool]{ val: [false, true, false] }) == '{"val":[false,true,false]}'
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user