Skip to content

Commit

Permalink
Implemented CONVERT test
Browse files Browse the repository at this point in the history
  • Loading branch information
noborus committed Aug 27, 2024
1 parent eed2f72 commit a0556e0
Show file tree
Hide file tree
Showing 7 changed files with 453 additions and 3 deletions.
3 changes: 3 additions & 0 deletions oviewer/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,9 @@ func (root *Root) modeConfig(modeName string) (general, error) {

func (root *Root) setConverter(ctx context.Context, name string) {
m := root.Doc
if m.general.Converter == name {
return
}
m.general.Converter = name
m.conv = m.converterType(name)
root.Doc.ClearCache()
Expand Down
48 changes: 48 additions & 0 deletions oviewer/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1505,3 +1505,51 @@ func TestRoot_tailSection(t *testing.T) {
})
}
}

func TestRoot_setConverter(t *testing.T) {
tcellNewScreen = fakeScreen
defer func() {
tcellNewScreen = tcell.NewScreen
}()
type args struct {
name string
}
tests := []struct {
name string
args args
want Converter
}{
{
name: "testSetConverterEscape",
args: args{
name: "es",
},
want: newESConverter(),
},
{
name: "testSetConverterRaw",
args: args{
name: "raw",
},
want: newRawConverter(),
},
{
name: "testSetConverterAlign",
args: args{
name: "align",
},
want: newAlignConverter(false),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
root := rootHelper(t)
ctx := context.Background()
root.setConverter(ctx, tt.args.name)
got := root.Doc.conv
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("setConverter() = %v, want %v", got, tt.want)
}
})
}
}
4 changes: 2 additions & 2 deletions oviewer/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ func StrToContents(str string, tabWidth int) contents {
// RawStrToContents converts a single-line string into a one line of contents.
// Does not interpret escape sequences.
// 1 Content matches the characters displayed on the screen.
func RawStrToContents(str string, tabWdith int) contents {
return parseString(newRawConverter(), str, tabWdith)
func RawStrToContents(str string, tabWidth int) contents {
return parseString(newRawConverter(), str, tabWidth)
}

// parseString converts a string to lineContents.
Expand Down
44 changes: 44 additions & 0 deletions oviewer/content_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -810,3 +810,47 @@ func Test_widthPos_n(t *testing.T) {
})
}
}

func TestRawStrToContents(t *testing.T) {
type args struct {
str string
tabWidth int
}
tests := []struct {
name string
args args
want contents
}{
{
name: "red",
args: args{
str: "\x1B[31mred\x1B[m", tabWidth: 8,
},
want: contents{
{width: 0, style: tcell.StyleDefault, mainc: rune('^'), combc: nil},
{width: 0, style: tcell.StyleDefault, mainc: rune('['), combc: nil},
{width: 1, style: tcell.StyleDefault, mainc: rune('['), combc: nil},
{width: 1, style: tcell.StyleDefault, mainc: rune('3'), combc: nil},
{width: 1, style: tcell.StyleDefault, mainc: rune('1'), combc: nil},
{width: 1, style: tcell.StyleDefault, mainc: rune('m'), combc: nil},
{width: 1, style: tcell.StyleDefault, mainc: rune('r'), combc: nil},
{width: 1, style: tcell.StyleDefault, mainc: rune('e'), combc: nil},
{width: 1, style: tcell.StyleDefault, mainc: rune('d'), combc: nil},
{width: 0, style: tcell.StyleDefault, mainc: rune('^'), combc: nil},
{width: 0, style: tcell.StyleDefault, mainc: rune('['), combc: nil},
{width: 1, style: tcell.StyleDefault, mainc: rune('['), combc: nil},
{width: 1, style: tcell.StyleDefault, mainc: rune('m'), combc: nil},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := RawStrToContents(tt.args.str, tt.args.tabWidth); !reflect.DeepEqual(got, tt.want) {
str, _ := ContentsToStr(got)
str2, _ := ContentsToStr(tt.want)
t.Logf("got: %#v %#v", str, str2)
t.Errorf("RawStrToContents() = \n%v, want \n%v", got, tt.want)
}
})
}
}
2 changes: 1 addition & 1 deletion oviewer/convert_align.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func newAlignConverter(widthF bool) *align {
// Returns true if it is an escape sequence and a non-printing character.
func (a *align) convert(st *parseState) bool {
if len(st.lc) == 0 {
if a.delimiter == "\t" {
if !a.WidthF && a.delimiter == "\t" {
st.tabWidth = 1
}
a.reset()
Expand Down
73 changes: 73 additions & 0 deletions oviewer/convert_align_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,79 @@ func Test_align_convert(t *testing.T) {
want: false,
wantStr: "a ,b ,c\n",
},
{
name: "convertAlignDelmTab",
fields: fields{
es: newESConverter(),
maxWidths: []int{1, 2},
WidthF: false,
delimiter: "\t",
count: 0,
},
args: args{
st: &parseState{
lc: StrToContents("", 8),
mainc: '\n',
},
},
want: false,
wantStr: "",
},
{
name: "convertAlignDelmES",
fields: fields{
es: newESConverter(),
maxWidths: []int{1, 2},
WidthF: false,
delimiter: "\t",
count: 0,
},
args: args{
st: &parseState{
lc: StrToContents("", 8),
mainc: '\x1b',
},
},
want: true,
wantStr: "",
},
{
name: "convertAlignNoDelm",
fields: fields{
es: newESConverter(),
maxWidths: []int{},
WidthF: false,
delimiter: "\t",
count: 0,
},
args: args{
st: &parseState{
lc: StrToContents("", 8),
mainc: 'a',
},
},
want: false,
wantStr: "",
},
{
name: "convertAlignDelm2",
fields: fields{
es: newESConverter(),
maxWidths: []int{1, 2},
WidthF: false,
delimiter: ",",
count: 0,
},
args: args{
st: &parseState{
lc: StrToContents("a,b,", 8),
mainc: 'あ',
},
},
want: false,
wantStr: "a,b,",
},

{
name: "convertAlignWidth",
fields: fields{
Expand Down
Loading

0 comments on commit a0556e0

Please sign in to comment.