mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
tests: add another function to the Animal interface
This commit is contained in:
parent
36760f0982
commit
dd2a1455dc
@ -8,16 +8,24 @@ struct Cat {
|
||||
}
|
||||
|
||||
fn (mut c Cat) name() string {
|
||||
assert c.breed == 'Persian'
|
||||
if c.breed != '' {
|
||||
assert c.breed == 'Persian'
|
||||
}
|
||||
return 'Cat'
|
||||
}
|
||||
|
||||
fn (c &Cat) speak(s string) {
|
||||
assert c.breed == 'Persian'
|
||||
if c.breed != '' {
|
||||
assert c.breed == 'Persian'
|
||||
}
|
||||
assert s == 'Hi !'
|
||||
println('meow')
|
||||
}
|
||||
|
||||
fn (c Cat) name_detailed(pet_name string) string {
|
||||
return '$pet_name the ${typeof(c)}, breed:${c.breed}'
|
||||
}
|
||||
|
||||
fn (d Dog) speak(s string) {
|
||||
assert s == 'Hi !'
|
||||
println('woof')
|
||||
@ -28,21 +36,24 @@ fn (d Dog) name() string {
|
||||
return 'Dog'
|
||||
}
|
||||
|
||||
fn test_todo() {
|
||||
if true {}
|
||||
//
|
||||
else{}
|
||||
fn (d Dog) name_detailed(pet_name string) string {
|
||||
return '$pet_name the ${typeof(d)}, breed:${d.breed}'
|
||||
}
|
||||
|
||||
fn perform_speak(s Animal) {
|
||||
s.speak('Hi !')
|
||||
fn test_todo() {
|
||||
if true {}
|
||||
else {}
|
||||
}
|
||||
|
||||
fn perform_speak(a Animal) {
|
||||
a.speak('Hi !')
|
||||
assert true
|
||||
name := s.name()
|
||||
name := a.name()
|
||||
assert name == 'Dog' || name == 'Cat'
|
||||
//if s is Dog {
|
||||
//if a is Dog {
|
||||
//assert name == 'Dog'
|
||||
//}
|
||||
println(s.name())
|
||||
println(a.name())
|
||||
}
|
||||
|
||||
fn test_perform_speak() {
|
||||
@ -59,6 +70,28 @@ fn test_perform_speak() {
|
||||
*/
|
||||
}
|
||||
|
||||
fn perform_name_detailed(a Animal) {
|
||||
name_full := a.name_detailed('MyPet')
|
||||
println(name_full)
|
||||
assert name_full.starts_with('MyPet the Dog') || name_full.starts_with('MyPet the Cat')
|
||||
}
|
||||
|
||||
fn test_perform_name_detailed() {
|
||||
dog := Dog{breed: 'Labrador Retriever'}
|
||||
println('Test on Dog: $dog ...')
|
||||
perform_name_detailed(dog)
|
||||
|
||||
cat := Cat{}
|
||||
println('Test on Cat: $cat ...')
|
||||
perform_speak(cat)
|
||||
|
||||
println('Test on another Cat: ...')
|
||||
perform_speak(Cat{breed: 'Persian'})
|
||||
|
||||
println('Test (dummy/empty) on array of animals ...')
|
||||
handle_animals([dog, cat])
|
||||
}
|
||||
|
||||
fn handle_animals(a []Animal) {}
|
||||
|
||||
interface Register {
|
||||
@ -92,6 +125,7 @@ struct Foo {
|
||||
|
||||
interface Animal {
|
||||
name() string
|
||||
name_detailed(pet_name string) string
|
||||
speak(s string)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user