Files
cheat/internal/sheet/sheet_test.go
Christopher Allen Lane 5ad1a3c39f chore: housekeeping and refactoring (bump to 4.7.1)
- Remove unused parameters, dead files, and inaccurate doc.go files
- Extract shared helpers, eliminate duplication
- Rename cheatpath.Cheatpath to cheatpath.Path
- Optimize filesystem walks (WalkDir, skip .git)
- Move sheet name validation to sheet.Validate
- Move integration tests to test/integration/
- Consolidate internal/mock into mocks/
- Move fuzz.sh to test/
- Inline loadSheets helper into command callers
- Extract config.New into its own file
- Fix stale references in HACKING.md and CLAUDE.md
- Restore plan9 build target
- Remove redundant and low-value tests
- Clean up project documentation

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-15 15:11:19 -05:00

91 lines
1.9 KiB
Go

package sheet
import (
"reflect"
"testing"
"github.com/cheat/cheat/mocks"
)
// TestSheetSuccess asserts that sheets initialize properly
func TestSheetSuccess(t *testing.T) {
// initialize a sheet
sheet, err := New(
"foo",
"community",
mocks.Path("sheet/foo"),
[]string{"alpha", "bravo"},
false,
)
if err != nil {
t.Errorf("failed to load sheet: %v", err)
}
// assert that the sheet loaded correctly
if sheet.Title != "foo" {
t.Errorf("failed to init title: want: foo, got: %s", sheet.Title)
}
if sheet.Path != mocks.Path("sheet/foo") {
t.Errorf(
"failed to init path: want: %s, got: %s",
mocks.Path("sheet/foo"),
sheet.Path,
)
}
wantText := "# To foo the bar:\n foo bar\n"
if sheet.Text != wantText {
t.Errorf("failed to init text: want: %s, got: %s", wantText, sheet.Text)
}
// NB: tags should sort alphabetically
wantTags := []string{"alpha", "bar", "baz", "bravo", "foo"}
if !reflect.DeepEqual(sheet.Tags, wantTags) {
t.Errorf("failed to init tags: want: %v, got: %v", wantTags, sheet.Tags)
}
if sheet.Syntax != "sh" {
t.Errorf("failed to init syntax: want: sh, got: %s", sheet.Syntax)
}
if sheet.ReadOnly != false {
t.Errorf("failed to init readonly")
}
}
// TestSheetFailure asserts that an error is returned if the sheet cannot be
// read
func TestSheetFailure(t *testing.T) {
// initialize a sheet
_, err := New(
"foo",
"community",
mocks.Path("/does-not-exist"),
[]string{"alpha", "bravo"},
false,
)
if err == nil {
t.Errorf("failed to return an error on unreadable sheet")
}
}
// TestSheetFrontMatterFailure asserts that an error is returned if the sheet's
// frontmatter cannot be parsed.
func TestSheetFrontMatterFailure(t *testing.T) {
// initialize a sheet
_, err := New(
"foo",
"community",
mocks.Path("sheet/bad-fm"),
[]string{"alpha", "bravo"},
false,
)
if err == nil {
t.Errorf("failed to return an error on malformed front-matter")
}
}