236 lines
6.5 KiB
Zig
236 lines
6.5 KiB
Zig
const std = @import("std");
|
|
const types = @import("../../src/types.zig");
|
|
const display = @import("../../src/display.zig");
|
|
|
|
test "displayManEntry - basic entry" {
|
|
const entry = types.ManEntry{
|
|
.name = "ls",
|
|
.section = "1",
|
|
.description = "list directory contents",
|
|
.path = null,
|
|
};
|
|
|
|
// This test verifies the function doesn't panic
|
|
// Actual output testing would require capturing stdout
|
|
try display.displayManEntry(entry);
|
|
}
|
|
|
|
test "displayManEntry - with path" {
|
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
|
defer _ = gpa.deinit();
|
|
const allocator = gpa.allocator();
|
|
|
|
const entry = types.ManEntry{
|
|
.name = "ls",
|
|
.section = "1",
|
|
.description = "list directory contents",
|
|
.path = try allocator.dupe(u8, "/usr/share/man/man1/ls.1"),
|
|
};
|
|
defer if (entry.path) |path| allocator.free(path);
|
|
|
|
try display.displayManEntry(entry);
|
|
}
|
|
|
|
test "displaySearchResults - non-verbose no sections" {
|
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
|
defer _ = gpa.deinit();
|
|
const allocator = gpa.allocator();
|
|
|
|
var entries = types.ManEntryList.init(allocator);
|
|
defer {
|
|
for (entries.items) |entry| {
|
|
allocator.free(entry.name);
|
|
allocator.free(entry.section);
|
|
allocator.free(entry.description);
|
|
}
|
|
entries.deinit();
|
|
}
|
|
|
|
try entries.append(types.ManEntry{
|
|
.name = try allocator.dupe(u8, "ls"),
|
|
.section = try allocator.dupe(u8, "1"),
|
|
.description = try allocator.dupe(u8, "list files"),
|
|
.path = null,
|
|
});
|
|
|
|
const config = types.SearchConfig{
|
|
.keyword = "ls",
|
|
.target_sections = null,
|
|
.show_paths = false,
|
|
.verbose = false,
|
|
};
|
|
|
|
try display.displaySearchResults(entries, config, 100.0);
|
|
}
|
|
|
|
test "displaySearchResults - non-verbose with sections" {
|
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
|
defer _ = gpa.deinit();
|
|
const allocator = gpa.allocator();
|
|
|
|
var entries = types.ManEntryList.init(allocator);
|
|
defer {
|
|
for (entries.items) |entry| {
|
|
allocator.free(entry.name);
|
|
allocator.free(entry.section);
|
|
allocator.free(entry.description);
|
|
}
|
|
entries.deinit();
|
|
}
|
|
|
|
const targets = &[_][]const u8{"1", "3"};
|
|
|
|
const config = types.SearchConfig{
|
|
.keyword = "test",
|
|
.target_sections = targets,
|
|
.show_paths = false,
|
|
.verbose = false,
|
|
};
|
|
|
|
try display.displaySearchResults(entries, config, 50.0);
|
|
}
|
|
|
|
test "displaySearchResults - verbose mode" {
|
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
|
defer _ = gpa.deinit();
|
|
const allocator = gpa.allocator();
|
|
|
|
var entries = types.ManEntryList.init(allocator);
|
|
defer {
|
|
for (entries.items) |entry| {
|
|
allocator.free(entry.name);
|
|
allocator.free(entry.section);
|
|
allocator.free(entry.description);
|
|
}
|
|
entries.deinit();
|
|
}
|
|
|
|
try entries.append(types.ManEntry{
|
|
.name = try allocator.dupe(u8, "cat"),
|
|
.section = try allocator.dupe(u8, "1"),
|
|
.description = try allocator.dupe(u8, "concatenate files"),
|
|
.path = null,
|
|
});
|
|
|
|
const config = types.SearchConfig{
|
|
.keyword = "cat",
|
|
.target_sections = null,
|
|
.show_paths = false,
|
|
.verbose = true,
|
|
};
|
|
|
|
try display.displaySearchResults(entries, config, 250.5);
|
|
}
|
|
|
|
test "displaySearchResults - multiple entries" {
|
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
|
defer _ = gpa.deinit();
|
|
const allocator = gpa.allocator();
|
|
|
|
var entries = types.ManEntryList.init(allocator);
|
|
defer {
|
|
for (entries.items) |entry| {
|
|
allocator.free(entry.name);
|
|
allocator.free(entry.section);
|
|
allocator.free(entry.description);
|
|
}
|
|
entries.deinit();
|
|
}
|
|
|
|
try entries.append(types.ManEntry{
|
|
.name = try allocator.dupe(u8, "ls"),
|
|
.section = try allocator.dupe(u8, "1"),
|
|
.description = try allocator.dupe(u8, "list files"),
|
|
.path = null,
|
|
});
|
|
|
|
try entries.append(types.ManEntry{
|
|
.name = try allocator.dupe(u8, "printf"),
|
|
.section = try allocator.dupe(u8, "3"),
|
|
.description = try allocator.dupe(u8, "format and print"),
|
|
.path = null,
|
|
});
|
|
|
|
try entries.append(types.ManEntry{
|
|
.name = try allocator.dupe(u8, "printf"),
|
|
.section = try allocator.dupe(u8, "1"),
|
|
.description = try allocator.dupe(u8, "format and print"),
|
|
.path = null,
|
|
});
|
|
|
|
const config = types.SearchConfig{
|
|
.keyword = "printf",
|
|
.target_sections = null,
|
|
.show_paths = false,
|
|
.verbose = false,
|
|
};
|
|
|
|
try display.displaySearchResults(entries, config, 150.0);
|
|
}
|
|
|
|
test "displaySearchStart - non-verbose" {
|
|
const config = types.SearchConfig{
|
|
.keyword = "test",
|
|
.target_sections = null,
|
|
.show_paths = false,
|
|
.verbose = false,
|
|
};
|
|
|
|
// Should not output anything in non-verbose mode
|
|
try display.displaySearchStart(config);
|
|
}
|
|
|
|
test "displaySearchStart - verbose no sections" {
|
|
const config = types.SearchConfig{
|
|
.keyword = "sleep",
|
|
.target_sections = null,
|
|
.show_paths = false,
|
|
.verbose = true,
|
|
};
|
|
|
|
try display.displaySearchStart(config);
|
|
}
|
|
|
|
test "displaySearchStart - verbose with single section" {
|
|
const targets = &[_][]const u8{"1"};
|
|
|
|
const config = types.SearchConfig{
|
|
.keyword = "ls",
|
|
.target_sections = targets,
|
|
.show_paths = false,
|
|
.verbose = true,
|
|
};
|
|
|
|
try display.displaySearchStart(config);
|
|
}
|
|
|
|
test "displaySearchStart - verbose with multiple sections" {
|
|
const targets = &[_][]const u8{"1", "3", "8"};
|
|
|
|
const config = types.SearchConfig{
|
|
.keyword = "admin",
|
|
.target_sections = targets,
|
|
.show_paths = false,
|
|
.verbose = true,
|
|
};
|
|
|
|
try display.displaySearchStart(config);
|
|
}
|
|
|
|
test "displaySearchResults - empty results" {
|
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
|
defer _ = gpa.deinit();
|
|
const allocator = gpa.allocator();
|
|
|
|
var entries = types.ManEntryList.init(allocator);
|
|
defer entries.deinit();
|
|
|
|
const config = types.SearchConfig{
|
|
.keyword = "nonexistent12345",
|
|
.target_sections = null,
|
|
.show_paths = false,
|
|
.verbose = true,
|
|
};
|
|
|
|
try display.displaySearchResults(entries, config, 10.0);
|
|
}
|