testmark-go

Rename Hunk.BlockTag -> InfoString.

commit cb78e34543b36ba3a7776a7f48c327a946146369 · parent 80687ba163 · browse this tree
Eric Myhre, 2021-10-05 11:16:14 UTC

"info string" is the correct term in some of the more authoritative
markdown specs for the subject.

Fixes https://github.com/warpfork/go-testmark/issues/2 .
diff --git a/patch.go b/patch.go
index 6ca50f2..014dc1b 100644
--- a/patch.go
+++ b/patch.go
@@ -47,7 +47,7 @@ func Patch(oldDoc *Document, hunks ...Hunk) (newDoc *Document) {
 			if len(newHunk.Body) > 0 && newHunk.Body[len(newHunk.Body)-1] == '\n' {
 				newBodyLines = newBodyLines[0 : len(newBodyLines)-1]
 			}
-			hunk.BlockTag = newHunk.BlockTag
+			hunk.InfoString = newHunk.InfoString
 
 			// Yeet from newHunks, as it's now handled.
 			delete(newHunks, hunk.Name)
@@ -60,7 +60,7 @@ func Patch(oldDoc *Document, hunks ...Hunk) (newDoc *Document) {
 		// Watch how this changes the offsets, so we can build a new DocHunk with info that's correct.
 		// (If you're just going to serialize this, it wouldn't matter, but if you want to patch multiple times, it matters.)
 		newLineStart := len(newDoc.Lines)
-		newDoc.Lines = appendHunkLines(newDoc.Lines, hunk.Name, hunk.BlockTag, newBodyLines)
+		newDoc.Lines = appendHunkLines(newDoc.Lines, hunk.Name, hunk.InfoString, newBodyLines)
 		newLineEnd := len(newDoc.Lines)
 		docHunk := DocHunk{
 			LineStart: newLineStart,
@@ -87,7 +87,7 @@ func Patch(oldDoc *Document, hunks ...Hunk) (newDoc *Document) {
 			newDoc.Lines = append(newDoc.Lines, []byte{})
 		}
 		// Append it.
-		newDoc.Lines = appendHunkLines(newDoc.Lines, hunk.Name, hunk.BlockTag, bytes.Split(hunk.Body, sigilLineBreak))
+		newDoc.Lines = appendHunkLines(newDoc.Lines, hunk.Name, hunk.InfoString, bytes.Split(hunk.Body, sigilLineBreak))
 		// And one more trailing line, at the end.
 		newDoc.Lines = append(newDoc.Lines, []byte{})
 	}
diff --git a/patch_test.go b/patch_test.go
index ce78332..f7a9e12 100644
--- a/patch_test.go
+++ b/patch_test.go
@@ -12,10 +12,10 @@ func TestPatch(t *testing.T) {
 	}
 	doc, err := ReadFile(filepath.Join(testdata, "example.md"))
 	doc = Patch(doc,
-		Hunk{Name: "more-data", BlockTag: "text", Body: []byte("you have been...\nreplaced.\nand gotten\nrather longer.")},
-		Hunk{Name: "this-is-the-data-name", BlockTag: "", Body: []byte("this one gets shorter.")},
-		Hunk{Name: "this-one-is-new", BlockTag: "json", Body: []byte(`{"hayo": "new data!"}`)},
-		Hunk{Name: "so-is-this", BlockTag: "json", Body: []byte(`{"appending": "is fun"}`)},
+		Hunk{Name: "more-data", InfoString: "text", Body: []byte("you have been...\nreplaced.\nand gotten\nrather longer.")},
+		Hunk{Name: "this-is-the-data-name", InfoString: "", Body: []byte("this one gets shorter.")},
+		Hunk{Name: "this-one-is-new", InfoString: "json", Body: []byte(`{"hayo": "new data!"}`)},
+		Hunk{Name: "so-is-this", InfoString: "json", Body: []byte(`{"appending": "is fun"}`)},
 	)
 	t.Logf("%s", doc.String())
 }
diff --git a/read.go b/read.go
index 2100876..6179b08 100644
--- a/read.go
+++ b/read.go
@@ -61,7 +61,7 @@ func Parse(data []byte) (*Document, error) {
 			switch inCodeBlock {
 			case false: // starting a block
 				if expectCodeBlock {
-					hunkInProgress.BlockTag = string(line[len(sigilCodeBlock):])
+					hunkInProgress.InfoString = string(line[len(sigilCodeBlock):])
 					codeBlockOffset = offset + len(origLine) + 1
 				}
 				expectCodeBlock = false
diff --git a/testmark.go b/testmark.go
index 0536c00..7b87315 100644
--- a/testmark.go
+++ b/testmark.go
@@ -48,7 +48,7 @@ type Hunk struct {
 
 	// The code block syntax hint (or more literally: anything that comes after the triple-tick that starts the code block).
 	// Usually we don't encourage use of this much in testmark, but it's here.  Can be empty.
-	BlockTag string
+	InfoString string
 
 	// The full body of the hunk, as bytes.
 	// (This is *still* a subslice of Document.Original, if this hunk was created by Parse, but probably a unique slice otherwise.)