diff --git a/vlib/x/json2/decode_struct_test.v b/vlib/x/json2/decode_struct_test.v new file mode 100644 index 0000000000..4bdb6c67b6 --- /dev/null +++ b/vlib/x/json2/decode_struct_test.v @@ -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 +} diff --git a/vlib/x/json2/decode_struct_todo_test.vv b/vlib/x/json2/decode_struct_todo_test.vv new file mode 100644 index 0000000000..e1fe43fe20 --- /dev/null +++ b/vlib/x/json2/decode_struct_todo_test.vv @@ -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 +} \ No newline at end of file diff --git a/vlib/x/json2/encode_struct_test.v b/vlib/x/json2/encode_struct_test.v new file mode 100644 index 0000000000..3835eb8257 --- /dev/null +++ b/vlib/x/json2/encode_struct_test.v @@ -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}' +} diff --git a/vlib/x/json2/encode_struct_todo_test.vv b/vlib/x/json2/encode_struct_todo_test.vv new file mode 100644 index 0000000000..158c578943 --- /dev/null +++ b/vlib/x/json2/encode_struct_todo_test.vv @@ -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' +} diff --git a/vlib/x/json2/json2_test.v b/vlib/x/json2/json2_test.v index ad42f95084..a27a5f735b 100644 --- a/vlib/x/json2/json2_test.v +++ b/vlib/x/json2/json2_test.v @@ -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(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(x) -// assert s == '{"name":"vshfvhsd","age":0,"salary":0.0,"title":0.0}' -// // y := json.decode(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]}' -}