mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
compiler: fix generation of default .str() methods in interpolation
This commit is contained in:
@ -1,3 +1,3 @@
|
||||
{
|
||||
MyStruct {
|
||||
s: 6
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
// This file tests whether v can generate default string methods for both:
|
||||
// a) an array of custom structs,
|
||||
// b) also for the custom struct itself (when the .str() for it is missing).
|
||||
//
|
||||
// NB: this is very simillar to string_interpolation_struct_test.v
|
||||
// but they should NOT be merged into 1 file. If you merge it with
|
||||
// string_interpolation_struct_test.v, which tests whether the compiler
|
||||
// can generate the default method for a struct, then the b) case of
|
||||
// this test will be done by *that* test, and so the testing will
|
||||
// be incomplete.
|
||||
|
||||
struct Man {
|
||||
name string
|
||||
age int
|
||||
interests []string
|
||||
}
|
||||
|
||||
fn test_default_struct_array_of_structs_interpolation(){
|
||||
people := [
|
||||
Man{'Superman', 30, ['flying','fighting evil','being nice']},
|
||||
Man{'Bilbo Baggins', 111, ['exploring', 'hiding']},
|
||||
]
|
||||
s := '$people' // the compiler should generate code for both a) and b)
|
||||
assert s.contains('Man {')
|
||||
assert s.contains('name: Superman')
|
||||
assert s.contains('age: 30')
|
||||
assert s.contains('"being nice"')
|
||||
assert s.contains('}, Man {')
|
||||
assert s.contains('name: Bilbo Baggins')
|
||||
assert s.contains('age: 111')
|
||||
assert s.contains('interests: ["exploring", "hiding"]')
|
||||
assert s.contains('}]')
|
||||
//println(s)
|
||||
}
|
21
vlib/compiler/tests/string_interpolation_struct_test.v
Normal file
21
vlib/compiler/tests/string_interpolation_struct_test.v
Normal file
@ -0,0 +1,21 @@
|
||||
// This file tests whether V can generate a convenience default .str() method
|
||||
// for a custom struct, when the developer has not defined one himself.
|
||||
// The .str() methods are used for string interpolation and for println() calls.
|
||||
|
||||
struct Man {
|
||||
name string
|
||||
age int
|
||||
interests []string
|
||||
}
|
||||
|
||||
fn test_default_struct_string_interpolation(){
|
||||
superman := Man{'Superman', 30, ['flying','fighting evil','being nice']}
|
||||
s := '$superman'
|
||||
assert s.contains('Man {')
|
||||
assert s.contains('name: Superman')
|
||||
assert s.contains('age: 30')
|
||||
assert s.contains('interests: [')
|
||||
assert s.contains('"being nice"')
|
||||
assert s.contains('}')
|
||||
//println(s)
|
||||
}
|
34
vlib/compiler/tests/string_interpolation_variadic_test.v
Normal file
34
vlib/compiler/tests/string_interpolation_variadic_test.v
Normal file
@ -0,0 +1,34 @@
|
||||
// This file tests whether V can generate a convenience default .str() method
|
||||
// for var args of a custom type, when the developer has NOT defined one.
|
||||
// Although similar to string_interpolation_struct_test.v, they should not be
|
||||
// merged.
|
||||
|
||||
struct Man {
|
||||
name string
|
||||
age int
|
||||
interests []string
|
||||
}
|
||||
|
||||
fn my_variadic_function(x ...Man) string {
|
||||
return '$x' // this interpolation should generate .str() methods for Man
|
||||
}
|
||||
|
||||
fn test_vargs_string_interpolation(){
|
||||
man := Man{'Me', 38, ['programming', 'reading', 'hiking']}
|
||||
superman := Man{'Superman', 30, ['flying','fighting evil','being nice']}
|
||||
|
||||
results := my_variadic_function(superman, man)
|
||||
|
||||
assert results.contains('Man {')
|
||||
//
|
||||
assert results.contains('name: Superman')
|
||||
assert results.contains('age: 30')
|
||||
assert results.contains('}, Man {')
|
||||
//
|
||||
assert results.contains('interests: ["programming"')
|
||||
assert results.contains('name: Me')
|
||||
//
|
||||
assert results.contains('}]')
|
||||
//
|
||||
println(results)
|
||||
}
|
Reference in New Issue
Block a user