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

tests: cleanup test cases

This commit is contained in:
shove70 2023-08-10 19:06:04 +08:00
parent 65a493d023
commit e523bb1a19
55 changed files with 145 additions and 251 deletions

View File

@ -248,9 +248,3 @@ fn test_repeat() {
assert b.repeat(1) == b.ascii_str()
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.'
'11StV1DL6CwTryKyV': '\x00\x00hello world'
'2NEpo7TZRRrLZSi2U': 'Hello World!'
'14cxpo3MBCYYWCgF74SWTdcmxipnGUsPw3': hex.decode('0027b5891b01da2db74cde1689a97a2acbe23d5fb1c0205bf6')?.bytestr()
'3vQB7B6MrGQZaxCuFg4oh': hex.decode('68656c6c6f20776f726c64bc62d4b8')?.bytestr()
'14cxpo3MBCYYWCgF74SWTdcmxipnGUsPw3': hex.decode('0027b5891b01da2db74cde1689a97a2acbe23d5fb1c0205bf6')!.bytestr()
'3vQB7B6MrGQZaxCuFg4oh': hex.decode('68656c6c6f20776f726c64bc62d4b8')!.bytestr()
} {
input := base58.decode(output)!
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.
## Usage
```v oksyntax
```v
import net.html
fn main() {
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.content) // Hello world!
println(tag.attributes) // {'class':'title'}
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)

View File

