@@ -804,6 +804,59 @@ func TestJQHandlesGithubJSONWithRealWorldExampleQuery(t *testing.T) {
804
804
}
805
805
}
806
806
807
+ func TestJQWithNewlineDelimitedInputAndFieldQueryProducesSelectedFields (t * testing.T ) {
808
+ t .Parallel ()
809
+ input := `{"timestamp": 1649264191, "iss_position": {"longitude": "52.8439", "latitude": "10.8107"}, "message": "success"}` + "\n "
810
+ input += input
811
+ want := `{"latitude":"10.8107","longitude":"52.8439"}` + "\n "
812
+ want += want
813
+ got , err := script .Echo (input ).JQ (".iss_position" ).String ()
814
+ if err != nil {
815
+ t .Fatal (err )
816
+ }
817
+ if want != got {
818
+ t .Error (want , got )
819
+ t .Error (cmp .Diff (want , got ))
820
+ }
821
+ }
822
+
823
+ func TestJQWithNewlineDelimitedInputAndArrayInputAndElementQueryProducesSelectedElements (t * testing.T ) {
824
+ t .Parallel ()
825
+ input := `[1, 2, 3]` + "\n " + `[4, 5, 6]`
826
+ want := "1\n 4\n "
827
+ got , err := script .Echo (input ).JQ (".[0]" ).String ()
828
+ if err != nil {
829
+ t .Fatal (err )
830
+ }
831
+ if want != got {
832
+ t .Error (want , got )
833
+ t .Error (cmp .Diff (want , got ))
834
+ }
835
+ }
836
+
837
+ func TestJQWithNewlineDelimitedMixedAndPrettyPrintedInputValues (t * testing.T ) {
838
+ t .Parallel ()
839
+ input := `
840
+ {
841
+ "key1": "val1",
842
+ "key2": "val2"
843
+ }
844
+ [
845
+ 0,
846
+ 1
847
+ ]
848
+ `
849
+ want := `{"key1":"val1","key2":"val2"}` + "\n " + "[0,1]" + "\n "
850
+ got , err := script .Echo (input ).JQ ("." ).String ()
851
+ if err != nil {
852
+ t .Fatal (err )
853
+ }
854
+ if want != got {
855
+ t .Error (want , got )
856
+ t .Error (cmp .Diff (want , got ))
857
+ }
858
+ }
859
+
807
860
func TestJQErrorsWithInvalidQuery (t * testing.T ) {
808
861
t .Parallel ()
809
862
input := `[1, 2, 3]`
@@ -813,6 +866,29 @@ func TestJQErrorsWithInvalidQuery(t *testing.T) {
813
866
}
814
867
}
815
868
869
+ func TestJQErrorsWithInvalidInput (t * testing.T ) {
870
+ t .Parallel ()
871
+ input := "invalid JSON value"
872
+ _ , err := script .Echo (input ).JQ ("." ).String ()
873
+ if err == nil {
874
+ t .Error ("want error from invalid JSON input, got nil" )
875
+ }
876
+ }
877
+
878
+ func TestJQWithNewlineDelimitedInputErrorsAfterFirstInvalidInput (t * testing.T ) {
879
+ t .Parallel ()
880
+ input := `[0]` + "\n " + `[1` + "\n " + `[2]` // missing `]` in second line
881
+ want := "0\n "
882
+ got , err := script .Echo (input ).JQ (".[0]" ).String ()
883
+ if err == nil {
884
+ t .Fatal ("want error from invalid JSON, got nil" )
885
+ }
886
+ if want != got {
887
+ t .Error (want , got )
888
+ t .Error (cmp .Diff (want , got ))
889
+ }
890
+ }
891
+
816
892
func TestLastDropsAllButLastNLinesOfInput (t * testing.T ) {
817
893
t .Parallel ()
818
894
input := "a\n b\n c\n "
0 commit comments