-
Notifications
You must be signed in to change notification settings - Fork 22
/
main_test.go
92 lines (82 loc) · 4.77 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import "testing"
func TestReplaceQuotesInPaths(t *testing.T) {
inputs := []string{
`[Address, Other."Alternative.Address"].City`,
`Account.( $AccName := function() { $."Account Name" }; Order[OrderID = "order104"].Product{ "Account": $AccName(), "SKU-" & $string(ProductID): $."Product Name" } )`,
`Account.Order.Product."Product Name".$uppercase().$substringBefore(" ")`,
`"foo".**.fud`,
`foo.**."fud"`,
`"foo".**."fud"`,
`Account.Order.Product[$."Product Name" ~> /hat/i].ProductID`,
`$sort(Account.Order.Product."Product Name")`,
`Account.Order.Product ~> $map(λ($prod, $index) { $index+1 & ": " & $prod."Product Name" })`,
`Account.Order.Product ~> $map(λ($prod, $index, $arr) { $index+1 & "/" & $count($arr) & ": " & $prod."Product Name" })`,
`Account.Order{OrderID: Product."Product Name"}`,
`Account.Order.{OrderID: Product."Product Name"}`,
`Account.Order.Product{$."Product Name": Price, $."Product Name": Price}`,
`Account.Order{ OrderID: { "TotalPrice":$sum(Product.(Price * Quantity)), "Items": Product."Product Name" }}`,
`{ "Order": Account.Order.{ "ID": OrderID, "Product": Product.{ "Name": $."Product Name", "SKU": ProductID, "Details": { "Weight": Description.Weight, "Dimensions": Description.(Width & " x " & Height & " x " & Depth) } }, "Total Price": $sum(Product.(Price * Quantity)) }}`,
`Account.Order.Product[$contains($."Product Name", /hat/)].ProductID`,
`Account.Order.Product[$contains($."Product Name", /hat/i)].ProductID`,
`Account.Order.Product.$replace($."Product Name", /hat/i, function($match) { "foo" })`,
`Account.Order.Product.$replace($."Product Name", /(h)(at)/i, function($match) { $uppercase($match.match) })`,
`$.'7a'`,
`$.'7'`,
`$lowercase($."NI.Number")`,
`$lowercase("COMPENSATION IS : " & Employment."Executive.Compensation")`,
`Account[$$.Account."Account Name" = "Firefly"].*[OrderID="order104"].Product.Price`,
}
outputs := []string{
"[Address, Other.`Alternative.Address`].City",
"Account.( $AccName := function() { $.`Account Name` }; Order[OrderID = \"order104\"].Product{ \"Account\": $AccName(), \"SKU-\" & $string(ProductID): $.`Product Name` } )",
"Account.Order.Product.`Product Name`.$uppercase().$substringBefore(\" \")",
"`foo`.**.fud",
"foo.**.`fud`",
"`foo`.**.`fud`",
"Account.Order.Product[$.`Product Name` ~> /hat/i].ProductID",
"$sort(Account.Order.Product.`Product Name`)",
"Account.Order.Product ~> $map(λ($prod, $index) { $index+1 & \": \" & $prod.`Product Name` })",
"Account.Order.Product ~> $map(λ($prod, $index, $arr) { $index+1 & \"/\" & $count($arr) & \": \" & $prod.`Product Name` })",
"Account.Order{OrderID: Product.`Product Name`}",
"Account.Order.{OrderID: Product.`Product Name`}",
"Account.Order.Product{$.`Product Name`: Price, $.`Product Name`: Price}",
"Account.Order{ OrderID: { \"TotalPrice\":$sum(Product.(Price * Quantity)), \"Items\": Product.`Product Name` }}",
"{ \"Order\": Account.Order.{ \"ID\": OrderID, \"Product\": Product.{ \"Name\": $.`Product Name`, \"SKU\": ProductID, \"Details\": { \"Weight\": Description.Weight, \"Dimensions\": Description.(Width & \" x \" & Height & \" x \" & Depth) } }, \"Total Price\": $sum(Product.(Price * Quantity)) }}",
"Account.Order.Product[$contains($.`Product Name`, /hat/)].ProductID",
"Account.Order.Product[$contains($.`Product Name`, /hat/i)].ProductID",
"Account.Order.Product.$replace($.`Product Name`, /hat/i, function($match) { \"foo\" })",
"Account.Order.Product.$replace($.`Product Name`, /(h)(at)/i, function($match) { $uppercase($match.match) })",
"$.`7a`",
"$.`7`",
"$lowercase($.`NI.Number`)",
"$lowercase(\"COMPENSATION IS : \" & Employment.`Executive.Compensation`)",
"Account[$$.Account.`Account Name` = \"Firefly\"].*[OrderID=\"order104\"].Product.Price",
}
for i := range inputs {
got, ok := replaceQuotesInPaths(inputs[i])
if got != outputs[i] {
t.Errorf("\n Input: %s\nExp. Output: %s\nAct. Output: %s", inputs[i], outputs[i], got)
}
if !ok {
t.Errorf("%s: Expected true, got %t", inputs[i], ok)
}
}
}
func TestReplaceQuotesInPathsNoOp(t *testing.T) {
inputs := []string{
`42 ~> "hello"`,
`"john@example.com" ~> $substringAfter("@") ~> $substringBefore(".")`,
`$ ~> |Account.Order.Product|{"Total":Price*Quantity},["Description", "SKU"]|`,
`$ ~> |(Account.Order.Product)[0]|{"Description":"blah"}|`,
}
for i := range inputs {
got, ok := replaceQuotesInPaths(inputs[i])
if got != inputs[i] {
t.Errorf("\n Input: %s\nExp. Output: %s\nAct. Output: %s", inputs[i], inputs[i], got)
}
if ok {
t.Errorf("%s: Expected false, got %t", inputs[i], ok)
}
}
}