kata-containers/tests/cmd/check-markdown/utils_test.go
Chelsea Mafrica 8ad433d4ad tests: move markdown check tool to main repo
Move the tool as a dependency for static checks migration.

Fixes #8187

Signed-off-by: Bin Liu <bin@hyper.sh>
Signed-off-by: Chelsea Mafrica <chelsea.e.mafrica@intel.com>
Signed-off-by: Gabriela Cervantes <gabriela.cervantes.tellez@intel.com>
Signed-off-by: Ganesh Maharaj Mahalingam <ganesh.mahalingam@intel.com>
Signed-off-by: James O. D. Hunt <james.o.hunt@intel.com>
Signed-off-by: Julio Montes <julio.montes@intel.com>
2023-11-28 11:13:55 -08:00

150 lines
3.1 KiB
Go

//
// Copyright (c) 2019 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package main
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestSplitLink(t *testing.T) {
assert := assert.New(t)
type testData struct {
linkName string
file string
section string
valid bool
}
data := []testData{
{"", "", "", false},
{"foo.md", "foo.md", "", true},
{"#bar", "", "bar", true},
{"foo.md#bar", "foo.md", "bar", true},
{"foo.md%%bar", "foo.md%%bar", "", true},
}
for i, d := range data {
file, section, err := splitLink(d.linkName)
if d.valid {
assert.NoErrorf(err, "test[%d]: %+v", i, d)
assert.Equal(file, d.file, "test[%d]: %+v", i, d)
assert.Equal(section, d.section, "test[%d]: %+v", i, d)
} else {
assert.Errorf(err, "test[%d]: %+v", i, d)
}
}
}
func TestValidHeadingIDChar(t *testing.T) {
assert := assert.New(t)
type testData struct {
ch rune
valid bool
}
data := []testData{
{' ', true},
{'\t', true},
{'\n', true},
{'a', true},
{'z', true},
{'A', true},
{'Z', true},
{'0', true},
{'9', true},
{'-', true},
{'_', true},
{'\000', false},
{'\001', false},
}
for i, d := range data {
result := validHeadingIDChar(d.ch)
var outcome bool
if d.valid {
outcome = result != -1
} else {
outcome = result == -1
}
assert.Truef(outcome, "test[%d]: %+v", i, d)
}
// the main list of invalid chars to test
invalid := "!@#$%^&*()+=[]{}\\|:\";'<>?,./"
for i, ch := range invalid {
result := validHeadingIDChar(ch)
outcome := result == -1
assert.Truef(outcome, "invalid[%d]: %+v", i, ch)
}
}
func TestCreateHeadingID(t *testing.T) {
assert := assert.New(t)
type testData struct {
heading string
id string
expecteError bool
}
data := []testData{
{"", "", true},
{"a", "a", false},
{"a.b/c:d", "abcd", false},
{"a ?", "a-", false},
{"a !?!", "a-", false},
{"foo", "foo", false},
{"foo bar", "foo-bar", false},
{"foo_bar", "foo_bar", false},
{"foo_bar()", "foo_bar", false},
{"`foo_bar()`", "foo_bar", false},
{"foo_bar()baz", "foo_barbaz", false},
{"Stability or Performance?", "stability-or-performance", false},
{"Hello - World", "hello---world", false},
{"metrics_json_init()", "metrics_json_init", false},
{"metrics_json_add_array_element(json)", "metrics_json_add_array_elementjson", false},
{"What is it ?", "what-is-it-", false},
{"Sandbox `DeviceInfo`", "sandbox-deviceinfo", false},
{"Build a custom QEMU for aarch64/arm64 - REQUIRED", "build-a-custom-qemu-for-aarch64arm64---required", false},
{"docker --net=host", "docker---nethost", false},
{"Containerd Runtime V2 API (Shim V2 API)", "containerd-runtime-v2-api-shim-v2-api", false},
{"Containerd Runtime V2 API: Shim V2 API", "containerd-runtime-v2-api-shim-v2-api", false},
{"Launch i3.metal instance", "launch-i3metal-instance", false},
{"Deploy!", "deploy", false},
}
for i, d := range data {
id, err := createHeadingID(d.heading)
msg := fmt.Sprintf("test[%d]: %+v, id: %q\n", i, d, id)
if d.expecteError {
assert.Error(err)
continue
}
assert.Equal(id, d.id, msg)
}
}