github.com/fluhus/gostuff@v0.4.1-0.20240331134726-be71864f2b5d/csvdec/decoder_test.go (about)

     1  package csvdec
     2  
     3  import (
     4  	"reflect"
     5  	"strings"
     6  	"testing"
     7  )
     8  
     9  // TODO(amit): Change output style to got, want.
    10  // TODO(amit): Add tests for bad input.
    11  
    12  func TestDecoder_struct(t *testing.T) {
    13  	decoder := NewDecoder(strings.NewReader("Hello,1,-1,3.14"))
    14  
    15  	act := struct {
    16  		S string
    17  		U uint
    18  		I int
    19  		F float64
    20  	}{}
    21  	exp := struct {
    22  		S string
    23  		U uint
    24  		I int
    25  		F float64
    26  	}{"Hello", 1, -1, 3.14}
    27  
    28  	err := decoder.Decode(&act)
    29  	if err != nil {
    30  		t.Fatal("Error decoding struct:", t)
    31  	}
    32  
    33  	if act.S != exp.S || act.U != exp.U || act.I != exp.I || act.F != exp.F {
    34  		t.Fatal("Wrong decoding. Expected:", exp, "Actual:", act)
    35  	}
    36  }
    37  
    38  func TestDecoder_uintSlice(t *testing.T) {
    39  	decoder := NewDecoder(strings.NewReader("2,3,5,7,11,13"))
    40  	var act []uint
    41  	exp := []uint{2, 3, 5, 7, 11, 13}
    42  
    43  	err := decoder.Decode(&act)
    44  	if err != nil {
    45  		t.Fatal("Error decoding struct:", t)
    46  	}
    47  
    48  	if len(act) != len(exp) {
    49  		t.Fatal("Wrong decoding. Expected:", exp, "Actual:", act)
    50  	}
    51  
    52  	for i := range act {
    53  		if act[i] != exp[i] {
    54  			t.Fatal("Wrong decoding. Expected:", exp, "Actual:", act)
    55  		}
    56  	}
    57  }
    58  
    59  func TestDecoder_intSlice(t *testing.T) {
    60  	decoder := NewDecoder(strings.NewReader("2,-3,5,-7,11,-13"))
    61  	var act []int
    62  	exp := []int{2, -3, 5, -7, 11, -13}
    63  
    64  	err := decoder.Decode(&act)
    65  	if err != nil {
    66  		t.Fatal("Error decoding struct:", t)
    67  	}
    68  
    69  	if len(act) != len(exp) {
    70  		t.Fatal("Wrong decoding. Expected:", exp, "Actual:", act)
    71  	}
    72  
    73  	for i := range act {
    74  		if act[i] != exp[i] {
    75  			t.Fatal("Wrong decoding. Expected:", exp, "Actual:", act)
    76  		}
    77  	}
    78  }
    79  
    80  func TestDecoder_floatSlice(t *testing.T) {
    81  	decoder := NewDecoder(strings.NewReader("2,-3.14,5.5,-7008,0.11,-1.3"))
    82  	var act []float64
    83  	exp := []float64{2, -3.14, 5.5, -7008, 0.11, -1.3}
    84  
    85  	err := decoder.Decode(&act)
    86  	if err != nil {
    87  		t.Fatal("Error decoding struct:", t)
    88  	}
    89  
    90  	if len(act) != len(exp) {
    91  		t.Fatal("Wrong decoding. Expected:", exp, "Actual:", act)
    92  	}
    93  
    94  	for i := range act {
    95  		if act[i] != exp[i] {
    96  			t.Fatal("Wrong decoding. Expected:", exp, "Actual:", act)
    97  		}
    98  	}
    99  }
   100  
   101  func TestDecoder_stringSlice(t *testing.T) {
   102  	decoder := NewDecoder(strings.NewReader("yar,har,fiddle,di,dee"))
   103  	var act []string
   104  	exp := []string{"yar", "har", "fiddle", "di", "dee"}
   105  
   106  	err := decoder.Decode(&act)
   107  	if err != nil {
   108  		t.Fatal("Error decoding struct:", t)
   109  	}
   110  
   111  	if len(act) != len(exp) {
   112  		t.Fatal("Wrong decoding. Expected:", exp, "Actual:", act)
   113  	}
   114  
   115  	for i := range act {
   116  		if act[i] != exp[i] {
   117  			t.Fatal("Wrong decoding. Expected:", exp, "Actual:", act)
   118  		}
   119  	}
   120  }
   121  
   122  func TestDecoder_structWithSlice(t *testing.T) {
   123  	type T struct {
   124  		S string
   125  		I []int
   126  	}
   127  	want := T{"hello", []int{5, 4, 3, 2, 1}}
   128  	got := T{}
   129  	input := "hello,5,4,3,2,1"
   130  	decoder := NewDecoder(strings.NewReader(input))
   131  
   132  	if err := decoder.Decode(&got); err != nil {
   133  		t.Fatalf("Decode(%q) failed: %v, want success", input, err)
   134  	}
   135  	if !reflect.DeepEqual(got, want) {
   136  		t.Fatalf("Decode(%q)=%v, want %v", input, got, want)
   137  	}
   138  }