-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathexample_test.go
69 lines (57 loc) · 1.53 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package base64
import (
"log"
lua "github.com/yuin/gopher-lua"
)
func ExampleEncodeToString() {
state := lua.NewState()
defer state.Close()
Preload(state)
source := `
local base64 = require("base64")
s = base64.RawStdEncoding:encode_to_string("foo\01bar")
print(s)
s = base64.StdEncoding:encode_to_string("foo\01bar")
print(s)
s = base64.RawURLEncoding:encode_to_string("this is a <tag> and should be encoded")
print(s)
s = base64.URLEncoding:encode_to_string("this is a <tag> and should be encoded")
print(s)
`
if err := state.DoString(source); err != nil {
log.Fatal(err.Error())
}
// Output:
// Zm9vAWJhcg
// Zm9vAWJhcg==
// dGhpcyBpcyBhIDx0YWc-IGFuZCBzaG91bGQgYmUgZW5jb2RlZA
// dGhpcyBpcyBhIDx0YWc-IGFuZCBzaG91bGQgYmUgZW5jb2RlZA==
}
func ExampleDecodeString() {
state := lua.NewState()
defer state.Close()
Preload(state)
source := `
local base64 = require("base64")
s, err = base64.RawStdEncoding:decode_string("Zm9vAWJhcg")
assert(not err, err)
print(s)
s, err = base64.StdEncoding:decode_string("Zm9vAWJhcg==")
assert(not err, err)
print(s)
s, err = base64.RawURLEncoding:decode_string("dGhpcyBpcyBhIDx0YWc-IGFuZCBzaG91bGQgYmUgZW5jb2RlZA")
assert(not err, err)
print(s)
s, err = base64.URLEncoding:decode_string("dGhpcyBpcyBhIDx0YWc-IGFuZCBzaG91bGQgYmUgZW5jb2RlZA==")
assert(not err, err)
print(s)
`
if err := state.DoString(source); err != nil {
log.Fatal(err.Error())
}
// Output:
// foobar
// foobar
// this is a <tag> and should be encoded
// this is a <tag> and should be encoded
}