tests: add another function to the Animal interface
parent
36760f0982
commit
dd2a1455dc
|
@ -8,16 +8,24 @@ struct Cat {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut c Cat) name() string {
|
fn (mut c Cat) name() string {
|
||||||
assert c.breed == 'Persian'
|
if c.breed != '' {
|
||||||
|
assert c.breed == 'Persian'
|
||||||
|
}
|
||||||
return 'Cat'
|
return 'Cat'
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (c &Cat) speak(s string) {
|
fn (c &Cat) speak(s string) {
|
||||||
assert c.breed == 'Persian'
|
if c.breed != '' {
|
||||||
|
assert c.breed == 'Persian'
|
||||||
|
}
|
||||||
assert s == 'Hi !'
|
assert s == 'Hi !'
|
||||||
println('meow')
|
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) {
|
fn (d Dog) speak(s string) {
|
||||||
assert s == 'Hi !'
|
assert s == 'Hi !'
|
||||||
println('woof')
|
println('woof')
|
||||||
|
@ -28,21 +36,24 @@ fn (d Dog) name() string {
|
||||||
return 'Dog'
|
return 'Dog'
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_todo() {
|
fn (d Dog) name_detailed(pet_name string) string {
|
||||||
if true {}
|
return '$pet_name the ${typeof(d)}, breed:${d.breed}'
|
||||||
//
|
|
||||||
else{}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn perform_speak(s Animal) {
|
fn test_todo() {
|
||||||
s.speak('Hi !')
|
if true {}
|
||||||
|
else {}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn perform_speak(a Animal) {
|
||||||
|
a.speak('Hi !')
|
||||||
assert true
|
assert true
|
||||||
name := s.name()
|
name := a.name()
|
||||||
assert name == 'Dog' || name == 'Cat'
|
assert name == 'Dog' || name == 'Cat'
|
||||||
//if s is Dog {
|
//if a is Dog {
|
||||||
//assert name == 'Dog'
|
//assert name == 'Dog'
|
||||||
//}
|
//}
|
||||||
println(s.name())
|
println(a.name())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_perform_speak() {
|
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) {}
|
fn handle_animals(a []Animal) {}
|
||||||
|
|
||||||
interface Register {
|
interface Register {
|
||||||
|
@ -92,6 +125,7 @@ struct Foo {
|
||||||
|
|
||||||
interface Animal {
|
interface Animal {
|
||||||
name() string
|
name() string
|
||||||
|
name_detailed(pet_name string) string
|
||||||
speak(s string)
|
speak(s string)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue