github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/lang/test_compare_test.go (about)

     1  package lang
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/lmorg/murex/lang/types"
     7  	"github.com/lmorg/murex/test/count"
     8  )
     9  
    10  type testIsArrayMap struct {
    11  	Input    string
    12  	DataType string
    13  	Expected TestStatus
    14  }
    15  
    16  func TestIsArray(t *testing.T) {
    17  	tests := []testIsArrayMap{
    18  		{
    19  			Input:    `a\nb\nc\n`,
    20  			DataType: types.Generic,
    21  			Expected: TestFailed,
    22  		},
    23  		{
    24  			Input:    `a\nb\nc\n`,
    25  			DataType: types.String,
    26  			Expected: TestPassed,
    27  		},
    28  		{
    29  			Input:    `["a","b","c"]`,
    30  			DataType: types.Json,
    31  			Expected: TestPassed,
    32  		},
    33  		{
    34  			Input:    `{"a": "a", "b": "b", "c": "c"}`,
    35  			DataType: types.Json,
    36  			Expected: TestFailed,
    37  		},
    38  		{
    39  			Input:    `- a\n- b\n- c\n`,
    40  			DataType: "yaml",
    41  			Expected: TestPassed,
    42  		},
    43  	}
    44  
    45  	testIsArrayTest(t, tests)
    46  }
    47  
    48  func testIsArrayTest(t *testing.T, testArray []testIsArrayMap) {
    49  	InitEnv()
    50  	count.Tests(t, len(testArray))
    51  	t.Helper()
    52  
    53  	for i := range testArray {
    54  		status, msg := testIsArray([]byte(testArray[i].Input), testArray[i].DataType, "TestIsArray")
    55  		if status != testArray[i].Expected {
    56  			t.Error("testIsArray() returned unexpected result:")
    57  			t.Logf("  Test Num: %d", i)
    58  			t.Logf("  Input:    %s", string(testArray[i].Input))
    59  			t.Logf("  DataType: %s", testArray[i].DataType)
    60  			t.Logf("  Expected: %s", testArray[i].Expected)
    61  			t.Logf("  Actual:   %s", status)
    62  			t.Logf("  Error:    %s", msg)
    63  		}
    64  	}
    65  }
    66  
    67  func TestIsMap(t *testing.T) {
    68  	tests := []testIsArrayMap{
    69  		{
    70  			Input:    `a\nb\nc\n`,
    71  			DataType: types.Generic,
    72  			Expected: TestFailed,
    73  		},
    74  		{
    75  			Input:    `a\nb\nc\n`,
    76  			DataType: types.String,
    77  			Expected: TestFailed,
    78  		},
    79  		{
    80  			Input:    `["a","b","c"]`,
    81  			DataType: types.Json,
    82  			Expected: TestFailed,
    83  		},
    84  		{
    85  			Input:    `{"a": "a", "b": "b", "c": "c"}`,
    86  			DataType: types.Json,
    87  			Expected: TestPassed,
    88  		},
    89  		{
    90  			Input:    `- a\n- b\n- c\n`,
    91  			DataType: "yaml",
    92  			Expected: TestFailed,
    93  		},
    94  	}
    95  
    96  	testIsMapTest(t, tests)
    97  }
    98  
    99  func testIsMapTest(t *testing.T, testArray []testIsArrayMap) {
   100  	InitEnv()
   101  	count.Tests(t, len(testArray))
   102  	t.Helper()
   103  
   104  	for i := range testArray {
   105  		status, msg := testIsMap([]byte(testArray[i].Input), testArray[i].DataType, "TestIsMap")
   106  		if status != testArray[i].Expected {
   107  			t.Error("testIsMap() returned unexpected result:")
   108  			t.Logf("  Test Num: %d", i)
   109  			t.Logf("  Input:    %s", string(testArray[i].Input))
   110  			t.Logf("  DataType: %s", testArray[i].DataType)
   111  			t.Logf("  Expected: %s", testArray[i].Expected)
   112  			t.Logf("  Actual:   %s", status)
   113  			t.Logf("  Error:    %s", msg)
   114  		}
   115  	}
   116  }