-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
249 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/** | ||
* This file is part of tz. | ||
* | ||
* tz is free software: you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License as published by the Free | ||
* Software Foundation, either version 3 of the License, or (at your | ||
* option) any later version. | ||
* | ||
* tz is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public | ||
* License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with tz. If not, see <https://www.gnu.org/licenses/>. | ||
**/ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestSetupZone(t *testing.T) { | ||
now := time.Now() | ||
|
||
tests := []struct { | ||
zoneName string | ||
ok bool | ||
}{ | ||
{ | ||
zoneName: "Europe/Paris", | ||
ok: true, | ||
}, | ||
{ | ||
zoneName: "America/London", | ||
ok: false, | ||
}, | ||
} | ||
for _, test := range tests { | ||
_, err := SetupZone(now, test.zoneName) | ||
if test.ok != (err == nil) { | ||
t.Errorf("Expected %v, but got: %v", test.ok, err) | ||
} | ||
} | ||
} | ||
|
||
func TestSetupZoneWithCustomNames(t *testing.T) { | ||
now := time.Now() | ||
|
||
tests := []struct { | ||
zoneName string | ||
shortName string | ||
ok bool | ||
}{ | ||
{ | ||
zoneName: "Europe/Paris;bonjour", | ||
shortName: "(CET) bonjour", | ||
ok: true, | ||
}, | ||
{ | ||
zoneName: "America/Mexico_City;hola", | ||
shortName: "(CST) hola", | ||
ok: true, | ||
}, | ||
{ | ||
zoneName: "America/Invalid", | ||
shortName: "", | ||
ok: false, | ||
}, | ||
} | ||
for _, test := range tests { | ||
z, err := SetupZone(now, test.zoneName) | ||
if test.ok != (err == nil) { | ||
t.Errorf("Expected %v, but got: %v", test.ok, err) | ||
} | ||
if z != nil && test.shortName != z.Name { | ||
t.Errorf("Expected %v, but got: %v", test.shortName, z.Name) | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/** | ||
* This file is part of tz. | ||
* | ||
* tz is free software: you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License as published by the Free | ||
* Software Foundation, either version 3 of the License, or (at your | ||
* option) any later version. | ||
* | ||
* tz is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public | ||
* License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with tz. If not, see <https://www.gnu.org/licenses/>. | ||
**/ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
tea "github.com/charmbracelet/bubbletea" | ||
) | ||
|
||
func TestUpdateIncHour(t *testing.T) { | ||
// "l" key -> go right | ||
msg := tea.KeyMsg{ | ||
Type: tea.KeyRunes, | ||
Runes: []rune{'l'}, | ||
Alt: false, | ||
} | ||
|
||
tests := []struct { | ||
startHour int | ||
nextHour int | ||
}{ | ||
{startHour: 0, nextHour: 1}, | ||
{startHour: 1, nextHour: 2}, | ||
// ... | ||
{startHour: 23, nextHour: 0}, | ||
} | ||
|
||
for _, test := range tests { | ||
m := model{ | ||
zones: DefaultZones, | ||
hour: test.startHour, | ||
} | ||
nextState, cmd := m.Update(msg) | ||
if cmd != nil { | ||
t.Errorf("Expected nil Cmd, but got %v", cmd) | ||
return | ||
} | ||
h := nextState.(model).hour | ||
if h != test.nextHour { | ||
t.Errorf("Expected %d, but got %d", test.nextHour, h) | ||
} | ||
} | ||
} | ||
|
||
func TestUpdateDecHour(t *testing.T) { | ||
// "h" key -> go right | ||
msg := tea.KeyMsg{ | ||
Type: tea.KeyRunes, | ||
Runes: []rune{'h'}, | ||
Alt: false, | ||
} | ||
|
||
tests := []struct { | ||
startHour int | ||
nextHour int | ||
}{ | ||
{startHour: 23, nextHour: 22}, | ||
{startHour: 22, nextHour: 21}, | ||
// ... | ||
{startHour: 0, nextHour: 23}, | ||
} | ||
|
||
for _, test := range tests { | ||
m := model{ | ||
zones: DefaultZones, | ||
hour: test.startHour, | ||
} | ||
nextState, cmd := m.Update(msg) | ||
if cmd != nil { | ||
t.Errorf("Expected nil Cmd, but got %v", cmd) | ||
return | ||
} | ||
h := nextState.(model).hour | ||
if h != test.nextHour { | ||
t.Errorf("Expected %d, but got %d", test.nextHour, h) | ||
} | ||
} | ||
} | ||
|
||
func TestUpdateQuitMsg(t *testing.T) { | ||
// "q" key -> quit | ||
msg := tea.KeyMsg{ | ||
Type: tea.KeyRunes, | ||
Runes: []rune{'q'}, | ||
Alt: false, | ||
} | ||
|
||
m := model{ | ||
zones: DefaultZones, | ||
hour: 10, | ||
} | ||
_, cmd := m.Update(msg) | ||
if cmd == nil { | ||
t.Errorf("Expected tea.Quit Cmd, but got %v", cmd) | ||
return | ||
} | ||
// tea.Quit is a function, we can't really test with == here, and | ||
// calling it is getting into internal territory. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters