<- Back to shtanton's homepage
aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharlie Stanton <charlie@shtanton.xyz>2024-05-02 21:34:53 +0100
committerCharlie Stanton <charlie@shtanton.xyz>2024-05-02 21:34:53 +0100
commit22ccb0c370cf2690f1b1a80fe003e05c6ba5e5ed (patch)
treec85d3a1ea0600e0f9ec78767cff963727662361a
parentf9a69d3121248a6fc285dbcb63b242b8425283ef (diff)
downloadstred-go-22ccb0c370cf2690f1b1a80fe003e05c6ba5e5ed.tar
Fix FullMerge command so it can work on non-structure values
-rw-r--r--main/command.go6
-rw-r--r--main/main_test.go19
2 files changed, 21 insertions, 4 deletions
diff --git a/main/command.go b/main/command.go
index e795333..bbbb036 100644
--- a/main/command.go
+++ b/main/command.go
@@ -143,10 +143,14 @@ type FullMergeCommand struct {
}
func (cmd FullMergeCommand) exec(state *ProgramState) {
_, notOk := runSubex(cmd.subex, state.value)
- if notOk || state.end {
+ if notOk {
state.pc++
return
}
+ if !state.start {
+ state.pc += 2
+ return
+ }
for {
item, err := state.Read()
diff --git a/main/main_test.go b/main/main_test.go
index 94e1f04..3d10c48 100644
--- a/main/main_test.go
+++ b/main/main_test.go
@@ -5,9 +5,10 @@ import (
"testing"
)
-var miscInput string = `{"something":{"nested":"Here is my test value"},"array":["Hello","world","these","are","values"],"people":[{"first_name":"Charlie","last_name":"Johnson","age":22},{"first_name":"Tom","last_name":"Johnson","age":18},{"first_name":"Charlie","last_name":"Chaplin","age":122},{"first_name":"John","last_name":"Johnson","age":48}]}`
+const miscInput string = `{"something":{"nested":"Here is my test value"},"array":["Hello","world","these","are","values"],"people":[{"first_name":"Charlie","last_name":"Johnson","age":22},{"first_name":"Tom","last_name":"Johnson","age":18},{"first_name":"Charlie","last_name":"Chaplin","age":122},{"first_name":"John","last_name":"Johnson","age":48}]}`
+const mixedArray string = `{"array":["first",{"name":"second"},"third"]}`
-func TestMain(t *testing.T) {
+func TestSpecificCases(t *testing.T) {
type test struct {
name string
program string
@@ -102,7 +103,7 @@ func TestMain(t *testing.T) {
},
{
name: "Short concat array values",
- program: "M/#( \"array\" :(): )#/{ s/#( \"array\"$_ :( .{-0} )- )-/ s/-( ~(.{-0}` `)-{-0} ~(.{-0})- )~/p }",
+ program: "M/#( \"array\" :(): )#/{ s/#( \"array\"$_ :( .{-0} )- )-/ s/-( ~(.{-0}` `)-{-0} ~(.{-0})- )~/p }",
quiet: true,
input: miscInput,
expected: `"Hello world these are values"`,
@@ -120,6 +121,18 @@ func TestMain(t *testing.T) {
expected: `{"something":{"nested":"Here is my test value"},"array":["Hello","world","these","are","values"],"people":[{"first_name":"Charlie","last_name":"Johnson","age":22},{"first_name":"Tom","last_name":"Johnson","age":18},{"first_name":"Charlie","last_name":"Chaplin","age":122}]}`,
},
{
+ name: "Drop last element of simple array",
+ program: `s/#( "array" @( . . )@ )#/{ Ed }`,
+ input: miscInput,
+ expected: `{"something":{"nested":"Here is my test value"},"array":["Hello","world","these","are"],"people":[{"first_name":"Charlie","last_name":"Johnson","age":22},{"first_name":"Tom","last_name":"Johnson","age":18},{"first_name":"Charlie","last_name":"Chaplin","age":122},{"first_name":"John","last_name":"Johnson","age":48}]}`,
+ },
+ {
+ name: "Drop last element of mixed array",
+ program: `M/#( "array" @( . (~[.]~|@()@|#()#) )@ )#/{ Ed }`,
+ input: mixedArray,
+ expected: `{"array":["first",{"name":"second"}]}`,
+ },
+ {
name: "Prepend to array",
program: "as/#( \"array\" :( `\"First\"` ): )#/",
input: miscInput,