-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
tests: add regression test for variadic interface chain forwarding #26773
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vincenzopalazzo
wants to merge
4
commits into
vlang:master
Choose a base branch
from
vincenzopalazzo:claude/agitated-allen
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+172
−1
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5e24d29
tests: add regression test for variadic interface chain forwarding (#…
vincenzopalazzo 899fc7d
fix: assert []u8 contents and fix vfmt formatting
vincenzopalazzo f4b032a
fix: heap-allocate interface values in array/variadic contexts
vincenzopalazzo 01a595b
fix: restrict inside_cast_in_heap to interface types only
vincenzopalazzo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
138 changes: 138 additions & 0 deletions
138
vlib/v/tests/fns/variadic_interface_chain_forwarding_test.v
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| import arrays | ||
|
|
||
| // Regression test for https://github.com/vlang/v/issues/26760 | ||
| // Interface values passed through variadic parameter chains must preserve | ||
| // their type tags and data pointers. | ||
|
|
||
| interface Value {} | ||
|
|
||
| fn process_params(params []Value) string { | ||
| mut result := []string{} | ||
| for i := 0; i < params.len; i++ { | ||
| param := params[i] | ||
| match param { | ||
| string { | ||
| result << 'string:${param}' | ||
| } | ||
| i32 { | ||
| result << 'i32:${param}' | ||
| } | ||
| []u8 { | ||
| result << '[]u8:${param.len}' | ||
| } | ||
| else { | ||
| result << 'unknown' | ||
| } | ||
| } | ||
| } | ||
| return result.join(', ') | ||
| } | ||
|
|
||
| struct Statement { | ||
| mut: | ||
| handle int | ||
| } | ||
|
|
||
| struct Transaction { | ||
| mut: | ||
| handle int | ||
| } | ||
|
|
||
| fn (mut stmt Statement) execute(params ...Value) !string { | ||
| return process_params(params) | ||
| } | ||
|
|
||
| fn (stmt Statement) close() ! {} | ||
|
|
||
| fn (mut t Transaction) prepare(query string) !Statement { | ||
| return Statement{ handle: 1 } | ||
| } | ||
|
|
||
| fn (mut t Transaction) execute(query string, params ...Value) !string { | ||
| mut stmt := t.prepare(query)! | ||
| result := stmt.execute(...params)! | ||
| stmt.close()! | ||
| return result | ||
| } | ||
|
|
||
| struct ListParams { | ||
| email ?string | ||
| role ?string | ||
| offset i32 | ||
| fetch i32 | ||
| } | ||
|
|
||
| fn get_conditions(p ListParams) (string, []Value) { | ||
| mut conditions := []string{} | ||
| mut params := []Value{} | ||
|
|
||
| if email := p.email { | ||
| conditions = arrays.concat(conditions, 'email = ?') | ||
| params = arrays.concat(params, email) | ||
| } | ||
|
|
||
| if role := p.role { | ||
| conditions = arrays.concat(conditions, 'role = ?') | ||
| params = arrays.concat(params, role) | ||
| } | ||
|
|
||
| return conditions.join(' AND '), params | ||
| } | ||
|
|
||
| fn test_variadic_interface_forwarding_with_match() ! { | ||
| p := ListParams{ | ||
| email: 'info@peony.com' | ||
| offset: 0 | ||
| fetch: 10 | ||
| } | ||
|
|
||
| conditions, mut params := get_conditions(p) | ||
| params = arrays.concat(params, p.offset, p.fetch) | ||
|
|
||
| mut tx := Transaction{ handle: 1 } | ||
| result := tx.execute('SELECT * FROM users WHERE ${conditions}', ...params)! | ||
| assert result == 'string:info@peony.com, i32:0, i32:10', 'got: ${result}' | ||
| } | ||
|
|
||
| fn test_variadic_interface_forwarding_single_param() ! { | ||
| p := ListParams{ | ||
| email: 'info@peony.com' | ||
| offset: 0 | ||
| fetch: 10 | ||
| } | ||
|
|
||
| conditions, params := get_conditions(p) | ||
|
|
||
| mut tx := Transaction{ handle: 1 } | ||
| result := tx.execute('SELECT COUNT(*) FROM users WHERE ${conditions}', ...params)! | ||
| assert result == 'string:info@peony.com', 'got: ${result}' | ||
| } | ||
|
|
||
| fn test_variadic_interface_forwarding_with_byte_arrays() ! { | ||
| user_id_bin := [u8(1), 2, 3, 4] | ||
| email := 'info@peony.com' | ||
|
|
||
| mut params := [Value(user_id_bin), email] | ||
|
|
||
| mut tx := Transaction{ handle: 1 } | ||
| result := tx.execute('INSERT INTO users', ...params)! | ||
| assert result == '[]u8:4, string:info@peony.com', 'got: ${result}' | ||
| } | ||
|
|
||
| fn test_variadic_interface_forwarding_repeated() ! { | ||
| for _ in 0 .. 100 { | ||
| p := ListParams{ | ||
| email: 'info@peony.com' | ||
| role: 'admin' | ||
| offset: 5 | ||
| fetch: 20 | ||
| } | ||
|
|
||
| conditions, mut params := get_conditions(p) | ||
| params = arrays.concat(params, p.offset, p.fetch) | ||
|
|
||
| mut tx := Transaction{ handle: 1 } | ||
| result := tx.execute('SELECT * FROM users WHERE ${conditions}', ...params)! | ||
| assert result == 'string:info@peony.com, string:admin, i32:5, i32:20', 'got: ${result}' | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.