-
Notifications
You must be signed in to change notification settings - Fork 379
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Description closes #2707 The `GetLength()`, `GetCapacity()` method in `TypedValue` has been updated to handle pointer type. <details><summary>Contributors' checklist...</summary> - [x] Added new tests, or not needed, or not feasible - [ ] Provided an example (e.g. screenshot) to aid review or the PR is self-explanatory - [ ] Updated the official documentation or not needed - [ ] No breaking changes were made, or a `BREAKING CHANGE: xxx` message was included in the description - [x] Added references to related issues and PRs - [ ] Provided any useful hints for running manual tests - [ ] Added new benchmarks to [generated graphs](https://gnoland.github.io/benchmarks), if any. More info [here](https://github.com/gnolang/gno/blob/master/.benchmarks/README.md). </details> --------- Co-authored-by: Morgan Bazalgette <[email protected]>
- Loading branch information
Showing
21 changed files
with
313 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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,70 @@ | ||
package gnolang | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
type mockTypedValueStruct struct { | ||
field int | ||
} | ||
|
||
func (m *mockTypedValueStruct) assertValue() {} | ||
|
||
func (m *mockTypedValueStruct) String() string { | ||
return fmt.Sprintf("MockTypedValueStruct(%d)", m.field) | ||
} | ||
|
||
func TestGetLengthPanic(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
tv TypedValue | ||
expected string | ||
}{ | ||
{ | ||
name: "NonArrayPointer", | ||
tv: TypedValue{ | ||
T: &PointerType{Elt: &StructType{}}, | ||
V: PointerValue{ | ||
TV: &TypedValue{ | ||
T: &StructType{}, | ||
V: &mockTypedValueStruct{field: 42}, | ||
}, | ||
}, | ||
}, | ||
expected: "unexpected type for len(): *struct{}", | ||
}, | ||
{ | ||
name: "UnexpectedType", | ||
tv: TypedValue{ | ||
T: &StructType{}, | ||
V: &mockTypedValueStruct{field: 42}, | ||
}, | ||
expected: "unexpected type for len(): struct{}", | ||
}, | ||
{ | ||
name: "UnexpectedPointerType", | ||
tv: TypedValue{ | ||
T: &PointerType{Elt: &StructType{}}, | ||
V: nil, | ||
}, | ||
expected: "unexpected type for len(): *struct{}", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
defer func() { | ||
if r := recover(); r == nil { | ||
t.Errorf("the code did not panic") | ||
} else { | ||
if r != tt.expected { | ||
t.Errorf("expected panic message to be %q, got %q", tt.expected, r) | ||
} | ||
} | ||
}() | ||
|
||
tt.tv.GetLength() | ||
}) | ||
} | ||
} |
This file contains 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,10 @@ | ||
package main | ||
|
||
func main() { | ||
exp := [...]string{"HELLO"} | ||
x := cap(&exp) | ||
println(x) | ||
} | ||
|
||
// Output: | ||
// 1 |
This file contains 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,8 @@ | ||
package main | ||
|
||
func main() { | ||
println("cap", cap(struct{ A int }{})) | ||
} | ||
|
||
// Error: | ||
// unexpected type for cap(): struct{A int} |
This file contains 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,9 @@ | ||
package main | ||
|
||
func main() { | ||
exp := [...]int{1, 2, 3, 4, 5} | ||
println(cap(exp)) | ||
} | ||
|
||
// Output: | ||
// 5 |
This file contains 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,9 @@ | ||
package main | ||
|
||
func main() { | ||
slice := make([]int, 3, 5) | ||
println(cap(slice)) | ||
} | ||
|
||
// Output: | ||
// 5 |
This file contains 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,9 @@ | ||
package main | ||
|
||
func main() { | ||
var slice []int | ||
println(cap(slice)) | ||
} | ||
|
||
// Output: | ||
// 0 |
This file contains 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,12 @@ | ||
package main | ||
|
||
func main() { | ||
printCap(nil) | ||
} | ||
|
||
func printCap(arr *[2]int) { | ||
println(cap(arr)) | ||
} | ||
|
||
// Output: | ||
// 2 |
This file contains 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,31 @@ | ||
package main | ||
|
||
func main() { | ||
var arr [5]int | ||
var nilArr *[5]int | ||
var nilSlice []int | ||
var nilArr2 *[8]struct{ A, B int } | ||
var nilMatrix *[2][3]int | ||
|
||
println("cap(arr): ", cap(arr)) | ||
println("cap(&arr): ", cap(&arr)) | ||
println("cap(nilArr): ", cap(nilArr)) | ||
println("cap(nilSlice): ", cap(nilSlice)) | ||
println("cap(nilArr2): ", cap(nilArr2)) | ||
println("cap(nilMatrix):", cap(nilMatrix)) | ||
|
||
printCap(nil) | ||
} | ||
|
||
func printCap(arr *[3]string) { | ||
println("printCap: ", cap(arr)) | ||
} | ||
|
||
// Output: | ||
// cap(arr): 5 | ||
// cap(&arr): 5 | ||
// cap(nilArr): 5 | ||
// cap(nilSlice): 0 | ||
// cap(nilArr2): 8 | ||
// cap(nilMatrix): 2 | ||
// printCap: 3 |
This file contains 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,9 @@ | ||
package main | ||
|
||
func main() { | ||
var s string | ||
println("cap", cap(s)) | ||
} | ||
|
||
// Error: | ||
// unexpected type for cap(): string |
This file contains 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,9 @@ | ||
package main | ||
|
||
func main() { | ||
var i *int | ||
println("cap", cap(i)) | ||
} | ||
|
||
// Error: | ||
// unexpected type for cap(): *int |
This file contains 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,9 @@ | ||
package main | ||
|
||
func main() { | ||
i := new(int) | ||
println("cap", cap(i)) | ||
} | ||
|
||
// Error: | ||
// unexpected type for cap(): *int |
This file contains 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,10 @@ | ||
package main | ||
|
||
func main() { | ||
exp := [...]string{"HELLO"} | ||
x := len(&exp) | ||
println(x) | ||
} | ||
|
||
// Output: | ||
// 1 |
This file contains 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,9 @@ | ||
package main | ||
|
||
func main() { | ||
exp := [...]string{"HELLO", "WORLD"} | ||
println(len(exp)) | ||
} | ||
|
||
// Output: | ||
// 2 |
This file contains 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,10 @@ | ||
package main | ||
|
||
func main() { | ||
exp := [...]int{1, 2, 3, 4, 5} | ||
ptr := &exp | ||
println(len(ptr)) | ||
} | ||
|
||
// Output: | ||
// 5 |
This file contains 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,12 @@ | ||
package main | ||
|
||
func main() { | ||
printLen(nil) | ||
} | ||
|
||
func printLen(arr *[2]int) { | ||
println(len(arr)) | ||
} | ||
|
||
// Output: | ||
// 2 |
This file contains 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,9 @@ | ||
package main | ||
|
||
func main() { | ||
var arr *[3]string | ||
println(cap(arr)) | ||
} | ||
|
||
// Output: | ||
// 3 |
Oops, something went wrong.