github.com/kintar/etxt@v0.0.9/renderer_iterator_test.go (about)

     1  //go:build gtxt
     2  
     3  package etxt
     4  
     5  import "testing"
     6  
     7  func testFailRunes(t *testing.T, expected rune, got rune) {
     8  	t.Fatalf("expected '%s', got '%s'", string(expected), string(got))
     9  }
    10  
    11  func testFailStr(t *testing.T, expected string, got string) {
    12  	t.Fatalf("expected '%s', got '%s'", expected, got)
    13  }
    14  
    15  func TestStrIterator(t *testing.T) {
    16  	// simple case
    17  	iter := newStrIterator("one day", false)
    18  	for _, expected := range []rune{'o', 'n', 'e', ' ', 'd', 'a', 'y', -1, -1, -1} {
    19  		got := iter.Next()
    20  		if got != expected {
    21  			testFailRunes(t, expected, got)
    22  		}
    23  	}
    24  	iter = newStrIterator("one day", false)
    25  	expected, got := "one day", iter.UntilNextLineBreak()
    26  	if got != expected {
    27  		testFailStr(t, expected, got)
    28  	}
    29  
    30  	// line breaks
    31  	iter = newStrIterator("0123\n0123\n", false)
    32  	expected, got = "0123", iter.UntilNextLineBreak()
    33  	if got != expected {
    34  		testFailStr(t, expected, got)
    35  	}
    36  	for _, expected := range []rune{'0', '1', '2', '3'} {
    37  		got := iter.Next()
    38  		if got != expected {
    39  			testFailRunes(t, expected, got)
    40  		}
    41  	}
    42  	expected, got = "", iter.UntilNextLineBreak()
    43  	if got != expected {
    44  		testFailStr(t, expected, got)
    45  	}
    46  	gotRune := iter.Next()
    47  	if gotRune != '\n' {
    48  		testFailRunes(t, '\n', gotRune)
    49  	}
    50  	expected, got = "0123", iter.UntilNextLineBreak()
    51  	if got != expected {
    52  		testFailStr(t, expected, got)
    53  	}
    54  	gotRune = iter.Next()
    55  	if gotRune != '0' {
    56  		testFailRunes(t, '0', gotRune)
    57  	}
    58  	expected, got = "123", iter.UntilNextLineBreak()
    59  	if got != expected {
    60  		testFailStr(t, expected, got)
    61  	}
    62  	for i := 0; i < 3; i++ {
    63  		iter.Next()
    64  	}
    65  	expected, got = "", iter.UntilNextLineBreak()
    66  	if got != expected {
    67  		testFailStr(t, expected, got)
    68  	}
    69  	gotRune = iter.Next()
    70  	if gotRune != '\n' {
    71  		testFailRunes(t, '\n', gotRune)
    72  	}
    73  
    74  	// no ending line break
    75  	iter = newStrIterator("B\nA", false)
    76  	expected, got = "B", iter.UntilNextLineBreak()
    77  	if got != expected {
    78  		testFailStr(t, expected, got)
    79  	}
    80  	gotRune = iter.Next()
    81  	if gotRune != 'B' {
    82  		testFailRunes(t, 'B', gotRune)
    83  	}
    84  	expected, got = "", iter.UntilNextLineBreak()
    85  	if got != expected {
    86  		testFailStr(t, expected, got)
    87  	}
    88  	gotRune = iter.Next()
    89  	if gotRune != '\n' {
    90  		testFailRunes(t, '\n', gotRune)
    91  	}
    92  	expected, got = "A", iter.UntilNextLineBreak()
    93  	if got != expected {
    94  		testFailStr(t, expected, got)
    95  	}
    96  	gotRune = iter.Next()
    97  	if gotRune != 'A' {
    98  		testFailRunes(t, 'A', gotRune)
    99  	}
   100  	gotRune = iter.Next()
   101  	if gotRune != -1 {
   102  		testFailRunes(t, -1, gotRune)
   103  	}
   104  	expected, got = "", iter.UntilNextLineBreak()
   105  	if got != expected {
   106  		testFailStr(t, expected, got)
   107  	}
   108  }
   109  
   110  func TestStrIteratorReverse(t *testing.T) {
   111  	iter := newStrIterator("0123\nAB CD\n", true)
   112  	expected, got := "0123", iter.UntilNextLineBreak()
   113  	if got != expected {
   114  		testFailStr(t, expected, got)
   115  	}
   116  	for _, expected := range []rune{'3', '2', '1', '0'} {
   117  		got := iter.Next()
   118  		if got != expected {
   119  			testFailRunes(t, expected, got)
   120  		}
   121  	}
   122  	expected, got = "", iter.UntilNextLineBreak()
   123  	if got != expected {
   124  		testFailStr(t, expected, got)
   125  	}
   126  	gotRune := iter.Next()
   127  	if gotRune != '\n' {
   128  		testFailRunes(t, '\n', gotRune)
   129  	}
   130  	expected, got = "AB CD", iter.UntilNextLineBreak()
   131  	if got != expected {
   132  		testFailStr(t, expected, got)
   133  	}
   134  	gotRune = iter.Next()
   135  	if gotRune != 'D' {
   136  		testFailRunes(t, 'D', gotRune)
   137  	}
   138  	expected, got = "AB C", iter.UntilNextLineBreak()
   139  	if got != expected {
   140  		testFailStr(t, expected, got)
   141  	}
   142  	for _, expected := range []rune{'C', ' ', 'B', 'A'} {
   143  		got := iter.Next()
   144  		if got != expected {
   145  			testFailRunes(t, expected, got)
   146  		}
   147  	}
   148  	expected, got = "", iter.UntilNextLineBreak()
   149  	if got != expected {
   150  		testFailStr(t, expected, got)
   151  	}
   152  	gotRune = iter.Next()
   153  	if gotRune != '\n' {
   154  		testFailRunes(t, '\n', gotRune)
   155  	}
   156  	gotRune = iter.Next()
   157  	if gotRune != -1 {
   158  		testFailRunes(t, -1, gotRune)
   159  	}
   160  }
   161  
   162  func TestGlyphsIterator(t *testing.T) {
   163  	iter := newGlyphsIterator([]GlyphIndex{1, 2, 3, 4}, false)
   164  	for n, expected := range []GlyphIndex{1, 2, 3, 4} {
   165  		got, done := iter.Next()
   166  		if done {
   167  			t.Fatalf("test#%d unexpectedly done", n)
   168  		}
   169  		if got != expected {
   170  			t.Fatalf("test#%d expected %d got %d", n, expected, got)
   171  		}
   172  	}
   173  
   174  	got, done := iter.Next()
   175  	if got != 0 || !done {
   176  		t.Fatalf("expected done")
   177  	}
   178  }
   179  
   180  func TestGlyphsIteratorReverse(t *testing.T) {
   181  	iter := newGlyphsIterator([]GlyphIndex{1, 2, 3, 4}, true)
   182  	for n, expected := range []GlyphIndex{4, 3, 2, 1} {
   183  		got, done := iter.Next()
   184  		if done {
   185  			t.Fatalf("test#%d unexpectedly done", n)
   186  		}
   187  		if got != expected {
   188  			t.Fatalf("test#%d expected %d got %d", n, expected, got)
   189  		}
   190  	}
   191  
   192  	got, done := iter.Next()
   193  	if got != 0 || !done {
   194  		t.Fatalf("expected done")
   195  	}
   196  }