2021-03-24 21:12:01 +00:00
package yqlib
import (
"testing"
)
var subtractOperatorScenarios = [ ] expressionScenario {
2021-05-16 04:36:13 +00:00
{
skipDoc : true ,
document : ` { } ` ,
2023-02-28 05:40:38 +00:00
expression : "(.a - .b) as $x | ." ,
2021-05-16 04:36:13 +00:00
expected : [ ] string {
2023-10-18 01:11:53 +00:00
"D0, P[], (!!map)::{}\n" ,
2021-05-16 04:36:13 +00:00
} ,
} ,
2024-09-29 05:21:11 +00:00
{
skipDoc : true ,
description : "subtract sequence creates a new sequence" ,
expression : ` ["a", "b"] as $f | { 0:$f - ["a"], 1:$f} ` ,
expected : [ ] string {
"D0, P[], (!!map)::0:\n - b\n1:\n - a\n - b\n" ,
} ,
} ,
2021-09-07 06:58:34 +00:00
{
description : "Array subtraction" ,
expression : ` [1,2] - [2,3] ` ,
expected : [ ] string {
"D0, P[], (!!seq)::- 1\n" ,
} ,
} ,
{
skipDoc : true ,
expression : ` [2,1,2,2] - [2,3] ` ,
expected : [ ] string {
"D0, P[], (!!seq)::- 1\n" ,
} ,
} ,
{
description : "Array subtraction with nested array" ,
expression : ` [[1], 1, 2] - [[1], 3] ` ,
expected : [ ] string {
"D0, P[], (!!seq)::- 1\n- 2\n" ,
} ,
} ,
{
skipDoc : true ,
expression : ` [[1], 1, [[[2]]]] - [[1], [[[3]]]] ` ,
expected : [ ] string {
"D0, P[], (!!seq)::- 1\n- - - - 2\n" ,
} ,
} ,
{
description : "Array subtraction with nested object" ,
subdescription : ` Note that order of the keys does not matter ` ,
document : ` [ { a: b, c: d}, { a: b}] ` ,
expression : ` . - [ { "c": "d", "a": "b"}] ` ,
expected : [ ] string {
"D0, P[], (!!seq)::[{a: b}]\n" ,
} ,
} ,
{
skipDoc : true ,
document : ` [ { a: [1], c: d}, { a: [2], c: d}, { a: b}] ` ,
expression : ` . - [ { "c": "d", "a": [1]}] ` ,
expected : [ ] string {
"D0, P[], (!!seq)::[{a: [2], c: d}, {a: b}]\n" ,
} ,
} ,
2021-03-24 21:12:01 +00:00
{
description : "Number subtraction - float" ,
subdescription : "If the lhs or rhs are floats then the expression will be calculated with floats." ,
document : ` { a: 3, b: 4.5} ` ,
expression : ` .a = .a - .b ` ,
expected : [ ] string {
2023-10-18 01:11:53 +00:00
"D0, P[], (!!map)::{a: -1.5, b: 4.5}\n" ,
2021-03-24 21:12:01 +00:00
} ,
} ,
{
description : "Number subtraction - int" ,
subdescription : "If both the lhs and rhs are ints then the expression will be calculated with ints." ,
document : ` { a: 3, b: 4} ` ,
expression : ` .a = .a - .b ` ,
expected : [ ] string {
2023-10-18 01:11:53 +00:00
"D0, P[], (!!map)::{a: -1, b: 4}\n" ,
2021-03-24 21:12:01 +00:00
} ,
} ,
{
description : "Decrement numbers" ,
document : ` { a: 3, b: 5} ` ,
expression : ` .[] -= 1 ` ,
expected : [ ] string {
2023-10-18 01:11:53 +00:00
"D0, P[], (!!map)::{a: 2, b: 4}\n" ,
2021-03-24 21:12:01 +00:00
} ,
} ,
2022-02-14 04:37:43 +00:00
{
description : "Date subtraction" ,
subdescription : "You can subtract durations from dates. Assumes RFC3339 date time format, see [date-time operators](https://mikefarah.gitbook.io/yq/operators/date-time-operators) for more information." ,
document : ` a: 2021-01-01T03:10:00Z ` ,
expression : ` .a -= "3h10m" ` ,
expected : [ ] string {
2023-10-18 01:11:53 +00:00
"D0, P[], (!!map)::a: 2021-01-01T00:00:00Z\n" ,
2022-11-04 01:21:12 +00:00
} ,
} ,
{
description : "Date subtraction - only date" ,
skipDoc : true ,
document : ` a: 2021-01-01 ` ,
expression : ` .a -= "24h" ` ,
expected : [ ] string {
2023-10-18 01:11:53 +00:00
"D0, P[], (!!map)::a: 2020-12-31T00:00:00Z\n" ,
2022-02-14 04:37:43 +00:00
} ,
} ,
{
description : "Date subtraction - custom format" ,
subdescription : "Use with_dtf to specify your datetime format. See [date-time operators](https://mikefarah.gitbook.io/yq/operators/date-time-operators) for more information." ,
document : ` a: Saturday, 15-Dec-01 at 6:00AM GMT ` ,
expression : ` with_dtf("Monday, 02-Jan-06 at 3:04PM MST", .a -= "3h1m") ` ,
expected : [ ] string {
2023-10-18 01:11:53 +00:00
"D0, P[], (!!map)::a: Saturday, 15-Dec-01 at 2:59AM GMT\n" ,
2022-02-14 04:37:43 +00:00
} ,
} ,
{
skipDoc : true ,
description : "Date subtraction - custom format" ,
subdescription : "You can subtract durations from dates. See [date-time operators](https://mikefarah.gitbook.io/yq/operators/date-time-operators) for more information." ,
document : ` a: !cat Saturday, 15-Dec-01 at 6:00AM GMT ` ,
expression : ` with_dtf("Monday, 02-Jan-06 at 3:04PM MST", .a -= "3h1m") ` ,
expected : [ ] string {
2023-10-18 01:11:53 +00:00
"D0, P[], (!!map)::a: !cat Saturday, 15-Dec-01 at 2:59AM GMT\n" ,
2022-02-14 04:37:43 +00:00
} ,
} ,
2022-01-22 02:47:22 +00:00
{
description : "Custom types: that are really numbers" ,
subdescription : "When custom tags are encountered, yq will try to decode the underlying type." ,
document : "a: !horse 2\nb: !goat 1" ,
expression : ` .a -= .b ` ,
expected : [ ] string {
2023-10-18 01:11:53 +00:00
"D0, P[], (!!map)::a: !horse 1\nb: !goat 1\n" ,
2022-01-22 02:47:22 +00:00
} ,
} ,
{
skipDoc : true ,
description : "Custom types: that are really floats" ,
subdescription : "When custom tags are encountered, yq will try to decode the underlying type." ,
document : "a: !horse 2.5\nb: !goat 1.5" ,
expression : ` .a - .b ` ,
expected : [ ] string {
"D0, P[a], (!horse)::1\n" ,
} ,
} ,
{
skipDoc : true ,
description : "Custom types: that are really maps" ,
document : ` [!horse { a: b, c: d}, !goat { a: b}] ` ,
expression : ` . - [ { "c": "d", "a": "b"}] ` ,
expected : [ ] string {
"D0, P[], (!!seq)::[!goat {a: b}]\n" ,
} ,
} ,
2021-03-24 21:12:01 +00:00
}
func TestSubtractOperatorScenarios ( t * testing . T ) {
for _ , tt := range subtractOperatorScenarios {
testScenario ( t , & tt )
}
2021-12-21 04:02:07 +00:00
documentOperatorScenarios ( t , "subtract" , subtractOperatorScenarios )
2021-03-24 21:12:01 +00:00
}