|
| 1 | +/** |
| 2 | + * This file is part of tz. |
| 3 | + * |
| 4 | + * tz is free software: you can redistribute it and/or modify it under |
| 5 | + * the terms of the GNU General Public License as published by the Free |
| 6 | + * Software Foundation, either version 3 of the License, or (at your |
| 7 | + * option) any later version. |
| 8 | + * |
| 9 | + * tz is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 11 | + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public |
| 12 | + * License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with tz. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + **/ |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + tea "github.com/charmbracelet/bubbletea" |
| 23 | +) |
| 24 | + |
| 25 | +func TestUpdateIncHour(t *testing.T) { |
| 26 | + // "l" key -> go right |
| 27 | + msg := tea.KeyMsg{ |
| 28 | + Type: tea.KeyRunes, |
| 29 | + Runes: []rune{'l'}, |
| 30 | + Alt: false, |
| 31 | + } |
| 32 | + |
| 33 | + tests := []struct { |
| 34 | + startHour int |
| 35 | + nextHour int |
| 36 | + }{ |
| 37 | + {startHour: 0, nextHour: 1}, |
| 38 | + {startHour: 1, nextHour: 2}, |
| 39 | + // ... |
| 40 | + {startHour: 23, nextHour: 0}, |
| 41 | + } |
| 42 | + |
| 43 | + for _, test := range tests { |
| 44 | + m := model{ |
| 45 | + zones: DefaultZones, |
| 46 | + hour: test.startHour, |
| 47 | + } |
| 48 | + nextState, cmd := m.Update(msg) |
| 49 | + if cmd != nil { |
| 50 | + t.Errorf("Expected nil Cmd, but got %v", cmd) |
| 51 | + return |
| 52 | + } |
| 53 | + h := nextState.(model).hour |
| 54 | + if h != test.nextHour { |
| 55 | + t.Errorf("Expected %d, but got %d", test.nextHour, h) |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func TestUpdateDecHour(t *testing.T) { |
| 61 | + // "h" key -> go right |
| 62 | + msg := tea.KeyMsg{ |
| 63 | + Type: tea.KeyRunes, |
| 64 | + Runes: []rune{'h'}, |
| 65 | + Alt: false, |
| 66 | + } |
| 67 | + |
| 68 | + tests := []struct { |
| 69 | + startHour int |
| 70 | + nextHour int |
| 71 | + }{ |
| 72 | + {startHour: 23, nextHour: 22}, |
| 73 | + {startHour: 22, nextHour: 21}, |
| 74 | + // ... |
| 75 | + {startHour: 0, nextHour: 23}, |
| 76 | + } |
| 77 | + |
| 78 | + for _, test := range tests { |
| 79 | + m := model{ |
| 80 | + zones: DefaultZones, |
| 81 | + hour: test.startHour, |
| 82 | + } |
| 83 | + nextState, cmd := m.Update(msg) |
| 84 | + if cmd != nil { |
| 85 | + t.Errorf("Expected nil Cmd, but got %v", cmd) |
| 86 | + return |
| 87 | + } |
| 88 | + h := nextState.(model).hour |
| 89 | + if h != test.nextHour { |
| 90 | + t.Errorf("Expected %d, but got %d", test.nextHour, h) |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func TestUpdateQuitMsg(t *testing.T) { |
| 96 | + // "q" key -> quit |
| 97 | + msg := tea.KeyMsg{ |
| 98 | + Type: tea.KeyRunes, |
| 99 | + Runes: []rune{'q'}, |
| 100 | + Alt: false, |
| 101 | + } |
| 102 | + |
| 103 | + m := model{ |
| 104 | + zones: DefaultZones, |
| 105 | + hour: 10, |
| 106 | + } |
| 107 | + _, cmd := m.Update(msg) |
| 108 | + if cmd == nil { |
| 109 | + t.Errorf("Expected tea.Quit Cmd, but got %v", cmd) |
| 110 | + return |
| 111 | + } |
| 112 | + // tea.Quit is a function, we can't really test with == here, and |
| 113 | + // calling it is getting into internal territory. |
| 114 | +} |
0 commit comments