Add entrypoint to the program

This commit is contained in:
sunboyy 2021-01-23 20:03:16 +07:00
parent f677158d69
commit 489edbd9e2
10 changed files with 927 additions and 266 deletions

View file

@ -0,0 +1,31 @@
package testutils
import (
"fmt"
"strings"
)
// ExpectMultiLineString compares two multi-line strings and report the difference
func ExpectMultiLineString(expected, actual string) error {
expectedLines := strings.Split(expected, "\n")
actualLines := strings.Split(actual, "\n")
numberOfComparableLines := len(expectedLines)
if len(actualLines) < numberOfComparableLines {
numberOfComparableLines = len(actualLines)
}
for i := 0; i < numberOfComparableLines; i++ {
if expectedLines[i] != actualLines[i] {
return fmt.Errorf("On line %d\nExpected: %v\nReceived: %v", i+1, expectedLines[i], actualLines[i])
}
}
if len(expectedLines) < len(actualLines) {
return fmt.Errorf("Unexpected lines:\n%s", strings.Join(actualLines[len(expectedLines):], "\n"))
} else if len(expectedLines) > len(actualLines) {
return fmt.Errorf("Missing lines:\n%s", strings.Join(expectedLines[len(actualLines):], "\n"))
}
return nil
}

View file

@ -0,0 +1,72 @@
package testutils_test
import (
"testing"
"github.com/sunboyy/repogen/internal/testutils"
)
func TestExpectMultiLineString(t *testing.T) {
t.Run("same string should return nil", func(t *testing.T) {
text := ` Hello world
this is a test text `
err := testutils.ExpectMultiLineString(text, text)
if err != nil {
t.Errorf("Expected = <nil>\nReceived = %s", err.Error())
}
})
t.Run("different string with same number of lines", func(t *testing.T) {
expectedText := ` Hello world
this is an expected text
how are you?`
actualText := ` Hello world
this is a real text
How are you?`
err := testutils.ExpectMultiLineString(expectedText, actualText)
expectedError := "On line 2\nExpected: this is an expected text\nReceived: this is a real text"
if err == nil || err.Error() != expectedError {
t.Errorf("Expected = %s\nReceived = %s", expectedError, err.Error())
}
})
t.Run("expected text longer than actual text", func(t *testing.T) {
expectedText := ` Hello world
this is an expected text
how are you?
I'm fine...
Thank you...`
actualText := ` Hello world
this is an expected text
how are you?`
err := testutils.ExpectMultiLineString(expectedText, actualText)
expectedError := "Missing lines:\nI'm fine...\nThank you..."
if err == nil || err.Error() != expectedError {
t.Errorf("Expected = %s\nReceived = %s", expectedError, err.Error())
}
})
t.Run("actual text longer than expected text", func(t *testing.T) {
expectedText := ` Hello world
this is an expected text
how are you?`
actualText := ` Hello world
this is an expected text
how are you?
I'm fine...
Thank you...`
err := testutils.ExpectMultiLineString(expectedText, actualText)
expectedError := "Unexpected lines:\nI'm fine...\nThank you..."
if err == nil || err.Error() != expectedError {
t.Errorf("Expected = %s\nReceived = %s", expectedError, err.Error())
}
})
}