Skip to content

Commit

Permalink
test: add tests for md package (kythe#5806)
Browse files Browse the repository at this point in the history
  • Loading branch information
schroederc authored Aug 23, 2023
1 parent de9ba4e commit afa8b21
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 1 deletion.
3 changes: 3 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 8 additions & 1 deletion kythe/go/util/md/BUILD
Original file line number Diff line number Diff line change
@@ -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"])

Expand All @@ -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"],
)
105 changes: 105 additions & 0 deletions kythe/go/util/md/md_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}

0 comments on commit afa8b21

Please sign in to comment.