MCPcopy Index your code
hub / github.com/expr-lang/expr / TestOptimize

Function TestOptimize

optimizer/optimizer_test.go:19–92  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

17)
18
19func TestOptimize(t *testing.T) {
20 env := map[string]any{
21 "a": 1,
22 "b": 2,
23 "c": 3,
24 }
25
26 tests := []struct {
27 expr string
28 want any
29 }{
30 {`1 + 2`, 3},
31 {`sum([])`, 0},
32 {`sum([a])`, 1},
33 {`sum([a, b])`, 3},
34 {`sum([a, b, c])`, 6},
35 {`sum([a, b, c, 4])`, 10},
36 {`sum(1..10, # * 1000)`, 55000},
37 {`sum(map(1..10, # * 1000), # / 1000)`, float64(55)},
38 {`all(1..3, {# > 0}) && all(1..3, {# < 4})`, true},
39 {`all(1..3, {# > 2}) && all(1..3, {# < 4})`, false},
40 {`all(1..3, {# > 0}) && all(1..3, {# < 2})`, false},
41 {`all(1..3, {# > 2}) && all(1..3, {# < 2})`, false},
42 {`all(1..3, {# > 0}) || all(1..3, {# < 4})`, true},
43 {`all(1..3, {# > 0}) || all(1..3, {# != 2})`, true},
44 {`all(1..3, {# != 3}) || all(1..3, {# < 4})`, true},
45 {`all(1..3, {# != 3}) || all(1..3, {# != 2})`, false},
46 {`none(1..3, {# == 0})`, true},
47 {`none(1..3, {# == 0}) && none(1..3, {# == 4})`, true},
48 {`none(1..3, {# == 0}) && none(1..3, {# == 3})`, false},
49 {`none(1..3, {# == 1}) && none(1..3, {# == 4})`, false},
50 {`none(1..3, {# == 1}) && none(1..3, {# == 3})`, false},
51 {`none(1..3, {# == 0}) || none(1..3, {# == 4})`, true},
52 {`none(1..3, {# == 0}) || none(1..3, {# == 3})`, true},
53 {`none(1..3, {# == 1}) || none(1..3, {# == 4})`, true},
54 {`none(1..3, {# == 1}) || none(1..3, {# == 3})`, false},
55 {`any([1, 1, 0, 1], {# == 0})`, true},
56 {`any(1..3, {# == 1}) && any(1..3, {# == 2})`, true},
57 {`any(1..3, {# == 0}) && any(1..3, {# == 2})`, false},
58 {`any(1..3, {# == 1}) && any(1..3, {# == 4})`, false},
59 {`any(1..3, {# == 0}) && any(1..3, {# == 4})`, false},
60 {`any(1..3, {# == 1}) || any(1..3, {# == 2})`, true},
61 {`any(1..3, {# == 0}) || any(1..3, {# == 2})`, true},
62 {`any(1..3, {# == 1}) || any(1..3, {# == 4})`, true},
63 {`any(1..3, {# == 0}) || any(1..3, {# == 4})`, false},
64 {`one([1, 1, 0, 1], {# == 0}) and not one([1, 0, 0, 1], {# == 0})`, true},
65 {`one(1..3, {# == 1}) and one(1..3, {# == 2})`, true},
66 {`one(1..3, {# == 1 || # == 2}) and one(1..3, {# == 2})`, false},
67 {`one(1..3, {# == 1}) and one(1..3, {# == 2 || # == 3})`, false},
68 {`one(1..3, {# == 1 || # == 2}) and one(1..3, {# == 2 || # == 3})`, false},
69 {`one(1..3, {# == 1}) or one(1..3, {# == 2})`, true},
70 {`one(1..3, {# == 1 || # == 2}) or one(1..3, {# == 2})`, true},
71 {`one(1..3, {# == 1}) or one(1..3, {# == 2 || # == 3})`, true},
72 {`one(1..3, {# == 1 || # == 2}) or one(1..3, {# == 2 || # == 3})`, false},
73 }
74
75 for _, tt := range tests {
76 t.Run(tt.expr, func(t *testing.T) {

Callers

nothing calls this directly

Calls 7

CompileFunction · 0.92
EnvStruct · 0.92
NoErrorFunction · 0.92
RunFunction · 0.92
EqualFunction · 0.92
OptimizeFunction · 0.92
RunMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…