enzyme_test.go raw

   1  package enzyme
   2  
   3  import (
   4  	"strings"
   5  	"testing"
   6  )
   7  
   8  func TestTextDigest(t *testing.T) {
   9  	input := "hello world! 42"
  10  	ch := Text{}.Digest(strings.NewReader(input))
  11  
  12  	var elems []struct{ tag, val string }
  13  	for e := range ch {
  14  		elems = append(elems, struct{ tag, val string }{e.Type(), e.Value().(string)})
  15  	}
  16  
  17  	expected := []struct{ tag, val string }{
  18  		{"w3", "hello"},
  19  		{"space", " "},
  20  		{"w3", "world"},
  21  		{"punct", "!"},
  22  		{"space", " "},
  23  		{"w2", "42"},
  24  	}
  25  
  26  	if len(elems) != len(expected) {
  27  		t.Fatalf("expected %d elements, got %d: %+v", len(expected), len(elems), elems)
  28  	}
  29  	for i, e := range elems {
  30  		if e != expected[i] {
  31  			t.Errorf("element %d: expected %+v, got %+v", i, expected[i], e)
  32  		}
  33  	}
  34  }
  35  
  36  func TestTextDigestEmpty(t *testing.T) {
  37  	ch := Text{}.Digest(strings.NewReader(""))
  38  	count := 0
  39  	for range ch {
  40  		count++
  41  	}
  42  	if count != 0 {
  43  		t.Errorf("expected 0 elements from empty input, got %d", count)
  44  	}
  45  }
  46  
  47  func TestLinesDigest(t *testing.T) {
  48  	input := "first line\nsecond line\nthird"
  49  	ch := Lines{}.Digest(strings.NewReader(input))
  50  
  51  	var lines []string
  52  	for e := range ch {
  53  		if e.Type() != "line" {
  54  			t.Errorf("expected tag 'line', got %q", e.Type())
  55  		}
  56  		lines = append(lines, e.Value().(string))
  57  	}
  58  
  59  	if len(lines) != 3 {
  60  		t.Fatalf("expected 3 lines, got %d", len(lines))
  61  	}
  62  	if lines[0] != "first line" || lines[1] != "second line" || lines[2] != "third" {
  63  		t.Errorf("unexpected lines: %v", lines)
  64  	}
  65  }
  66  
  67  func TestElem(t *testing.T) {
  68  	e := Elem("test", "value")
  69  	if e.Type() != "test" {
  70  		t.Errorf("expected type 'test', got %q", e.Type())
  71  	}
  72  	if e.Value().(string) != "value" {
  73  		t.Errorf("expected value 'value', got %v", e.Value())
  74  	}
  75  }
  76