From 2e597e8b60add713fc1841329c2c793239ff6f56 Mon Sep 17 00:00:00 2001 From: Jakub Sobon Date: Fri, 1 Feb 2019 00:12:52 -0500 Subject: [PATCH] First test for sixteen. --- draw/segdisp/segment/segment.go | 16 +-- draw/segdisp/segment/segment_test.go | 8 +- .../segment/testsegment/testsegment.go | 24 ++++ draw/segdisp/sixteen/sixteen.go | 7 +- draw/segdisp/sixteen/sixteen_test.go | 118 +++++------------- 5 files changed, 73 insertions(+), 100 deletions(-) create mode 100644 draw/segdisp/segment/testsegment/testsegment.go diff --git a/draw/segdisp/segment/segment.go b/draw/segdisp/segment/segment.go index 7e07dc2..92da56c 100644 --- a/draw/segdisp/segment/segment.go +++ b/draw/segdisp/segment/segment.go @@ -24,25 +24,25 @@ import ( "github.com/mum4k/termdash/draw" ) -// SegmentType identifies the type of the segment that is drawn. -type SegmentType int +// Type identifies the type of the segment that is drawn. +type Type int // String implements fmt.Stringer() -func (st SegmentType) String() string { +func (st Type) String() string { if n, ok := segmentTypeNames[st]; ok { return n } - return "SegmentTypeUnknown" + return "TypeUnknown" } -// segmentTypeNames maps SegmentType values to human readable names. -var segmentTypeNames = map[SegmentType]string{ +// segmentTypeNames maps Type values to human readable names. +var segmentTypeNames = map[Type]string{ Horizontal: "Horizontal", Vertical: "Vertical", } const ( - segmentTypeUnknown SegmentType = iota + segmentTypeUnknown Type = iota // Horizontal is a horizontal segment. Horizontal @@ -125,7 +125,7 @@ func validArea(ar image.Rectangle) error { // HV draws a horizontal or a vertical display segment, filling the provided area. // The segment will have slopes on both of its ends. -func HV(bc *braille.Canvas, ar image.Rectangle, st SegmentType, opts ...Option) error { +func HV(bc *braille.Canvas, ar image.Rectangle, st Type, opts ...Option) error { if err := validArea(ar); err != nil { return err } diff --git a/draw/segdisp/segment/segment_test.go b/draw/segdisp/segment/segment_test.go index 2da9697..ba9ea7e 100644 --- a/draw/segdisp/segment/segment_test.go +++ b/draw/segdisp/segment/segment_test.go @@ -34,7 +34,7 @@ func TestHV(t *testing.T) { opts []Option cellCanvas image.Rectangle // Canvas in cells that will be converted to braille canvas for drawing. ar image.Rectangle - st SegmentType + st Type want func(size image.Point) *faketerm.Terminal wantErr bool }{ @@ -84,14 +84,14 @@ func TestHV(t *testing.T) { desc: "fails on unsupported segment type (too small)", cellCanvas: image.Rect(0, 0, 1, 1), ar: image.Rect(0, 0, 2, 2), - st: SegmentType(0), + st: Type(0), wantErr: true, }, { desc: "fails on unsupported segment type (too large)", cellCanvas: image.Rect(0, 0, 1, 1), ar: image.Rect(0, 0, 2, 2), - st: SegmentType(int(Vertical) + 1), + st: Type(int(Vertical) + 1), wantErr: true, }, { @@ -1112,7 +1112,7 @@ func TestHV(t *testing.T) { // hvSegment is one horizontal or vertical segment. type hvSegment struct { ar image.Rectangle - st SegmentType + st Type } // diagSegment is one diagonal segment. diff --git a/draw/segdisp/segment/testsegment/testsegment.go b/draw/segdisp/segment/testsegment/testsegment.go new file mode 100644 index 0000000..2308ab0 --- /dev/null +++ b/draw/segdisp/segment/testsegment/testsegment.go @@ -0,0 +1,24 @@ +// Package testsegment provides helpers for tests that use the segment package. +package testsegment + +import ( + "fmt" + "image" + + "github.com/mum4k/termdash/canvas/braille" + "github.com/mum4k/termdash/draw/segdisp/segment" +) + +// MustHV draws the segment or panics. +func MustHV(bc *braille.Canvas, ar image.Rectangle, st segment.Type, opts ...segment.Option) { + if err := segment.HV(bc, ar, st, opts...); err != nil { + panic(fmt.Sprintf("segment.HV => unexpected error: %v", err)) + } +} + +// MustDiagonal draws the segment or panics. +func MustDiagonal(bc *braille.Canvas, ar image.Rectangle, width int, dt segment.DiagonalType, opts ...segment.DiagonalOption) { + if err := segment.Diagonal(bc, ar, width, dt, opts...); err != nil { + panic(fmt.Sprintf("segment.Diagonal => unexpected error: %v", err)) + } +} diff --git a/draw/segdisp/sixteen/sixteen.go b/draw/segdisp/sixteen/sixteen.go index 5dc646e..0b97d8f 100644 --- a/draw/segdisp/sixteen/sixteen.go +++ b/draw/segdisp/sixteen/sixteen.go @@ -82,7 +82,7 @@ var segmentNames = map[Segment]string{ K: "K", L: "L", M: "M", - N: "M", + N: "N", } const ( @@ -254,7 +254,6 @@ func (d *Display) Draw(cvs *canvas.Canvas, opts ...Option) error { } bcAr := area.WithRatio(bc.Area(), image.Point{3, 5}) - log.Printf("XXX before:%v after:%v", bc.Area(), bcAr) segW := segWidth(bcAr) if segW == 4 { segW = 5 @@ -310,7 +309,7 @@ func (d *Display) Draw(cvs *canvas.Canvas, opts ...Option) error { for _, segArg := range []struct { s Segment - st segment.SegmentType + st segment.Type ar image.Rectangle opts []segment.Option }{ @@ -334,6 +333,7 @@ func (d *Display) Draw(cvs *canvas.Canvas, opts ...Option) error { if !d.segments[segArg.s] { continue } + log.Printf("segment.HV for %v, ar:%v", segArg.s, segArg.ar) if err := segment.HV(bc, segArg.ar, segArg.st, segArg.opts...); err != nil { return fmt.Errorf("failed to draw segment %v, segment.HV => %v", segArg.s, err) } @@ -376,6 +376,7 @@ func (d *Display) Draw(cvs *canvas.Canvas, opts ...Option) error { if !d.segments[segArg.s] { continue } + log.Printf("segment.Diagonal for %v, ar:%v", segArg.s, segArg.ar) if err := segment.Diagonal(bc, segArg.ar, segW, segArg.dt); err != nil { return fmt.Errorf("failed to draw segment %v, segment.Diagonal => %v", segArg.s, err) } diff --git a/draw/segdisp/sixteen/sixteen_test.go b/draw/segdisp/sixteen/sixteen_test.go index db81ed4..c167f04 100644 --- a/draw/segdisp/sixteen/sixteen_test.go +++ b/draw/segdisp/sixteen/sixteen_test.go @@ -20,6 +20,9 @@ import ( "github.com/mum4k/termdash/area" "github.com/mum4k/termdash/canvas" + "github.com/mum4k/termdash/canvas/braille/testbraille" + "github.com/mum4k/termdash/draw/segdisp/segment" + "github.com/mum4k/termdash/draw/segdisp/segment/testsegment" "github.com/mum4k/termdash/terminal/faketerm" ) @@ -35,40 +38,12 @@ func TestDraw(t *testing.T) { wantErr bool }{ { - desc: "smallest display 6x5, all segments", + desc: "empty when no segments set", cellCanvas: image.Rect(0, 0, 6, 5), - /* - update: func(d *Display) error { - for _, s := range AllSegments() { - if err := d.SetSegment(s); err != nil { - return err - } - } - return nil - },*/ - update: func(d *Display) error { - return d.SetCharacter('w') - }, }, { - desc: "8x6, all segments", - cellCanvas: image.Rect(0, 0, 8, 6), - /* - update: func(d *Display) error { - for _, s := range AllSegments() { - if err := d.SetSegment(s); err != nil { - return err - } - } - return nil - },*/ - update: func(d *Display) error { - return d.SetCharacter('W') - }, - }, - { - desc: "16x12, all segments", - cellCanvas: image.Rect(0, 0, 16, 12), + desc: "smallest valid display 6x5, all segments", + cellCanvas: image.Rect(0, 0, 6, 5), update: func(d *Display) error { for _, s := range AllSegments() { if err := d.SetSegment(s); err != nil { @@ -77,60 +52,33 @@ func TestDraw(t *testing.T) { } return nil }, - /* - update: func(d *Display) error { - return d.SetCharacter('W') - },*/ - }, - { - desc: "32x24, all segments", - cellCanvas: image.Rect(0, 0, 32, 24), - /* - update: func(d *Display) error { - for _, s := range AllSegments() { - if err := d.SetSegment(s); err != nil { - return err - } - } - return nil - },*/ - update: func(d *Display) error { - return d.SetCharacter('W') - }, - }, - { - desc: "64x48, all segments", - cellCanvas: image.Rect(0, 0, 64, 48), - /* - update: func(d *Display) error { - for _, s := range AllSegments() { - if err := d.SetSegment(s); err != nil { - return err - } - } - return nil - },*/ - update: func(d *Display) error { - return d.SetCharacter('W') - }, - }, - { - desc: "80x64, all segments", - cellCanvas: image.Rect(0, 0, 80, 64), - update: func(d *Display) error { - for _, s := range AllSegments() { - if err := d.SetSegment(s); err != nil { - return err - } - } - return nil - }, - }, - { - desc: "80x64, W", - cellCanvas: image.Rect(0, 0, 80, 64), - update: func(d *Display) error { - return d.SetCharacter('W') + want: func(size image.Point) *faketerm.Terminal { + ft := faketerm.MustNew(size) + bc := testbraille.MustNew(ft.Area()) + + testsegment.MustHV(bc, image.Rect(1, 0, 4, 1), segment.Horizontal) // A1 + testsegment.MustHV(bc, image.Rect(5, 0, 8, 1), segment.Horizontal) // A2 + + testsegment.MustHV(bc, image.Rect(0, 1, 1, 8), segment.Vertical) // F + testsegment.MustHV(bc, image.Rect(4, 1, 5, 8), segment.Vertical) // J + testsegment.MustHV(bc, image.Rect(8, 1, 9, 8), segment.Vertical) // B + + testsegment.MustHV(bc, image.Rect(1, 8, 4, 9), segment.Horizontal) // G1 + testsegment.MustHV(bc, image.Rect(5, 8, 8, 9), segment.Horizontal) // G2 + + testsegment.MustHV(bc, image.Rect(0, 9, 1, 16), segment.Vertical) // E + testsegment.MustHV(bc, image.Rect(4, 9, 5, 16), segment.Vertical) // M + testsegment.MustHV(bc, image.Rect(8, 9, 9, 16), segment.Vertical) // C + + testsegment.MustHV(bc, image.Rect(1, 16, 4, 17), segment.Horizontal) // D1 + testsegment.MustHV(bc, image.Rect(5, 16, 8, 17), segment.Horizontal) // D2 + + testsegment.MustDiagonal(bc, image.Rect(1, 1, 4, 8), 1, segment.LeftToRight) // H + testsegment.MustDiagonal(bc, image.Rect(5, 1, 8, 8), 1, segment.RightToLeft) // K + testsegment.MustDiagonal(bc, image.Rect(1, 9, 4, 16), 1, segment.RightToLeft) // N + testsegment.MustDiagonal(bc, image.Rect(5, 9, 8, 16), 1, segment.LeftToRight) // L + testbraille.MustApply(bc, ft) + return ft }, }, }