MCPcopy
hub / github.com/expr-lang/expr / TestBuiltin_with_deref

Function TestBuiltin_with_deref

builtin/builtin_test.go:698–774  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

696}
697
698func TestBuiltin_with_deref(t *testing.T) {
699 x := 42
700 arr := []int{1, 2, 3}
701 arrStr := []string{"1", "2", "3"}
702 m := map[string]any{"a": 1, "b": 2}
703 jsonString := `["1"]`
704 str := "1,2,3"
705 env := map[string]any{
706 "x": &x,
707 "arr": &arr,
708 "arrStr": &arrStr,
709 "m": &m,
710 "json": &jsonString,
711 "str": &str,
712 }
713
714 tests := []struct {
715 input string
716 want any
717 }{
718 {`all(arr, # > 0)`, true},
719 {`none(arr, # < 0)`, true},
720 {`any(arr, # > 0)`, true},
721 {`one(arr, # > 2)`, true},
722 {`filter(arr, # > 0)`, []any{1, 2, 3}},
723 {`map(arr, # * #)`, []any{1, 4, 9}},
724 {`count(arr, # > 0)`, 3},
725 {`sum(arr)`, 6},
726 {`find(arr, # > 0)`, 1},
727 {`findIndex(arr, # > 1)`, 1},
728 {`findLast(arr, # > 0)`, 3},
729 {`findLastIndex(arr, # > 0)`, 2},
730 {`groupBy(arr, # % 2 == 0)`, map[any][]any{false: {1, 3}, true: {2}}},
731 {`sortBy(arr, -#)`, []any{3, 2, 1}},
732 {`reduce(arr, # + #acc, x)`, 6 + 42},
733 {`ceil(x)`, 42.0},
734 {`floor(x)`, 42.0},
735 {`round(x)`, 42.0},
736 {`int(x)`, 42},
737 {`float(x)`, 42.0},
738 {`abs(x)`, 42},
739 {`first(arr)`, 1},
740 {`last(arr)`, 3},
741 {`take(arr, 1)`, []int{1}},
742 {`take(arr, x)`, []int{1, 2, 3}},
743 {`'a' in keys(m)`, true},
744 {`1 in values(m)`, true},
745 {`len(arr)`, 3},
746 {`type(arr)`, "array"},
747 {`type(m)`, "map"},
748 {`reverse(arr)`, []any{3, 2, 1}},
749 {`uniq(arr)`, []any{1, 2, 3}},
750 {`concat(arr, arr)`, []any{1, 2, 3, 1, 2, 3}},
751 {`flatten([arr, [arr]])`, []any{1, 2, 3, 1, 2, 3}},
752 {`flatten(arr)`, []any{1, 2, 3}},
753 {`toJSON(arr)`, "[\n 1,\n 2,\n 3\n]"},
754 {`fromJSON(json)`, []any{"1"}},
755 {`split(str, ",")`, []string{"1", "2", "3"}},

Callers

nothing calls this directly

Calls 8

CompileFunction · 0.92
EnvStruct · 0.92
NoErrorFunction · 0.92
RunFunction · 0.92
EqualFunction · 0.92
EvalFunction · 0.92
RunMethod · 0.80
DisassembleMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…