github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/builtins/core/index/index_test.go (about)

     1  package index_test
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	_ "github.com/lmorg/murex/builtins/core/io"
     8  	_ "github.com/lmorg/murex/builtins/types/json"
     9  	_ "github.com/lmorg/murex/builtins/types/jsonlines"
    10  	_ "github.com/lmorg/murex/lang/expressions"
    11  	"github.com/lmorg/murex/test"
    12  	"github.com/lmorg/murex/utils/json"
    13  )
    14  
    15  var table = [][]string{
    16  	{"a", "s", "l"},
    17  	{"21", "m", "london"},
    18  	{"32", "f", "spain"},
    19  	{"43", "m", "italy"},
    20  	{"54", "f", "france"},
    21  	{"65", "m", "london"},
    22  }
    23  var jTable = json.LazyLogging(table)
    24  
    25  var object = map[string]map[string][]int{
    26  	"london": {"m": {21, 65}},
    27  	"spain":  {"f": {32}},
    28  	"italy":  {"m": {43}},
    29  	"france": {"f": {54}},
    30  }
    31  var jObject = json.LazyLogging(object)
    32  
    33  func TestIndexObject(t *testing.T) {
    34  	tests := []test.MurexTest{
    35  		{
    36  			Block:  fmt.Sprintf(`tout json (%s) -> [spain] -> [f]`, jObject),
    37  			Stdout: "[32]\n",
    38  		},
    39  		{
    40  			Block:  fmt.Sprintf(`tout json (%s) -> [ [/spain/f] ]`, jObject),
    41  			Stdout: "[32]\n",
    42  		},
    43  		{
    44  			Block:  fmt.Sprintf(`tout json (%s) -> [spain] -> [f] -> [0]`, jObject),
    45  			Stdout: "32",
    46  		},
    47  		{
    48  			Block:  fmt.Sprintf(`tout json (%s) -> [ [/spain/f/0] ]`, jObject),
    49  			Stdout: "32",
    50  		},
    51  		{
    52  			Block:  fmt.Sprintf(`tout json (%s) -> [london] -> [m]`, jObject),
    53  			Stdout: "[21,65]\n",
    54  		},
    55  		{
    56  			Block:  fmt.Sprintf(`tout json (%s) -> [ [/london/m] ]`, jObject),
    57  			Stdout: "[21,65]\n",
    58  		},
    59  		{
    60  			Block:  fmt.Sprintf(`tout json (%s) -> [london] -> [m] -> [1]`, jObject),
    61  			Stdout: "65",
    62  		},
    63  		{
    64  			Block:  fmt.Sprintf(`tout json (%s) -> [ [/london/m/1] ]`, jObject),
    65  			Stdout: "65",
    66  		},
    67  	}
    68  
    69  	test.RunMurexTests(tests, t)
    70  }
    71  
    72  func TestIndexTable(t *testing.T) {
    73  	tests := []test.MurexTest{
    74  		{
    75  			Block:  fmt.Sprintf(`tout json (%s)->format jsonl->[1]`, jTable),
    76  			Stdout: `["21","m","london"]` + "\n",
    77  		},
    78  		{
    79  			Block:  fmt.Sprintf(`tout json (%s)->format jsonl->[l]`, jTable),
    80  			Stdout: "[\"l\"]\n[\"london\"]\n[\"spain\"]\n[\"italy\"]\n[\"france\"]\n[\"london\"]\n",
    81  		},
    82  		{
    83  			Block:  fmt.Sprintf(`tout json (%s)->format jsonl->[:2]`, jTable),
    84  			Stdout: "[\"l\"]\n[\"london\"]\n[\"spain\"]\n[\"italy\"]\n[\"france\"]\n[\"london\"]\n",
    85  		},
    86  		{
    87  			Block:  fmt.Sprintf(`tout json (%s)->format jsonl->[2:]`, jTable),
    88  			Stdout: "[\"32\",\"f\",\"spain\"]\n",
    89  		},
    90  		{
    91  			Block:  fmt.Sprintf(`tout json (%s)->format jsonl->[2]`, jTable),
    92  			Stdout: "[\"32\",\"f\",\"spain\"]\n",
    93  		},
    94  	}
    95  
    96  	test.RunMurexTests(tests, t)
    97  }