Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1c96d74

Browse files
committed
chore: wip
1 parent e104307 commit 1c96d74

File tree

6 files changed

+262
-10
lines changed

6 files changed

+262
-10
lines changed

packages/zig/build.zig

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
11
const std = @import("std");
22

33
pub fn build(b: *std.Build) void {
4+
const target = b.standardTargetOptions(.{});
5+
const optimize = b.standardOptimizeOption(.{});
6+
47
// Create module for use as dependency
58
_ = b.addModule("clapp", .{
69
.root_source_file = b.path("src/main.zig"),
710
});
11+
12+
// Add test step
13+
const test_module = b.createModule(.{
14+
.root_source_file = b.path("src/main.zig"),
15+
.target = target,
16+
.optimize = optimize,
17+
});
18+
19+
const lib_unit_tests = b.addTest(.{
20+
.root_module = test_module,
21+
});
22+
23+
const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
24+
25+
const test_step = b.step("test", "Run unit tests");
26+
test_step.dependOn(&run_lib_unit_tests.step);
827
}

packages/zig/src/debug.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,18 @@ pub fn trace(allocator: std.mem.Allocator, comptime fmt: []const u8, args: anyty
4949

5050
pub const Timer = struct {
5151
name: []const u8,
52-
start: i64,
52+
start_time: i64,
5353

5454
pub fn start(name: []const u8) Timer {
5555
return Timer{
5656
.name = name,
57-
.start = std.time.milliTimestamp(),
57+
.start_time = std.time.milliTimestamp(),
5858
};
5959
}
6060

6161
pub fn stop(self: *const Timer, allocator: std.mem.Allocator) !void {
6262
const end = std.time.milliTimestamp();
63-
const duration = end - self.start;
63+
const duration = end - self.start_time;
6464

6565
try debug(allocator, "{s} took {d}ms", .{ self.name, duration });
6666
}

packages/zig/src/state.zig

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ var global_settings: ?Settings = null;
132132
var settings_mutex = std.Thread.Mutex{};
133133

134134
/// Update global settings
135-
pub fn updateSettings(allocator: std.mem.Allocator, new_settings: Settings) !void {
135+
pub fn updateSettings(_: std.mem.Allocator, new_settings: Settings) !void {
136136
settings_mutex.lock();
137137
defer settings_mutex.unlock();
138138

@@ -200,4 +200,90 @@ test "settings" {
200200
settings.use_vim_keys = true;
201201
try std.testing.expect(settings.isActionKey("k", .up));
202202
try std.testing.expect(settings.isActionKey("j", .down));
203+
try std.testing.expect(settings.isActionKey("h", .left));
204+
try std.testing.expect(settings.isActionKey("l", .right));
205+
try std.testing.expect(settings.isActionKey("q", .cancel));
206+
}
207+
208+
test "action from key - edge cases" {
209+
// Test all arrow keys
210+
try std.testing.expect(Action.fromKey("\x1B[A") == .up);
211+
try std.testing.expect(Action.fromKey("\x1B[B") == .down);
212+
try std.testing.expect(Action.fromKey("\x1B[C") == .right);
213+
try std.testing.expect(Action.fromKey("\x1B[D") == .left);
214+
215+
// Test home and end
216+
try std.testing.expect(Action.fromKey("\x1B[H") == .home);
217+
try std.testing.expect(Action.fromKey("\x1B[F") == .end);
218+
219+
// Test delete
220+
try std.testing.expect(Action.fromKey("\x1B[3~") == .delete);
221+
222+
// Test single character keys
223+
try std.testing.expect(Action.fromKey(" ") == .space);
224+
try std.testing.expect(Action.fromKey("\n") == .enter);
225+
try std.testing.expect(Action.fromKey("\r") == .enter);
226+
try std.testing.expect(Action.fromKey("\t") == .tab);
227+
try std.testing.expect(Action.fromKey("\x1B") == .cancel);
228+
try std.testing.expect(Action.fromKey("\x7F") == .backspace);
229+
try std.testing.expect(Action.fromKey("\x08") == .backspace);
230+
231+
// Test invalid keys
232+
try std.testing.expect(Action.fromKey("") == null);
233+
try std.testing.expect(Action.fromKey("a") == null);
234+
try std.testing.expect(Action.fromKey("\x1B[Z") == null);
235+
try std.testing.expect(Action.fromKey("\x1B[") == null);
236+
}
237+
238+
test "state transitions" {
239+
var current = State.initial;
240+
try std.testing.expect(!current.isActive());
241+
try std.testing.expect(!current.isFinished());
242+
243+
current = State.active;
244+
try std.testing.expect(current.isActive());
245+
try std.testing.expect(!current.isFinished());
246+
247+
current = State.submit;
248+
try std.testing.expect(!current.isActive());
249+
try std.testing.expect(current.isFinished());
250+
251+
current = State.cancel;
252+
try std.testing.expect(!current.isActive());
253+
try std.testing.expect(current.isFinished());
254+
255+
current = State.@"error";
256+
try std.testing.expect(!current.isActive());
257+
try std.testing.expect(current.isFinished());
258+
}
259+
260+
test "settings - custom mappings override standard" {
261+
const allocator = std.testing.allocator;
262+
263+
var settings = Settings.init(allocator);
264+
defer settings.deinit();
265+
266+
// Map 'k' to cancel (overriding vim up)
267+
try settings.mapKey("k", .cancel);
268+
settings.use_vim_keys = true;
269+
270+
// Custom mapping should take precedence
271+
try std.testing.expect(settings.isActionKey("k", .cancel));
272+
try std.testing.expect(!settings.isActionKey("k", .up));
273+
}
274+
275+
test "settings - multiple custom mappings" {
276+
const allocator = std.testing.allocator;
277+
278+
var settings = Settings.init(allocator);
279+
defer settings.deinit();
280+
281+
try settings.mapKey("a", .up);
282+
try settings.mapKey("b", .down);
283+
try settings.mapKey("c", .cancel);
284+
285+
try std.testing.expect(settings.isActionKey("a", .up));
286+
try std.testing.expect(settings.isActionKey("b", .down));
287+
try std.testing.expect(settings.isActionKey("c", .cancel));
288+
try std.testing.expect(!settings.isActionKey("d", .up));
203289
}

packages/zig/src/style.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub const Theme = struct {
4848
secondary: AnsiCode = codes.cyan,
4949
success: AnsiCode = codes.green,
5050
warning: AnsiCode = codes.yellow,
51-
error: AnsiCode = codes.red,
51+
@"error": AnsiCode = codes.red,
5252
info: AnsiCode = codes.magenta,
5353
muted: AnsiCode = codes.gray,
5454
};
@@ -212,7 +212,7 @@ pub fn warning(allocator: std.mem.Allocator, text: []const u8) ![]const u8 {
212212
}
213213

214214
pub fn err(allocator: std.mem.Allocator, text: []const u8) ![]const u8 {
215-
return apply(allocator, text, global_theme.error);
215+
return apply(allocator, text, global_theme.@"error");
216216
}
217217

218218
pub fn info(allocator: std.mem.Allocator, text: []const u8) ![]const u8 {

packages/zig/src/terminal.zig

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,14 +217,60 @@ test "unicode support detection" {
217217
test "terminal width" {
218218
const cols = getColumns();
219219
try std.testing.expect(cols > 0);
220+
try std.testing.expect(cols <= 1000); // Reasonable upper bound
220221
}
221222

222223
test "strip VT control characters" {
223224
const allocator = std.testing.allocator;
224225

225-
const input = "\x1B[31mHello\x1B[0m World";
226-
const stripped = try stripVTControlCharacters(allocator, input);
227-
defer allocator.free(stripped);
226+
// Test basic ANSI colors
227+
const input1 = "\x1B[31mHello\x1B[0m World";
228+
const stripped1 = try stripVTControlCharacters(allocator, input1);
229+
defer allocator.free(stripped1);
230+
try std.testing.expectEqualStrings("Hello World", stripped1);
231+
232+
// Test multiple sequences
233+
const input2 = "\x1B[1m\x1B[31mBold Red\x1B[0m\x1B[0m";
234+
const stripped2 = try stripVTControlCharacters(allocator, input2);
235+
defer allocator.free(stripped2);
236+
try std.testing.expectEqualStrings("Bold Red", stripped2);
237+
238+
// Test empty string
239+
const input3 = "";
240+
const stripped3 = try stripVTControlCharacters(allocator, input3);
241+
defer allocator.free(stripped3);
242+
try std.testing.expectEqualStrings("", stripped3);
243+
244+
// Test string with no ANSI codes
245+
const input4 = "Plain text";
246+
const stripped4 = try stripVTControlCharacters(allocator, input4);
247+
defer allocator.free(stripped4);
248+
try std.testing.expectEqualStrings("Plain text", stripped4);
249+
250+
// Test cursor movement sequences
251+
const input5 = "\x1B[10;5HText at position";
252+
const stripped5 = try stripVTControlCharacters(allocator, input5);
253+
defer allocator.free(stripped5);
254+
try std.testing.expectEqualStrings("Text at position", stripped5);
255+
}
256+
257+
test "raw mode enable/disable" {
258+
const raw_mode = RawMode{};
259+
260+
// Test that original_termios starts as null
261+
try std.testing.expect(raw_mode.original_termios == null);
262+
263+
// Note: We can't fully test raw mode without affecting the test environment
264+
// But we can verify the structure is correct
265+
}
266+
267+
test "block function" {
268+
const start = std.time.milliTimestamp();
269+
block(50); // Block for 50ms
270+
const end = std.time.milliTimestamp();
271+
const elapsed = end - start;
228272

229-
try std.testing.expectEqualStrings("Hello World", stripped);
273+
// Should be at least 50ms, with some tolerance
274+
try std.testing.expect(elapsed >= 45);
275+
try std.testing.expect(elapsed < 200); // Upper bound for slow systems
230276
}

packages/zig/src/utils.zig

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,104 @@ test "findLongest" {
255255
const result = findLongest(&strings);
256256
try std.testing.expectEqual(@as(usize, 13), result);
257257
}
258+
259+
test "diffLines - simple changes" {
260+
const allocator = std.testing.allocator;
261+
262+
const old = "line1\nline2\nline3";
263+
const new = "line1\nmodified\nline3";
264+
265+
const diff = try diffLines(allocator, old, new);
266+
defer freeDiffLines(allocator, diff);
267+
268+
try std.testing.expect(diff.len == 4);
269+
try std.testing.expect(diff[0].type == .unchanged);
270+
try std.testing.expect(diff[1].type == .removed);
271+
try std.testing.expect(diff[2].type == .added);
272+
try std.testing.expect(diff[3].type == .unchanged);
273+
}
274+
275+
test "diffLines - additions" {
276+
const allocator = std.testing.allocator;
277+
278+
const old = "line1\nline2";
279+
const new = "line1\nline2\nline3";
280+
281+
const diff = try diffLines(allocator, old, new);
282+
defer freeDiffLines(allocator, diff);
283+
284+
try std.testing.expect(diff.len == 3);
285+
try std.testing.expect(diff[2].type == .added);
286+
}
287+
288+
test "diffLines - deletions" {
289+
const allocator = std.testing.allocator;
290+
291+
const old = "line1\nline2\nline3";
292+
const new = "line1\nline2";
293+
294+
const diff = try diffLines(allocator, old, new);
295+
defer freeDiffLines(allocator, diff);
296+
297+
try std.testing.expect(diff.len == 3);
298+
try std.testing.expect(diff[2].type == .removed);
299+
}
300+
301+
test "diffLines - empty strings" {
302+
const allocator = std.testing.allocator;
303+
304+
const old = "";
305+
const new = "";
306+
307+
const diff = try diffLines(allocator, old, new);
308+
defer freeDiffLines(allocator, diff);
309+
310+
try std.testing.expect(diff.len == 0);
311+
}
312+
313+
test "diffLines - one empty" {
314+
const allocator = std.testing.allocator;
315+
316+
const old = "";
317+
const new = "line1\nline2";
318+
319+
const diff = try diffLines(allocator, old, new);
320+
defer freeDiffLines(allocator, diff);
321+
322+
try std.testing.expect(diff.len == 2);
323+
try std.testing.expect(diff[0].type == .added);
324+
try std.testing.expect(diff[1].type == .added);
325+
}
326+
327+
test "camelcaseOptionName with dot notation" {
328+
const allocator = std.testing.allocator;
329+
330+
const result = try camelcaseOptionName(allocator, "env-var.NODE_ENV");
331+
defer allocator.free(result);
332+
try std.testing.expectEqualStrings("envVar.NODE_ENV", result);
333+
}
334+
335+
test "padRight - exact length" {
336+
const allocator = std.testing.allocator;
337+
338+
const result = try padRight(allocator, "test", 4);
339+
defer allocator.free(result);
340+
try std.testing.expectEqualStrings("test", result);
341+
}
342+
343+
test "padRight - longer than target" {
344+
const allocator = std.testing.allocator;
345+
346+
const result = try padRight(allocator, "testing", 4);
347+
defer allocator.free(result);
348+
try std.testing.expectEqualStrings("testing", result);
349+
}
350+
351+
test "padRight - shorter than target" {
352+
const allocator = std.testing.allocator;
353+
354+
const result = try padRight(allocator, "hi", 6);
355+
defer allocator.free(result);
356+
try std.testing.expectEqual(@as(usize, 6), result.len);
357+
try std.testing.expect(std.mem.startsWith(u8, result, "hi"));
358+
}

0 commit comments

Comments
 (0)