-
Notifications
You must be signed in to change notification settings - Fork 307
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix bytecode encoding/decoding of builtin modules (#154)
* fix bytecode encoding/decoding of builtin modules * Bytecode.Decode() to take map[string]objects.Importable * add objects.ModuleMap * update docs * stdlib.GetModuleMap()
- Loading branch information
Showing
22 changed files
with
308 additions
and
140 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package cli_test | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/d5/tengo/assert" | ||
"github.com/d5/tengo/cli" | ||
"github.com/d5/tengo/stdlib" | ||
) | ||
|
||
func TestCLICompileAndRun(t *testing.T) { | ||
tempDir := filepath.Join(os.TempDir(), "tengo_tests") | ||
_ = os.MkdirAll(tempDir, os.ModePerm) | ||
binFile := filepath.Join(tempDir, "cli_bin") | ||
outFile := filepath.Join(tempDir, "cli_out") | ||
defer func() { | ||
_ = os.RemoveAll(tempDir) | ||
}() | ||
|
||
src := []byte(` | ||
os := import("os") | ||
rand := import("rand") | ||
times := import("times") | ||
rand.seed(times.time_nanosecond(times.now())) | ||
rand_num := func() { | ||
return rand.intn(100) | ||
} | ||
file := os.create("` + outFile + `") | ||
file.write_string("random number is " + rand_num()) | ||
file.close() | ||
`) | ||
|
||
mods := stdlib.GetModuleMap(stdlib.AllModuleNames()...) | ||
|
||
err := cli.CompileOnly(mods, src, "src", binFile) | ||
if !assert.NoError(t, err) { | ||
return | ||
} | ||
|
||
compiledBin, err := ioutil.ReadFile(binFile) | ||
if !assert.NoError(t, err) { | ||
return | ||
} | ||
|
||
err = cli.RunCompiled(mods, compiledBin) | ||
if !assert.NoError(t, err) { | ||
return | ||
} | ||
|
||
read, err := ioutil.ReadFile(outFile) | ||
if !assert.NoError(t, err) { | ||
return | ||
} | ||
ok, err := regexp.Match(`^random number is \d+$`, read) | ||
assert.NoError(t, err) | ||
assert.True(t, ok, string(read)) | ||
} |
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
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
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
Oops, something went wrong.