tests: interface: minor fixes

pull/4739/head
Sandro Martini 2020-05-05 19:56:11 +02:00 committed by GitHub
parent fd0f8d06f8
commit 50351eded8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 1 deletions

View File

@ -26,6 +26,11 @@ fn (c Cat) name_detailed(pet_name string) string {
return '$pet_name the ${typeof(c)}, breed:${c.breed}' return '$pet_name the ${typeof(c)}, breed:${c.breed}'
} }
// utility function to convert to string, as a sample
fn (c Cat) str() string {
return 'Cat: $c.breed'
}
fn (d Dog) speak(s string) { fn (d Dog) speak(s string) {
assert s == 'Hi !' assert s == 'Hi !'
println('woof') println('woof')
@ -40,6 +45,9 @@ fn (d Dog) name_detailed(pet_name string) string {
return '$pet_name the ${typeof(d)}, breed:${d.breed}' return '$pet_name the ${typeof(d)}, breed:${d.breed}'
} }
// do not add to Dog the utility function 'str', as a sample
fn test_todo() { fn test_todo() {
if true {} if true {}
else {} else {}
@ -129,10 +137,18 @@ interface Animal {
speak(s string) speak(s string)
} }
// utility function to convert to string, as a sample
fn (a Animal) str() string {
return 'Animal: type:${typeof(a)}, name:' + a.name() + '.'
}
fn test_interface_array() { fn test_interface_array() {
println('Test on array of animals ...')
mut animals := []Animal{} mut animals := []Animal{}
animals = [ Cat{}, Dog{} ] animals = [ Cat{}, Dog{breed: 'Labrador Retriever'} ]
animals << Cat{} animals << Cat{}
assert true assert true
println('Animals array contains: ${animals.str()}') // explicit call to 'str' function
println('Animals array contains: ${animals}') // implicit call to 'str' function
assert animals.len == 3 assert animals.len == 3
} }