vendor tsgo
This commit is contained in:
61
tools/tsgo/internal/debug/debug.go
Normal file
61
tools/tsgo/internal/debug/debug.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package debug
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func Fail(reason string) {
|
||||
if len(reason) == 0 {
|
||||
reason = "Debug failure."
|
||||
} else {
|
||||
reason = "Debug failure. " + reason
|
||||
}
|
||||
// runtime.Breakpoint()
|
||||
panic(reason)
|
||||
}
|
||||
|
||||
func FailBadSyntaxKind(node interface{ KindString() string }, message ...any) {
|
||||
var msg string
|
||||
if len(message) == 0 {
|
||||
msg = "Unexpected node."
|
||||
} else {
|
||||
msg = fmt.Sprint(message...)
|
||||
}
|
||||
Fail(fmt.Sprintf("%s\nNode %s was unexpected.", msg, node.KindString()))
|
||||
}
|
||||
|
||||
func AssertNever(member any, message ...any) {
|
||||
var msg string
|
||||
if len(message) == 0 {
|
||||
msg = "Illegal value:"
|
||||
} else {
|
||||
msg = fmt.Sprint(message...)
|
||||
}
|
||||
var detail string
|
||||
if m, ok := member.(interface{ KindString() string }); ok {
|
||||
detail = m.KindString()
|
||||
} else if m, ok := member.(fmt.Stringer); ok {
|
||||
detail = m.String()
|
||||
} else {
|
||||
detail = fmt.Sprintf("%v", member)
|
||||
}
|
||||
Fail(fmt.Sprintf("%s %s", msg, detail))
|
||||
}
|
||||
|
||||
func Assert(value bool, message ...any) {
|
||||
if value {
|
||||
return
|
||||
}
|
||||
assertSlow(message...)
|
||||
}
|
||||
|
||||
func assertSlow(message ...any) {
|
||||
// See https://dave.cheney.net/2020/05/02/mid-stack-inlining-in-go
|
||||
var msg string
|
||||
if len(message) > 0 {
|
||||
msg = "False expression: " + fmt.Sprint(message...)
|
||||
} else {
|
||||
msg = "False expression."
|
||||
}
|
||||
Fail(msg)
|
||||
}
|
||||
96
tools/tsgo/internal/debug/debug_test.go
Normal file
96
tools/tsgo/internal/debug/debug_test.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package debug_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/microsoft/typescript-go/internal/debug"
|
||||
"github.com/microsoft/typescript-go/internal/testutil"
|
||||
)
|
||||
|
||||
func TestFailEmptyReason(t *testing.T) {
|
||||
t.Parallel()
|
||||
testutil.AssertPanics(t, func() {
|
||||
debug.Fail("")
|
||||
}, "Debug failure.")
|
||||
}
|
||||
|
||||
func TestFailWithReason(t *testing.T) {
|
||||
t.Parallel()
|
||||
testutil.AssertPanics(t, func() {
|
||||
debug.Fail("something went wrong")
|
||||
}, "Debug failure. something went wrong")
|
||||
}
|
||||
|
||||
type mockNode struct{ kind string }
|
||||
|
||||
func (m mockNode) KindString() string { return m.kind }
|
||||
|
||||
func TestFailBadSyntaxKindNoMessage(t *testing.T) {
|
||||
t.Parallel()
|
||||
testutil.AssertPanics(t, func() {
|
||||
debug.FailBadSyntaxKind(mockNode{"FooNode"})
|
||||
}, "Debug failure. Unexpected node.\nNode FooNode was unexpected.")
|
||||
}
|
||||
|
||||
func TestFailBadSyntaxKindWithMessage(t *testing.T) {
|
||||
t.Parallel()
|
||||
testutil.AssertPanics(t, func() {
|
||||
debug.FailBadSyntaxKind(mockNode{"BarNode"}, "custom message")
|
||||
}, "Debug failure. custom message\nNode BarNode was unexpected.")
|
||||
}
|
||||
|
||||
func TestAssertNeverDefaultMessageKindString(t *testing.T) {
|
||||
t.Parallel()
|
||||
testutil.AssertPanics(t, func() {
|
||||
debug.AssertNever(mockNode{"TestNode"})
|
||||
}, "Debug failure. Illegal value: TestNode")
|
||||
}
|
||||
|
||||
func TestAssertNeverCustomMessageKindString(t *testing.T) {
|
||||
t.Parallel()
|
||||
testutil.AssertPanics(t, func() {
|
||||
debug.AssertNever(mockNode{"TestNode"}, "bad value:")
|
||||
}, "Debug failure. bad value: TestNode")
|
||||
}
|
||||
|
||||
type mockStringer struct{ s string }
|
||||
|
||||
func (m mockStringer) String() string { return m.s }
|
||||
|
||||
func TestAssertNeverStringer(t *testing.T) {
|
||||
t.Parallel()
|
||||
testutil.AssertPanics(t, func() {
|
||||
debug.AssertNever(mockStringer{"hello"})
|
||||
}, "Debug failure. Illegal value: hello")
|
||||
}
|
||||
|
||||
func TestAssertNeverFallback(t *testing.T) {
|
||||
t.Parallel()
|
||||
testutil.AssertPanics(t, func() {
|
||||
debug.AssertNever(42)
|
||||
}, "Debug failure. Illegal value: 42")
|
||||
}
|
||||
|
||||
func TestAssertTrue(t *testing.T) {
|
||||
t.Parallel()
|
||||
debug.Assert(true)
|
||||
}
|
||||
|
||||
func TestAssertTrueWithMessage(t *testing.T) {
|
||||
t.Parallel()
|
||||
debug.Assert(true, "this should not trigger")
|
||||
}
|
||||
|
||||
func TestAssertFalseNoMessage(t *testing.T) {
|
||||
t.Parallel()
|
||||
testutil.AssertPanics(t, func() {
|
||||
debug.Assert(false)
|
||||
}, "Debug failure. False expression.")
|
||||
}
|
||||
|
||||
func TestAssertFalseWithMessage(t *testing.T) {
|
||||
t.Parallel()
|
||||
testutil.AssertPanics(t, func() {
|
||||
debug.Assert(false, "expected x > 0")
|
||||
}, "Debug failure. False expression: expected x > 0")
|
||||
}
|
||||
Reference in New Issue
Block a user