tests: cleanup test cases (#19104)

This commit is contained in:
shove 2023-08-13 18:36:08 +08:00 committed by GitHub
parent a711e17f41
commit 11a8a46ade
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
52 changed files with 131 additions and 184 deletions

View file

@ -248,9 +248,3 @@ fn test_repeat() {
assert b.repeat(1) == b.ascii_str() assert b.repeat(1) == b.ascii_str()
assert b.repeat(0) == '' assert b.repeat(0) == ''
} }
fn test_byte_vs_u8() {
bb := byte(1)
uu := u8(1)
assert bb == uu
}

View file

@ -32,8 +32,8 @@ fn test_decode() {
'USm3fpXnKG5EUBx2ndxBDMPVciP5hGey2Jh4NDv6gmeo1LkMeiKrLJUUBk6Z': 'The quick brown fox jumps over the lazy dog.' 'USm3fpXnKG5EUBx2ndxBDMPVciP5hGey2Jh4NDv6gmeo1LkMeiKrLJUUBk6Z': 'The quick brown fox jumps over the lazy dog.'
'11StV1DL6CwTryKyV': '\x00\x00hello world' '11StV1DL6CwTryKyV': '\x00\x00hello world'
'2NEpo7TZRRrLZSi2U': 'Hello World!' '2NEpo7TZRRrLZSi2U': 'Hello World!'
'14cxpo3MBCYYWCgF74SWTdcmxipnGUsPw3': hex.decode('0027b5891b01da2db74cde1689a97a2acbe23d5fb1c0205bf6')?.bytestr() '14cxpo3MBCYYWCgF74SWTdcmxipnGUsPw3': hex.decode('0027b5891b01da2db74cde1689a97a2acbe23d5fb1c0205bf6')!.bytestr()
'3vQB7B6MrGQZaxCuFg4oh': hex.decode('68656c6c6f20776f726c64bc62d4b8')?.bytestr() '3vQB7B6MrGQZaxCuFg4oh': hex.decode('68656c6c6f20776f726c64bc62d4b8')!.bytestr()
} { } {
input := base58.decode(output)! input := base58.decode(output)!
println('> output: `${output}` | decoded input: `${input}` | bytes: ${input.bytes().hex()}') println('> output: `${output}` | decoded input: `${input}` | bytes: ${input.bytes().hex()}')

View file

@ -1,16 +1,18 @@
net/html is an **HTML Parser** written in pure V. net/html is an **HTML Parser** written in pure V.
## Usage ## Usage
```v oksyntax
```v
import net.html import net.html
fn main() { fn main() {
doc := html.parse('<html><body><h1 class="title">Hello world!</h1></body></html>') doc := html.parse('<html><body><h1 class="title">Hello world!</h1></body></html>')
tag := doc.get_tag('h1')[0] // <h1>Hello world!</h1> tag := doc.get_tags(name: 'h1')[0] // <h1>Hello world!</h1>
println(tag.name) // h1 println(tag.name) // h1
println(tag.content) // Hello world! println(tag.content) // Hello world!
println(tag.attributes) // {'class':'title'} println(tag.attributes) // {'class':'title'}
println(tag.str()) // <h1 class="title">Hello world!</h1> println(tag.str()) // <h1 class="title">Hello world!</h1>
} }
``` ```
More examples found on [`parser_test.v`](parser_test.v) and [`html_test.v`](html_test.v) More examples found on [`parser_test.v`](parser_test.v) and [`html_test.v`](html_test.v)

View file

@ -2,7 +2,7 @@ module html
fn test_parse() { fn test_parse() {
doc := parse('<html><body><h1 class="title">Hello world!</h1></body></html>') doc := parse('<html><body><h1 class="title">Hello world!</h1></body></html>')
tags := doc.get_tag('h1') tags := doc.get_tags(name: 'h1')
assert tags.len == 1 assert tags.len == 1
h1_tag := tags[0] // <h1>Hello world!</h1> h1_tag := tags[0] // <h1>Hello world!</h1>
assert h1_tag.name == 'h1' assert h1_tag.name == 'h1'
@ -16,13 +16,13 @@ fn test_parse() {
fn test_parse_inline_tags() { fn test_parse_inline_tags() {
doc := parse('<html><body><p>before <span>in between</span> after</p></body></html>') doc := parse('<html><body><p>before <span>in between</span> after</p></body></html>')
tags := doc.get_tag('span') tags := doc.get_tags(name: 'span')
assert tags.len == 1 assert tags.len == 1
span_tag := tags[0] span_tag := tags[0]
assert span_tag.str() == '<span>in between</span>' assert span_tag.str() == '<span>in between</span>'
p_tags := doc.get_tag('p') p_tags := doc.get_tags(name: 'p')
assert p_tags.len == 1 assert p_tags.len == 1
p_tag := p_tags[0] p_tag := p_tags[0]

View file

@ -32,7 +32,7 @@ const (
fn test_search_tag_by_type() { fn test_search_tag_by_type() {
mut dom := parse(html.html) mut dom := parse(html.html)
tag := dom.get_tags(GetTagsOptions{'body'})[0] tag := dom.get_tags(name: 'body')[0]
assert tag.get_tag('div')?.attributes['id'] == '1st' assert tag.get_tag('div')?.attributes['id'] == '1st'
assert tag.get_tag_by_attribute('href')?.content == 'V' assert tag.get_tag_by_attribute('href')?.content == 'V'
// TODO: update after improved parsing to not add trailing white space to attribute values // TODO: update after improved parsing to not add trailing white space to attribute values
@ -65,7 +65,7 @@ fn generate_temp_html_with_classes() string {
fn test_search_by_class() { fn test_search_by_class() {
mut dom := parse(generate_temp_html_with_classes()) mut dom := parse(generate_temp_html_with_classes())
tag := dom.get_tags(GetTagsOptions{'body'})[0] tag := dom.get_tags(name: 'body')[0]
single_class_tags := tag.get_tags_by_class_name('single') single_class_tags := tag.get_tags_by_class_name('single')
common_class_tags := tag.get_tags_by_class_name('common') common_class_tags := tag.get_tags_by_class_name('common')
complex_class_tags := tag.get_tags_by_class_name('complex-0', 'complex-1', 'complex-2') complex_class_tags := tag.get_tags_by_class_name('complex-0', 'complex-1', 'complex-2')

View file

@ -346,7 +346,7 @@ fn parse_headers_test(s string, expected map[string]string) ? {
assert parse_headers(s)? == new_custom_header_from_map(expected)? assert parse_headers(s)? == new_custom_header_from_map(expected)?
} }
fn test_parse_headers() ? { fn test_parse_headers() ! {
parse_headers_test('foo: bar', { parse_headers_test('foo: bar', {
'foo': 'bar' 'foo': 'bar'
})? })?
@ -371,7 +371,7 @@ fn test_parse_headers() ? {
'foo': 'bar' 'foo': 'bar'
'bar': 'baz' 'bar': 'baz'
})? })?
assert parse_headers('foo: bar\r\nfoo:baz')?.custom_values('foo') == ['bar', 'baz'] assert parse_headers('foo: bar\r\nfoo:baz')!.custom_values('foo') == ['bar', 'baz']
if x := parse_headers(' oops: oh no') { if x := parse_headers(' oops: oh no') {
return error('should have errored, but got ${x}') return error('should have errored, but got ${x}')

View file

@ -14,7 +14,7 @@ fn test_format() {
f3 := 1234.300e-6 f3 := 1234.300e-6
sc0 := 'ciao: [%-08u] %d %hhd [%8s] [%08X] [%-20.4f] [%-20.4f] [%c]' sc0 := 'ciao: [%-08u] %d %hhd [%8s] [%08X] [%-20.4f] [%-20.4f] [%c]'
temp_s = strconv.v_sprintf(sc0, a0, b0, c0, s0, b0, f0, f1, ch0) temp_s = unsafe { strconv.v_sprintf(sc0, a0, b0, c0, s0, b0, f0, f1, ch0) }
tmp_str = 'ciao: [10 ] 200 12 [ ciAo] [000000C8] [0.3123 ] [200000.0000 ] [B]' tmp_str = 'ciao: [10 ] 200 12 [ ciAo] [000000C8] [0.3123 ] [200000.0000 ] [B]'
// C.printf(sc0.str,a0 ,b0 ,c0 ,s0.str ,b0 ,f0, f1, ch0) // C.printf(sc0.str,a0 ,b0 ,c0 ,s0.str ,b0 ,f0, f1, ch0)
// println("\n$temp_s") // println("\n$temp_s")
@ -25,7 +25,7 @@ fn test_format() {
c := 14 c := 14
d := i64(15) d := i64(15)
sc1 := '==>%hhd %hd %d %ld' sc1 := '==>%hhd %hd %d %ld'
temp_s = strconv.v_sprintf(sc1, a, b, c, d) temp_s = unsafe { strconv.v_sprintf(sc1, a, b, c, d) }
tmp_str = '==>12 13 14 15' tmp_str = '==>12 13 14 15'
// C.printf(sc1.str, a ,b ,c, d) // C.printf(sc1.str, a ,b ,c, d)
// println("\n$temp_s") // println("\n$temp_s")
@ -36,28 +36,28 @@ fn test_format() {
c1 := u32(0xffff_ffff) c1 := u32(0xffff_ffff)
d1 := u64(-1) d1 := u64(-1)
sc2 := '%hhu %hu %u %lu' sc2 := '%hhu %hu %u %lu'
temp_s = strconv.v_sprintf(sc2, a1, b1, c1, d1) temp_s = unsafe { strconv.v_sprintf(sc2, a1, b1, c1, d1) }
tmp_str = '255 65535 4294967295 18446744073709551615' tmp_str = '255 65535 4294967295 18446744073709551615'
// C.printf(sc2.str, a1 ,b1 ,c1, d1) // C.printf(sc2.str, a1 ,b1 ,c1, d1)
// println("\n$temp_s") // println("\n$temp_s")
assert tmp_str == temp_s assert tmp_str == temp_s
sc3 := '%hhx %hx %x %lx' sc3 := '%hhx %hx %x %lx'
temp_s = strconv.v_sprintf(sc3, a1, b1, c1, d1) temp_s = unsafe { strconv.v_sprintf(sc3, a1, b1, c1, d1) }
tmp_str = 'ff ffff ffffffff ffffffffffffffff' tmp_str = 'ff ffff ffffffff ffffffffffffffff'
// C.printf(sc3.str, a1 ,b1 ,c1, d1) // C.printf(sc3.str, a1 ,b1 ,c1, d1)
// println("\n$temp_s") // println("\n$temp_s")
assert tmp_str == temp_s assert tmp_str == temp_s
sc4 := '[%-20.3e] [%20.3e] [%-020.3e] [%-020.3E] [%-020.3e] [%-020.3e]' sc4 := '[%-20.3e] [%20.3e] [%-020.3e] [%-020.3E] [%-020.3e] [%-020.3e]'
temp_s = strconv.v_sprintf(sc4, f0, f1, f1, f1, f2, f3) temp_s = unsafe { strconv.v_sprintf(sc4, f0, f1, f1, f1, f2, f3) }
tmp_str = '[3.123e-01 ] [ 2.000e+05] [2.000e+05 ] [2.000E+05 ] [-1.234e+09 ] [1.234e-03 ]' tmp_str = '[3.123e-01 ] [ 2.000e+05] [2.000e+05 ] [2.000E+05 ] [-1.234e+09 ] [1.234e-03 ]'
// C.printf(sc4.str, f0, f1, f1, f1, f2, f3) // C.printf(sc4.str, f0, f1, f1, f1, f2, f3)
// println("\n$temp_s") // println("\n$temp_s")
assert tmp_str == temp_s assert tmp_str == temp_s
sc5 := '[%.3f] [%0.3f] [%0.3F] [%0.3f] [%0.3F]' sc5 := '[%.3f] [%0.3f] [%0.3F] [%0.3f] [%0.3F]'
temp_s = strconv.v_sprintf(sc5, f0, f1, f1, f2, f3) temp_s = unsafe { strconv.v_sprintf(sc5, f0, f1, f1, f2, f3) }
tmp_str = '[0.312] [200000.000] [200000.000] [-1234300000.000] [0.001]' tmp_str = '[0.312] [200000.000] [200000.000] [-1234300000.000] [0.001]'
// C.printf(sc5.str, f0, f1, f1, f2, f3, f3) // C.printf(sc5.str, f0, f1, f1, f2, f3, f3)
// println("\n$temp_s") // println("\n$temp_s")
@ -65,7 +65,7 @@ fn test_format() {
ml := 3 ml := 3
sc6 := '%.*s [%05hhX]' sc6 := '%.*s [%05hhX]'
temp_s = strconv.v_sprintf(sc6, ml, s0, a) temp_s = unsafe { strconv.v_sprintf(sc6, ml, s0, a) }
tmp_str = 'ciA [0000C]' tmp_str = 'ciA [0000C]'
// C.printf(sc6.str, ml, s0.str, a) // C.printf(sc6.str, ml, s0.str, a)
// println("\n$temp_s") // println("\n$temp_s")
@ -73,7 +73,7 @@ fn test_format() {
a2 := 125 a2 := 125
sc7 := '[%9x] [%9X] [%-9x] [%-9X] [%09x] [%09X]' sc7 := '[%9x] [%9X] [%-9x] [%-9X] [%09x] [%09X]'
temp_s = strconv.v_sprintf(sc7, a2, a2, a2, a2, a2, a2) temp_s = unsafe { strconv.v_sprintf(sc7, a2, a2, a2, a2, a2, a2) }
tmp_str = '[ 7d] [ 7D] [7d ] [7D ] [00000007d] [00000007D]' tmp_str = '[ 7d] [ 7D] [7d ] [7D ] [00000007d] [00000007D]'
// C.printf(sc7.str, a2, a2, a2, a2, a2, a2) // C.printf(sc7.str, a2, a2, a2, a2, a2, a2)
// println("\n$temp_s") // println("\n$temp_s")
@ -99,7 +99,7 @@ fn test_format() {
mut cnt := 0 mut cnt := 0
sc8 := '[%20g][%20G]|' sc8 := '[%20g][%20G]|'
for x < 12 { for x < 12 {
temp_s = strconv.v_sprintf(sc8, ft, ft) temp_s = unsafe { strconv.v_sprintf(sc8, ft, ft) }
// C.printf(sc8.str, ft, ft) // C.printf(sc8.str, ft, ft)
// println("\n$temp_s") // println("\n$temp_s")
assert temp_s == g_test[cnt] assert temp_s == g_test[cnt]
@ -111,11 +111,11 @@ fn test_format() {
fn test_sprintf_does_not_double_free_on_g() { fn test_sprintf_does_not_double_free_on_g() {
x := 3.141516 x := 3.141516
assert strconv.v_sprintf('aaa %G', x) == 'aaa 3.141516' assert unsafe { strconv.v_sprintf('aaa %G', x) } == 'aaa 3.141516'
} }
fn test_sprintf_with_escape() { fn test_sprintf_with_escape() {
n := 69 n := 69
s := strconv.v_sprintf('%d is 100%% awesome', n) s := unsafe { strconv.v_sprintf('%d is 100%% awesome', n) }
assert s == '69 is 100% awesome' assert s == '69 is 100% awesome'
} }

View file

@ -1,41 +0,0 @@
fn returns_sumtype() int|string {
return 1
}
fn returns_sumtype_reverse() int|string {
return 1
}
fn test_stringification() {
x := returns_sumtype()
y := returns_sumtype_reverse()
assert '${x}' == '${y}'
}
struct Milk {
egg int|string
}
fn test_struct_with_inline_sumtype() {
m := Milk{
egg: 1
}
assert m.egg is int
}
interface IMilk {
egg int|string
}
fn receive_imilk(milk IMilk) {}
fn test_interface_with_inline_sumtype() {
m := Milk{
egg: 1
}
receive_imilk(m)
}
fn returns_sumtype_in_multireturn() (int|string, string) {
return 1, ''
}

View file

@ -247,7 +247,7 @@ fn test_array_init_inferred_from_option() {
println(b) println(b)
} }
fn read() ?[]string { fn read() ![]string {
return error('failed') return error('failed')
} }

View file

@ -12,7 +12,7 @@ pub:
details Possibilities details Possibilities
} }
fn (t PossOwner) get_file(path string) ?(PossOwner, Poss1) { fn (t PossOwner) get_file(path string) !(PossOwner, Poss1) {
match t.details { match t.details {
Poss1 { return t, t.details } Poss1 { return t, t.details }
else { return error('not a file') } else { return error('not a file') }

View file

@ -13,7 +13,7 @@ pub struct PubStructAttrTest {
struct StructFieldAttrTest { struct StructFieldAttrTest {
foo string [attr: bar; attr0; attr1: 'foo'] foo string [attr: bar; attr0; attr1: 'foo']
bar int [attr0: 123; attr1: true; attr2: false] bar int [attr0: 123; attr1: true; attr2: false]
baz bool [prefix.attr0] = false baz bool [prefix.attr0] = true
} }
[testing] [testing]

View file

@ -12,12 +12,12 @@ struct SubInfo {
fn test_autogen_free() { fn test_autogen_free() {
info := &Info{} info := &Info{}
info.free() unsafe { info.free() }
assert true assert true
} }
fn test_multiple_autogen_free() { fn test_multiple_autogen_free() {
info := &Info{} info := &Info{}
info.free() unsafe { info.free() }
assert true assert true
} }

View file

@ -1,5 +0,0 @@
fn test_cast_to_anon_sumtype() {
x := string|none(none)
println(x)
assert '${x}' == 'string|none(none)'
}

View file

@ -24,7 +24,7 @@ fn encode[T](typ T) map[string]Any {
fn test_main() { fn test_main() {
a := Arr{[5], [2.0], ['asdf']} a := Arr{[5], [2.0], ['asdf']}
r := encode[Arr](a) r := encode[Arr](a)
assert r['ints'] == Any([Any(5)]) assert unsafe { r['ints'] } == Any([Any(5)])
assert r['floats'] == Any([Any(2.0)]) assert unsafe { r['floats'] } == Any([Any(2.0)])
assert r['strs'] == Any([Any('asdf')]) assert unsafe { r['strs'] } == Any([Any('asdf')])
} }

View file

@ -23,7 +23,7 @@ struct Foo {
name string = 'foo' name string = 'foo'
} }
fn foo_decode(name string) ?Foo { fn foo_decode(name string) !Foo {
if name == 'baz' { if name == 'baz' {
return error('baz is not allowed') return error('baz is not allowed')
} }
@ -32,7 +32,7 @@ fn foo_decode(name string) ?Foo {
pub const ( pub const (
def = foo_decode('baz') or { Foo{} } def = foo_decode('baz') or { Foo{} }
bar = foo_decode('bar')? bar = foo_decode('bar')!
) )
fn test_opt_const() { fn test_opt_const() {

View file

@ -77,7 +77,7 @@ fn test_defer_opt_return() {
assert y.n == 1 assert y.n == 1
} }
fn option_return_err(mut a Qwe) ?Qwe { fn option_return_err(mut a Qwe) !Qwe {
defer { defer {
a.n += 5 a.n += 5
} }

View file

@ -30,7 +30,7 @@ struct Foo {
struct Bar { struct Bar {
pub: pub:
foo ?Foo = none foo ?Foo
} }
fn test_main() { fn test_main() {

View file

@ -4,7 +4,7 @@ enum Color {
green green
} }
fn enum_option_helper(b bool) ?Color { fn enum_option_helper(b bool) !Color {
if b { if b {
return .red return .red
} }

View file

@ -1,4 +1,4 @@
fn foo() ? { fn foo() ! {
return error('something') return error('something')
} }
@ -10,7 +10,7 @@ fn test_option_void() {
} }
} }
fn bar() ? { fn bar() ! {
return error('bar error') return error('bar error')
} }
@ -27,7 +27,7 @@ fn test_option_void_with_empty_or() {
assert true assert true
} }
fn option_void(a int) ? { fn option_void(a int) ! {
if a != 0 { if a != 0 {
return return
} else { } else {

View file

@ -20,10 +20,10 @@ fn fn_mr_get_user() (string, int, []string, UserData) {
return 'joe', 34, groups, data return 'joe', 34, groups, data
} }
fn split_to_two(s string) ?(string, string) { fn split_to_two(s string) !(string, string) {
mut tokens := s.split_nth(' ', 2) mut tokens := s.split_nth(' ', 2)
if s.len == 0 { if s.len == 0 {
return none return error('error')
} }
if tokens.len != 2 { if tokens.len != 2 {
return error('error') return error('error')
@ -51,7 +51,7 @@ fn test_multiple_ret() {
// none case // none case
wrapper1 := fn () (string, string) { wrapper1 := fn () (string, string) {
res2_1, res2_2 := split_to_two('') or { res2_1, res2_2 := split_to_two('') or {
assert err.msg() == '' assert err.msg() == 'error'
return 'replaced', 'val' return 'replaced', 'val'
} }
return res2_1, res2_2 return res2_1, res2_2

View file

@ -1,12 +1,12 @@
struct Animal { struct Animal {
mut: mut:
age fn (p int) int age fn (p int) int = unsafe { nil }
duck Duck duck Duck
} }
struct Duck { struct Duck {
mut: mut:
age &fn (p int) int age &fn (p int) int = unsafe { nil }
} }
fn test_main() { fn test_main() {

View file

@ -10,7 +10,7 @@ fn f() shared St {
return x return x
} }
fn g(good bool) ?shared St { fn g(good bool) !shared St {
if !good { if !good {
return error('no shared St created') return error('no shared St created')
} }
@ -28,8 +28,8 @@ fn test_shared_fn_return() {
assert val == 3.25 assert val == 3.25
} }
fn shared_opt_propagate(good bool) ?f64 { fn shared_opt_propagate(good bool) !f64 {
shared x := g(good)? shared x := g(good)!
ret := rlock x { ret := rlock x {
x.x x.x
} }

View file

@ -11,7 +11,7 @@ const (
struct Packet { struct Packet {
pid int pid int
handle fn () string handle fn () string = unsafe { nil }
restricted bool restricted bool
} }
@ -24,14 +24,14 @@ mut:
fn (mut p Reader) next() ?&Packet { fn (mut p Reader) next() ?&Packet {
if p.index + 1 > packets.len { if p.index + 1 > packets.len {
return error('') return none
} }
if p.index !in packets { if p.index !in packets {
return error('') return none
} }
p.index++ p.index++
return packets[p.index - 1] return unsafe { packets[p.index - 1] }
} }
fn test_for_in_mut_interator_val() { fn test_for_in_mut_interator_val() {

View file

@ -18,7 +18,7 @@ mut:
fn (mut s StructsRowIterator) next() ?[]int { fn (mut s StructsRowIterator) next() ?[]int {
if s.position >= s.array.len { if s.position >= s.array.len {
return error('out of range') return none
} }
defer { defer {
s.position++ s.position++

View file

@ -8,7 +8,7 @@ struct Container {
concrete Any concrete Any
} }
fn (container &Container) get_first_struct[T]() ?&T { fn (container &Container) get_first_struct[T]() !&T {
concrete := container.concrete concrete := container.concrete
if concrete is T { if concrete is T {
println(concrete.a) println(concrete.a)

View file

@ -1,7 +1,7 @@
struct Node[T] { struct Node[T] {
mut: mut:
value T value T
next ?&Node[T] = none next ?&Node[T]
} }
fn test_main() { fn test_main() {

View file

@ -1,6 +1,6 @@
pub type Result[S] = Ok[S] | string pub type Result[S] = Ok[S] | string
pub fn (x Result[S]) unwrap[S]() ?S { pub fn (x Result[S]) unwrap[S]() !S {
match x { match x {
Ok[S] { Ok[S] {
return x.value return x.value

View file

@ -16,7 +16,7 @@ fn fill(mut s MyStruct[i64]) {
fn test_generics_call_with_reference_arg() { fn test_generics_call_with_reference_arg() {
mut s := MyStruct[i64]{ mut s := MyStruct[i64]{
pos: 1 pos: 1
buffer: []&i64{len: 2} buffer: unsafe { []&i64{len: 2} }
} }
fill(mut s) fill(mut s)
println(s.pos) println(s.pos)

View file

@ -13,7 +13,7 @@ fn test_generics_with_nested_external_generics_fn() {
mut arr := [11, 32, 24, 45, 57, 32, 37, 52, 37, 24] mut arr := [11, 32, 24, 45, 57, 32, 37, 52, 37, 24]
println(arr) println(arr)
ret := sample[int](arr, 5)? ret := sample[int](arr, 5)!
println(ret) println(ret)
assert ret == [32, 45, 57, 11, 37] assert ret == [32, 45, 57, 11, 37]

View file

@ -13,7 +13,7 @@ pub:
name string name string
} }
pub fn (mut gitstructure GitStructure) repo_get(name string) ?&GitRepo { pub fn (mut gitstructure GitStructure) repo_get(name string) !&GitRepo {
for r in gitstructure.repos { for r in gitstructure.repos {
if r.name == name { if r.name == name {
if name != '' { if name != '' {
@ -60,7 +60,7 @@ pub mut:
repos []GitRepo repos []GitRepo
} }
pub fn (mut gitstructure GitStructureNoRef) repo_get(name string) ?&GitRepo { pub fn (mut gitstructure GitStructureNoRef) repo_get(name string) !&GitRepo {
for r in gitstructure.repos { for r in gitstructure.repos {
if r.name == name { if r.name == name {
if name != '' { if name != '' {

View file

@ -1,4 +1,4 @@
fn f(n int) ?f64 { fn f(n int) !f64 {
if n < 0 { if n < 0 {
return error('negative') return error('negative')
} }

View file

@ -2,7 +2,7 @@ type Abc = int | string
fn test_map_get_decl_assign_blank() { fn test_map_get_decl_assign_blank() {
x := map[string]Abc{} x := map[string]Abc{}
_ := x['nonexisting'] _ := unsafe { x['nonexisting'] }
if y := x['nonexisting'] { if y := x['nonexisting'] {
println(y) println(y)
} }
@ -11,7 +11,7 @@ fn test_map_get_decl_assign_blank() {
fn test_map_get_assign_blank() { fn test_map_get_assign_blank() {
x := map[string]Abc{} x := map[string]Abc{}
_ = x['nonexisting'] _ = unsafe { x['nonexisting'] }
if y := x['nonexisting'] { if y := x['nonexisting'] {
println(y) println(y)
} }

View file

@ -1,15 +1,12 @@
fn do_a_thing(i int) ?int { fn do_a_thing(i int) ?int {
if i < 0 { if i < 0 {
return error("can't be negative")
}
if i == 0 {
return none return none
} }
return i return i
} }
fn test_match_error_to_none() { fn test_match_error_to_none() {
i := 0 for i := -1; i < 1; i++ {
if r := do_a_thing(i) { if r := do_a_thing(i) {
println(r) println(r)
} else { } else {
@ -22,4 +19,5 @@ fn test_match_error_to_none() {
} }
} }
} }
}
} }

View file

@ -12,9 +12,9 @@ enum Operator {
type Value = Operator | int type Value = Operator | int
struct Expression { struct Expression {
left ?&Expression = none left ?&Expression
val Value val Value
right ?&Expression = none right ?&Expression
} }
enum State { enum State {

View file

@ -1,13 +0,0 @@
fn number() int|none {
return none
}
fn test_match_sumtype_var_with_none() {
n := number()
ret := match n {
int { 'n: ${n}' }
none { '?' }
}
println(ret)
assert ret == '?'
}

View file

@ -8,3 +8,8 @@ struct C.sub_foo {
} }
pub type Foo = C.sub_foo pub type Foo = C.sub_foo
// avoiding compiler warnings: module 'c (sub.foo.c)' is imported but never used
fn bar() {
_ = c.used_import
}

View file

@ -31,7 +31,7 @@ mut:
} }
fn (mut s St) raise() ?f64 { fn (mut s St) raise() ?f64 {
return error('some error') return none
} }
fn retf(f f64) f64 { fn retf(f f64) f64 {

View file

@ -1,6 +1,6 @@
fn f(n int) ?int { fn f(n int) ?int {
if n < 0 { if n < 0 {
return error('negative arg') return none
} }
return n return n
} }

View file

@ -4,28 +4,28 @@ struct Abc {
fn i_0(x int) ?int { fn i_0(x int) ?int {
if x == 0 { if x == 0 {
return error('my error 1') return none
} }
return x return x
} }
fn struct_0(x int) ?Abc { fn struct_0(x int) ?Abc {
if x == 0 { if x == 0 {
return error('my error 2') return none
} }
return Abc{x} return Abc{x}
} }
fn string_0(x int) ?string { fn string_0(x int) ?string {
if x == 0 { if x == 0 {
return error('my error 3') return none
} }
return '${x}' return '${x}'
} }
fn b_0(b bool) ?bool { fn b_0(b bool) ?bool {
if b == false { if b == false {
return error('my error 4') return none
} }
return b return b
} }

View file

@ -8,7 +8,7 @@ fn print_error() ?[]Empty {
empty: 'Test' empty: 'Test'
} }
if test[0].empty != '' { if test[0].empty != '' {
return error('Not empty') return none
} }
return test return test
} }

View file

@ -2,7 +2,7 @@ fn err_call(ok bool) ?int {
if ok { if ok {
return 42 return 42
} }
return error('Not ok!') return none
} }
fn test_if_opt() { fn test_if_opt() {

View file

@ -1,6 +1,6 @@
fn opt_0_10_20(x int) ?int { fn opt_0_10_20(x int) ?int {
if x < 0 || (x >= 10 && x <= 20) { if x < 0 || (x >= 10 && x <= 20) {
return error('invalid') return none
} }
return x return x
} }

View file

@ -1,14 +1,14 @@
struct Node { struct Node {
value int value int
mut: mut:
prev ?&Node = none prev ?&Node
next ?&Node = none next ?&Node
} }
struct LinkedList { struct LinkedList {
mut: mut:
head ?&Node = none head ?&Node
tail ?&Node = none tail ?&Node
} }
pub fn (mut l LinkedList) push(value int) { pub fn (mut l LinkedList) push(value int) {

View file

@ -7,6 +7,6 @@ fn test_or_expr_with_multi_stmts() {
assert x == -100 assert x == -100
} }
fn fmt_test() ?int { fn fmt_test() !int {
return error('foo') return error('foo')
} }

View file

@ -21,7 +21,7 @@ fn delete_secret_v1() API_error {
println(response) println(response)
} }
fn req_do() ?string { fn req_do() !string {
return error('dial_tcp failed') return error('dial_tcp failed')
} }

View file

@ -11,6 +11,16 @@ fn f() St {
} }
fn g(good bool) ?St { fn g(good bool) ?St {
if !good {
return none
}
x := St{
x: 12.75
}
return x
}
fn r(good bool) !St {
if !good { if !good {
return error('no St created') return error('no St created')
} }
@ -36,9 +46,22 @@ fn shared_opt_propagate(good bool) ?f64 {
return ret return ret
} }
fn shared_err_propagate(good bool) !f64 {
shared x := r(good)!
ret := rlock x {
x.x
}
return ret
}
fn test_shared_opt_propagate() { fn test_shared_opt_propagate() {
x := shared_opt_propagate(true) or { 1.25 } mut x := shared_opt_propagate(true) or { 1.25 }
y := shared_opt_propagate(false) or { 2.125 } mut y := shared_opt_propagate(false) or { 2.125 }
assert x == 12.75
assert y == 2.125
x = shared_err_propagate(true) or { 1.25 }
y = shared_err_propagate(false) or { 2.125 }
assert x == 12.75 assert x == 12.75
assert y == 2.125 assert y == 2.125
} }

View file

@ -1,5 +1,5 @@
fn test_string_array_of_ref_type() { fn test_string_array_of_ref_type() {
a := []&int{len: 2} a := unsafe { []&int{len: 2} }
println(a) println(a)
assert '${a}' == '[nil, nil]' assert '${a}' == '[nil, nil]'
} }

View file

@ -11,7 +11,7 @@ fn (err MyError) code() int {
return err.code return err.code
} }
fn foo() int|none|IError { fn foo() IError {
return MyError{} return MyError{}
} }

View file

@ -3,12 +3,12 @@ interface Greeting {
} }
struct Hello { struct Hello {
tt ?string = none tt ?string
value string value string
} }
struct Hi { struct Hi {
tt ?string = none tt ?string
} }
fn greet(g Greeting) string { fn greet(g Greeting) string {

View file

@ -360,13 +360,13 @@ fn test_struct_with_default_values_no_init() {
} }
struct FieldsWithOptionVoidReturnType { struct FieldsWithOptionVoidReturnType {
f fn () ? f fn () ! [required]
g fn () ? g fn () ? [required]
} }
fn test_fields_anon_fn_with_option_void_return_type() { fn test_fields_anon_fn_with_option_void_return_type() {
foo := FieldsWithOptionVoidReturnType{ foo := FieldsWithOptionVoidReturnType{
f: fn () ? { f: fn () ! {
return error('oops') return error('oops')
} }
g: fn () ? { g: fn () ? {

View file

@ -1,16 +0,0 @@
fn string_none() string|none {
return none
}
fn test_sumtype_with_none() {
x := string_none()
res := match x {
string {
false
}
none {
true
}
}
assert res
}

View file

@ -19,14 +19,14 @@ fn test_envbang_script_runs() {
import os import os
println('hello') println('hello')
println(os.args) println(os.args)
")? ")!
os.chmod(rnd_vsh_script_path, 0o700)? os.chmod(rnd_vsh_script_path, 0o700)!
res := os.execute('${os.quoted_path(rnd_vsh_script_path)} abc 123 -option') res := os.execute('${os.quoted_path(rnd_vsh_script_path)} abc 123 -option')
assert res.exit_code == 0 assert res.exit_code == 0
lines := res.output.split_into_lines() lines := res.output.split_into_lines()
assert lines[0] == 'hello' assert lines[0] == 'hello'
assert lines[1].ends_with(", 'abc', '123', '-option']") assert lines[1].ends_with(", 'abc', '123', '-option']")
os.rm(rnd_vsh_script_path)? os.rm(rnd_vsh_script_path)!
} }
[noreturn] [noreturn]