Repository: kythe/kythe
Pull request #5806 (2023-08-23 17:54:25)

test: add tests for md package


Patch:
diff --git a/BUILD b/BUILD
index ae2790281..71456e4b0 100644
--- a/BUILD
+++ b/BUILD
@@ -34,6 +34,9 @@ sh_test(
 # gazelle:exclude **/*_go_proto/
 # gazelle:exclude **/*_rust_proto/
 # gazelle:prefix kythe.io
+# gazelle:map_kind go_binary go_binary //tools:build_rules/shims.bzl
+# gazelle:map_kind go_library go_library //tools:build_rules/shims.bzl
+# gazelle:map_kind go_test go_test //tools:build_rules/shims.bzl
 gazelle(
     name = "gazelle",
     gazelle = "//tools/gazelle",
diff --git a/kythe/go/util/md/BUILD b/kythe/go/util/md/BUILD
index ab11db925..49cb6eed5 100644
--- a/kythe/go/util/md/BUILD
+++ b/kythe/go/util/md/BUILD
@@ -1,4 +1,4 @@
-load("//tools:build_rules/shims.bzl", "go_library")
+load("//tools:build_rules/shims.bzl", "go_library", "go_test")
 
 package(default_visibility = ["//kythe:default_visibility"])
 
@@ -7,3 +7,10 @@ go_library(
     srcs = ["md.go"],
     importpath = "kythe.io/kythe/go/util/md",
 )
+
+go_test(
+    name = "md_test",
+    srcs = ["md_test.go"],
+    library = ":md",
+    deps = ["@com_github_google_go_cmp//cmp"],
+)


Test patch:
diff --git a/kythe/go/util/md/md_test.go b/kythe/go/util/md/md_test.go
new file mode 100644
index 000000000..3573ae20c
--- /dev/null
+++ b/kythe/go/util/md/md_test.go
@@ -0,0 +1,105 @@
+/*
+ * Copyright 2023 The Kythe Authors. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package md
+
+import (
+	"testing"
+
+	"github.com/google/go-cmp/cmp"
+)
+
+func TestEscape(t *testing.T) {
+	tests := []struct {
+		content     string
+		destination string
+		want        string
+	}{{
+		"foo",
+		"dest",
+		"[foo](dest)",
+	}, {
+		"f<b>oo&lt;/b&gt;",
+		"dest",
+		"[f<b>oo&lt;/b&gt;](dest)",
+	}, {
+		"f\\[oo\\]",
+		"dest",
+		"[f\\[oo\\]](dest)",
+	}, {
+		"foo",
+		"d(est)",
+		"[foo](d(est))",
+	}}
+
+	for _, tc := range tests {
+		got := Link(tc.content, tc.destination)
+		if diff := cmp.Diff(tc.want, got); diff != "" {
+			t.Errorf("(-want, +got): %v", diff)
+		}
+
+	}
+}
+
+func TestProcessLinks(t *testing.T) {
+	tests := []struct {
+		content string
+		links   []LinkInfo
+		want    string
+	}{{
+		"No links",
+		[]LinkInfo{},
+		"No links",
+	}, {
+		"Link",
+		[]LinkInfo{
+			LinkInfo{0, 4, "http://test"},
+		},
+		"[Link](http://test)",
+	}, {
+		"Link Here",
+		[]LinkInfo{
+			LinkInfo{0, 9, "http://test"},
+		},
+		"[Link Here](http://test)",
+	}, {
+		"Link here but not here",
+		[]LinkInfo{
+			LinkInfo{5, 4, "http://test"},
+		},
+		"Link [here](http://test) but not here",
+	}, {
+		"Two links",
+		[]LinkInfo{
+			LinkInfo{0, 3, "http://test"},
+			LinkInfo{4, 5, "http://test2"},
+		},
+		"[Two](http://test) [links](http://test2)",
+	}, {
+		"Link out of bounds",
+		[]LinkInfo{
+			LinkInfo{18, 1, "http://test"},
+		},
+		"Link out of bounds",
+	}}
+
+	for _, tc := range tests {
+		got := ProcessLinks(tc.content, tc.links)
+		if diff := cmp.Diff(tc.want, got); diff != "" {
+			t.Errorf("TestConvert(%s, %#v): got unexpected diff (-want, +got): %v", tc.content, tc.links, diff)
+		}
+	}
+}