| 1 | package deck_test |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "reflect" |
| 8 | "testing" |
| 9 | |
| 10 | "amadan.net/oficina/slides/internal/slides/deck" |
| 11 | ) |
| 12 | |
| 13 | // readVectors decodes fixtures/vectors/<name>, the file the JS twin's |
| 14 | // test (fixtures/presses.test.mjs, moves.test.mjs) reads too. A case |
| 15 | // that fails here and passes there is a drift between the two sides, |
| 16 | // never something to fix by editing the vector. |
| 17 | func readVectors(t *testing.T, name string, into any) { |
| 18 | t.Helper() |
| 19 | b, err := os.ReadFile(filepath.Join("..", "..", "..", "fixtures", "vectors", name)) |
| 20 | if err != nil { |
| 21 | t.Fatalf("read %s: %v", name, err) |
| 22 | } |
| 23 | if err := json.Unmarshal(b, into); err != nil { |
| 24 | t.Fatalf("decode %s: %v", name, err) |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | func TestPressesVectors(t *testing.T) { |
| 29 | var cases []struct { |
| 30 | Name string `json:"name"` |
| 31 | Scenes map[string]deck.Scene `json:"scenes"` |
| 32 | Slide deck.Slide `json:"slide"` |
| 33 | Presses []deck.Press `json:"presses"` |
| 34 | } |
| 35 | readVectors(t, "presses.json", &cases) |
| 36 | if len(cases) < 10 { |
| 37 | t.Fatalf("%d cases, want at least 10", len(cases)) |
| 38 | } |
| 39 | for _, c := range cases { |
| 40 | got := deck.Presses(c.Slide, c.Scenes) |
| 41 | if len(got) == 0 && len(c.Presses) == 0 { |
| 42 | continue |
| 43 | } |
| 44 | if !reflect.DeepEqual(got, c.Presses) { |
| 45 | t.Errorf("%s:\n got %v\nwant %v", c.Name, got, c.Presses) |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | func TestArrivalOfNumbersEveryRevealByItsPress(t *testing.T) { |
| 51 | s := deck.Slide{ID: "s", Blocks: []deck.Block{ |
| 52 | {ID: "a", Type: "text", Step: 2, Text: deck.Text{{{T: "A"}}}}, |
| 53 | {ID: "l", Type: "list", Step: 1, Items: "each", Text: deck.Text{{{T: "x"}}, {{T: "y"}}}}, |
| 54 | {ID: "w", Type: "text", Text: deck.Text{{{T: "with the slide"}}}}, |
| 55 | }} |
| 56 | want := map[deck.Reveal]int{ |
| 57 | {Block: "l", Item: 0}: 1, |
| 58 | {Block: "l", Item: 1}: 2, |
| 59 | {Block: "a", Item: -1}: 3, |
| 60 | } |
| 61 | if got := deck.ArrivalOf(s, nil); !reflect.DeepEqual(got, want) { |
| 62 | t.Errorf("ArrivalOf = %v, want %v", got, want) |
| 63 | } |
| 64 | if got := deck.ArrivalOf(deck.Slide{}, nil); len(got) != 0 { |
| 65 | t.Errorf("ArrivalOf(no blocks) = %v, want empty", got) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | func TestNotePartsSplitAtThePressLine(t *testing.T) { |
| 70 | para := func(runs ...deck.Run) []deck.Run { return runs } |
| 71 | p := func(s string) []deck.Run { return para(deck.Run{T: s}) } |
| 72 | plain := func(parts []deck.Text) []string { |
| 73 | var out []string |
| 74 | for _, part := range parts { |
| 75 | out = append(out, part.Plain()) |
| 76 | } |
| 77 | return out |
| 78 | } |
| 79 | for name, tc := range map[string]struct { |
| 80 | in deck.Text |
| 81 | want []string |
| 82 | }{ |
| 83 | "no line": {deck.Text{p("a"), p("b")}, []string{"a\nb"}}, |
| 84 | "empty": {nil, []string{""}}, |
| 85 | "middle": {deck.Text{p("a"), p("(Press.)"), p("b")}, []string{"a", "b"}}, |
| 86 | "leading": {deck.Text{p("(Press.)"), p("a")}, []string{"", "a"}}, |
| 87 | "trailing": {deck.Text{p("a"), p("(Press.)")}, []string{"a", ""}}, |
| 88 | "two": {deck.Text{p("a"), p("(Press.)"), p("b"), p("(Press.)"), p("c")}, []string{"a", "b", "c"}}, |
| 89 | "marks inside": {deck.Text{p("a"), para(deck.Run{T: "(", B: true}, deck.Run{T: "Press.)"}), p("b")}, []string{"a", "b"}}, |
| 90 | "surrounding space": {deck.Text{p("a"), p(" (Press.) "), p("b")}, []string{"a", "b"}}, |
| 91 | "lowercase": {deck.Text{p("a"), p("(press.)"), p("b")}, []string{"a\n(press.)\nb"}}, |
| 92 | "inside a line": {deck.Text{p("(Press.) now")}, []string{"(Press.) now"}}, |
| 93 | } { |
| 94 | if got := plain(deck.NoteParts(tc.in)); !reflect.DeepEqual(got, tc.want) { |
| 95 | t.Errorf("%s: parts %q, want %q", name, got, tc.want) |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |