github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/engine/function/jsfuncs_test.go (about)

     1  package funcs
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestJSFuncs_Execute(t *testing.T) {
     9  	type args struct {
    10  		f *Funcs
    11  	}
    12  	tests := []struct {
    13  		name string
    14  		cf   *JSFuncs
    15  		args args
    16  	}{}
    17  	for _, tt := range tests {
    18  		t.Run(tt.name, func(t *testing.T) {
    19  			tt.cf.Execute(tt.args.f)
    20  		})
    21  	}
    22  }
    23  
    24  func TestJSFuncs_Validate(t *testing.T) {
    25  	type args struct {
    26  		f *Funcs
    27  	}
    28  	tests := []struct {
    29  		name    string
    30  		cf      *JSFuncs
    31  		args    args
    32  		want    bool
    33  		wantErr bool
    34  	}{
    35  		// TODO: Add test cases.
    36  	}
    37  	for _, tt := range tests {
    38  		t.Run(tt.name, func(t *testing.T) {
    39  			got, err := tt.cf.Validate(tt.args.f)
    40  			if (err != nil) != tt.wantErr {
    41  				t.Errorf("JSFuncs.Validate() error = %v, wantErr %v", err, tt.wantErr)
    42  				return
    43  			}
    44  			if got != tt.want {
    45  				t.Errorf("JSFuncs.Validate() = %v, want %v", got, tt.want)
    46  			}
    47  		})
    48  	}
    49  }
    50  
    51  func TestJSFuncs_Testfunction(t *testing.T) {
    52  	type args struct {
    53  		content string
    54  		inputs  interface{}
    55  		outputs []string
    56  	}
    57  	tests := []struct {
    58  		name    string
    59  		cf      *JSFuncs
    60  		args    args
    61  		want    map[string]interface{}
    62  		wantErr bool
    63  	}{
    64  		{
    65  			name: "test",
    66  			cf:   &JSFuncs{},
    67  			args: args{
    68  				content: `var result;
    69  				var x = 3;
    70  		
    71  				if (x > 0) {
    72  					result = "Positive";
    73  				} else if (x < 0) {
    74  					result = "Negative";
    75  				} else {
    76  					result = "Zero";
    77  				}
    78  		
    79  				result;`,
    80  				inputs: map[string]interface{}{
    81  					"test": "test",
    82  				},
    83  				outputs: []string{"result"},
    84  			},
    85  			want: map[string]interface{}{
    86  				"result": "Positive",
    87  			},
    88  			wantErr: false,
    89  		},
    90  		{
    91  			name: "test1",
    92  			cf:   &JSFuncs{},
    93  			args: args{
    94  				content: `DataDecimal = 0.0; DataInt = 0; DataBoolean = false; DataString = ""; 							 					
    95  				if(DataType == 1 || DataType == "1")
    96  				  DataInt = parseInt(Value)
    97  				else if(DataType == 2)
    98  						DataDecimal = parseFloat(Value);
    99  				else if(DataType == 3)
   100  						DataBoolean = (Value == "true" || Value== "1")? true: false;
   101  				else
   102  				  DataString = Value;`,
   103  				inputs: map[string]interface{}{
   104  					"DataType": 1,
   105  					"Value":    "456",
   106  				},
   107  				outputs: []string{"DataInt", "DataDecimal", "DataBoolean"},
   108  			},
   109  			want: map[string]interface{}{
   110  				"DataInt":     456,
   111  				"DataDecimal": 0.0,
   112  				"DataBoolean": false,
   113  			},
   114  			wantErr: false,
   115  		},
   116  	}
   117  	for _, tt := range tests {
   118  		t.Run(tt.name, func(t *testing.T) {
   119  			got, err := tt.cf.Testfunction(tt.args.content, tt.args.inputs, tt.args.outputs)
   120  			if (err != nil) != tt.wantErr {
   121  				t.Errorf("JSFuncs.Testfunction() error = %v, wantErr %v", err, tt.wantErr)
   122  				return
   123  			}
   124  			if !reflect.DeepEqual(got, tt.want) {
   125  				t.Errorf("JSFuncs.Testfunction() = %v, want %v", got, tt.want)
   126  			}
   127  		})
   128  	}
   129  }