github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/transform/staticlark/unit_test.go (about)

     1  package staticlark
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/google/go-cmp/cmp"
     8  )
     9  
    10  func TestUnitCases(t *testing.T) {
    11  	cases := []struct {
    12  		desc string
    13  		unit unit
    14  		str  string
    15  		srcs []string
    16  		invs string
    17  	}{
    18  		{
    19  			"atomic identifer",
    20  			unit{
    21  				atom: "a",
    22  			},
    23  			"a",
    24  			[]string{},
    25  			`[]`,
    26  		},
    27  		{
    28  			"assign literal to var",
    29  			unit{
    30  				atom: "set!",
    31  				tail: []*unit{
    32  					{atom: "a"},
    33  					{atom: "0"},
    34  				},
    35  			},
    36  			"[set! a 0]",
    37  			[]string{},
    38  			`[]`,
    39  		},
    40  		{
    41  			"var assigned another var",
    42  			unit{
    43  				atom: "set!",
    44  				tail: []*unit{
    45  					{atom: "a"},
    46  					{atom: "b"},
    47  				},
    48  			},
    49  			"[set! a b]",
    50  			[]string{"b"},
    51  			`[]`,
    52  		},
    53  		{
    54  			"increment a var",
    55  			unit{
    56  				atom: "set!",
    57  				tail: []*unit{
    58  					{atom: "a"},
    59  					{
    60  						atom: "+=",
    61  						tail: []*unit{
    62  							{atom: "a"},
    63  							{atom: "1"},
    64  						},
    65  					},
    66  				},
    67  			},
    68  			"[set! a [+= a 1]]",
    69  			[]string{"a"},
    70  			`[]`,
    71  		},
    72  		{
    73  			"add two vars and assign",
    74  			unit{
    75  				atom: "set!",
    76  				tail: []*unit{
    77  					{atom: "a"},
    78  					{
    79  						atom: "+",
    80  						tail: []*unit{
    81  							{atom: "a"},
    82  							{atom: "b"},
    83  						},
    84  					},
    85  				},
    86  			},
    87  			"[set! a [+ a b]]",
    88  			[]string{"a", "b"},
    89  			`[]`,
    90  		},
    91  		{
    92  			"assign var from function call",
    93  			unit{
    94  				atom: "set!",
    95  				tail: []*unit{
    96  					{atom: "a"},
    97  					{
    98  						atom: "add",
    99  						tail: []*unit{
   100  							{atom: "b"},
   101  							{atom: "c"},
   102  						},
   103  					},
   104  				},
   105  			},
   106  			"[set! a [add b c]]",
   107  			[]string{"b", "c"},
   108  			`[{"name":"add","args":["b","c"]}]`,
   109  		},
   110  		{
   111  			"just function call",
   112  			unit{
   113  				atom: "upload",
   114  				tail: []*unit{
   115  					{atom: "a"},
   116  					{atom: "b"},
   117  					{atom: "c"},
   118  				},
   119  			},
   120  			"[upload a b c]",
   121  			[]string{"a", "b", "c"},
   122  			`[{"name":"upload","args":["a","b","c"]}]`,
   123  		},
   124  		{
   125  			"nested function calls",
   126  			unit{
   127  				atom: "upload",
   128  				tail: []*unit{
   129  					{atom: "a"},
   130  					{atom: "b"},
   131  					{
   132  						atom: "lookup",
   133  						tail: []*unit{
   134  							{atom: "a"},
   135  							{atom: "c"},
   136  						},
   137  					},
   138  				},
   139  			},
   140  			"[upload a b [lookup a c]]",
   141  			[]string{"a", "b", "a", "c"},
   142  			`[{"name":"upload","args":["a","b","a","c"]},{"name":"lookup","args":["a","c"]}]`,
   143  		},
   144  		{
   145  			"return a value",
   146  			unit{
   147  				atom: "return",
   148  				tail: []*unit{
   149  					{atom: "a"},
   150  				},
   151  			},
   152  			"[return a]",
   153  			[]string{"a"},
   154  			`[]`,
   155  		},
   156  		{
   157  			"return an expression",
   158  			unit{
   159  				atom: "return",
   160  				tail: []*unit{
   161  					{
   162  						atom: "add",
   163  						tail: []*unit{
   164  							{atom: "a"},
   165  							{atom: "b"},
   166  						},
   167  					},
   168  				},
   169  			},
   170  			"[return [add a b]]",
   171  			[]string{"a", "b"},
   172  			`[{"name":"add","args":["a","b"]}]`,
   173  		},
   174  	}
   175  
   176  	for _, c := range cases {
   177  		t.Run(c.desc, func(t *testing.T) {
   178  			if diff := cmp.Diff(c.str, c.unit.String()); diff != "" {
   179  				t.Errorf("String(), mismatch (-want +got):\n%s", diff)
   180  			}
   181  			if diff := cmp.Diff(c.srcs, c.unit.DataSources()); diff != "" {
   182  				t.Errorf("DataSources(), mismatch (-want +got):\n%s", diff)
   183  			}
   184  			invs := c.unit.Invocations()
   185  			data, err := json.Marshal(invs)
   186  			if err != nil {
   187  				t.Fatal(err)
   188  			}
   189  			actual := string(data)
   190  			if diff := cmp.Diff(c.invs, actual); diff != "" {
   191  				t.Errorf("Invocations(), mismatch (-want +got):\n%s", diff)
   192  			}
   193  		})
   194  	}
   195  }