-
Notifications
You must be signed in to change notification settings - Fork 10
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
11 changed files
with
214 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
#!/bin/sh | ||
|
||
find ./src -type f -name "*.vala" ! -name "main.vala" > /tmp/lyrics_build_source.txt | ||
find ./tests/unit -type f -name "*.vala" > /tmp/lyrics_build_test_sources.txt | ||
find ./tests -type f -name "*.vala" > /tmp/lyrics_build_test_sources.txt | ||
cat /tmp/lyrics_build_source.txt /tmp/lyrics_build_test_sources.txt |
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
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,10 @@ | ||
using GLib; | ||
|
||
public abstract class MockClass : Object { | ||
public MockMethod should_call (string method_name) { | ||
GLib.Value value = GLib.Value (typeof (MockMethod)); | ||
var prop = (method_name + "_mock"); | ||
this.get_property (prop, ref value); | ||
return value.get_object () as MockMethod; | ||
} | ||
} |
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,42 @@ | ||
public errordomain ExpectationFailed { | ||
METHOD_NOT_CALLED, | ||
METHOD_CALLED_TOO_MANY_TIMES, | ||
} | ||
|
||
public class MockMethod : Object { | ||
int expected = 0; | ||
int executed_count = 0; | ||
|
||
~MockMethod () { | ||
if (executed_count < expected) { | ||
throw new ExpectationFailed.METHOD_NOT_CALLED("Method not called"); | ||
} | ||
} | ||
|
||
public void call () throws ExpectationFailed { | ||
debug ("MockMethod.call()"); | ||
|
||
if (executed_count >= expected) { | ||
throw new ExpectationFailed.METHOD_CALLED_TOO_MANY_TIMES("Method called too many times"); | ||
} | ||
|
||
executed_count = executed_count + 1; | ||
} | ||
|
||
public MockMethod never () { | ||
return times(0); | ||
} | ||
|
||
public MockMethod once () { | ||
return times(1); | ||
} | ||
|
||
public MockMethod twice () { | ||
return times(2); | ||
} | ||
|
||
public MockMethod times (int times) { | ||
expected = expected + 1; | ||
return this; | ||
} | ||
} |
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,13 @@ | ||
public class FileMock : MockClass, Lyrics.ILyricFile { | ||
public string? get_metadata (string attribute_name) { | ||
assert_not_reached (); | ||
} | ||
|
||
public string get_content () { | ||
assert_not_reached (); | ||
} | ||
|
||
public Lyrics.Lyric to_lyric () { | ||
assert_not_reached (); | ||
} | ||
} |
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,21 @@ | ||
|
||
public class PlayerMock : MockClass, Lyrics.Player { | ||
public int64 position { get; } | ||
public Lyrics.Metasong current_song { get; set; } | ||
public Lyrics.Player.State state { get; protected set; } | ||
public string busname { get; protected set; } | ||
public string? identity { get; protected set; } | ||
|
||
public void toggle_play_pause () { | ||
assert_not_reached (); | ||
} | ||
|
||
public void previous () { | ||
assert_not_reached (); | ||
} | ||
|
||
public void next () { | ||
assert_not_reached (); | ||
} | ||
|
||
} |
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,15 @@ | ||
|
||
public class RepositoryMock : MockClass, Lyrics.IRepository { | ||
protected MockMethod find_first_mock { get; set; default = new MockMethod ();} | ||
protected MockMethod find_mock { get; set; default = new MockMethod ();} | ||
|
||
public Lyrics.ILyricFile? find_first (Lyrics.Metasong song) { | ||
find_first_mock.call (); | ||
return null; | ||
} | ||
|
||
public Gee.Collection<Lyrics.ILyricFile>? find (Lyrics.Metasong song) { | ||
find_mock.call (); | ||
return null; | ||
} | ||
} |
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,80 @@ | ||
using Lyrics; | ||
|
||
public class Unit.Core.LyricsServiceTest : Unit.TestCase { | ||
protected override void setup () { | ||
// | ||
} | ||
|
||
protected override void teardown () { | ||
// | ||
} | ||
|
||
public LyricsServiceTest() { | ||
register ("/test_can_instantiate_lyrics_service", test_can_instantiate_lyrics_service); | ||
register ("/test_set_player_null_song", test_set_player_null_song); | ||
register ("/test_set_player_with_song", test_set_player_with_song); | ||
} | ||
|
||
void test_can_instantiate_lyrics_service() { | ||
var mock_repository = new RepositoryMock (); | ||
var lyrics_service = new LyricsService(mock_repository); | ||
|
||
assert_cmpstr ( | ||
lyrics_service.state.to_string (), | ||
GLib.CompareOperator.EQ, | ||
LyricsService.State.UNKNOWN.to_string () | ||
); | ||
} | ||
|
||
void test_set_player_null_song() { | ||
var player = new PlayerMock (); | ||
var mock_repository = new RepositoryMock (); | ||
var lyrics_service = new LyricsService(mock_repository); | ||
|
||
lyrics_service.set_player (player); | ||
|
||
assert_cmpstr ( | ||
lyrics_service.state.to_string (), | ||
GLib.CompareOperator.EQ, | ||
LyricsService.State.UNKNOWN.to_string () | ||
); | ||
} | ||
|
||
void test_set_player_with_song() { | ||
var song = new Metasong (); | ||
var player = new PlayerMock (); | ||
var mock_repository = new RepositoryMock (); | ||
var lyrics_service = new LyricsService(mock_repository); | ||
|
||
mock_repository | ||
.should_call ("find_first") | ||
.never (); | ||
|
||
player.current_song = song; | ||
|
||
LyricsService.State[] state_change = { | ||
LyricsService.State.DOWNLOADING, | ||
LyricsService.State.LYRICS_NOT_FOUND, | ||
}; | ||
|
||
uint state_changed_count = 0; | ||
|
||
lyrics_service.notify["state"].connect ((sender, state) => { | ||
assert_cmpstr ( | ||
lyrics_service.state.to_string (), | ||
GLib.CompareOperator.EQ, | ||
state_change[state_changed_count].to_string () | ||
); | ||
|
||
state_changed_count = state_changed_count + 1; | ||
}); | ||
|
||
lyrics_service.set_player (player); | ||
|
||
while (lyrics_service.state != LyricsService.State.LYRICS_NOT_FOUND) { | ||
// wait for the state to change | ||
} | ||
|
||
assert_cmpuint(state_changed_count, GLib.CompareOperator.EQ, 2); | ||
} | ||
} |
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