Skip to content

Commit

Permalink
Mark test helpers (#383)
Browse files Browse the repository at this point in the history
  • Loading branch information
rtsao authored Sep 12, 2020
1 parent c63ea95 commit f9858fc
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 0 deletions.
5 changes: 5 additions & 0 deletions internal/bundler/bundler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func es(version int) compat.Feature {
}

func assertEqual(t *testing.T, a interface{}, b interface{}) {
t.Helper()
if a != b {
stringA := fmt.Sprintf("%v", a)
stringB := fmt.Sprintf("%v", b)
Expand All @@ -43,6 +44,7 @@ func assertEqual(t *testing.T, a interface{}, b interface{}) {
}

func assertLog(t *testing.T, msgs []logger.Msg, expected string) {
t.Helper()
text := ""
for _, msg := range msgs {
text += msg.String(logger.StderrOptions{}, logger.TerminalInfo{})
Expand Down Expand Up @@ -76,8 +78,10 @@ type suite struct {
}

func (s *suite) expectBundled(t *testing.T, args bundled) {
t.Helper()
testName := t.Name()
t.Run("", func(t *testing.T) {
t.Helper()
fs := fs.MockFS(args.files)
args.options.ExtensionOrder = []string{".tsx", ".ts", ".jsx", ".js", ".json"}
if args.options.AbsOutputFile != "" {
Expand Down Expand Up @@ -129,6 +133,7 @@ var globalSuites map[*suite]bool
var globalUpdateSnapshots bool

func (s *suite) compareSnapshot(t *testing.T, testName string, generated string) {
t.Helper()
// Initialize the test suite during the first test
s.mutex.Lock()
defer s.mutex.Unlock()
Expand Down
2 changes: 2 additions & 0 deletions internal/fs/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ func TestRel(t *testing.T) {
fs := MockFS(map[string]string{})

expect := func(a string, b string, c string) {
t.Helper()
t.Run(fmt.Sprintf("Rel(%q, %q) == %q", a, b, c), func(t *testing.T) {
t.Helper()
rel, ok := fs.Rel(a, b)
if !ok {
t.Fatalf("!ok")
Expand Down
13 changes: 13 additions & 0 deletions internal/lexer/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
)

func assertEqualStrings(t *testing.T, a string, b string) {
t.Helper()
pretty := func(text string) string {
builder := strings.Builder{}
builder.WriteRune('"')
Expand All @@ -36,7 +37,9 @@ func lexToken(t *testing.T, contents string) T {
}

func expectLexerError(t *testing.T, contents string, expected string) {
t.Helper()
t.Run(contents, func(t *testing.T) {
t.Helper()
log := logger.NewDeferLog()
func() {
defer func() {
Expand All @@ -57,7 +60,9 @@ func expectLexerError(t *testing.T, contents string, expected string) {
}

func expectHashbang(t *testing.T, contents string, expected string) {
t.Helper()
t.Run(contents, func(t *testing.T) {
t.Helper()
log := logger.NewDeferLog()
lexer := func() Lexer {
defer func() {
Expand All @@ -83,7 +88,9 @@ func TestHashbang(t *testing.T) {
}

func expectIdentifier(t *testing.T, contents string, expected string) {
t.Helper()
t.Run(contents, func(t *testing.T) {
t.Helper()
log := logger.NewDeferLog()
lexer := func() Lexer {
defer func() {
Expand Down Expand Up @@ -120,7 +127,9 @@ func TestIdentifier(t *testing.T) {
}

func expectNumber(t *testing.T, contents string, expected float64) {
t.Helper()
t.Run(contents, func(t *testing.T) {
t.Helper()
log := logger.NewDeferLog()
lexer := func() Lexer {
defer func() {
Expand Down Expand Up @@ -325,7 +334,9 @@ func TestNumericLiteral(t *testing.T) {
}

func expectBigInteger(t *testing.T, contents string, expected string) {
t.Helper()
t.Run(contents, func(t *testing.T) {
t.Helper()
log := logger.NewDeferLog()
lexer := func() Lexer {
defer func() {
Expand Down Expand Up @@ -378,7 +389,9 @@ func TestBigIntegerLiteral(t *testing.T) {
}

func expectString(t *testing.T, contents string, expected string) {
t.Helper()
t.Run(contents, func(t *testing.T) {
t.Helper()
log := logger.NewDeferLog()
lexer := func() Lexer {
defer func() {
Expand Down
6 changes: 6 additions & 0 deletions internal/parser/parser_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import (
)

func expectParseErrorJSON(t *testing.T, contents string, expected string) {
t.Helper()
t.Run(contents, func(t *testing.T) {
t.Helper()
log := logger.NewDeferLog()
ParseJSON(log, test.SourceForTest(contents), ParseJSONOptions{})
msgs := log.Done()
Expand All @@ -27,7 +29,9 @@ func expectParseErrorJSON(t *testing.T, contents string, expected string) {
// code may not be valid JSON. That's ok because esbuild always outputs JS
// bundles, not JSON bundles.
func expectPrintedJSON(t *testing.T, contents string, expected string) {
t.Helper()
t.Run(contents, func(t *testing.T) {
t.Helper()
log := logger.NewDeferLog()
expr, ok := ParseJSON(log, test.SourceForTest(contents), ParseJSONOptions{})
msgs := log.Done()
Expand All @@ -47,7 +51,9 @@ func expectPrintedJSON(t *testing.T, contents string, expected string) {
}

func expectPrintedJSONWithWarning(t *testing.T, contents string, warning string, expected string) {
t.Helper()
t.Run(contents, func(t *testing.T) {
t.Helper()
log := logger.NewDeferLog()
expr, ok := ParseJSON(log, test.SourceForTest(contents), ParseJSONOptions{})
msgs := log.Done()
Expand Down
16 changes: 16 additions & 0 deletions internal/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import (
)

func expectParseError(t *testing.T, contents string, expected string) {
t.Helper()
t.Run(contents, func(t *testing.T) {
t.Helper()
log := logger.NewDeferLog()
Parse(log, test.SourceForTest(contents), config.Options{})
msgs := log.Done()
Expand All @@ -28,7 +30,9 @@ func expectParseError(t *testing.T, contents string, expected string) {
}

func expectParseErrorTarget(t *testing.T, esVersion int, contents string, expected string) {
t.Helper()
t.Run(contents, func(t *testing.T) {
t.Helper()
log := logger.NewDeferLog()
Parse(log, test.SourceForTest(contents), config.Options{
UnsupportedFeatures: compat.UnsupportedFeatures(map[compat.Engine][]int{
Expand All @@ -45,7 +49,9 @@ func expectParseErrorTarget(t *testing.T, esVersion int, contents string, expect
}

func expectPrinted(t *testing.T, contents string, expected string) {
t.Helper()
t.Run(contents, func(t *testing.T) {
t.Helper()
log := logger.NewDeferLog()
tree, ok := Parse(log, test.SourceForTest(contents), config.Options{})
msgs := log.Done()
Expand All @@ -68,7 +74,9 @@ func expectPrinted(t *testing.T, contents string, expected string) {
}

func expectPrintedMangle(t *testing.T, contents string, expected string) {
t.Helper()
t.Run(contents, func(t *testing.T) {
t.Helper()
log := logger.NewDeferLog()
tree, ok := Parse(log, test.SourceForTest(contents), config.Options{
MangleSyntax: true,
Expand All @@ -91,7 +99,9 @@ func expectPrintedMangle(t *testing.T, contents string, expected string) {
}

func expectPrintedTarget(t *testing.T, esVersion int, contents string, expected string) {
t.Helper()
t.Run(contents, func(t *testing.T) {
t.Helper()
log := logger.NewDeferLog()
unsupportedFeatures := compat.UnsupportedFeatures(map[compat.Engine][]int{
compat.ES: {esVersion},
Expand Down Expand Up @@ -121,7 +131,9 @@ func expectPrintedTarget(t *testing.T, esVersion int, contents string, expected
}

func expectPrintedTargetStrict(t *testing.T, esVersion int, contents string, expected string) {
t.Helper()
t.Run(contents, func(t *testing.T) {
t.Helper()
log := logger.NewDeferLog()
tree, ok := Parse(log, test.SourceForTest(contents), config.Options{
UnsupportedFeatures: compat.UnsupportedFeatures(map[compat.Engine][]int{
Expand Down Expand Up @@ -153,7 +165,9 @@ func expectPrintedTargetStrict(t *testing.T, esVersion int, contents string, exp
}

func expectParseErrorJSX(t *testing.T, contents string, expected string) {
t.Helper()
t.Run(contents, func(t *testing.T) {
t.Helper()
log := logger.NewDeferLog()
Parse(log, test.SourceForTest(contents), config.Options{
JSX: config.JSXOptions{
Expand All @@ -170,7 +184,9 @@ func expectParseErrorJSX(t *testing.T, contents string, expected string) {
}

func expectPrintedJSX(t *testing.T, contents string, expected string) {
t.Helper()
t.Run(contents, func(t *testing.T) {
t.Helper()
log := logger.NewDeferLog()
tree, ok := Parse(log, test.SourceForTest(contents), config.Options{
JSX: config.JSXOptions{
Expand Down
8 changes: 8 additions & 0 deletions internal/parser/parser_ts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (
)

func expectParseErrorTS(t *testing.T, contents string, expected string) {
t.Helper()
t.Run(contents, func(t *testing.T) {
t.Helper()
log := logger.NewDeferLog()
Parse(log, test.SourceForTest(contents), config.Options{
TS: config.TSOptions{
Expand All @@ -29,7 +31,9 @@ func expectParseErrorTS(t *testing.T, contents string, expected string) {
}

func expectPrintedTS(t *testing.T, contents string, expected string) {
t.Helper()
t.Run(contents, func(t *testing.T) {
t.Helper()
log := logger.NewDeferLog()
tree, ok := Parse(log, test.SourceForTest(contents), config.Options{
TS: config.TSOptions{
Expand All @@ -54,7 +58,9 @@ func expectPrintedTS(t *testing.T, contents string, expected string) {
}

func expectParseErrorTSX(t *testing.T, contents string, expected string) {
t.Helper()
t.Run(contents, func(t *testing.T) {
t.Helper()
log := logger.NewDeferLog()
Parse(log, test.SourceForTest(contents), config.Options{
TS: config.TSOptions{
Expand All @@ -74,7 +80,9 @@ func expectParseErrorTSX(t *testing.T, contents string, expected string) {
}

func expectPrintedTSX(t *testing.T, contents string, expected string) {
t.Helper()
t.Run(contents, func(t *testing.T) {
t.Helper()
log := logger.NewDeferLog()
tree, ok := Parse(log, test.SourceForTest(contents), config.Options{
TS: config.TSOptions{
Expand Down
9 changes: 9 additions & 0 deletions internal/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ import (
)

func assertEqual(t *testing.T, a interface{}, b interface{}) {
t.Helper()
if a != b {
t.Fatalf("%s != %s", a, b)
}
}

func expectPrintedCommon(t *testing.T, name string, contents string, expected string, options PrintOptions) {
t.Helper()
t.Run(name, func(t *testing.T) {
t.Helper()
log := logger.NewDeferLog()
tree, ok := parser.Parse(log, test.SourceForTest(contents), config.Options{})
msgs := log.Done()
Expand All @@ -40,22 +43,26 @@ func expectPrintedCommon(t *testing.T, name string, contents string, expected st
}

func expectPrinted(t *testing.T, contents string, expected string) {
t.Helper()
expectPrintedCommon(t, contents, contents, expected, PrintOptions{})
}

func expectPrintedMinify(t *testing.T, contents string, expected string) {
t.Helper()
expectPrintedCommon(t, contents+" [minified]", contents, expected, PrintOptions{
RemoveWhitespace: true,
})
}

func expectPrintedMangle(t *testing.T, contents string, expected string) {
t.Helper()
expectPrintedCommon(t, contents+" [minified]", contents, expected, PrintOptions{
MangleSyntax: true,
})
}

func expectPrintedTarget(t *testing.T, esVersion int, contents string, expected string) {
t.Helper()
expectPrintedCommon(t, contents, contents, expected, PrintOptions{
UnsupportedFeatures: compat.UnsupportedFeatures(map[compat.Engine][]int{
compat.ES: {esVersion},
Expand All @@ -64,6 +71,7 @@ func expectPrintedTarget(t *testing.T, esVersion int, contents string, expected
}

func expectPrintedTargetMinify(t *testing.T, esVersion int, contents string, expected string) {
t.Helper()
expectPrintedCommon(t, contents+" [minified]", contents, expected, PrintOptions{
UnsupportedFeatures: compat.UnsupportedFeatures(map[compat.Engine][]int{
compat.ES: {esVersion},
Expand All @@ -73,6 +81,7 @@ func expectPrintedTargetMinify(t *testing.T, esVersion int, contents string, exp
}

func expectPrintedTargetMangle(t *testing.T, esVersion int, contents string, expected string) {
t.Helper()
expectPrintedCommon(t, contents+" [minified]", contents, expected, PrintOptions{
UnsupportedFeatures: compat.UnsupportedFeatures(map[compat.Engine][]int{
compat.ES: {esVersion},
Expand Down
1 change: 1 addition & 0 deletions internal/test/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
)

func AssertEqual(t *testing.T, a interface{}, b interface{}) {
t.Helper()
if a != b {
t.Fatalf("%s != %s", a, b)
}
Expand Down

0 comments on commit f9858fc

Please sign in to comment.