@ -2,7 +2,7 @@ module html
fn test_parse() {
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
h1_tag := tags[0] // <h1>Hello world!</h1>
assert h1_tag.name == 'h1'
@ -16,13 +16,13 @@ fn test_parse() {
fn test_parse_inline_tags() {
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
span_tag := tags[0]
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
p_tag := p_tags[0]

View File

@ -32,7 +32,7 @@ const (
fn test_search_tag_by_type() {
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_by_attribute('href')?.content == 'V'
// 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() {
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')
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')

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)?
}
fn test_parse_headers() ? {
fn test_parse_headers() ! {
parse_headers_test('foo: bar', {
'foo': 'bar'
})?
@ -371,7 +371,7 @@ fn test_parse_headers() ? {
'foo': 'bar'
'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') {
return error('should have errored, but got ${x}')

View File

@ -2,32 +2,32 @@ module websocket
// MessageEventHandler represents a callback on a new message
struct MessageEventHandler {
handler SocketMessageFn // callback function
handler2 SocketMessageFn2 // callback function with reference
handler SocketMessageFn = unsafe { nil } // callback function
handler2 SocketMessageFn2 = unsafe { nil } // callback function with reference
is_ref bool // true if has a reference object
ref voidptr // referenced object
}
// ErrorEventHandler represents a callback on error
struct ErrorEventHandler {
handler SocketErrorFn // callback function
handler2 SocketErrorFn2 // callback function with reference
handler SocketErrorFn = unsafe { nil } // callback function
handler2 SocketErrorFn2 = unsafe { nil } // callback function with reference
is_ref bool // true if has a reference object
ref voidptr // referenced object
}
// OpenEventHandler represents a callback when connection is opened
struct OpenEventHandler {
handler SocketOpenFn // callback function
handler2 SocketOpenFn2 // callback function with reference
handler SocketOpenFn = unsafe { nil } // callback function
handler2 SocketOpenFn2 = unsafe { nil } // callback function with reference
is_ref bool // true if has a reference object
ref voidptr // referenced object
}
// CloseEventHandler represents a callback on a closing event
struct CloseEventHandler {
handler SocketCloseFn // callback function
handler2 SocketCloseFn2 // callback function with reference
handler SocketCloseFn = unsafe { nil } // callback function
handler2 SocketCloseFn2 = unsafe { nil } // callback function with reference
is_ref bool // true if has a reference object
ref voidptr // referenced object
}

View File

@ -14,7 +14,7 @@ fn test_format() {
f3 := 1234.300e-6
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]'
// C.printf(sc0.str,a0 ,b0 ,c0 ,s0.str ,b0 ,f0, f1, ch0)
// println("\n$temp_s")
@ -25,7 +25,7 @@ fn test_format() {
c := 14
d := i64(15)
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'
// C.printf(sc1.str, a ,b ,c, d)
// println("\n$temp_s")
@ -36,28 +36,28 @@ fn test_format() {
c1 := u32(0xffff_ffff)
d1 := u64(-1)
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'
// C.printf(sc2.str, a1 ,b1 ,c1, d1)
// println("\n$temp_s")
assert tmp_str == temp_s
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'
// C.printf(sc3.str, a1 ,b1 ,c1, d1)
// println("\n$temp_s")
assert tmp_str == temp_s
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 ]'
// C.printf(sc4.str, f0, f1, f1, f1, f2, f3)
// println("\n$temp_s")
assert tmp_str == temp_s
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]'
// C.printf(sc5.str, f0, f1, f1, f2, f3, f3)
// println("\n$temp_s")
@ -65,7 +65,7 @@ fn test_format() {
ml := 3
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]'
// C.printf(sc6.str, ml, s0.str, a)
// println("\n$temp_s")
@ -73,7 +73,7 @@ fn test_format() {
a2 := 125
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]'
// C.printf(sc7.str, a2, a2, a2, a2, a2, a2)
// println("\n$temp_s")
@ -99,7 +99,7 @@ fn test_format() {
mut cnt := 0
sc8 := '[%20g][%20G]|'
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)
// println("\n$temp_s")
assert temp_s == g_test[cnt]
@ -111,11 +111,11 @@ fn test_format() {
fn test_sprintf_does_not_double_free_on_g() {
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() {
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'
}

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)
}
fn read() ?[]string {
fn read() ![]string {
return error('failed')
}

View File

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

View File

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

View File

@ -12,12 +12,12 @@ struct SubInfo {
fn test_autogen_free() {
info := &Info{}
info.free()
unsafe { info.free() }
assert true
}
fn test_multiple_autogen_free() {
info := &Info{}
info.free()
unsafe { info.free() }
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() {
a := Arr{[5], [2.0], ['asdf']}
r := encode[Arr](a)
assert r['ints'] == Any([Any(5)])
assert r['floats'] == Any([Any(2.0)])
assert r['strs'] == Any([Any('asdf')])
assert unsafe { r['ints'] } == Any([Any(5)])
assert unsafe { r['floats'] } == Any([Any(2.0)])
assert unsafe { r['strs'] } == Any([Any('asdf')])
}

View File

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

View File

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

View File

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

View File

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

View File

@ -20,10 +20,10 @@ fn fn_mr_get_user() (string, int, []string, UserData) {
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)
if s.len == 0 {
return none
return error('error')
}
if tokens.len != 2 {
return error('error')
@ -51,7 +51,7 @@ fn test_multiple_ret() {
// none case
wrapper1 := fn () (string, string) {
res2_1, res2_2 := split_to_two('') or {
assert err.msg() == ''
assert err.msg() == 'error'
return 'replaced', 'val'
}
return res2_1, res2_2

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,6 +1,6 @@
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 {
Ok[S] {
return x.value

View File

@ -16,7 +16,7 @@ fn fill(mut s MyStruct[i64]) {
fn test_generics_call_with_reference_arg() {
mut s := MyStruct[i64]{
pos: 1
buffer: []&i64{len: 2}
buffer: unsafe { []&i64{len: 2} }
}
fill(mut s)
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]
println(arr)
ret := sample[int](arr, 5)?
ret := sample[int](arr, 5)!
println(ret)
assert ret == [32, 45, 57, 11, 37]

View File

@ -13,7 +13,7 @@ pub:
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 {
if r.name == name {
if name != '' {
@ -60,7 +60,7 @@ pub mut:
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 {
if r.name == name {
if name != '' {

View File

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

View File

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

View File

@ -1,24 +1,22 @@
fn do_a_thing(i int) ?int {
if i < 0 {
return error("can't be negative")
}
if i == 0 {
return none
}
return i
}
fn test_match_error_to_none() {
i := 0
if r := do_a_thing(i) {
println(r)
} else {
match err {
none {
assert true
}
else {
assert false
for i := -1; i < 1; i++ {
if r := do_a_thing(i) {
println(r)
} else {
match err {
none {
assert true
}
else {
assert false
}
}
}
}

View File

@ -12,9 +12,9 @@ enum Operator {
type Value = Operator | int
struct Expression {
left ?&Expression = none
left ?&Expression
val Value
right ?&Expression = none
right ?&Expression
}
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
// avoiding compiler warnings: module 'c (sub.foo.c)' is imported but never used
fn bar() {
_ = c.used_import
}

View File

@ -30,7 +30,7 @@ mut:
z f64
}
fn (mut s St) raise() ?f64 {
fn (mut s St) raise() !f64 {
return error('some error')
}
@ -52,8 +52,8 @@ fn test_nested_or_method_call() {
assert x.z == 2.25
}
fn (mut s St) aa_propagate() ? {
f := retf(s.raise()?)
fn (mut s St) aa_propagate() ! {
f := retf(s.raise()!)
s.z = 7.5
println(f)
}

View File

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

View File

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

View File

@ -2,7 +2,7 @@ struct Empty {
empty string
}
fn print_error() ?[]Empty {
fn print_error() ![]Empty {
mut test := []Empty{}
test << Empty{
empty: 'Test'

View File

@ -1,4 +1,4 @@
fn err_call(ok bool) ?int {
fn err_call(ok bool) !int {
if ok {
return 42
}

View File

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

View File

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

View File

@ -1,5 +1,5 @@
// TODO: remove this after the deprecation period for `?Type` representing both Result and Option passes.
fn opt_err_with_code(code int) ?string {
fn opt_err_with_code(code int) !string {
return error_with_code('hi', code)
}
@ -20,7 +20,7 @@ fn test_err_with_code() {
_ := v
}
fn opt_err() ?string {
fn opt_err() !string {
return error('hi')
}
@ -33,7 +33,7 @@ fn test_err() {
println(v) // suppress not used error
}
fn err_call(ok bool) ?int {
fn err_call(ok bool) !int {
if !ok {
return error('Not ok!')
}
@ -79,7 +79,7 @@ fn test_if_else_opt() {
}
}
fn for_opt_default() ?string {
fn for_opt_default() !string {
return error('awww')
}
@ -99,13 +99,13 @@ fn foo_str() ?string {
return 'something'
}
fn propagate_option(b bool) ?int {
a := err_call(b)?
fn propagate_option(b bool) !int {
a := err_call(b)!
return a
}
fn propagate_different_type(b bool) ?bool {
err_call(b)?
fn propagate_different_type(b bool) !bool {
err_call(b)!
return true
}
@ -137,7 +137,7 @@ fn or_return_val() int {
return a
}
fn or_return_error() ?int {
fn or_return_error() !int {
a := ret_none() or { return error('Nope') }
return a
}
@ -282,7 +282,7 @@ fn test_option_val_with_empty_or() {
}
fn test_option_void_return_types_of_anon_fn() {
f := fn (i int) ? {
f := fn (i int) ! {
if i == 0 {
return error('0')
}
@ -297,12 +297,12 @@ fn test_option_void_return_types_of_anon_fn() {
}
struct Foo {
f fn (int) ?
f fn (int) ! [required]
}
fn test_option_void_return_types_of_anon_fn_in_struct() {
foo := Foo{
f: fn (i int) ? {
f: fn (i int) ! {
if i == 0 {
return error('0')
}
@ -327,7 +327,7 @@ struct CC {
str string
}
fn option_sum_type(a int) ?AA {
fn option_sum_type(a int) !AA {
match a {
1 {
return BB{'Test'}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,22 +0,0 @@
struct MyError {
msg string
code int
}
fn (err MyError) msg() string {
return err.msg
}
fn (err MyError) code() int {
return err.code
}
fn foo() int|none|IError {
return MyError{}
}
fn test_string_option_none() {
x := foo()
println(x)
assert true
}

View File

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

View File

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

@ -1,11 +1,10 @@
fn test_typeof_on_simple_expressions() {
a := int(123)
assert typeof(int(42)) == 'int'
assert typeof(f64(3.14)) == 'f64'
assert typeof(int(2) + 2 * 10) == 'int'
assert typeof(f64(1.0) * 12.2) == 'f64'
// assert typeof(1.0 * f32(12.2)) == 'f32'
assert typeof(a) == 'int'
assert typeof(int(42)).name == 'int'
assert typeof(f64(3.14)).name == 'f64'
assert typeof(int(2) + 2 * 10).name == 'int'
assert typeof(f64(1.0) * 12.2).name == 'f64'
// assert typeof(1.0 * f32(12.2)).name == 'f32'
assert typeof(a).name == 'int'
// a2 := 123
// assert typeof(a2) == 'int_literal'
@ -18,8 +17,6 @@ fn test_typeof_on_simple_expressions() {
fn test_arrays() {
aint := []int{}
astring := []string{}
assert typeof(aint) == '[]int'
assert typeof(astring) == '[]string'
assert typeof(aint).name == '[]int'
assert typeof(astring).name == '[]string'
}
@ -38,12 +35,10 @@ struct FooBar {
}
fn test_typeof_on_structs() {
assert typeof(FooBar{}) == 'FooBar'
assert typeof(FooBar{}).name == 'FooBar'
astruct_static := [2]FooBar{}
astruct_dynamic := [FooBar{}, FooBar{}]
assert typeof(astruct_static) == '[2]FooBar'
assert typeof(astruct_static).name == '[2]FooBar'
assert typeof(astruct_dynamic) == '[]FooBar'
assert typeof(astruct_dynamic).name == '[]FooBar'
}
@ -64,9 +59,6 @@ fn test_typeof_on_sumtypes() {
c := MySumType(FooBar{
x: 43
})
assert typeof(a) == 'int'
assert typeof(b) == 'f32'
assert typeof(c) == 'FooBar'
assert a.type_name() == 'int'
assert b.type_name() == 'f32'
assert c.type_name() == 'FooBar'
@ -110,10 +102,10 @@ fn test_typeof_on_sumtypes_of_structs() {
b := fexpr(2)
c := fexpr(3)
d := ExprType(UnaryExpr{})
assert typeof(a) == 'UnaryExpr'
assert typeof(b) == 'BinExpr'
assert typeof(c) == 'BoolExpr'
assert typeof(d) == 'UnaryExpr'
assert typeof(a).name == 'ExprType'
assert typeof(b).name == 'ExprType'
assert typeof(c).name == 'ExprType'
assert typeof(d).name == 'ExprType'
assert a.type_name() == 'UnaryExpr'
assert b.type_name() == 'BinExpr'
assert c.type_name() == 'BoolExpr'
@ -136,10 +128,10 @@ fn myfn4() i8 {
}
fn test_typeof_on_fn() {
assert typeof(myfn) == 'fn (int) int'
assert typeof(myfn2) == 'fn ()'
assert typeof(myfn3) == 'fn (int, string) u8'
assert typeof(myfn4) == 'fn () i8'
assert typeof(myfn).name == 'fn (int) int'
assert typeof(myfn2).name == 'fn ()'
assert typeof(myfn3).name == 'fn (int, string) u8'
assert typeof(myfn4).name == 'fn () i8'
assert typeof(myfn).name == typeof(myfn)
assert typeof(&myfn).name == '&fn (int) int'
assert typeof(myfn2).name == typeof(myfn2)

View File

@ -19,14 +19,14 @@ fn test_envbang_script_runs() {
import os
println('hello')
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')
assert res.exit_code == 0
lines := res.output.split_into_lines()
assert lines[0] == 'hello'
assert lines[1].ends_with(", 'abc', '123', '-option']")
os.rm(rnd_vsh_script_path)?
os.rm(rnd_vsh_script_path)!
}
[noreturn]