package main

import (
	"errors"
	"testing"
)

func Example_writers() {
	writers()
	// Output:
	// "count=3\n" "count=3\n" <nil> <nil>
	// *bytes.Buffer *bytes.Buffer
	// write failure: true
}
func Example_assertions() {
	assertions()
	// Output:
	// 42 true
	// "" false
	// false 7 true
	// true
}
func Example_anySlices() {
	anySlices()
	// Output:
	// []int [9 2] | []interface {} [1 2]
}
func Example_nilInterfaces() {
	nilInterfaces()
	// Output:
	// true false false
	// *main.Problem *main.Problem
	// <nil *Problem>
	// false true
	// true
}
func Example_switches() {
	switches()
	// Output:
	// nil interface
	// int 42
	// string "go"
	// nil *Problem
	// other main.UserID
}
func Example_comparisons() {
	comparisons()
	// Output:
	// false true
	// false
}

func TestWriteFailure(t *testing.T) {
	sentinel := errors.New("blocked")
	fail := WriterFunc(func([]byte) (int, error) { return 0, sentinel })
	if err := WriteCount(fail, 3); !errors.Is(err, sentinel) {
		t.Fatalf("got %v", err)
	}
}
func TestPanics(t *testing.T) {
	for _, tc := range []struct {
		name string
		run  func()
	}{
		{"failed assertion", func() { var x any = 42; _ = x.(string) }},
		{"nil assertion", func() { var x any; _ = x.(int) }},
		{"nil interface call", func() { var err error; _ = err.Error() }},
		{"slice equality", func() { var a any = []int{1}; var b any = []int{1}; _ = a == b }},
	} {
		t.Run(tc.name, func(t *testing.T) {
			defer func() {
				if recover() == nil {
					t.Fatal("expected panic")
				}
			}()
			tc.run()
		})
	}
}
func TestTypedNilAssertion(t *testing.T) {
	var err error = (*Problem)(nil)
	p, ok := err.(*Problem)
	if !ok || p != nil || err == nil {
		t.Fatal("typed nil semantics changed")
	}
}
