mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
v2: minor fixes
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
struct AA {
|
||||
mut:
|
||||
val int
|
||||
val int
|
||||
nums []int
|
||||
}
|
||||
|
||||
@ -11,32 +11,31 @@ mut:
|
||||
|
||||
struct CC {
|
||||
mut:
|
||||
b BB
|
||||
b BB
|
||||
nums []int
|
||||
aarr []AA
|
||||
num int
|
||||
num int
|
||||
}
|
||||
|
||||
struct User {
|
||||
name string
|
||||
age int
|
||||
age int
|
||||
}
|
||||
|
||||
struct Foo {
|
||||
@type string
|
||||
}
|
||||
|
||||
struct Empty {
|
||||
}
|
||||
struct Empty {}
|
||||
|
||||
//We need to make sure that this compiles with all the reserved names.
|
||||
// We need to make sure that this compiles with all the reserved names.
|
||||
struct ReservedKeywords {
|
||||
delete int
|
||||
exit int
|
||||
unix int
|
||||
error int
|
||||
malloc int
|
||||
calloc int
|
||||
calloc int
|
||||
free int
|
||||
panic int
|
||||
auto int
|
||||
@ -66,6 +65,7 @@ fn test_empty_struct() {
|
||||
|
||||
fn test_struct_levels() {
|
||||
mut c := CC{}
|
||||
println(c.nums.len)
|
||||
assert c.nums.len == 0
|
||||
c.nums << 3
|
||||
assert c.nums.len == 1
|
||||
@ -80,41 +80,49 @@ fn test_struct_levels() {
|
||||
assert c.b.a.nums.len == 2
|
||||
assert c.b.a.nums[0] == 0
|
||||
assert c.b.a.nums[1] == 2
|
||||
c.b.a.nums [0] = 7
|
||||
c.b.a.nums[0] = 7
|
||||
assert c.b.a.nums[0] == 7
|
||||
c.aarr << AA{val:8}
|
||||
c.aarr << AA{
|
||||
val: 8
|
||||
}
|
||||
assert c.aarr.len == 1
|
||||
assert c.aarr[0].val == 8
|
||||
c.num = 20
|
||||
assert c.num == 20
|
||||
c.aarr[0].val = 10
|
||||
c.aarr[0].val = 10
|
||||
assert c.aarr[0].val == 10
|
||||
}
|
||||
|
||||
fn test_struct_str() {
|
||||
u := User{'Bob', 30}
|
||||
println(u) // make sure the struct is printable
|
||||
u := User{
|
||||
'Bob',30}
|
||||
println(u) // make sure the struct is printable
|
||||
// assert u.str() == '{name:"Bob", age:30}' // TODO
|
||||
}
|
||||
|
||||
fn test_at() {
|
||||
foo := Foo{ @type: 'test' }
|
||||
foo := Foo{
|
||||
@type: 'test'
|
||||
}
|
||||
println(foo.@type)
|
||||
}
|
||||
|
||||
fn test_reserved_keywords() {
|
||||
//Make sure we can initialize them correctly using full syntax.
|
||||
rk_holder := ReservedKeywords{0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3}
|
||||
//Test a few as it'll take too long to test all. If it's initialized
|
||||
//correctly, other fields are also probably valid.
|
||||
// Make sure we can initialize them correctly using full syntax.
|
||||
rk_holder := ReservedKeywords{
|
||||
0,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3}
|
||||
// Test a few as it'll take too long to test all. If it's initialized
|
||||
// correctly, other fields are also probably valid.
|
||||
assert rk_holder.unix == 5
|
||||
assert rk_holder.while == 3
|
||||
|
||||
rk_holder2 := ReservedKeywords{inline: 9, volatile: 11}
|
||||
//Make sure partial initialization works too.
|
||||
rk_holder2 := ReservedKeywords{
|
||||
inline: 9
|
||||
volatile: 11
|
||||
}
|
||||
// Make sure partial initialization works too.
|
||||
assert rk_holder2.inline == 9
|
||||
assert rk_holder2.volatile == 11
|
||||
assert rk_holder2.while == 0 //Zero value as not specified.
|
||||
assert rk_holder2.while == 0 // Zero value as not specified.
|
||||
}
|
||||
|
||||
struct User2 {
|
||||
@ -128,32 +136,43 @@ fn test_mutable_fields() {
|
||||
assert u.name == 'Peter'
|
||||
}
|
||||
|
||||
|
||||
struct Def {
|
||||
a int
|
||||
b int = 7
|
||||
b int=7
|
||||
}
|
||||
|
||||
fn test_default_vals() {
|
||||
d := Def{}
|
||||
assert d.a == 0
|
||||
assert d.b == 7
|
||||
d2 := Def{10, 20}
|
||||
d2 := Def{
|
||||
10,20}
|
||||
assert d2.a == 10
|
||||
assert d2.b == 20
|
||||
}
|
||||
|
||||
fn test_assoc_with_vars() {
|
||||
def2 := Def { a: 12 }
|
||||
merged := { def2 | a: 42 }
|
||||
def2 := Def{
|
||||
a: 12
|
||||
}
|
||||
merged := {
|
||||
def2 |
|
||||
a:42
|
||||
}
|
||||
assert merged.a == 42
|
||||
assert merged.b == 7
|
||||
}
|
||||
|
||||
const (
|
||||
const_def = Def { a: 100 }
|
||||
const_def = Def{
|
||||
a: 100
|
||||
}
|
||||
)
|
||||
|
||||
fn test_assoc_with_constants() {
|
||||
println(1)
|
||||
/*
|
||||
QTODO
|
||||
merged := { const_def | a: 42 }
|
||||
assert merged.a == 42
|
||||
assert merged.b == 7
|
||||
@ -161,23 +180,26 @@ fn test_assoc_with_constants() {
|
||||
again := { const_def | b: 22 }
|
||||
assert again.a == 100
|
||||
assert again.b == 22
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
struct AttrTest{
|
||||
a int // private immutable (default)
|
||||
struct AttrTest {
|
||||
a int // private immutable (default)
|
||||
mut:
|
||||
b int // private mutable
|
||||
c int // (you can list multiple fields with the same access modifier)
|
||||
b int // private mutable
|
||||
c int // (you can list multiple fields with the same access modifier)
|
||||
pub:
|
||||
d int // public immmutable (readonly)
|
||||
d int // public immmutable (readonly)
|
||||
pub mut:
|
||||
e int // public, but mutable only in parent module
|
||||
e int // public, but mutable only in parent module
|
||||
__global:
|
||||
f int // public and mutable both inside and outside parent module
|
||||
f int // public and mutable both inside and outside parent module
|
||||
}
|
||||
|
||||
fn fooo(){
|
||||
a:=AttrTest{1,2,3,4,5,6}
|
||||
fn fooo() {
|
||||
a := AttrTest{
|
||||
1,2,3,4,5,6}
|
||||
}
|
||||
|
||||
/*
|
||||
@ -205,21 +227,24 @@ fn test_fixed_field() {
|
||||
|
||||
|
||||
struct Config {
|
||||
n int
|
||||
def int = 10
|
||||
n int
|
||||
def int=10
|
||||
}
|
||||
|
||||
fn foo_config(c Config) {
|
||||
}
|
||||
fn foo_config(c Config) {}
|
||||
|
||||
fn foo2(u User) {
|
||||
|
||||
}
|
||||
fn foo2(u User) {}
|
||||
|
||||
fn test_config() {
|
||||
foo_config({n: 10, def: 20})
|
||||
|
||||
/*
|
||||
foo_config({
|
||||
n: 10
|
||||
def: 20
|
||||
})
|
||||
foo_config({})
|
||||
foo2({name:'Peter'})
|
||||
foo2({
|
||||
name: 'Peter'
|
||||
})
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user