Generated from test cases.
A very basic test that shows how a pattern variable (?likes
) gets bound during matching.
The pattern
{"likes":"?likes"}
matched against
{"likes":"tacos"}
should return
[{"?likes":"tacos"}]
A map with a variable and a constant. The pattern
{"likes":"?likes","when":"now"}
matched against
{"likes":"tacos","when":"now"}
should return
[{"?likes":"tacos"}]
A map with two constants. The pattern
{"likes":"queso","when":"now"}
matched against
{"likes":"queso","when":"now"}
should return
[{}]
This simple example shows bindings for two pattern variables. The pattern
{"likes":"?likes","wants":"?wants"}
matched against
{"likes":"tacos","wants":"queso"}
should return
[{"?likes":"tacos","?wants":"queso"}]
Pattern matching is fully structured The pattern
{"needs":{"tacos":{"n":"?n"}}}
matched against
{"needs":{"tacos":{"n":2}}}
should return
[{"?n":2}]
If you use a pattern variable more than once, then the bindings must agree. See the next example. The pattern
{"n":"?n","needs":{"tacos":{"n":"?n"}}}
matched against
{"n":2,"needs":{"tacos":{"n":2}}}
should return
[{"?n":2}]
If you use a pattern variable more than once, then the bindings must agree. See the previous example. The pattern
{"n":"?n","needs":{"tacos":{"n":"?n"}}}
matched against
{"n":3,"needs":{"tacos":{"n":2}}}
should return
[]
An array is treated as a set. The pattern
{"a":["?a"],"is":"?a"}
matched against
{"a":[1,2,3,4],"is":3}
should return
[{"?a":3}]
An array is treated as a set; multiple bindings possible. The pattern
["a","?x"]
matched against
["a","b","c"]
should return
[{"?x":"b"},{"?x":"c"}]
The pattern
[{"likes":"tacos"},"?x"]
matched against
[{"likes":"tacos"},"b","c"]
should return
[{"?x":"b"},{"?x":"c"}]
The pattern
["a","b","?x"]
matched against
[{"likes":"tacos"},"b","a"]
should return
[{"?x":{"likes":"tacos"}}]
The pattern
["a","b",{"likes":"?x"}]
matched against
[{"likes":"tacos"},{"likes":"chips"},"b","a"]
should return
[{"?x":"tacos"},{"?x":"chips"}]
An array is treated as a set. The pattern
{"a":["?a"],"is":["?a"]}
matched against
{"a":[1,2,3,4],"is":[2,3]}
should return
[{"?a":2},{"?a":3}]
Two pattern variables inside an array isn't allowed (because the computational complexity means that some input could be very costly to process). The pattern
{"a":["?x","?y"]}
matched against
{"a":[1]}
should return an error.
You can have at most one pattern variable as a key in a given map. The pattern
{"?x":1}
matched against
{"n":1}
should return
[{"?x":"n"}]
You can have at most one pattern variable as a key in a given map. The pattern
{"?x":1,"?y":2}
matched against
{"m":2,"n":1}
should return an error.
The pattern
{"wants":"?wants"}
matched against
{"needs":null,"wants":"tacos"}
should return
[{"?wants":"tacos"}]
The pattern
{"wants":1}
matched against
{"wants":"one"}
should return
[]
The pattern
{"wants":1}
matched against
{"wants":true}
should return
[]
The pattern
{"count":"?","wants":"?"}
matched against
{"count":48,"wants":"tacos"}
should return
[{}]
The pattern
{"count":"?","wants":"?","when":"?when"}
matched against
{"count":48,"wants":"tacos","when":"today"}
should return
[{"?when":"today"}]
The pattern
{"?":"tacos"}
matched against
{"likes":"tacos","needs":"chips"}
should return
[{}]
The pattern
{"?":{"likes":"?likes"}}
matched against
{"homer":{"likes":"tacos"}}
should return
[{"?likes":"tacos"}]
The pattern
{"?":"tacos"}
matched against
{"needs":"chips"}
should return
[]
The pattern
{"n":"?<n"}
matched against
{"n":3}
with bindings
{"?<n":10}
should return
[{"?<n":10,"?n":3}]
The pattern
{"n":"?<n"}
matched against
{"n":3}
with bindings
{"?<n":2}
should return
[]
The pattern
{"n":"?>=n"}
matched against
{"n":11}
with bindings
{"?>=n":11}
should return
[{"?>=n":11,"?n":11}]
The pattern
{"n":"?>=n"}
matched against
{"n":11}
with bindings
{"?>=n":12}
should return
[]
The pattern
{"n":"?<n"}
matched against
{"n":"queso"}
with bindings
{"?<n":2}
should return
[]
The pattern
{"n":"?<n"}
matched against
{"n":3}
with bindings
{"?<n":10,"?n":3}
should return
[{"?<n":10,"?n":3}]
The pattern
{"n":"?<n"}
matched against
{"n":3}
with bindings
{"?<n":10,"?n":4}
should return
[]
The pattern
{"needs":"?n","wants":{"n":"?<n"}}
matched against
{"needs":3,"wants":{"n":3}}
with bindings
{"?<n":10}
should return
[{"?<n":10,"?n":3}]
The pattern
{"needs":"?n","wants":{"n":"?<n"}}
matched against
{"needs":4,"wants":{"n":3}}
with bindings
{"?<n":10}
should return
[]
The pattern
{"opt":"??maybe","wants":"?wanted"}
matched against
{"wants":"tacos"}
with bindings
{}
should return
[{"?wanted":"tacos"}]
The pattern
{"a":"??maybe","wants":"?wanted"}
matched against
{"a":"queso","wants":"tacos"}
with bindings
{}
should return
[{"??maybe":"queso","?wanted":"tacos"}]
The pattern
["??opt"]
matched against
[]
with bindings
{}
should return
[{}]
The pattern
["??opt","a","b"]
matched against
["a","b"]
with bindings
{}
should return
[{}]
The pattern
["??opt","a","b"]
matched against
["a","b","c"]
with bindings
{}
should return
[{"??opt":"c"}]
The pattern
["??opt","a","b"]
matched against
["a","b","c","d"]
with bindings
{}
should return
[{"??opt":"c"},{"??opt":"d"}]