Skip to content

Commit a1b5891

Browse files
committed
Add app call tests
1 parent 196cafe commit a1b5891

3 files changed

Lines changed: 85 additions & 2 deletions

File tree

tests/appcall_linux_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package tests
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestLsCallSuccess(t *testing.T) {
11+
transpileBashFunc(t, func(dir string) string {
12+
return `
13+
` + fmt.Sprintf(`var a = @ls("%s")`, dir) + `
14+
15+
print(a)
16+
`
17+
}, func(output string, err error) {
18+
require.Nil(t, err)
19+
require.Equal(t, "test.sh\ntest.tsh", output)
20+
})
21+
}
22+
23+
func TestLsCallPipeToGrepCallSuccess(t *testing.T) {
24+
transpileBashFunc(t, func(dir string) string {
25+
return `
26+
` + fmt.Sprintf(`var a = @ls("%s") | @grep(".tsh")`, dir) + `
27+
28+
print(a)
29+
`
30+
}, func(output string, err error) {
31+
require.Nil(t, err)
32+
require.Equal(t, "test.tsh", output)
33+
})
34+
}

tests/appcall_windows_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package tests
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestDirCallSuccess(t *testing.T) {
11+
transpileBatchFunc(t, func(dir string) string {
12+
return `
13+
` + fmt.Sprintf(`var a = @dir("/B", "%s")`, dir) + `
14+
15+
print(a)
16+
`
17+
}, func(output string, err error) {
18+
require.Nil(t, err)
19+
require.Equal(t, "test.bat\ntest.tsh", output)
20+
})
21+
}
22+
23+
func TestLsCallPipeToGrepCallSuccess(t *testing.T) {
24+
transpileBatchFunc(t, func(dir string) string {
25+
return `
26+
` + fmt.Sprintf(`var a = @dir("/B", "%s") | @findstr(".tsh")`, dir) + `
27+
28+
print(a)
29+
`
30+
}, func(output string, err error) {
31+
require.Nil(t, err)
32+
require.Equal(t, "test.tsh", output)
33+
})
34+
}

tests/helpers.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,19 @@ import (
1515
"github.com/stretchr/testify/require"
1616
)
1717

18+
type sourceCallout func(dir string) string
1819
type compareCallout func(output string, err error)
1920
type transpilerFunc func(t *testing.T, source string, compare compareCallout)
2021

21-
func transpile(t *testing.T, source string, targetFileName string, converter transpiler.Converter, compare compareCallout) {
22+
func transpileFunc(t *testing.T, source sourceCallout, targetFileName string, converter transpiler.Converter, compare compareCallout) {
2223
trans := transpiler.New()
2324
dir, err := os.MkdirTemp("", "typeshell_tests")
2425

2526
require.Nil(t, err)
2627
defer os.RemoveAll(dir)
2728

2829
file := filepath.Join(dir, "test.tsh")
29-
err = os.WriteFile(file, []byte(source), 0x777)
30+
err = os.WriteFile(file, []byte(source(dir)), 0x777)
3031

3132
require.Nil(t, err)
3233
code, err := trans.Transpile(file, converter)
@@ -48,14 +49,28 @@ func transpile(t *testing.T, source string, targetFileName string, converter tra
4849
compare(outputString, err)
4950
}
5051

52+
func transpile(t *testing.T, source string, targetFileName string, converter transpiler.Converter, compare compareCallout) {
53+
transpileFunc(t, func(_ string) string {
54+
return source
55+
}, targetFileName, converter, compare)
56+
}
57+
5158
func transpileBash(t *testing.T, source string, compare compareCallout) {
5259
transpile(t, source, "test.sh", bash.New(), compare)
5360
}
5461

62+
func transpileBashFunc(t *testing.T, source sourceCallout, compare compareCallout) {
63+
transpileFunc(t, source, "test.sh", bash.New(), compare)
64+
}
65+
5566
func transpileBatch(t *testing.T, source string, compare compareCallout) {
5667
transpile(t, source, "test.bat", batch.New(), compare)
5768
}
5869

70+
func transpileBatchFunc(t *testing.T, source sourceCallout, compare compareCallout) {
71+
transpileFunc(t, source, "test.bat", batch.New(), compare)
72+
}
73+
5974
func shortenError(err error) error {
6075
if err != nil {
6176
s := err.Error()

0 commit comments

Comments
 (0)