1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

tests: add more tests for struct option fields in vlib/v/tests/option_test.v (#17297)

This commit is contained in:
zakuro 2023-02-13 17:02:47 +09:00 committed by GitHub
parent 65d218e005
commit e7fcf66095
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -180,52 +180,59 @@ fn test_reassignment() {
assert x3 == 777 assert x3 == 777
} }
struct Person { struct OptionFieldsStruct {
mut: mut:
name string text ?string
age int n ?int
title ?string n1 ?int = 1
} }
fn test_field_or() { fn get_opt_struct() ?OptionFieldsStruct {
name := foo_str() or { 'nada' } return OptionFieldsStruct{}
assert name == 'something' }
/*
QTODO fn test_option_field() ? {
mut p := Person{} mut v := OptionFieldsStruct{}
p.name = foo_str() or { assert v.text or { 'default' } == 'default'
'nothing' assert v.n or { 42 } == 42
assert v.n1 or { 42 } == 1
assert (v.n or { v.n1? }) == 1
if n := v.n {
assert false
} else {
assert true
} }
assert p.name == 'something' if n1 := v.n1 {
p.age = foo_ok() or { assert n1 == 1
panic('no age') } else {
assert false
} }
assert p.age == 777
mytitle := p.title or { n := v.n or { 10 }
'default' assert n == 10
n1 := v.n1 or { 10 }
assert n1 == 1
n1_1 := v.n1? + (v.n or { 10 })
assert n1_1 == 11
v.text = 'text'
assert v.text? == 'text'
v.n = 42
assert v.n? == 42
v.n1 = 43
assert v.n1? == 43
v = OptionFieldsStruct{
text: 'init'
n: 0
n1: none
} }
assert mytitle == 'default' assert v.text? == 'init'
*/ assert v.n? == 0
} assert v.n1 or { 42 } == 42
struct Thing { assert get_opt_struct()?.n1? == 1
mut:
opt ?int
}
struct Thing2 {
mut:
opt ?Thing
}
fn test_opt_field() {
/*
QTODO
mut t := Thing{}
t.opt = 5
val := t.opt or { return }
assert val == 5
*/
} }
fn opt_ptr(a &int) ?&int { fn opt_ptr(a &int) ?&int {
@ -360,11 +367,6 @@ fn test_option_sum_type() {
assert false assert false
} }
struct MultiOptionFieldTest {
a ?int
b ?int
}
fn foo() ?int { fn foo() ?int {
return 0 return 0
} }