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

     1  package parameters_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/lmorg/murex/lang/parameters"
     7  	"github.com/lmorg/murex/test/count"
     8  )
     9  
    10  func TestGetString(t *testing.T) {
    11  	count.Tests(t, 6)
    12  
    13  	p := new(parameters.Parameters)
    14  	p.DefineParsed([]string{"one"})
    15  
    16  	var err error
    17  
    18  	_, err = p.Block(0)
    19  	if err == nil {
    20  		t.Error("Error should have been raised: can't import string as block")
    21  	}
    22  
    23  	_, err = p.Bool(0)
    24  	if err != nil {
    25  		t.Error(err)
    26  	}
    27  
    28  	_, err = p.Byte(0)
    29  	if err != nil {
    30  		t.Error(err)
    31  	}
    32  
    33  	_, err = p.String(0)
    34  	if err != nil {
    35  		t.Error(err)
    36  	}
    37  
    38  	_, err = p.Int(0)
    39  	if err == nil {
    40  		t.Error("Error should have been raised: can't import string as int")
    41  	}
    42  
    43  	_, err = p.Uint32(0)
    44  	if err == nil {
    45  		t.Error("Error should have been raised: can't import string as uint32")
    46  	}
    47  }
    48  
    49  func TestGetInt(t *testing.T) {
    50  	count.Tests(t, 6)
    51  
    52  	p := new(parameters.Parameters)
    53  	p.DefineParsed([]string{"1"})
    54  
    55  	var err error
    56  
    57  	_, err = p.Block(0)
    58  	if err == nil {
    59  		t.Error("Error should have been raised: can't import int as block")
    60  	}
    61  
    62  	_, err = p.Bool(0)
    63  	if err != nil {
    64  		t.Error(err)
    65  	}
    66  
    67  	_, err = p.Byte(0)
    68  	if err != nil {
    69  		t.Error(err)
    70  	}
    71  
    72  	_, err = p.String(0)
    73  	if err != nil {
    74  		t.Error(err)
    75  	}
    76  
    77  	_, err = p.Int(0)
    78  	if err != nil {
    79  		t.Error(err)
    80  	}
    81  
    82  	_, err = p.Uint32(0)
    83  	if err != nil {
    84  		t.Error(err)
    85  	}
    86  }
    87  
    88  func TestGetUint32(t *testing.T) {
    89  	count.Tests(t, 6)
    90  
    91  	p := new(parameters.Parameters)
    92  	p.DefineParsed([]string{"-1"})
    93  
    94  	var err error
    95  
    96  	_, err = p.Block(0)
    97  	if err == nil {
    98  		t.Error("Error should have been raised: can't import int as block")
    99  	}
   100  
   101  	_, err = p.Bool(0)
   102  	if err != nil {
   103  		t.Error(err)
   104  	}
   105  
   106  	_, err = p.Byte(0)
   107  	if err != nil {
   108  		t.Error(err)
   109  	}
   110  
   111  	_, err = p.String(0)
   112  	if err != nil {
   113  		t.Error(err)
   114  	}
   115  
   116  	_, err = p.Int(0)
   117  	if err != nil {
   118  		t.Error(err)
   119  	}
   120  
   121  	_, err = p.Uint32(0)
   122  	if err == nil {
   123  		t.Error("Error should have been raised: can't import int as uint32")
   124  	}
   125  }
   126  
   127  // We don't test this because if this condition arises then it's because any Go
   128  // code for murex builtins is wrong, so a panic is absolutely the right way to
   129  // flush out those errors
   130  /*func TestGetBoundsNeg1(t *testing.T) {
   131  	count.Tests(t, 6)
   132  
   133  	p := new(parameters.Parameters)
   134  	p.DefineParsed([]string{"1"})
   135  
   136  	var err error
   137  
   138  	_, err = p.Block(-1)
   139  	if err == nil {
   140  		t.Error("Out of bounds error should have been raised")
   141  	}
   142  
   143  	_, err = p.Bool(-1)
   144  	if err == nil {
   145  		t.Error("Out of bounds error should have been raised")
   146  	}
   147  
   148  	_, err = p.Byte(-1)
   149  	if err == nil {
   150  		t.Error("Out of bounds error should have been raised")
   151  	}
   152  
   153  	_, err = p.String(-1)
   154  	if err == nil {
   155  		t.Error("Out of bounds error should have been raised")
   156  	}
   157  
   158  	_, err = p.Int(-1)
   159  	if err == nil {
   160  		t.Error("Out of bounds error should have been raised")
   161  	}
   162  
   163  	_, err = p.Uint32(-1)
   164  	if err == nil {
   165  		t.Error("Out of bounds error should have been raised")
   166  	}
   167  }*/
   168  
   169  func TestGetBounds1(t *testing.T) {
   170  	count.Tests(t, 6)
   171  
   172  	p := new(parameters.Parameters)
   173  	p.DefineParsed([]string{"1"})
   174  
   175  	var err error
   176  
   177  	_, err = p.Block(1)
   178  	if err == nil {
   179  		t.Error("Out of bounds error should have been raised")
   180  	}
   181  
   182  	_, err = p.Bool(1)
   183  	if err == nil {
   184  		t.Error("Out of bounds error should have been raised")
   185  	}
   186  
   187  	_, err = p.Byte(1)
   188  	if err == nil {
   189  		t.Error("Out of bounds error should have been raised")
   190  	}
   191  
   192  	_, err = p.String(1)
   193  	if err == nil {
   194  		t.Error("Out of bounds error should have been raised")
   195  	}
   196  
   197  	_, err = p.Int(1)
   198  	if err == nil {
   199  		t.Error("Out of bounds error should have been raised")
   200  	}
   201  
   202  	_, err = p.Uint32(1)
   203  	if err == nil {
   204  		t.Error("Out of bounds error should have been raised")
   205  	}
   206  }