github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/funcs_b_test.go (about)

     1  // Unless explicitly stated otherwise all files in this repository are licensed
     2  // under the MIT License.
     3  // This product includes software developed at Guance Cloud (https://www.guance.com/).
     4  // Copyright 2021-present Guance, Inc.
     5  
     6  package funcs
     7  
     8  /*
     9  import (
    10  	"strconv"
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/stretchr/testify/assert"
    15  	tu "github.com/GuanceCloud/cliutils/testutil"
    16  )
    17  
    18  type funcCase struct {
    19  	name     string
    20  	data     string
    21  	script   string
    22  	expected interface{}
    23  	key      string
    24  	fail     bool
    25  }
    26  
    27  type EscapeError string
    28  
    29  func (e EscapeError) Error() string {
    30  	return "invalid URL escape " + strconv.Quote(string(e))
    31  }
    32  
    33  func TestJsonFunc(t *testing.T) {
    34  	testCase := []*funcCase{
    35  		{
    36  			data: `{
    37  			  "name": {"first": "Tom", "last": "Anderson"},
    38  			  "age":37,
    39  			  "children": ["Sara","Alex","Jack"],
    40  			  "fav.movie": "Deer Hunter",
    41  			  "friends": [
    42  			    {"first": "Dale", "last": "Murphy", "age": 44, "nets": ["ig", "fb", "tw"]},
    43  			    {"first": "Roger", "last": "Craig", "age": 68, "nets": ["fb", "tw"]},
    44  			    {"first": "Jane", "last": "Murphy", "age": 47, "nets": ["ig", "tw"]}
    45  			  ]
    46  			}`,
    47  			script:   `json(_, name) json(name, first)`,
    48  			expected: "Tom",
    49  			key:      "first",
    50  		},
    51  		{
    52  			data: `[
    53  				    {"first": "Dale", "last": "Murphy", "age": 44, "nets": ["ig", "fb", "tw"]},
    54  				    {"first": "Roger", "last": "Craig", "age": 68, "nets": ["fb", "tw"]},
    55  				    {"first": "Jane", "last": "Murphy", "age": 47, "nets": ["ig", "tw"]}
    56  				]`,
    57  			script:   `json(_, [0].nets[-1])`,
    58  			expected: "tw",
    59  			key:      "[0].nets[-1]",
    60  		},
    61  	}
    62  
    63  	for _, tt := range testCase {
    64  		p, err := NewPipeline(tt.script)
    65  
    66  		assert.Equal(t, err, nil)
    67  
    68  		p.Run(tt.data)
    69  
    70  		r, err := p.getContentStr(tt.key)
    71  
    72  		assertEqual(t, err, nil)
    73  
    74  		assert.Equal(t, r, tt.expected)
    75  	}
    76  }
    77  
    78  func TestDefaultTimeFunc(t *testing.T) {
    79  	testCase := []*funcCase{
    80  		{
    81  			data:     `{"a":{"time":"14 May 2019 19:11:40.164","second":2,"third":"abc","forth":true},"age":47}`,
    82  			script:   `json(_, a.time) default_time(a.time)`,
    83  			expected: int64(1557832300164000000),
    84  			key:      "a.time",
    85  		},
    86  		{
    87  			data:     `{"a":{"time":"06/Jan/2017:16:16:37 +0000","second":2,"third":"abc","forth":true},"age":47}`,
    88  			script:   `json(_, a.time) default_time(a.time)`,
    89  			expected: int64(1483719397000000000),
    90  			key:      "a.time",
    91  		},
    92  		{
    93  			data:     `{"a":{"time":"2014-12-16 06:20:00 UTC","second":2,"third":"abc","forth":true},"age":47}`,
    94  			script:   `json(_, a.time) default_time(a.time)`,
    95  			expected: int64(1418682000000000000),
    96  			key:      "a.time",
    97  		},
    98  
    99  		/*
   100  			{ // fail
   101  				data:     `{"a":{"time":"171113 14:14:20","second":2,"third":"abc","forth":true},"age":47}`,
   102  				script:   `json(_, a.time) default_time(a.time)`,
   103  				expected: int64(1510582460000000000),
   104  				key:      "a.time",
   105  			}, */
   106  
   107  /* fail
   108  {
   109  	data:     `{"str":"2021/02/27 - 08:11:46"}`,
   110  	script:   `json(_, str) default_time(str)`,
   111  	expected: int64(1614413506000000000),
   112  	key:      "str",
   113  }, */
   114  
   115  /*
   116  	{
   117  		data:     `{"a":{"time":"2021-03-15 13:50:47,000"}}`,
   118  		script:   `json(_, a.time) default_time(a.time)`,
   119  		expected: int64(1615787447000000000),
   120  		key:      "a.time",
   121  	},
   122  
   123  	{
   124  		// data:     `{"a":{"time":"2021-03-15 13:50:47,000 UTC"}}`,
   125  		data:     `{"a":{"time":"2021-03-15 13:50:47,000"}}`,
   126  		script:   `json(_, a.time) default_time(a.time)`,
   127  		expected: int64(1615787447000000000),
   128  		key:      "a.time",
   129  	},
   130  
   131  	/* fail
   132  	{
   133  		data:     `{"a":{"time":"15 Mar 06:20:12.000"}}`,
   134  		script:   `json(_, a.time) default_time(a.time)`,
   135  		expected: int64(time.Second * 1615789212),
   136  		key:      "a.time",
   137  	}, */
   138  
   139  /*
   140  		{
   141  			data:     `{"a":{"time":"Wed Mar 17 15:40:49 CST 2021"}}`,
   142  			script:   `json(_, a.time) default_time(a.time)`,
   143  			expected: int64(time.Second * 1615966849),
   144  			key:      "a.time",
   145  		},
   146  		{
   147  			data:     `{"a":{"time":"Wed Mar 17 15:40:49 CST 2021"}}`,
   148  			script:   `json(_, a.time) default_time(a.time, "Asia/Shanghai")`,
   149  			expected: int64(time.Second * 1615966849),
   150  			key:      "a.time",
   151  		},
   152  	}
   153  
   154  	for _, tt := range testCase {
   155  		t.Run(tt.name, func(t *testing.T) {
   156  			p, err := NewPipeline(tt.script)
   157  
   158  			tu.Equals(t, err, nil)
   159  
   160  			p.Run(tt.data)
   161  
   162  			r, err := p.getContent(tt.key)
   163  			tu.Equals(t, err, nil)
   164  
   165  			tu.Equals(t, r, tt.expected)
   166  
   167  			// t.Logf("[passed? %v]out: %s <> %v", ok, tt.data, p.Output)
   168  		})
   169  	}
   170  }
   171  
   172  func TestUrlencodeFunc(t *testing.T) {
   173  	testCase := []*funcCase{
   174  		{
   175  			data:     `{"url[0]":"+%3F%26%3D%23%2B%25%21%3C%3E%23%22%7B%7D%7C%5C%5E%5B%5D%60%E2%98%BA%09%3A%2F%40%24%27%28%29%2A%2C%3B","second":2}`,
   176  			script:   "json(_, `url[0]`) url_decode(`url[0]`)",
   177  			expected: " ?&=#+%!<>#\"{}|\\^[]`☺\t:/@$'()*,;",
   178  			key:      "url[0]",
   179  		},
   180  		{
   181  			data:     `{"url":"http%3a%2f%2fwww.baidu.com%2fs%3fwd%3d%e6%b5%8b%e8%af%95","second":2}`,
   182  			script:   `json(_, url) url_decode(url)`,
   183  			expected: `http://www.baidu.com/s?wd=测试`,
   184  			key:      "url",
   185  		},
   186  		{
   187  			data:     `{"url":"","second":2}`,
   188  			script:   `json(_, url) url_decode(url)`,
   189  			expected: ``,
   190  			key:      "url",
   191  		},
   192  		{
   193  			data:     `{"url":"+%3F%26%3D%23%2B%25%21%3C%3E%23%22%7B%7D%7C%5C%5E%5B%5D%60%E2%98%BA%09%3A%2F%40%24%27%28%29%2A%2C%3B","second":2}`,
   194  			script:   `json(_, url) url_decode(url)`,
   195  			expected: " ?&=#+%!<>#\"{}|\\^[]`☺\t:/@$'()*,;",
   196  			key:      "url",
   197  		},
   198  		{
   199  			data:     `{"url":"+%3F%26%3D%23%2B%25%21%3C%3E%23%22%7B%7D%7C%5C%5E%5B%5D%60%E2%98%BA%09%3A%2F%40%24%27%28%29%2A%2C%3B","second":2}`,
   200  			script:   `json(_, url) url_decode("url", "aaa")`,
   201  			expected: "+%3F%26%3D%23%2B%25%21%3C%3E%23%22%7B%7D%7C%5C%5E%5B%5D%60%E2%98%BA%09%3A%2F%40%24%27%28%29%2A%2C%3B",
   202  			key:      "url",
   203  		},
   204  		{
   205  			data:     `{"aa":{"url":"http%3a%2f%2fwww.baidu.com%2fs%3fwd%3d%e6%b5%8b%e8%af%95"},"second":2}`,
   206  			script:   `json(_, aa.url) url_decode(aa.url)`,
   207  			expected: `http://www.baidu.com/s?wd=测试`,
   208  			key:      "aa.url",
   209  		},
   210  		{
   211  			data:     `{"aa":{"aa.url":"http%3a%2f%2fwww.baidu.com%2fs%3fwd%3d%e6%b5%8b%e8%af%95"},"second":2}`,
   212  			script:   "json(_, aa.`aa.url`) url_decode(aa.`aa.url`)",
   213  			expected: `http://www.baidu.com/s?wd=测试`,
   214  			key:      "aa.aa.url",
   215  		},
   216  	}
   217  
   218  	for _, tt := range testCase {
   219  		p, err := NewPipeline(tt.script)
   220  
   221  		assertEqual(t, err, nil)
   222  
   223  		p.Run(tt.data)
   224  
   225  		r, err := p.getContentStr(tt.key)
   226  		assertEqual(t, err, nil)
   227  
   228  		assertEqual(t, r, tt.expected)
   229  	}
   230  }
   231  
   232  func TestUserAgentFunc(t *testing.T) {
   233  	testCase := []*funcCase{
   234  		{
   235  			data:     `{"userAgent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36", "second":2,"third":"abc","forth":true}`,
   236  			script:   `json(_, userAgent) user_agent(userAgent)`,
   237  			expected: "Windows 7",
   238  			key:      "os",
   239  		},
   240  		{
   241  			data:     `{"userAgent":"Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"}`,
   242  			script:   `json(_, userAgent) user_agent(userAgent)`,
   243  			expected: "Googlebot",
   244  			key:      "browser",
   245  		},
   246  		{
   247  			data:     `{"userAgent":"Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25 (compatible; Googlebot/2.1; +http://www.google.com/bot.html"}`,
   248  			script:   `json(_, userAgent) user_agent(userAgent)`,
   249  			expected: "",
   250  			key:      "engine",
   251  		},
   252  		{
   253  			data:     `{"userAgent":"Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)"}`,
   254  			script:   `json(_, userAgent) user_agent(userAgent)`,
   255  			expected: "bingbot",
   256  			key:      "browser",
   257  		},
   258  	}
   259  
   260  	for _, tt := range testCase {
   261  		p, err := NewPipeline(tt.script)
   262  		assertEqual(t, err, p.lastErr)
   263  
   264  		p.Run(tt.data)
   265  
   266  		r, err := p.getContentStr(tt.key)
   267  		assertEqual(t, err, nil)
   268  
   269  		assertEqual(t, r, tt.expected)
   270  	}
   271  }
   272  
   273  func TestDatetimeFunc(t *testing.T) {
   274  	testCase := []*funcCase{
   275  		{
   276  			data:     `{"a":{"timestamp": "1610960605", "second":2},"age":47}`,
   277  			script:   `json(_, a.timestamp) datetime(a.timestamp, 's', 'RFC3339')`,
   278  			expected: "2021-01-18T17:03:25+08:00",
   279  			key:      "a.timestamp",
   280  		},
   281  		{
   282  			data:     `{"a":{"timestamp": "1610960605000", "second":2},"age":47}`,
   283  			script:   `json(_, a.timestamp) datetime(a.timestamp, 'ms', 'RFC3339')`,
   284  			expected: "2021-01-18T17:03:25+08:00",
   285  			key:      "a.timestamp",
   286  		},
   287  	}
   288  
   289  	for _, tt := range testCase {
   290  		p, err := NewPipeline(tt.script)
   291  		assertEqual(t, err, p.lastErr)
   292  
   293  		p.Run(tt.data)
   294  
   295  		r, err := p.getContentStr(tt.key)
   296  		assertEqual(t, err, nil)
   297  
   298  		assertEqual(t, r, tt.expected)
   299  	}
   300  }
   301  
   302  func TestGroupFunc(t *testing.T) {
   303  	testCase := []*funcCase{
   304  		{
   305  			data:     `{"status": 200,"age":47}`,
   306  			script:   `json(_, status) group_between(status, [200, 400], false, newkey)`,
   307  			expected: false,
   308  			key:      "newkey",
   309  		},
   310  		{
   311  			data:     `{"status": 200,"age":47}`,
   312  			script:   `json(_, status) group_between(status, [200, 400], 10, newkey)`,
   313  			expected: int64(10),
   314  			key:      "newkey",
   315  		},
   316  		{
   317  			data:     `{"status": 200,"age":47}`,
   318  			script:   `json(_, status) group_between(status, [, 400], "ok", newkey)`,
   319  			expected: nil,
   320  			key:      "newkey",
   321  		},
   322  		{
   323  			data:     `{"status": 200,"age":47}`,
   324  			script:   `json(_, status) group_between(status, [200, 400], "ok", newkey)`,
   325  			expected: "ok",
   326  			key:      "newkey",
   327  		},
   328  		{
   329  			data:     `{"status": 200,"age":47}`,
   330  			script:   `json(_, status) group_between(status, [200, 299], "ok")`,
   331  			expected: "ok",
   332  			key:      "status",
   333  		},
   334  		{
   335  			data:     `{"status": 200,"age":47}`,
   336  			script:   `json(_, status) group_between(status, [299, 200], "ok")`,
   337  			expected: float64(200),
   338  			key:      "status",
   339  		},
   340  		{
   341  			data:     `{"status": 200,"age":47}`,
   342  			script:   `json(_, status) group_between(status, [299, 200], "ok", newkey)`,
   343  			expected: float64(200),
   344  			key:      "status",
   345  		},
   346  		{
   347  			data:     `{"status": 200,"age":47}`,
   348  			script:   `json(_, status) group_between(status, [200, 299], "ok", newkey)`,
   349  			expected: "ok",
   350  			key:      "newkey",
   351  		},
   352  		{
   353  			data:     `{"status": 200,"age":47}`,
   354  			script:   `json(_, status) group_between(status, [300, 400], "ok", newkey)`,
   355  			expected: nil,
   356  			key:      "newkey",
   357  		},
   358  	}
   359  
   360  	for _, tt := range testCase {
   361  		t.Run(tt.name, func(t *testing.T) {
   362  			p, err := NewPipeline(tt.script)
   363  			tu.Equals(t, p.lastErr, err)
   364  
   365  			p.Run(tt.data)
   366  
   367  			r, err := p.getContent(tt.key)
   368  			if tt.expected != nil {
   369  				tu.Equals(t, nil, err)
   370  			}
   371  
   372  			tu.Equals(t, tt.expected, r)
   373  		})
   374  	}
   375  }
   376  
   377  func TestGroupInFunc(t *testing.T) {
   378  	testCase := []*funcCase{
   379  		{
   380  			data:     `{"status": true,"age":"47"}`,
   381  			script:   `json(_, status) group_in(status, [true], "ok", "newkey")`,
   382  			expected: "ok",
   383  			key:      "newkey",
   384  		},
   385  		{
   386  			data:     `{"status": true,"age":"47"}`,
   387  			script:   `json(_, status) group_in(status, [true], "ok", "newkey")`,
   388  			expected: "ok",
   389  			key:      "newkey",
   390  		},
   391  		{
   392  			data:     `{"status": "aa","age":"47"}`,
   393  			script:   `json(_, status) group_in(status, [], "ok")`,
   394  			expected: "aa",
   395  			key:      "status",
   396  		},
   397  		{
   398  			data:     `{"status": "aa","age":"47"}`,
   399  			script:   `json(_, status) group_in(status, ["aa"], "ok", "newkey")`,
   400  			expected: "ok",
   401  			key:      "newkey",
   402  		},
   403  		{
   404  			data:     `{"status": "test","age":"47"}`,
   405  			script:   `json(_, status) group_in(status, [200, 47, "test"], "ok", newkey)`,
   406  			expected: "ok",
   407  			key:      "newkey",
   408  		},
   409  		{
   410  			data:     `{"status": "test","age":"47"}`,
   411  			script:   `json(_, status) group_in(status, [200, "test"], "ok")`,
   412  			expected: "ok",
   413  			key:      "status",
   414  		},
   415  	}
   416  
   417  	for _, tt := range testCase {
   418  		p, err := NewPipeline(tt.script)
   419  		assertEqual(t, err, p.lastErr)
   420  
   421  		p.Run(tt.data)
   422  
   423  		r, err := p.getContentStr(tt.key)
   424  		assertEqual(t, err, nil)
   425  
   426  		assertEqual(t, r, tt.expected)
   427  	}
   428  }
   429  
   430  func TestNullIfFunc(t *testing.T) {
   431  	testCase := []*funcCase{
   432  		{
   433  			name:     "",
   434  			data:     `{"a":{"first": 1,"second":2,"third":"aBC","forth":true},"age":47}`,
   435  			script:   `json(_, a.first) nullif(a.first, "1")`,
   436  			expected: float64(1),
   437  			key:      "a.first",
   438  		},
   439  		{
   440  			name:     "",
   441  			data:     `{"a":{"first": "1","second":2,"third":"aBC","forth":true},"age":47}`,
   442  			script:   `json(_, a.first) nullif(a.first, 1)`,
   443  			expected: "1",
   444  			key:      "a.first",
   445  		},
   446  		{
   447  			name:     "",
   448  			data:     `{"a":{"first": "","second":2,"third":"aBC","forth":true},"age":47}`,
   449  			script:   `json(_, a.first) nullif(a.first, "")`,
   450  			expected: nil,
   451  			key:      "a.first",
   452  		},
   453  		{
   454  			name:     "",
   455  			data:     `{"a":{"first": null,"second":2,"third":"aBC","forth":true},"age":47}`,
   456  			script:   `json(_, a.first) nullif(a.first, nil)`,
   457  			expected: nil,
   458  			key:      "a.first",
   459  		},
   460  		{
   461  			name:     "",
   462  			data:     `{"a":{"first": true,"second":2,"third":"aBC","forth":true},"age":47}`,
   463  			script:   `json(_, a.first) nullif(a.first, true)`,
   464  			expected: nil,
   465  			key:      "a.first",
   466  		},
   467  		{
   468  			name:     "",
   469  			data:     `{"a":{"first": 2.3, "second":2,"third":"aBC","forth":true},"age":47}`,
   470  			script:   `json(_, a.first) nullif(a.first, 2.3)`,
   471  			expected: nil,
   472  			key:      "a.first",
   473  		},
   474  		{
   475  			name:     "",
   476  			data:     `{"a":{"first": 2,"second":2,"third":"aBC","forth":true},"age":47}`,
   477  			script:   `json(_, a.first) nullif(a.first, 2, "newkey")`,
   478  			expected: nil,
   479  			key:      "newkey",
   480  		},
   481  		{
   482  			name:     "",
   483  			data:     `{"a":{"first":"2.3","second":2,"third":"aBC","forth":true},"age":47}`,
   484  			script:   `json(_, a.first) nullif(a.first, "2.3", "newkey")`,
   485  			expected: nil,
   486  			key:      "newkey",
   487  		},
   488  	}
   489  
   490  	for _, tt := range testCase {
   491  		t.Run(tt.name, func(t *testing.T) {
   492  			p, err := NewPipeline(tt.script)
   493  			assertEqual(t, err, p.lastErr)
   494  
   495  			p.Run(tt.data)
   496  
   497  			r, err := p.getContent(tt.key)
   498  			if tt.expected != nil {
   499  				assertEqual(t, err, nil)
   500  			}
   501  
   502  			assertEqual(t, r, tt.expected)
   503  		})
   504  	}
   505  }
   506  
   507  func TestParseDuration(t *testing.T) {
   508  	cases := []*funcCase{
   509  		{
   510  			data:     `{"str": "1s"}`,
   511  			script:   `json(_, str) parse_duration(str)`,
   512  			expected: int64(time.Second),
   513  			key:      "str",
   514  		},
   515  
   516  		{
   517  			data:     `{"str": "1ms"}`,
   518  			script:   `json(_, str) parse_duration(str)`,
   519  			expected: int64(time.Millisecond),
   520  			key:      "str",
   521  		},
   522  
   523  		{
   524  			data:     `{"str": "1us"}`,
   525  			script:   `json(_, str) parse_duration(str)`,
   526  			expected: int64(time.Microsecond),
   527  			key:      "str",
   528  		},
   529  
   530  		{
   531  			data:     `{"str": "1µs"}`,
   532  			script:   `json(_, str) parse_duration(str)`,
   533  			expected: int64(time.Microsecond),
   534  			key:      "str",
   535  		},
   536  
   537  		{
   538  			data:     `{"str": "1m"}`,
   539  			script:   `json(_, str) parse_duration(str)`,
   540  			expected: int64(time.Minute),
   541  			key:      "str",
   542  		},
   543  
   544  		{
   545  			data:     `{"str": "1h"}`,
   546  			script:   `json(_, str) parse_duration(str)`,
   547  			expected: int64(time.Hour),
   548  			key:      "str",
   549  		},
   550  
   551  		{
   552  			data:     `{"str": "-23h"}`,
   553  			script:   `json(_, str) parse_duration(str)`,
   554  			expected: -23 * int64(time.Hour),
   555  			key:      "str",
   556  		},
   557  
   558  		{
   559  			data:     `{"str": "-23ns"}`,
   560  			script:   `json(_, str) parse_duration(str)`,
   561  			expected: int64(-23),
   562  			key:      "str",
   563  		},
   564  
   565  		{
   566  			data:     `{"str": "-2.3s"}`,
   567  			script:   `json(_, str) parse_duration(str)`,
   568  			expected: int64(time.Second*-2 - 300*time.Millisecond),
   569  			key:      "str",
   570  		},
   571  
   572  		{
   573  			data:   `{"str": "1uuus"}`,
   574  			script: `json(_, str) parse_duration(str)`,
   575  			key:    "str",
   576  			fail:   true,
   577  		},
   578  	}
   579  
   580  	for _, tt := range cases {
   581  		p, err := NewPipeline(tt.script)
   582  		assertEqual(t, err, p.lastErr)
   583  
   584  		p.Run(tt.data)
   585  
   586  		r, err := p.getContent(tt.key)
   587  		assertEqual(t, err, nil)
   588  
   589  		if !tt.fail {
   590  			assertEqual(t, r, tt.expected)
   591  		}
   592  	}
   593  }
   594  
   595  func TestJsonAllFunc(t *testing.T) {
   596  	testCase := []*funcCase{
   597  		{
   598  			data: `{
   599  			  "name": {"first": "Tom", "last": "Anderson"},
   600  			  "age":37,
   601  			  "children": ["Sara","Alex","Jack"],
   602  			  "fav.movie": "Deer Hunter",
   603  			  "friends": [
   604  			    {"first": "Dale", "last": "Murphy", "age": 44, "nets": ["ig", "fb", "tw"]},
   605  			    {"first": "Roger", "last": "Craig", "age": 68, "nets": ["fb", "tw"]},
   606  			    {"first": "Jane", "last": "Murphy", "age": 47, "nets": ["ig", "tw"]}
   607  			  ]
   608  			}`,
   609  			script:   `json_all()`,
   610  			expected: "Sara",
   611  			key:      "children[0]",
   612  		},
   613  		{
   614  			data: `[
   615  			    {"first": "Dale", "last": "Murphy", "age": 44, "nets": ["ig", "fb", "tw"]},
   616  			    {"first": "Roger", "last": "Craig", "age": 68, "nets": ["fb", "tw"]},
   617  			    {"first": "Jane", "last": "Murphy", "age": 47, "nets": ["ig", "tw"]}
   618  			  ]`,
   619  			script:   `json_all()`,
   620  			expected: "Dale",
   621  			key:      "[0].first",
   622  		},
   623  	}
   624  
   625  	for _, tt := range testCase {
   626  		p, err := NewPipeline(tt.script)
   627  		assertEqual(t, err, p.lastErr)
   628  
   629  		p.Run(tt.data)
   630  
   631  		r, err := p.getContentStr(tt.key)
   632  		assertEqual(t, err, nil)
   633  
   634  		assertEqual(t, r, tt.expected)
   635  	}
   636  
   637  	js := `
   638  {
   639    "name": {"first": "Tom", "last": "Anderson"},
   640    "age":37,
   641    "children": ["Sara","Alex","Jack"],
   642    "fav.movie": "Deer Hunter",
   643    "friends": [
   644      {"first": "Dale", "last": "Murphy", "age": 44, "nets": ["ig", "fb", "tw"]},
   645      {"first": "Roger", "last": "Craig", "age": 68, "nets": ["fb", "tw"]},
   646      {"first": "Jane", "last": "Murphy", "age": 47, "nets": ["ig", "tw"]}
   647    ]
   648  }
   649  `
   650  	script := `json_all()`
   651  
   652  	p, err := NewPipeline(script)
   653  	assertEqual(t, err, nil)
   654  
   655  	p.Run(js)
   656  
   657  	r, err := p.getContent("children[0]")
   658  	assertEqual(t, err, nil)
   659  
   660  	assertEqual(t, r, "Sara")
   661  } */
   662  
   663  /*
   664  func TestDz(t *testing.T) {
   665  	cases := []*funcCase{
   666  		{
   667  			data:     `{"str": "13838130517"}`,
   668  			script:   `json(_, str) cover(str, [8, 13])`,
   669  			expected: "1383813****",
   670  			key:      "str",
   671  		},
   672  		{
   673  			data:     `{"str": "13838130517"}`,
   674  			script:   `json(_, str) cover(str, [8, 11])`,
   675  			expected: "1383813****",
   676  			key:      "str",
   677  		},
   678  		{
   679  			data:     `{"str": "13838130517"}`,
   680  			script:   `json(_, str) cover(str, [2, 4])`,
   681  			expected: "1***8130517",
   682  			key:      "str",
   683  		},
   684  		{
   685  			data:     `{"str": "13838130517"}`,
   686  			script:   `json(_, str) cover(str, [0, 3])`,
   687  			expected: "***38130517",
   688  			key:      "str",
   689  		},
   690  		{
   691  			data:     `{"str": "13838130517"}`,
   692  			script:   `json(_, str) cover(str, [1, 1])`,
   693  			expected: "*3838130517",
   694  			key:      "str",
   695  		},
   696  		{
   697  			data:     `{"str": "刘少波"}`,
   698  			script:   `json(_, str) cover(str, [2, 2])`,
   699  			expected: "刘*波",
   700  			key:      "str",
   701  		},
   702  	}
   703  
   704  	for _, tt := range cases {
   705  		p, err := NewPipeline(tt.script)
   706  		assertEqual(t, err, p.lastErr)
   707  
   708  		p.Run(tt.data)
   709  		r, err := p.getContentStr(tt.key)
   710  		assertEqual(t, err, nil)
   711  
   712  		if !tt.fail {
   713  			assertEqual(t, r, tt.expected)
   714  		}
   715  	}
   716  }
   717  
   718  func TestReplace(t *testing.T) {
   719  	cases := []*funcCase{
   720  		{
   721  			data:     `{"str": "13789123014"}`,
   722  			script:   `json(_, str) replace(str, "(1[0-9]{2})[0-9]{4}([0-9]{4})", "$1****$2")`,
   723  			expected: "137****3014",
   724  			key:      "str",
   725  		},
   726  		{
   727  			data:     `{"str": "zhang san"}`,
   728  			script:   `json(_, str) replace(str, "([a-z]*) \\w*", "$1 ***")`,
   729  			expected: "zhang ***",
   730  			key:      "str",
   731  		},
   732  		{
   733  			data:     `{"str": "362201200005302565"}`,
   734  			script:   `json(_, str) replace(str, "([1-9]{4})[0-9]{10}([0-9]{4})", "$1**********$2")`,
   735  			expected: "3622**********2565",
   736  			key:      "str",
   737  		},
   738  		{
   739  			data:     `{"str": "小阿卡"}`,
   740  			script:   `json(_, str) replace(str, '([\u4e00-\u9fa5])[\u4e00-\u9fa5]([\u4e00-\u9fa5])', "$1*$2")`,
   741  			expected: "小*卡",
   742  			key:      "str",
   743  		},
   744  	}
   745  
   746  	for _, tt := range cases {
   747  		p, err := NewPipeline(tt.script)
   748  		assertEqual(t, err, p.lastErr)
   749  
   750  		p.Run(tt.data)
   751  
   752  		r, err := p.getContentStr(tt.key)
   753  		assertEqual(t, err, nil)
   754  
   755  		if !tt.fail {
   756  			assertEqual(t, r, tt.expected)
   757  		}
   758  	}
   759  }
   760  */