github.com/informationsea/shellflow@v0.1.3/flowscript/flowscript_lookahead_test.go (about)

     1  package flowscript
     2  
     3  import (
     4  	"bufio"
     5  	"reflect"
     6  	"strconv"
     7  	"strings"
     8  	"testing"
     9  )
    10  
    11  func TestLookAheadScannerFirstHead(t *testing.T) {
    12  	reader := strings.NewReader("1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20")
    13  	scanner := bufio.NewScanner(reader)
    14  	scanner.Split(bufio.ScanWords)
    15  	loa := NewLookAheadScanner(scanner)
    16  
    17  	for i := 1; i <= 20; i++ {
    18  		index := strconv.FormatInt(int64(i), 10)
    19  		if v := loa.LookAheadBytes(i - 1); !reflect.DeepEqual(v, []byte(index)) {
    20  			t.Fatalf("Bad data: %s / expected: %s", v, index)
    21  		}
    22  	}
    23  
    24  	for i := 1; i <= 20; i++ {
    25  		if !loa.Scan() {
    26  			t.Fatalf("Cannot scan %s", loa.Err())
    27  		}
    28  
    29  		index := strconv.FormatInt(int64(i), 10)
    30  		if v := loa.Bytes(); !reflect.DeepEqual(v, []byte(index)) {
    31  			t.Fatalf("Bad data: %s / expected %s", v, index)
    32  		}
    33  	}
    34  }
    35  
    36  func TestLookAheadScanner(t *testing.T) {
    37  	reader := strings.NewReader("1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20")
    38  	scanner := bufio.NewScanner(reader)
    39  	scanner.Split(bufio.ScanWords)
    40  	loa := NewLookAheadScanner(scanner)
    41  
    42  	// not scanned yet
    43  	if v := loa.Bytes(); v != nil {
    44  		t.Fatalf("Bad data: %s", v)
    45  	}
    46  
    47  	for i := 1; i <= 20; i++ {
    48  		index := strconv.FormatInt(int64(i), 10)
    49  		if !loa.Scan() {
    50  			t.Fatalf("Failed to scan %s", loa.Err())
    51  		}
    52  
    53  		if e := loa.Err(); e != nil {
    54  			t.Fatalf("Failed to scan %s", e)
    55  		}
    56  
    57  		if v := loa.Text(); v != index {
    58  			t.Fatalf("Bad data: %s / length %d", v, len(v))
    59  		}
    60  
    61  		if v := loa.Bytes(); !reflect.DeepEqual(v, []byte(index)) {
    62  			t.Fatalf("Bad data: %s / length %d", v, len(v))
    63  		}
    64  	}
    65  
    66  	if r := loa.Scan(); r {
    67  		t.Fatal("loa should reached EOF")
    68  	}
    69  
    70  	if e := loa.Err(); e != nil {
    71  		t.Fatalf("EOF should not create error: %s", e)
    72  	}
    73  }
    74  
    75  func TestLookAheadScanner2(t *testing.T) {
    76  	reader := strings.NewReader("1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20")
    77  	scanner := bufio.NewScanner(reader)
    78  	scanner.Split(bufio.ScanWords)
    79  	loa := NewLookAheadScanner(scanner)
    80  
    81  	// not scanned yet
    82  	if v := loa.Bytes(); v != nil {
    83  		t.Fatalf("Bad data: %s", v)
    84  	}
    85  
    86  	for i := 1; i <= 5; i++ {
    87  		index := strconv.FormatInt(int64(i), 10)
    88  		if !loa.Scan() {
    89  			t.Fatalf("Failed to scan %s", loa.Err())
    90  		}
    91  
    92  		if e := loa.Err(); e != nil {
    93  			t.Fatalf("Failed to scan %s", e)
    94  		}
    95  
    96  		if v := loa.Text(); v != index {
    97  			t.Fatalf("Bad data: %s / length %d", v, len(v))
    98  		}
    99  
   100  		if v := loa.Bytes(); !reflect.DeepEqual(v, []byte(index)) {
   101  			t.Fatalf("Bad data: %s / length %d", v, len(v))
   102  		}
   103  	}
   104  
   105  	if v := loa.LookAheadBytes(1); !reflect.DeepEqual(v, []byte("6")) {
   106  		t.Fatalf("Bad look ahead: %s", v)
   107  	}
   108  
   109  	if loa.lookAhead.Len() != 2 {
   110  		t.Fatalf("Wrong number of look ahead list: %d", loa.lookAhead.Len())
   111  	}
   112  
   113  	if v := loa.LookAheadBytes(3); !reflect.DeepEqual(v, []byte("8")) {
   114  		t.Fatalf("Bad look ahead: %s", v)
   115  	}
   116  
   117  	if v := loa.LookAheadText(3); v != "8" {
   118  		t.Fatalf("Bad look ahead: %s", v)
   119  	}
   120  
   121  	if loa.lookAhead.Len() != 4 {
   122  		t.Fatalf("Wrong number of look ahead list: %d", loa.lookAhead.Len())
   123  	}
   124  
   125  	for i := 6; i <= 20; i++ {
   126  		index := strconv.FormatInt(int64(i), 10)
   127  		if !loa.Scan() {
   128  			t.Fatalf("Failed to scan %s", loa.Err())
   129  		}
   130  
   131  		if e := loa.Err(); e != nil {
   132  			t.Fatalf("Failed to scan %s", e)
   133  		}
   134  
   135  		if v := loa.Text(); v != index {
   136  			t.Fatalf("Bad data: %s / length %d", v, len(v))
   137  		}
   138  
   139  		if v := loa.Bytes(); !reflect.DeepEqual(v, []byte(index)) {
   140  			t.Fatalf("Bad data: %s / length %d", v, len(v))
   141  		}
   142  	}
   143  
   144  	if v := loa.LookAheadBytes(1); v != nil {
   145  		t.Fatalf("Bad look ahead: %s", v)
   146  	}
   147  
   148  	if r := loa.Scan(); r {
   149  		t.Fatal("loa should reached EOF")
   150  	}
   151  
   152  	if e := loa.Err(); e != nil {
   153  		t.Fatalf("EOF should not create error: %s", e)
   154  	}
   155  }