Completed unit tests and added README and LICENSE
This commit is contained in:
parent
1651987399
commit
f11008ab45
6 changed files with 96 additions and 8 deletions
19
LICENSE.txt
Normal file
19
LICENSE.txt
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
Copyright (c) 2023 Leon Mika
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
5
README.md
Normal file
5
README.md
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
# scriptx - Extras for Script
|
||||||
|
|
||||||
|
Utilities that extend [github.com/bitfield/script](https://github.com/bitfield/script).
|
||||||
|
|
||||||
|
Currently a WIP. The API is subject to change.
|
||||||
|
|
@ -31,10 +31,3 @@ func verifyPipeLines(t *testing.T, fn func(p *script.Pipe) *script.Pipe, inLines
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, expOut, outLines)
|
assert.Equal(t, expOut, outLines)
|
||||||
}
|
}
|
||||||
|
|
||||||
func verifyPipeErr(t *testing.T, fn func(p *script.Pipe) *script.Pipe, inLines []string) {
|
|
||||||
pipe := script.Slice(inLines)
|
|
||||||
_, err := fn(pipe).Slice()
|
|
||||||
|
|
||||||
assert.Error(t, err)
|
|
||||||
}
|
|
||||||
|
|
|
||||||
45
json_test.go
Normal file
45
json_test.go
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
package scriptx_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/bitfield/script"
|
||||||
|
"github.com/lmika/scriptx"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestToJSON(t *testing.T) {
|
||||||
|
t.Run("should convert a line to a JSON object", func(t *testing.T) {
|
||||||
|
verifyPipeLines(t, func(p *script.Pipe) *script.Pipe {
|
||||||
|
return p.Filter(scriptx.ToJSON(func(line string) any {
|
||||||
|
return map[string]any{
|
||||||
|
"line": line,
|
||||||
|
"line_length": len(line),
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
}, []string{
|
||||||
|
"Just three words",
|
||||||
|
"She sells sea shells by the \"sea shore\"",
|
||||||
|
"Another example of a line",
|
||||||
|
}, []string{
|
||||||
|
`{"line":"Just three words","line_length":16}`,
|
||||||
|
`{"line":"She sells sea shells by the \"sea shore\"","line_length":39}`,
|
||||||
|
`{"line":"Another example of a line","line_length":25}`,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("should convert a line to a JSON array", func(t *testing.T) {
|
||||||
|
verifyPipeLines(t, func(p *script.Pipe) *script.Pipe {
|
||||||
|
return p.Filter(scriptx.ToJSON(func(line string) any {
|
||||||
|
return []any{line, line}
|
||||||
|
}))
|
||||||
|
}, []string{
|
||||||
|
"line",
|
||||||
|
"words words",
|
||||||
|
"foobar",
|
||||||
|
}, []string{
|
||||||
|
`["line","line"]`,
|
||||||
|
`["words words","words words"]`,
|
||||||
|
`["foobar","foobar"]`,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
26
subpipe_test.go
Normal file
26
subpipe_test.go
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
package scriptx_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/bitfield/script"
|
||||||
|
"github.com/lmika/scriptx"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSubPipe(t *testing.T) {
|
||||||
|
t.Run("should run the subpipe for each line in the parent pipe", func(t *testing.T) {
|
||||||
|
verifyPipeLines(t, func(p *script.Pipe) *script.Pipe {
|
||||||
|
return p.Filter(scriptx.SubPipe(func (src *script.Pipe) *script.Pipe {
|
||||||
|
return src.Replace("Line", "Subpipe")
|
||||||
|
}))
|
||||||
|
}, []string{
|
||||||
|
"Line 1",
|
||||||
|
"Line 2",
|
||||||
|
"Line 3",
|
||||||
|
}, []string{
|
||||||
|
"Subpipe 1",
|
||||||
|
"Subpipe 2",
|
||||||
|
"Subpipe 3",
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue