-
Notifications
You must be signed in to change notification settings - Fork 364
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: improve tests and docs of persisted types (#833)
Co-authored-by: Morgan <git@howl.moe>
- Loading branch information
Showing
4 changed files
with
191 additions
and
33 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,18 @@ | ||
// PKGPATH: gno.land/r/demo/tests_test | ||
package tests_test | ||
|
||
var amap map[string]string = map[string]string{"a": "1"} | ||
|
||
func init() { | ||
println("preinit", amap) | ||
amap["b"] = "2" | ||
println("postinit", amap) | ||
} | ||
|
||
func main() { | ||
println("premain", amap) | ||
amap["b"] = "2" | ||
println("postmain", amap) | ||
} | ||
|
||
// Current behavior: panics |
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,114 @@ | ||
// PKGPATH: gno.land/r/demo/tests_test | ||
package tests_test | ||
|
||
type myStruct struct { | ||
A int | ||
B string | ||
} | ||
|
||
var ( | ||
// Native types | ||
abigint bigint = 16 | ||
abool bool = true | ||
abyte byte = 0x16 | ||
afloat32 float32 = 16.16 | ||
afloat64 float64 = 16.16 | ||
aint int = 16 | ||
aint8 int8 = 16 | ||
aint16 int16 = 16 | ||
aint32 int32 = 16 | ||
aint64 int64 = 16 | ||
arune rune = 'a' | ||
astring string = "hello" | ||
astringslice []string = []string{"A"} | ||
astruct myStruct = myStruct{16, "A"} | ||
auint uint = 16 | ||
auint8 uint8 = 16 | ||
auint16 uint16 = 16 | ||
auint32 uint32 = 16 | ||
auint64 uint64 = 16 | ||
ainterface interface{} = struct{ a float32 }{16.0} | ||
afunc func() string = func() string { return "A" } | ||
|
||
// TODO: | ||
// amap map[string]string = map[string]string{"a": "1"} | ||
// more slices | ||
// slice/map containing unsupported types | ||
|
||
// Not supported: | ||
// error | ||
) | ||
|
||
func init() { | ||
printVars("preinit ") | ||
mutateVars("B") | ||
printVars("postinit") | ||
} | ||
|
||
func main() { | ||
printVars("premain ") | ||
mutateVars("C") | ||
printVars("postmain") | ||
} | ||
|
||
func mutateVars(stringModifier string) { | ||
abigint *= 2 | ||
abool = !abool | ||
abyte *= 2 | ||
afloat32 *= 2 | ||
afloat64 *= 2 | ||
aint *= 2 | ||
aint8 *= 2 | ||
aint16 *= 2 | ||
aint32 *= 2 | ||
aint64 *= 2 | ||
arune = rune(stringModifier[0]) | ||
astring += stringModifier | ||
astringslice = append(astringslice, stringModifier) | ||
auint *= 2 | ||
auint8 *= 2 | ||
auint16 *= 2 | ||
auint32 *= 2 | ||
auint64 *= 2 | ||
astruct.A *= 2 | ||
astruct.B = stringModifier | ||
ainterface = struct{ a string }{stringModifier} | ||
afunc = func() string { return stringModifier } | ||
|
||
// TODO: | ||
// amap["c"] = "3" | ||
} | ||
|
||
func printVars(phase string) { | ||
println(phase, | ||
// variables | ||
abigint, | ||
abool, | ||
abyte, | ||
afloat32, | ||
afloat64, | ||
aint, | ||
aint8, | ||
aint16, | ||
aint32, | ||
aint64, | ||
arune, | ||
astring, | ||
astringslice, | ||
astringslice[0], | ||
astruct, | ||
auint, | ||
auint8, | ||
auint16, | ||
auint32, | ||
auint64, | ||
ainterface, | ||
afunc(), | ||
) | ||
} | ||
|
||
// Output: | ||
// preinit 16 true 22 16.16 16.16 16 16 16 16 16 97 hello slice[("A" string)] A struct{(16 int),("A" string)} 16 16 16 16 16 struct{(16 float32)} A | ||
// postinit 32 false 44 32.32 32.32 32 32 32 32 32 66 helloB slice[("A" string),("B" string)] A struct{(32 int),("B" string)} 32 32 32 32 32 struct{("B" string)} B | ||
// premain 32 false 44 32.32 32.32 32 32 32 32 32 66 helloB slice[("A" string),("B" string)] A struct{(32 int),("B" string)} 32 32 32 32 32 struct{("B" string)} B | ||
// postmain 64 true 88 64.64 64.64 64 64 64 64 64 67 helloBC slice[("A" string),("B" string),("C" string)] A struct{(64 int),("C" string)} 64 64 64 64 64 struct{("C" string)} C |