dubbo.apache.org/dubbo-go/v3@v3.1.1/remoting/polaris/parser/parser_test.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package parser
    19  
    20  import (
    21  	"encoding/json"
    22  	"testing"
    23  )
    24  
    25  import (
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  var (
    30  	testDataStore = `
    31  	{
    32  		"book":[
    33  			{
    34  				"category":"reference",
    35  				"author":"Nigel Rees",
    36  				"title":"Sayings of the Century",
    37  				"price":8.95
    38  			},
    39  			{
    40  				"category":"fiction",
    41  				"author":"Evelyn Waugh",
    42  				"title":"Sword of Honor",
    43  				"price":12.99
    44  			},
    45  			{
    46  				"category":"fiction",
    47  				"author":"Herman Melville",
    48  				"title":"Moby Dick",
    49  				"isbn":"0-553-21311-3",
    50  				"price":8.99
    51  			},
    52  			{
    53  				"category":"fiction",
    54  				"author":"J. R. R. Tolkien",
    55  				"title":"The Lord of the Rings",
    56  				"isbn":"0-395-19395-8",
    57  				"price":22.99
    58  			}
    59  		],
    60  		"bicycle":{
    61  			"color":"red",
    62  			"price":19.95
    63  		}
    64  	}
    65  	`
    66  
    67  	testDataBicyle = `
    68  	{
    69  		"color":"red",
    70  		"price":19.95
    71  	}
    72  	`
    73  )
    74  
    75  func TestParseArgumentsByExpression(t *testing.T) {
    76  
    77  	var (
    78  		argStore, argBicyle interface{}
    79  	)
    80  
    81  	json.Unmarshal([]byte(testDataStore), &argStore)
    82  	json.Unmarshal([]byte(testDataBicyle), &argBicyle)
    83  
    84  	t.Run("test-case-1", func(t *testing.T) {
    85  		ret := ParseArgumentsByExpression("param.$.book[0].category", []interface{}{argStore})
    86  		assert.Equal(t, "reference", ret)
    87  	})
    88  
    89  	t.Run("test-case-2", func(t *testing.T) {
    90  		ret := ParseArgumentsByExpression("param[0].$.book[0].category", []interface{}{argStore, argBicyle})
    91  		assert.Equal(t, "reference", ret)
    92  	})
    93  
    94  	t.Run("test-case-2", func(t *testing.T) {
    95  		ret := ParseArgumentsByExpression("param[1].$.color", []interface{}{argStore, argBicyle})
    96  		assert.Equal(t, "red", ret)
    97  	})
    98  
    99  	t.Run("test-case-3", func(t *testing.T) {
   100  		ret := ParseArgumentsByExpression("param.$.color", []interface{}{argBicyle})
   101  		assert.Equal(t, "red", ret)
   102  	})
   103  
   104  }
   105  
   106  func Test_resolveIndex(t *testing.T) {
   107  	type args struct {
   108  		key string
   109  	}
   110  	tests := []struct {
   111  		name  string
   112  		args  args
   113  		want  int
   114  		want1 string
   115  	}{
   116  		{
   117  			name: "case-1",
   118  			args: args{
   119  				key: "param.$.key",
   120  			},
   121  			want:  0,
   122  			want1: "$.key",
   123  		},
   124  		{
   125  			name: "case-2",
   126  			args: args{
   127  				key: "param[1].$.key",
   128  			},
   129  			want:  1,
   130  			want1: "$.key",
   131  		},
   132  		{
   133  			name: "case-3",
   134  			args: args{
   135  				key: "param[10].$.key",
   136  			},
   137  			want:  10,
   138  			want1: "$.key",
   139  		},
   140  		{
   141  			name: "case-4",
   142  			args: args{
   143  				key: "param[11]$.key",
   144  			},
   145  			want:  11,
   146  			want1: "$.key",
   147  		},
   148  		{
   149  			name: "case-5",
   150  			args: args{
   151  				key: "param[11]key",
   152  			},
   153  			want:  11,
   154  			want1: "key",
   155  		},
   156  	}
   157  	for _, tt := range tests {
   158  		t.Run(tt.name, func(t *testing.T) {
   159  			got, got1 := resolveIndex(tt.args.key)
   160  			if got != tt.want {
   161  				t.Errorf("resolveIndex() got = %v, want %v", got, tt.want)
   162  			}
   163  			if got1 != tt.want1 {
   164  				t.Errorf("resolveIndex() got1 = %v, want %v", got1, tt.want1)
   165  			}
   166  		})
   167  	}
   168  }