-
-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Execute code block for supported languages (bash,go,ruby,python)
- Loading branch information
1 parent
2a4ea2e
commit be0a54a
Showing
3 changed files
with
212 additions
and
0 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,114 @@ | ||
package code_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/maaslalani/slides/internal/code" | ||
) | ||
|
||
func TestExecute(t *testing.T) { | ||
tt := []struct { | ||
block code.Block | ||
expected code.Result | ||
}{ | ||
{ | ||
block: code.Block{ | ||
Code: `puts "Hello, world!"`, | ||
Language: "ruby", | ||
}, | ||
expected: code.Result{ | ||
Out: "Hello, world!\n", | ||
ExitCode: 0, | ||
}, | ||
}, | ||
{ | ||
block: code.Block{ | ||
Code: `puts "Hi, there!"`, | ||
Language: "ruby", | ||
}, | ||
expected: code.Result{ | ||
Out: "Hi, there!\n", | ||
ExitCode: 0, | ||
}, | ||
}, | ||
{ | ||
block: code.Block{ | ||
Code: `print "No new line"`, | ||
Language: "ruby", | ||
}, | ||
expected: code.Result{ | ||
Out: "No new line", | ||
ExitCode: 0, | ||
}, | ||
}, | ||
{ | ||
block: code.Block{ | ||
Code: ` | ||
package main | ||
import "fmt" | ||
func main() { | ||
fmt.Print("Hello, go!") | ||
} | ||
`, | ||
Language: "go", | ||
}, | ||
expected: code.Result{ | ||
Out: "Hello, go!", | ||
ExitCode: 0, | ||
}, | ||
}, | ||
{ | ||
block: code.Block{ | ||
Code: `print("Hello, python!")`, | ||
Language: "python", | ||
}, | ||
expected: code.Result{ | ||
Out: "Hello, python!\n", | ||
ExitCode: 0, | ||
}, | ||
}, | ||
{ | ||
block: code.Block{ | ||
Code: `echo "Hello, bash!"`, | ||
Language: "bash", | ||
}, | ||
expected: code.Result{ | ||
Out: "Hello, bash!\n", | ||
ExitCode: 0, | ||
}, | ||
}, | ||
{ | ||
block: code.Block{ | ||
Code: `Invalid Code`, | ||
Language: "bash", | ||
}, | ||
expected: code.Result{ | ||
Out: "", | ||
ExitCode: 1, | ||
}, | ||
}, | ||
{ | ||
block: code.Block{ | ||
Code: `Invalid Code`, | ||
Language: "invalid", | ||
}, | ||
expected: code.Result{ | ||
Out: "Error: unsupported language", | ||
ExitCode: code.ExitCodeInternalError, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range tt { | ||
r := code.Execute(tc.block) | ||
if r.Out != tc.expected.Out { | ||
t.Fatalf("invalid output, got %s, want %s", r.Out, tc.expected.Out) | ||
} | ||
|
||
if r.ExitCode != tc.expected.ExitCode { | ||
t.Fatalf("unexpected exit code, got %d, want %d", r.ExitCode, tc.expected.ExitCode) | ||
} | ||
} | ||
} |
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,33 @@ | ||
package code | ||
|
||
type Language struct { | ||
Extension string | ||
Command []string | ||
} | ||
|
||
// Supported Languages | ||
const ( | ||
Bash = "bash" | ||
Go = "go" | ||
Ruby = "ruby" | ||
Python = "python" | ||
) | ||
|
||
var Languages = map[string]Language{ | ||
Bash: { | ||
Extension: "sh", | ||
Command: []string{"bash"}, | ||
}, | ||
Go: { | ||
Extension: "go", | ||
Command: []string{"go", "run"}, | ||
}, | ||
Ruby: { | ||
Extension: "rb", | ||
Command: []string{"ruby"}, | ||
}, | ||
Python: { | ||
Extension: "py", | ||
Command: []string{"python"}, | ||
}, | ||
} |