Skip to content

Commit

Permalink
chore: some more connector tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Musat authored and gabotechs committed Dec 24, 2022
1 parent 95ee0b0 commit a77056a
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
long◁
b─┘
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
a┐
bb┘
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
a
└┐
bb◁
69 changes: 53 additions & 16 deletions internal/board/board_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,12 @@ func fileExists(path string) bool {
return err == nil
}

func expectTest(t *testing.T, result string) {
a := require.New(t)
_ = os.Mkdir(testPath, os.ModePerm)
fullPath := path.Join(testPath, path.Base(t.Name())+".txt")
print(result)
if fileExists(fullPath) && os.Getenv(RebuildTestsEnv) != "true" {
expected, err := os.ReadFile(fullPath)
a.NoError(err)
a.Equal(string(expected), result)
} else {
err := os.WriteFile(fullPath, []byte(result), os.ModePerm)
a.NoError(err)
}
}

type TestBlock struct {
name string
x int
y int
}

type TestConnection struct {
from int
to []int
Expand All @@ -61,6 +47,17 @@ func TestBoard(t *testing.T) {
{from: 1, to: []int{2}},
},
},
{
Name: "Cannot draw line when one is just above",
Blocks: []TestBlock{
{name: "foo.ts", x: 3, y: 4},
{name: "bar.ts", x: 3, y: 5},
},
Connections: []TestConnection{
{from: 0, to: []int{1}},
},
ExpectedError: "could not draw first vertical step on (3, 5) because there is no space",
},
{
Name: "ReverseDeps",
Blocks: []TestBlock{
Expand All @@ -71,6 +68,36 @@ func TestBoard(t *testing.T) {
{from: 1, to: []int{0}},
},
},
{
Name: "it should increase the board's size if necessary",
Blocks: []TestBlock{
{name: "long", x: 0, y: 0},
{name: "b", x: 2, y: 1},
},
Connections: []TestConnection{
{from: 1, to: []int{0}},
},
},
{
Name: "it should increase the board's size if necessary 2",
Blocks: []TestBlock{
{name: "a", x: 1, y: 0},
{name: "bb", x: 0, y: 2},
},
Connections: []TestConnection{
{from: 1, to: []int{0}},
},
},
{
Name: "it should increase the board's size if necessary 3",
Blocks: []TestBlock{
{name: "a", x: 1, y: 0},
{name: "bb", x: 0, y: 2},
},
Connections: []TestConnection{
{from: 0, to: []int{1}},
},
},
{
Name: "CrossedDeps",
Blocks: []TestBlock{
Expand Down Expand Up @@ -226,7 +253,17 @@ func TestBoard(t *testing.T) {
result, err := board.Render()
if tt.ExpectedError == "" {
a.NoError(err)
expectTest(t, result)
_ = os.Mkdir(testPath, os.ModePerm)
fullPath := path.Join(testPath, path.Base(t.Name())+".txt")
print(result)
if fileExists(fullPath) && os.Getenv(RebuildTestsEnv) != "true" {
expected, err := os.ReadFile(fullPath)
a.NoError(err)
a.Equal(string(expected), result)
} else {
err := os.WriteFile(fullPath, []byte(result), os.ModePerm)
a.NoError(err)
}
} else {
a.ErrorContains(err, tt.ExpectedError)
}
Expand Down
6 changes: 3 additions & 3 deletions internal/board/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ func (c *Connector) Render(matrix *graphics.Matrix) error {
var cur utils.Vector
if reverseY {
cur = tracer.MoveHorizontal(false)
if cur.X >= matrix.H() {
if cur.X >= matrix.W() {
matrix.ExpandRight(1)
}
} else {
cur = tracer.MoveVertical(false)
}
cell := matrix.Cell(cur)
if cell.Is(cellType, blockChar) || cell.Is(cellType, arrow) {
if cell == nil || cell.Is(cellType, blockChar) || cell.Is(cellType, arrow) {
return fmt.Errorf("could not draw first vertical step on (%d, %d) because there is no space", cur.X, cur.Y)
}

Expand All @@ -86,7 +86,7 @@ func (c *Connector) Render(matrix *graphics.Matrix) error {
cell = matrix.Cell(cur)
}
if cell == nil {
return fmt.Errorf("moved to invalid position (%d, %d) while tracing horizontal line", cur.X, cur.Y)
return fmt.Errorf("moved to invalid position (%d, %d) while tracing horizontal line. This error should not be reachable", cur.X, cur.Y)
}
cell.Tag(noCrossOwnership, c.from.Id)
}
Expand Down

0 comments on commit a77056a

Please sign in to comment.