tests: test more edge cases for interfaces (#5292)
							parent
							
								
									cf274f262c
								
							
						
					
					
						commit
						85633fe546
					
				|  | @ -58,13 +58,50 @@ fn (mut d Dog) set_breed(new string) { | |||
| } | ||||
| 
 | ||||
| // do not add to Dog the utility function 'str', so the default one will be used, as a sample
 | ||||
| fn test_todo() { | ||||
| 	if true { | ||||
| 	} else { | ||||
| 	} | ||||
| 
 | ||||
| struct Bird { | ||||
| mut: | ||||
| 	breed string | ||||
| } | ||||
| 
 | ||||
| fn (b Bird) speak(s string) { | ||||
| 	println('tweet') | ||||
| } | ||||
| 
 | ||||
| fn (b Bird) name() string { | ||||
| 	return b.breed | ||||
| } | ||||
| 
 | ||||
| fn (b Bird) name_detailed(pet_name string) string { | ||||
| 	return '$pet_name the ${typeof(b).name}, breed:$b.breed' | ||||
| } | ||||
| 
 | ||||
| fn (mut b Bird) set_breed(new string) { | ||||
| 	println('canary') | ||||
| 	b.breed = new | ||||
| } | ||||
| 
 | ||||
| // do not add to Bird the utility function 'str', so the default one will be used, as a sample
 | ||||
| 
 | ||||
| fn is_dog_or_cat(a Animal) bool { | ||||
| 	is_dog := a is Dog | ||||
| 	is_cat := a is Cat | ||||
| 	println('Animal is Dog or Cat: is a Dog: $is_dog, is a Cat: $is_cat') | ||||
| 	// return is_dog || is_cat
 | ||||
| 	// shorter syntax
 | ||||
| 	is_dog_or_cat := if (a is Dog) || (a is Cat) { true } else { false } | ||||
| 	println('Animal is Dog or Cat: $is_dog_or_cat') | ||||
| 	return is_dog_or_cat | ||||
| } | ||||
| 
 | ||||
| fn is_dog_or_cat_or_bird(a Animal) bool { | ||||
| 	ret := a is Dog || a is Cat || a is Bird | ||||
| 	println('Animal is Dog or Cat or Bird: $ret') | ||||
| 	return ret | ||||
| } | ||||
| 
 | ||||
| fn perform_speak(a Animal) { | ||||
| 	println('---- ${@FN}, given Animal: $a ----') | ||||
| 	a.speak('Hi !') | ||||
| 	assert true | ||||
| 	name := a.name() | ||||
|  | @ -77,9 +114,11 @@ fn perform_speak(a Animal) { | |||
| 	println(a.name()) | ||||
| 	println('Got animal of type: ${typeof(a).name}') // TODO: get implementation type (if possible)
 | ||||
| 	assert a is Dog || a is Cat | ||||
| 	assert is_dog_or_cat(a) | ||||
| } | ||||
| 
 | ||||
| fn perform_speak_on_ptr(a &Animal) { | ||||
| 	println('---- ${@FN}, given &Animal: $a ----') | ||||
| 	a.speak('Hi !') | ||||
| 	assert true | ||||
| 	name := a.name() | ||||
|  | @ -90,9 +129,11 @@ fn perform_speak_on_ptr(a &Animal) { | |||
| 	println(a.name()) | ||||
| 	println('Got animal of type: ${typeof(a).name}') // TODO: get implementation type (if possible)
 | ||||
| 	assert a is Dog || a is Cat | ||||
| 	assert is_dog_or_cat(a) | ||||
| } | ||||
| 
 | ||||
| fn test_perform_speak() { | ||||
| 	println('---- ${@FN} ----') | ||||
| 	dog := Dog{ | ||||
| 		breed: 'Labrador Retriever' | ||||
| 	} | ||||
|  | @ -112,11 +153,6 @@ fn test_perform_speak() { | |||
| 	}) | ||||
| 	perform_speak_on_ptr(new_cat('Persian')) | ||||
| 	handle_animals([dog, cat]) | ||||
| 	/* | ||||
| 	f := Foo { | ||||
| 		speaker: dog | ||||
| 	} | ||||
| 	*/ | ||||
| } | ||||
| 
 | ||||
| fn change_animal_breed(mut a Animal, new string) { | ||||
|  | @ -124,10 +160,10 @@ fn change_animal_breed(mut a Animal, new string) { | |||
| } | ||||
| 
 | ||||
| fn test_interface_ptr_modification() { | ||||
| 	println('---- ${@FN} ----') | ||||
| 	mut cat := Cat{ | ||||
| 		breed: 'Persian' | ||||
| 	} | ||||
| 	// TODO Should fail and require `mut cat`
 | ||||
| 	change_animal_breed(mut cat, 'Siamese') | ||||
| 	assert cat.breed == 'Siamese' | ||||
| } | ||||
|  | @ -139,6 +175,7 @@ fn perform_name_detailed(a Animal) { | |||
| } | ||||
| 
 | ||||
| fn test_perform_name_detailed() { | ||||
| 	println('---- ${@FN} ----') | ||||
| 	dog := Dog{ | ||||
| 		breed: 'Labrador Retriever' | ||||
| 	} | ||||
|  | @ -187,6 +224,7 @@ fn handle_reg(r Register) { | |||
| } | ||||
| 
 | ||||
| fn test_register() { | ||||
| 	println('---- ${@FN} ----') | ||||
| 	f := RegTest{} | ||||
| 	f.register() | ||||
| 	handle_reg(f) | ||||
|  | @ -256,6 +294,7 @@ mut: | |||
| } | ||||
| 
 | ||||
| fn test_interface_array() { | ||||
| 	println('---- ${@FN} ----') | ||||
| 	println('Test on array of animals ...') | ||||
| 	mut animals := []Animal{} | ||||
| 	animals = [Cat{}, Dog{ | ||||
|  | @ -264,13 +303,15 @@ fn test_interface_array() { | |||
| 	assert true | ||||
| 	animals << Cat{} | ||||
| 	assert true | ||||
| 	// TODO .str() from the real types should be called
 | ||||
| 	animals << Bird{} | ||||
| 	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 | ||||
| 	println('Animals array contains: $animals') // implicit call to 'str' function
 | ||||
| 	assert animals.len == 4 | ||||
| } | ||||
| 
 | ||||
| fn test_interface_ptr_array() { | ||||
| 	println('---- ${@FN} ----') | ||||
| 	mut animals := []&Animal{} | ||||
| 	animals = [Cat{}, Dog{ | ||||
| 		breed: 'Labrador Retriever' | ||||
|  | @ -278,15 +319,20 @@ fn test_interface_ptr_array() { | |||
| 	assert true | ||||
| 	animals << Cat{} | ||||
| 	assert true | ||||
| 	assert animals.len == 3 | ||||
| 	animals << Bird{} | ||||
| 	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 == 4 | ||||
| } | ||||
| 
 | ||||
| fn test_is() { | ||||
| 	println('---- ${@FN} ----') | ||||
| 	dog := Dog{} | ||||
| 	assert foo2(dog) == 1 | ||||
| 	assert is_dog_int(dog) == 1 | ||||
| } | ||||
| 
 | ||||
| fn foo2(a Animal) int { | ||||
| fn is_dog_int(a Animal) int { | ||||
| 	if a is Dog { | ||||
| 		return 1 | ||||
| 	} else { | ||||
|  | @ -294,6 +340,36 @@ fn foo2(a Animal) int { | |||
| 	} | ||||
| } | ||||
| 
 | ||||
| fn test_is_bool() { | ||||
| 	println('---- ${@FN} ----') | ||||
| 	dog := Dog{} | ||||
| 	assert is_dog(dog) == true | ||||
| 	assert is_dog_or_cat(dog) | ||||
| 	cat := Cat{} | ||||
| 	assert is_dog(cat) == false | ||||
| 	assert is_dog_or_cat(cat) | ||||
| 	bird := Bird{} | ||||
| 	assert is_dog(bird) == false | ||||
| 	assert !is_dog_or_cat(bird) | ||||
| 	assert is_dog_or_cat_or_bird(bird) | ||||
| } | ||||
| 
 | ||||
| fn is_dog(a Animal) bool { | ||||
| 	println("Got animal: '$a'") // implicit call to 'str' function of implementations
 | ||||
| 	println('with type: ${typeof(a).name}') // get implementation type (if possible)
 | ||||
| 
 | ||||
| 	// sample (additional checks) here
 | ||||
| 	is_dog_or_cat := if (a is Dog) || (a is Cat) { true } else { false } | ||||
| 	println('Animal is Dog or Cat: $is_dog_or_cat') | ||||
| 
 | ||||
| 	// use/test even another syntax
 | ||||
| 	if a is Dog { | ||||
| 		return true | ||||
| 	} else { | ||||
| 		return false | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| fn new_animal() Animal { | ||||
| 	dog := Dog{} | ||||
| 	return dog | ||||
|  | @ -309,7 +385,7 @@ fn animal_match(a Animal) { | |||
| 	match a { | ||||
| 		Dog { println('(dog)') } | ||||
| 		Cat { println('(cat)') } | ||||
| 		else {} | ||||
| 		else { println('(other)' } | ||||
| 	} | ||||
| } | ||||
| */ | ||||
|  | @ -354,7 +430,8 @@ fn get_animal_name(mut a Animal2) string { | |||
| 	return a.get_name() | ||||
| } | ||||
| 
 | ||||
| fn main() { | ||||
| fn test_aa() { | ||||
| 	println('---- ${@FN} ----') | ||||
| 	mut aa := AA{} | ||||
| 	mut ii := II(aa) | ||||
| 	assert ii.my_field == 0 | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue