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