github.com/mithrandie/csvq@v1.18.1/lib/value/type_test.go (about)

     1  package value
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/mithrandie/ternary"
     8  )
     9  
    10  func TestIsTrue(t *testing.T) {
    11  	var p Primary
    12  
    13  	p = NewInteger(1)
    14  	if IsTrue(p) {
    15  		t.Errorf("value %#p is evaluated as is a ternary-true, but it is not so", p)
    16  	}
    17  
    18  	p = NewTernary(ternary.TRUE)
    19  	if !IsTrue(p) {
    20  		t.Errorf("value %#p is evaluated as is not a ternary-true, but it is so", p)
    21  	}
    22  }
    23  
    24  func TestIsFalse(t *testing.T) {
    25  	var p Primary
    26  
    27  	p = NewInteger(1)
    28  	if IsFalse(p) {
    29  		t.Errorf("value %#p is evaluated as is a ternary-false, but it is not so", p)
    30  	}
    31  
    32  	p = NewTernary(ternary.FALSE)
    33  	if !IsFalse(p) {
    34  		t.Errorf("value %#p is evaluated as is not a ternary-false, but it is so", p)
    35  	}
    36  }
    37  
    38  func TestIsUnknown(t *testing.T) {
    39  	var p Primary
    40  
    41  	p = NewInteger(1)
    42  	if IsUnknown(p) {
    43  		t.Errorf("value %#p is evaluated as is a ternary-unknown, but it is not so", p)
    44  	}
    45  
    46  	p = NewTernary(ternary.UNKNOWN)
    47  	if !IsUnknown(p) {
    48  		t.Errorf("value %#p is evaluated as is not a ternary-unknown, but it is so", p)
    49  	}
    50  }
    51  
    52  func TestIsNull(t *testing.T) {
    53  	var p Primary
    54  
    55  	p = NewInteger(1)
    56  	if IsNull(p) {
    57  		t.Errorf("value %#p is evaluated as is a null, but it is not so", p)
    58  	}
    59  
    60  	p = NewNull()
    61  	if !IsNull(p) {
    62  		t.Errorf("value %#p is evaluated as is not a null, but it is so", p)
    63  	}
    64  }
    65  
    66  func TestString_String(t *testing.T) {
    67  	s := "abcde"
    68  	p := NewString(s)
    69  	expect := "'" + s + "'"
    70  	if p.String() != expect {
    71  		t.Errorf("string = %q, want %q for %#v", p.String(), expect, p)
    72  	}
    73  }
    74  
    75  func TestString_Value(t *testing.T) {
    76  	s := "abcde"
    77  	p := NewString(s)
    78  	if p.Raw() != s {
    79  		t.Errorf("value = %q, want %q for %#v", p.Raw(), s, p)
    80  	}
    81  }
    82  
    83  func TestString_Ternary(t *testing.T) {
    84  	s := " 1"
    85  	p := NewString(s)
    86  	if p.Ternary() != ternary.TRUE {
    87  		t.Errorf("ternary = %s, want %s for %#v", p.Ternary(), ternary.TRUE, p)
    88  	}
    89  
    90  	s = "0"
    91  	p = NewString(s)
    92  	if p.Ternary() != ternary.FALSE {
    93  		t.Errorf("ternary = %s, want %s for %#v", p.Ternary(), ternary.FALSE, p)
    94  	}
    95  	s = "unknown"
    96  	p = NewString(s)
    97  	if p.Ternary() != ternary.UNKNOWN {
    98  		t.Errorf("ternary = %s, want %s for %#v", p.Ternary(), ternary.UNKNOWN, p)
    99  	}
   100  }
   101  
   102  func TestInteger_String(t *testing.T) {
   103  	s := "1"
   104  	p := NewInteger(1)
   105  	if p.String() != s {
   106  		t.Errorf("string = %q, want %q for %#v", p.String(), s, p)
   107  	}
   108  }
   109  
   110  func TestInteger_Value(t *testing.T) {
   111  	i := NewInteger(1)
   112  	expect := int64(1)
   113  
   114  	if i.Raw() != expect {
   115  		t.Errorf("value = %d, want %d for %#v", i.Raw(), expect, i)
   116  	}
   117  }
   118  
   119  func TestInteger_Ternary(t *testing.T) {
   120  	p := NewInteger(1)
   121  	if p.Ternary() != ternary.TRUE {
   122  		t.Errorf("ternary = %s, want %s for %#v", p.Ternary(), ternary.TRUE, p)
   123  	}
   124  	p = NewInteger(0)
   125  	if p.Ternary() != ternary.FALSE {
   126  		t.Errorf("ternary = %s, want %s for %#v", p.Ternary(), ternary.FALSE, p)
   127  	}
   128  	p = NewInteger(2)
   129  	if p.Ternary() != ternary.UNKNOWN {
   130  		t.Errorf("ternary = %s, want %s for %#v", p.Ternary(), ternary.UNKNOWN, p)
   131  	}
   132  }
   133  
   134  func TestFloat_String(t *testing.T) {
   135  	s := "1.234"
   136  	p := NewFloat(1.234)
   137  	if p.String() != s {
   138  		t.Errorf("string = %q, want %q for %#v", p.String(), s, p)
   139  	}
   140  }
   141  
   142  func TestFloat_Value(t *testing.T) {
   143  	f := NewFloat(1.234)
   144  	expect := 1.234
   145  
   146  	if f.Raw() != expect {
   147  		t.Errorf("value = %f, want %f for %#v", f.Raw(), expect, f)
   148  	}
   149  }
   150  
   151  func TestFloat_Ternary(t *testing.T) {
   152  	p := NewFloat(1)
   153  	if p.Ternary() != ternary.TRUE {
   154  		t.Errorf("ternary = %s, want %s for %#v", p.Ternary(), ternary.TRUE, p)
   155  	}
   156  	p = NewFloat(0)
   157  	if p.Ternary() != ternary.FALSE {
   158  		t.Errorf("ternary = %s, want %s for %#v", p.Ternary(), ternary.FALSE, p)
   159  	}
   160  	p = NewFloat(2)
   161  	if p.Ternary() != ternary.UNKNOWN {
   162  		t.Errorf("ternary = %s, want %s for %#v", p.Ternary(), ternary.UNKNOWN, p)
   163  	}
   164  }
   165  
   166  func TestBoolean_String(t *testing.T) {
   167  	s := "true"
   168  	p := NewBoolean(true)
   169  	if p.String() != s {
   170  		t.Errorf("string = %q, want %q for %#v", p.String(), s, p)
   171  	}
   172  }
   173  
   174  func TestBoolean_Value(t *testing.T) {
   175  	p := NewBoolean(true)
   176  	if p.Raw() != true {
   177  		t.Errorf("bool = %t, want %t for %#v", p.Raw(), true, p)
   178  	}
   179  }
   180  
   181  func TestBoolean_Ternary(t *testing.T) {
   182  	p := NewBoolean(true)
   183  	if p.Ternary() != ternary.TRUE {
   184  		t.Errorf("ternary = %s, want %s for %#v", p.Ternary(), ternary.TRUE, p)
   185  	}
   186  }
   187  
   188  func TestTernary_String(t *testing.T) {
   189  	s := "TRUE"
   190  	p := NewTernary(ternary.TRUE)
   191  	if p.String() != s {
   192  		t.Errorf("string = %q, want %q for %#v", p.String(), s, p)
   193  	}
   194  }
   195  
   196  func TestTernary_Ternary(t *testing.T) {
   197  	p := NewTernary(ternary.TRUE)
   198  	if p.Ternary() != ternary.TRUE {
   199  		t.Errorf("ternary = %s, want %s for %#v", p.Ternary(), ternary.TRUE, p)
   200  	}
   201  
   202  	p = NewTernary(ternary.FALSE)
   203  	if p.Ternary() != ternary.FALSE {
   204  		t.Errorf("ternary = %s, want %s for %#v", p.Ternary(), ternary.TRUE, p)
   205  	}
   206  
   207  	p = NewTernary(ternary.UNKNOWN)
   208  	if p.Ternary() != ternary.UNKNOWN {
   209  		t.Errorf("ternary = %s, want %s for %#v", p.Ternary(), ternary.TRUE, p)
   210  	}
   211  }
   212  
   213  func TestDatetime_String(t *testing.T) {
   214  	location, _ := time.LoadLocation("UTC")
   215  
   216  	s := "2012-01-01T12:34:56Z"
   217  	p := NewDatetimeFromString(s, nil, location)
   218  
   219  	expect := "'" + s + "'"
   220  	if p.String() != expect {
   221  		t.Errorf("string = %q, want %q for %#v", p.String(), expect, p)
   222  	}
   223  }
   224  
   225  func TestDatetime_Value(t *testing.T) {
   226  	location, _ := time.LoadLocation("Local")
   227  
   228  	d := NewDatetimeFromString("2012-01-01 12:34:56", nil, location)
   229  	expect := time.Date(2012, time.January, 1, 12, 34, 56, 0, time.Local)
   230  
   231  	if d.Raw() != expect {
   232  		t.Errorf("value = %v, want %v for %#v", d.Raw(), expect, d)
   233  	}
   234  
   235  	d = NewDatetimeFromString("2012-01-01T12:34:56-08:00", nil, location)
   236  	l, _ := time.LoadLocation("America/Los_Angeles")
   237  	expect = time.Date(2012, time.January, 1, 12, 34, 56, 0, l)
   238  
   239  	if d.Raw().Sub(expect).Seconds() != 0 {
   240  		t.Errorf("value = %v, want %v for %#v", d.Raw(), expect, d)
   241  	}
   242  }
   243  
   244  func TestDatetime_Ternary(t *testing.T) {
   245  	location, _ := time.LoadLocation("UTC")
   246  
   247  	p := NewDatetimeFromString("2012-01-01T12:34:56-08:00", nil, location)
   248  	if p.Ternary() != ternary.UNKNOWN {
   249  		t.Errorf("ternary = %s, want %s for %#v", p.Ternary(), ternary.UNKNOWN, p)
   250  	}
   251  }
   252  
   253  func TestDatetime_Format(t *testing.T) {
   254  	location, _ := time.LoadLocation("UTC")
   255  
   256  	dtstring := "2012-08-01T04:03:05.123-08:00"
   257  	dt := NewDatetimeFromString(dtstring, nil, location)
   258  	expect := "2012-08-01T04:03:05-08:00"
   259  	if dt.Format(time.RFC3339) != expect {
   260  		t.Errorf("result = %q, want %q for %q ", dt.Format(time.RFC3339), expect, dtstring)
   261  	}
   262  }
   263  
   264  func TestNull_String(t *testing.T) {
   265  	p := NewNull()
   266  	if p.String() != "NULL" {
   267  		t.Errorf("string = %q, want %q for %#v", p.String(), "NULL", p)
   268  	}
   269  }
   270  
   271  func TestNull_Ternary(t *testing.T) {
   272  	p := NewNull()
   273  	if p.Ternary() != ternary.UNKNOWN {
   274  		t.Errorf("ternary = %s, want %s for %#v", p.Ternary(), ternary.UNKNOWN, p)
   275  	}
   276  }