github.com/gop9/olt@v0.0.0-20200202132135-d956aad50b08/framework/richtext/richtext_test.go (about)

     1  package richtext
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestSeparateStyles(t *testing.T) {
     8  	out := separateStyles([]styleSel{
     9  		{Sel: Sel{0, 20}, align: 'a'},
    10  		{Sel: Sel{10, 15}, align: 'b'},
    11  		{Sel: Sel{18, 30}, align: 'c'},
    12  		{Sel: Sel{25, 35}, align: 'd'},
    13  		{Sel: Sel{40, 45}, align: 'e'},
    14  		{Sel: Sel{50, 60}, align: 'f'},
    15  		{Sel: Sel{55, 57}, align: 'g'},
    16  		{Sel: Sel{100, 150}, align: 'h'},
    17  		{Sel: Sel{110, 140}, align: 'i'},
    18  		{Sel: Sel{120, 130}, align: 'j'},
    19  	})
    20  
    21  	tgt := []styleSel{
    22  		{Sel: Sel{0, 10}, align: 'a'},
    23  		{Sel: Sel{10, 15}, align: 'b'},
    24  		{Sel: Sel{15, 18}, align: 'a'},
    25  		{Sel: Sel{18, 25}, align: 'c'},
    26  		{Sel: Sel{25, 35}, align: 'd'},
    27  		{Sel: Sel{40, 45}, align: 'e'},
    28  		{Sel: Sel{50, 55}, align: 'f'},
    29  		{Sel: Sel{55, 57}, align: 'g'},
    30  		{Sel: Sel{57, 60}, align: 'f'},
    31  		{Sel: Sel{100, 110}, align: 'h'},
    32  		{Sel: Sel{110, 120}, align: 'i'},
    33  		{Sel: Sel{120, 130}, align: 'j'},
    34  		{Sel: Sel{130, 140}, align: 'i'},
    35  		{Sel: Sel{140, 150}, align: 'h'},
    36  	}
    37  
    38  	for i := range out {
    39  		t.Logf("%d %#v %c\n", i, out[i].Sel, out[i].align)
    40  	}
    41  
    42  	if len(out) != len(tgt) {
    43  		t.Fatalf("length mismatch\n")
    44  	}
    45  
    46  	for i := range out {
    47  		if out[i].Sel != tgt[i].Sel || out[i].align != tgt[i].align {
    48  			t.Fatalf("content mismatch at index %d\n", i)
    49  		}
    50  	}
    51  }
    52  
    53  func TestMergeStyles(t *testing.T) {
    54  	link := func(string) {}
    55  	out := mergeStyles(
    56  		[]styleSel{ // styleSels
    57  			{Sel: Sel{10, 20}, flags: 'a'},
    58  			{Sel: Sel{50, 60}, flags: 'b'},
    59  			{Sel: Sel{150, 170}, flags: 'c'},
    60  		},
    61  		[]styleSel{ // alignSels
    62  			{Sel: Sel{0, 100}, align: AlignLeft},
    63  		},
    64  		[]styleSel{ // linkSels
    65  			{Sel: Sel{10, 20}, link: link},
    66  			{Sel: Sel{200, 210}, link: link},
    67  		})
    68  
    69  	tgt := []styleSel{
    70  		{Sel: Sel{0, 10}, align: AlignLeft},
    71  		{Sel: Sel{10, 20}, flags: 'a', align: AlignLeft, link: link},
    72  		{Sel: Sel{20, 50}, align: AlignLeft},
    73  		{Sel: Sel{50, 60}, flags: 'b', align: AlignLeft},
    74  		{Sel: Sel{60, 100}, align: AlignLeft},
    75  		{Sel: Sel{150, 170}, flags: 'c'},
    76  		{Sel: Sel{200, 210}, link: link},
    77  	}
    78  
    79  	for i := range out {
    80  		t.Logf("%d %#v flags:%d align:%d link:%p", i, out[i].Sel, out[i].flags, out[i].align, out[i].link)
    81  	}
    82  
    83  	if len(out) != len(tgt) {
    84  		t.Fatal("length mismatch")
    85  	}
    86  
    87  	for i := range out {
    88  		if out[i].Sel != tgt[i].Sel || out[i].flags != tgt[i].flags || out[i].align != tgt[i].align || (out[i].link != nil) != (tgt[i].link != nil) {
    89  			t.Fatalf("content mismatch at %d", i)
    90  		}
    91  	}
    92  }