Skip to content

Commit 24bd69b

Browse files
committed
Add example tests.
1 parent 61853bd commit 24bd69b

File tree

1 file changed

+43
-31
lines changed

1 file changed

+43
-31
lines changed

hex/example_test.go

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package hex
33
import (
44
"log"
55

6+
"github.com/vadv/gopher-lua-libs/strings"
67
lua "github.com/yuin/gopher-lua"
78
)
89

@@ -11,59 +12,70 @@ func ExampleEncodeToString() {
1112
defer state.Close()
1213
Preload(state)
1314
source := `
14-
local base64 = require("base64")
15-
s = base64.RawStdEncoding:encode_to_string("foo\01bar")
15+
local hex = require 'hex'
16+
s = hex.encode_to_string("foo\01bar")
1617
print(s)
17-
18-
s = base64.StdEncoding:encode_to_string("foo\01bar")
19-
print(s)
20-
21-
s = base64.RawURLEncoding:encode_to_string("this is a <tag> and should be encoded")
22-
print(s)
23-
24-
s = base64.URLEncoding:encode_to_string("this is a <tag> and should be encoded")
25-
print(s)
26-
2718
`
2819
if err := state.DoString(source); err != nil {
2920
log.Fatal(err.Error())
3021
}
3122
// Output:
32-
// Zm9vAWJhcg
33-
// Zm9vAWJhcg==
34-
// dGhpcyBpcyBhIDx0YWc-IGFuZCBzaG91bGQgYmUgZW5jb2RlZA
35-
// dGhpcyBpcyBhIDx0YWc-IGFuZCBzaG91bGQgYmUgZW5jb2RlZA==
23+
// 666f6f01626172
3624
}
3725

3826
func ExampleDecodeString() {
3927
state := lua.NewState()
4028
defer state.Close()
4129
Preload(state)
4230
source := `
43-
local base64 = require("base64")
44-
s, err = base64.RawStdEncoding:decode_string("Zm9vAWJhcg")
45-
assert(not err, err)
46-
print(s)
47-
48-
s, err = base64.StdEncoding:decode_string("Zm9vAWJhcg==")
31+
local hex = require 'hex'
32+
s, err = hex.decode_string("666f6f01626172")
4933
assert(not err, err)
5034
print(s)
35+
`
36+
if err := state.DoString(source); err != nil {
37+
log.Fatal(err.Error())
38+
}
39+
// Output:
40+
// foobar
41+
}
5142

52-
s, err = base64.RawURLEncoding:decode_string("dGhpcyBpcyBhIDx0YWc-IGFuZCBzaG91bGQgYmUgZW5jb2RlZA")
53-
assert(not err, err)
54-
print(s)
43+
func ExampleNewEncoder() {
44+
state := lua.NewState()
45+
defer state.Close()
46+
Preload(state)
47+
strings.Preload(state)
48+
source := `
49+
local hex = require 'hex'
50+
local strings = require 'strings'
51+
local writer = strings.new_builder()
52+
encoder = hex.new_encoder(writer)
53+
encoder:write("foo\01bar")
54+
print(writer:string())
55+
`
56+
if err := state.DoString(source); err != nil {
57+
log.Fatal(err.Error())
58+
}
59+
// Output:
60+
// 666f6f01626172
61+
}
5562

56-
s, err = base64.URLEncoding:decode_string("dGhpcyBpcyBhIDx0YWc-IGFuZCBzaG91bGQgYmUgZW5jb2RlZA==")
57-
assert(not err, err)
63+
func ExampleNewDecoder() {
64+
state := lua.NewState()
65+
defer state.Close()
66+
Preload(state)
67+
strings.Preload(state)
68+
source := `
69+
local hex = require 'hex'
70+
local strings = require 'strings'
71+
local reader = strings.new_reader('666f6f01626172')
72+
decoder = hex.new_decoder(reader)
73+
local s = decoder:read("*a")
5874
print(s)
59-
6075
`
6176
if err := state.DoString(source); err != nil {
6277
log.Fatal(err.Error())
6378
}
6479
// Output:
6580
// foobar
66-
// foobar
67-
// this is a <tag> and should be encoded
68-
// this is a <tag> and should be encoded
6981
}

0 commit comments

Comments
 (0)