Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

maybe a test package?? #468

Open
komuw opened this issue Aug 8, 2024 · 2 comments
Open

maybe a test package?? #468

komuw opened this issue Aug 8, 2024 · 2 comments

Comments

@komuw
Copy link
Owner

komuw commented Aug 8, 2024

// is.go
package is

import (
	"fmt"
	"strings"
	"testing"
)

func Equal[T comparable](t *testing.T, got, want T) {
	// t := &testing.T{} // TODO: is this possible?
	t.Helper()

	if got == want {
		return
	}

	txt1 := fmt.Sprintf("%+#v", got)
	txt2 := fmt.Sprintf("%+#v", want)
	if !strings.HasSuffix(txt1, "\n") {
		txt1 = txt1 + "\n"
	}
	if !strings.HasSuffix(txt2, "\n") {
		txt2 = txt2 + "\n"
	}

	msg := ""
	df := diff(txt1, txt2)
	if len(df) > 0 {
		msg = msg + "got != want\n"
		msg = msg + "diff (-got, +want):"
		msg = msg + fmt.Sprintf("\n  %T(\n%v  )", got, df)
	} else {
		msg = msg + fmt.Sprintf("got: %T(%v). want: %T(%v)", got, got, want, want)
	}

	t.Fatal(msg)
}
@komuw
Copy link
Owner Author

komuw commented Aug 8, 2024

from https://github.com/komuw/kama/blob/main/diff.go

package is

import (
	"fmt"
	"strings"
)

// Most of the code here is insipired by(or taken from):
//   (a) https://github.com/rsc/diff whose license(BSD 3-Clause "New" or "Revised" License) can be found here: https://github.com/rsc/diff/blob/master/LICENSE

// diff returns a formatted diff of the two texts,
// showing the entire text and the minimum line-level
// additions and removals to turn text1 into text2.
// (That is, lines only in text1 appear with a leading -,
// and lines only in text2 appear with a leading +.)
func diff(text1, text2 string) string {
	if text1 != "" && !strings.HasSuffix(text1, "\n") {
		text1 += "(missing final newline)"
	}
	lines1 := strings.Split(text1, "\n")
	lines1 = lines1[:len(lines1)-1] // remove empty string after final line
	if text2 != "" && !strings.HasSuffix(text2, "\n") {
		text2 += "(missing final newline)"
	}
	lines2 := strings.Split(text2, "\n")
	lines2 = lines2[:len(lines2)-1] // remove empty string after final line

	// Naive dynamic programming algorithm for edit distance.
	// https://en.wikipedia.org/wiki/Wagner–Fischer_algorithm
	// dist[i][j] = edit distance between lines1[:len(lines1)-i] and lines2[:len(lines2)-j]
	// (The reversed indices make following the minimum cost path
	// visit lines in the same order as in the text.)
	dist := make([][]int, len(lines1)+1)
	for i := range dist {
		dist[i] = make([]int, len(lines2)+1)
		if i == 0 {
			for j := range dist[0] {
				dist[0][j] = j
			}
			continue
		}
		for j := range dist[i] {
			if j == 0 {
				dist[i][0] = i
				continue
			}
			cost := dist[i][j-1] + 1
			if cost > dist[i-1][j]+1 {
				cost = dist[i-1][j] + 1
			}
			if lines1[len(lines1)-i] == lines2[len(lines2)-j] {
				if cost > dist[i-1][j-1] {
					cost = dist[i-1][j-1]
				}
			}
			dist[i][j] = cost
		}
	}

	var buf strings.Builder
	i, j := len(lines1), len(lines2)
	for i > 0 || j > 0 {
		cost := dist[i][j]
		if i > 0 && j > 0 && cost == dist[i-1][j-1] && lines1[len(lines1)-i] == lines2[len(lines2)-j] {
			fmt.Fprintf(&buf, " %s\n", lines1[len(lines1)-i])
			i--
			j--
		} else if i > 0 && cost == dist[i-1][j]+1 {
			fmt.Fprintf(&buf, "-\t%s,\n", lines1[len(lines1)-i])
			i--
		} else {
			fmt.Fprintf(&buf, "+\t%s,\n", lines2[len(lines2)-j])
			j--
		}
	}
	return buf.String()
}

@komuw
Copy link
Owner Author

komuw commented Aug 8, 2024

// is_test.go
package is

import (
	"fmt"
	"testing"

	"go.akshayshah.org/attest"
)

func TestEqual(t *testing.T) {
	t.Run("ok", func(t *testing.T) {
		Equal(t, 4, 5)

		fmt.Println()
		fmt.Println()
		attest.Equal(t, 4, 5)
		_ = attest.Cmp()

		// Equal(t, "komu", "kojk")
	})
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant