summaryrefslogtreecommitdiff
path: root/examples/live/test
diff options
context:
space:
mode:
Diffstat (limited to 'examples/live/test')
-rw-r--r--examples/live/test/grammar.zig54
-rw-r--r--examples/live/test/live.script117
-rw-r--r--examples/live/test/probe.script4
-rw-r--r--examples/live/test/story.script157
4 files changed, 332 insertions, 0 deletions
diff --git a/examples/live/test/grammar.zig b/examples/live/test/grammar.zig
new file mode 100644
index 0000000..2fa982f
--- /dev/null
+++ b/examples/live/test/grammar.zig
@@ -0,0 +1,54 @@
+//! The language's grammar (grammar/grammar.js, its parser.c compiled into
+//! the sketch) against src/Lang.zig, which compiles the text: they must
+//! agree on what is well formed, or what is coloured and walked would not
+//! be the program that plays.
+const std = @import("std");
+const app = @import("app");
+const Lang = @import("Lang");
+const pe = @import("pardes-editor");
+
+test "the grammar and Lang.zig agree: both take a program, both refuse a broken line; what only Lang.zig refuses is its meaning" {
+ const parser = pe.syntax.ts.Parser.create();
+ defer parser.destroy();
+ try parser.setLanguage(app.tree_sitter_live());
+ const Case = struct { text: []const u8, grammar: bool, lang: bool };
+ for ([_]Case{
+ .{ .text = Lang.survey, .grammar = true, .lang = true },
+ .{ .text = "out = sin(tau * 220 * t) * 0.3\ncolor = hsv(x, 0.5, y)", .grammar = true, .lang = true },
+ .{ .text = "\n # nothing\n a = 1 + 2 * -t / 4 - (5 - -6) # trailing\n\nout = a + 1e-3\n", .grammar = true, .lang = true },
+ // broken lines: both refuse
+ .{ .text = "out = sin(tau * 220 * t * 0.3", .grammar = false, .lang = false },
+ .{ .text = "out = 1 +", .grammar = false, .lang = false },
+ .{ .text = "= 1", .grammar = false, .lang = false },
+ .{ .text = "out 1", .grammar = false, .lang = false },
+ .{ .text = "out", .grammar = false, .lang = false },
+ .{ .text = "out = * 2", .grammar = false, .lang = false },
+ .{ .text = "out = 1 2", .grammar = false, .lang = false },
+ .{ .text = "out = sin(1 2)", .grammar = false, .lang = false },
+ .{ .text = "out = 1.2.3", .grammar = false, .lang = false },
+ .{ .text = "out = 1 $ 2", .grammar = false, .lang = false },
+ .{ .text = "out = (1 + 2))", .grammar = false, .lang = false },
+ // well formed, but meaning nothing: only Lang.zig knows its names,
+ // builtins and arities, and the problem it shows is the one drawn
+ .{ .text = "out = kick\nkick = 1", .grammar = true, .lang = false },
+ .{ .text = "a = 1\na = 2", .grammar = true, .lang = false },
+ .{ .text = "sin = 1", .grammar = true, .lang = false },
+ .{ .text = "out = sin", .grammar = true, .lang = false },
+ .{ .text = "out = foo(2)", .grammar = true, .lang = false },
+ .{ .text = "out = mix(1, 2)", .grammar = true, .lang = false },
+ .{ .text = "out = rgb(1, 1, 1)", .grammar = true, .lang = false },
+ .{ .text = "a = 1\nout = a(2)", .grammar = true, .lang = false },
+ }) |case| {
+ const tree = parser.parseString(case.text, null) orelse return error.NoTree;
+ defer tree.destroy();
+ const lang = Lang.compile(case.text, null) == .program;
+ std.testing.expectEqual(case.grammar, !tree.rootNode().hasError()) catch |e| {
+ std.debug.print("the grammar on {s}\n", .{case.text});
+ return e;
+ };
+ std.testing.expectEqual(case.lang, lang) catch |e| {
+ std.debug.print("Lang.zig on {s}\n", .{case.text});
+ return e;
+ };
+ }
+}
diff --git a/examples/live/test/live.script b/examples/live/test/live.script
new file mode 100644
index 0000000..f39275d
--- /dev/null
+++ b/examples/live/test/live.script
@@ -0,0 +1,117 @@
+# A program typed through the editor (helix's keys: `i`, text, Escape,
+# motions), its tapes after each line, a broken line and its mend (an
+# undo); then this run's session played back, and sought to two moments
+# marked as they happened. (A mark ends a clip, `record`, so the session is
+# replayed.)
+# Screenshots land in .zig-cache/live/ for review; they are compared over
+# the picture alone (640,300 600x300: not the text, the scope or the time).
+frames 2
+type i
+type out = sin(tau * 220 * t) * 0.3
+expect problem.len 0
+expect out.len 8
+expect out.code[5].op sin
+expect out.code[7].op mul
+# The GPU's blocks reach the speakers' stream (SDL's dummy driver here).
+frames 10
+expect-audio 1
+key return
+type color = hsv(x, 0.5, y)
+expect color.len 4
+expect color.code[0].op x
+expect color.code[3].op hsv
+key escape
+expect ed.cursor.mode normal
+frames 2
+console save typed
+screenshot .zig-cache/live/gradient.png
+# An edit: y becomes x (to the line's end, a character left, replaced),
+# and the picture changes.
+type gl
+type h
+type rx
+expect color.len 3
+expect color.code[2].op hsv
+expect color.code[2].c 0
+frames 2
+screenshot .zig-cache/live/edited.png
+expect-different-screenshot .zig-cache/live/gradient.png .zig-cache/live/edited.png 640 300 600 300
+# Broken: the ) deleted; the tape and the picture stay, the problem is set
+# at its byte.
+type l
+type d
+expect problem.at 52
+expect color.len 3
+expect color.code[2].c 0
+frames 2
+screenshot .zig-cache/live/broken.png
+expect-same-screenshot .zig-cache/live/edited.png .zig-cache/live/broken.png 0 640 300 600 300
+# Mended: undone.
+type u
+expect problem.len 0
+expect color.len 3
+console save end
+# Played back from the start, then sought: each moment's text, tapes and
+# time as they were live, leaf by leaf (a mark is taken before its frame's
+# tick: 0.3 s and 0.5333 s; a seek lands on the last frame at or before).
+console play session 0
+frames 100
+console seek 538
+expect-snapshot end
+console seek 305
+expect-snapshot typed
+expect color.len 4
+screenshot .zig-cache/live/sought.png
+expect-same-screenshot .zig-cache/live/gradient.png .zig-cache/live/sought.png 0 0 0 1000 600
+# Live again from there: the typed program, before its edit.
+console stop
+expect ed.front 53
+expect ed.back 0
+expect color.code[3].op hsv
+# Hear this: Alt-o from the 2 of 220 to the number, then to `tau * 220`,
+# which alone is heard and seen until the next key.
+type gg
+type 16l
+hold left_alt
+key o
+expect probe.len 1
+key o
+release left_alt
+expect probe.len 3
+expect probe.code[2].op mul
+key escape
+expect probe.len 0
+# Drag a number: 220 (line 1, x 182 to 212: the code is laid out from 16,
+# 10 pixels a character, the name `out` a step larger at 12, its line 26
+# high) pressed and dragged 40 pixels right is 230, then 12 left of where
+# it was pressed is 217; the program follows; the drag is one step to undo.
+# Pressed 6 pixels left, where 220 began when every character was 10
+# pixels, it is the space before it: nothing drags.
+down 181 27
+move 221 27
+up 221 27
+expect out.code[1].a 1130102784
+down 187 27
+move 207 27
+move 227 27
+expect out.code[1].a 1130758144
+move 175 27
+expect out.code[1].a 1129906176
+up 175 27
+type u
+expect out.code[1].a 1130102784
+# Square units: a disc of radius 0.5 (half the window's height is 1) is
+# 180 pixels across each way from the centre, round on 16:9.
+type %
+type c
+type color = step(x * x + y * y, 0.25)
+key escape
+expect color.len 7
+frames 2
+screenshot .zig-cache/live/disc.png
+expect-pixel .zig-cache/live/disc.png 810 360 255 255 255 255 2
+expect-pixel .zig-cache/live/disc.png 640 190 255 255 255 255 2
+expect-pixel .zig-cache/live/disc.png 470 360 255 255 255 255 2
+expect-pixel .zig-cache/live/disc.png 830 360 0 0 0 255 2
+expect-pixel .zig-cache/live/disc.png 640 170 0 0 0 255 2
+expect-pixel .zig-cache/live/disc.png 640 550 0 0 0 255 2
diff --git a/examples/live/test/probe.script b/examples/live/test/probe.script
new file mode 100644
index 0000000..fc2e162
--- /dev/null
+++ b/examples/live/test/probe.script
@@ -0,0 +1,4 @@
+# The probe (src/probe.zig): frames enough for steady means, and what the
+# 60-instruction tape draws, to see that the interpreter runs.
+frames 600
+screenshot .zig-cache/live/probe.png
diff --git a/examples/live/test/story.script b/examples/live/test/story.script
new file mode 100644
index 0000000..0849b28
--- /dev/null
+++ b/examples/live/test/story.script
@@ -0,0 +1,157 @@
+# The storyboard (reports/demo1.md, section 4), two minutes typed through
+# the editor: a tone, its pitch dragged, a gradient, a kick shared by sound
+# and picture, a `)` deleted and put back, a probe, a bass line and a ring;
+# then the session played back, sought, stepped back and branched. State
+# is asserted at each beat; each beat's screenshot lands in
+# .zig-cache/story/, named by the beat.
+console capture
+frames 2
+# 0:05 a tone
+type i
+type out = sin(tau * 220 * t) * 0.3
+key escape
+expect problem.len 0
+expect out.len 8
+frames 30
+expect-audio 1
+console save tone
+screenshot .zig-cache/story/1-tone.png
+# 0:15 the pitch dragged, 220 to 330 (x 182 to 212 of line 1, after the
+# larger `out`; a step every 4 pixels: 245 on the way)
+down 187 27
+move 287 27
+expect out.code[1].a 1131741184
+move 627 27
+up 627 27
+expect out.code[1].a 1134886912
+frames 10
+screenshot .zig-cache/story/2-drag.png
+# 0:25 a gradient behind the code
+type o
+type color = hsv(x / 4, 0.5, y / 2 + 0.5)
+key escape
+expect color.len 10
+expect color.code[9].op hsv
+frames 10
+screenshot .zig-cache/story/3-gradient.png
+# 0:35 a kick, shared: the picture pulses because it says kick too
+type gg
+type O
+type beat = t * 2
+key return
+type kick = exp(-7 * fract(beat))
+key return
+type thump = sin(tau * 48 * t * (1 + 2 * kick)) * kick * 0.5
+key escape
+type j
+type x
+type c
+type out = thump
+key return
+key escape
+type x
+type c
+type color = hsv(x / 4, 0.5, (y / 2 + 0.5) * (0.5 + kick))
+key escape
+expect problem.len 0
+expect out.len 20
+expect out.code[19].op mul
+expect color.len 20
+expect color.code[19].op hsv
+# the picture's tape has the kick's exp in it, as the sound's does
+expect color.code[16].op exp
+console save kick
+frames 20
+screenshot .zig-cache/story/4-kick.png
+# 0:55 a ) deleted: marked, and the last good program plays on
+type gl
+type d
+expect problem.at 162
+expect color.len 20
+frames 10
+screenshot .zig-cache/story/5-broken.png
+# 1:00 put back
+type a
+type )
+key escape
+expect problem.len 0
+frames 10
+screenshot .zig-cache/story/6-mended.png
+# 1:05 inside thump's sin, Alt-o: only the call is heard and seen; Alt-i
+# back inside it; Escape, the program again
+type gg
+type jj
+type f*
+type ;
+type mi(
+hold left_alt
+key o
+expect probe.len 17
+expect probe.code[16].op sin
+frames 10
+screenshot .zig-cache/story/7-probe.png
+key i
+expect probe.len 16
+expect probe.code[15].op mul
+release left_alt
+key escape
+expect probe.len 0
+# 1:15 a bass line under it, and the ring
+type o
+type root = seq(beat / 4, 55, 55, 65.4, 49)
+key return
+type bass = saw(root * t) * (1 - kick) * 0.25
+key escape
+type j
+type x
+type c
+type out = bass + thump
+key return
+key escape
+type O
+type r = sqrt(x * x + y * y)
+key escape
+type j
+type x
+type c
+type color = hsv(fract(beat / 16 + r), 0.6, smoothstep(0.06, 0, abs(r - 0.4 - 0.2 * kick)) + 0.2 * kick)
+key escape
+expect problem.len 0
+expect out.len 35
+expect out.code[34].op add
+expect color.len 31
+expect color.code[30].op hsv
+console save end
+frames 60
+screenshot .zig-cache/story/8-bass-ring.png
+# 1:40 played back, paused, sought to the tone and to the kick (each
+# moment's text, tapes and time as they were live, leaf by leaf), stepped
+# back half a second
+console play session 0
+frames 30
+console pause
+console seek 590
+expect-snapshot tone
+screenshot .zig-cache/story/9-sought-tone.png
+console seek 1405
+expect-snapshot kick
+console step -30
+expect time 0.9 ~0.001
+expect color.len 10
+screenshot .zig-cache/story/9-stepped-back.png
+# 1:55 live again at the kick, another way on: the beat a third faster
+console seek 1405
+console stop
+type gg
+type f2
+type ;
+type r3
+expect problem.len 0
+expect out.code[8].a 1077936128
+frames 30
+screenshot .zig-cache/story/10-branched.png
+# nothing was overwritten: the first session still ends as it did
+console play session 0
+frames 10
+console seek 2890
+expect-snapshot end