Click here to Skip to main content
15,884,472 members
Articles / Programming Languages / C#

LyricsFetcher - The Easiest Way to Find Lyrics for your Songs

Rate me:
Please Sign up or sign in to vote.
4.93/5 (82 votes)
29 Oct 2009GPL325 min read 201.2K   2.4K   184  
An article describing the development of a non-trivial C#/.NET application to fetch lyrics for songs.
/*
 * [File purpose]
 * Author: Phillip Piper
 * Date: 2009-02-09 10:52 PM
 * 
 * CHANGE LOG:
 * 2009-02-09  JPP  Initial Version
 */

using System;
using System.Collections.Generic;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;

using LyricsFetcher;

namespace LyricsFetcher.Tests
{
    [TestFixture]
    public class TestLyricsCache
    {
        [Test]
        public void TestEmpty()
        {
            LyricsCache cache = new LyricsCache();
            Song s1 = new TestSong("title", "artist", "album", "genre");
            Assert.IsNull(cache.GetLyrics(s1));
        }

        [Test]
        public void TestCaching()
        {
            LyricsCache cache = new LyricsCache();
            Song s1 = new TestSong("title", "artist", "album", "genre");
            s1.Lyrics = "the new lyrics";

            Song s2 = new TestSong("title2", "artist2", "album", "genre");
            s2.Lyrics = "something else2";

            Assert.IsNull(cache.GetLyrics(s1));
            Assert.IsNull(cache.GetLyrics(s2));

            cache.PutLyrics(s1);
            cache.PutLyrics(s2);

            Assert.AreEqual(s1.Lyrics, cache.GetLyrics(s1));
            Assert.AreEqual(s2.Lyrics, cache.GetLyrics(s2));
        }

        [Test]
        public void TestUpdating()
        {
            LyricsCache cache = new LyricsCache();
            Song s1 = new TestSong("title", "artist", "album", "genre");

            Assert.IsFalse(cache.UpdateLyrics(s1));
            s1.Lyrics = "the new lyrics";
            cache.PutLyrics(s1);

            Song sameTitleAndArtist = new TestSong("title", "artist", "album", "genre");
            sameTitleAndArtist.Lyrics = "something else";

            Assert.IsTrue(cache.UpdateLyrics(sameTitleAndArtist));
            Assert.AreEqual(s1.Lyrics, sameTitleAndArtist.Lyrics);
        }

        [Test]
        public void TestSaveLoad()
        {
            Song s1 = new TestSong("title", "artist", "album", "genre");
            Song s2 = new TestSong("title2", "artist2", "album", "genre");
            Song s3 = new TestSong("title3", "artist3", "album", "genre");

            s1.Lyrics = "the lyrics";
            s2.Lyrics = "something else2";
            s3.Lyrics = "another something else3";

            LyricsCache cache = new LyricsCache();

            cache.PutLyrics(s1);
            cache.PutLyrics(s2);
            cache.PutLyrics(s3);
            cache.SaveLyricsCache(@"c:\temp\tempCache.bin");

            LyricsCache cache2 = new LyricsCache();
            cache2.LoadLyricsCache(@"c:\temp\tempCache.bin");

            Song t1 = new TestSong("title", "artist", "album", "genre");
            Song t2 = new TestSong("title2", "artist2", "album", "genre");
            Song t3 = new TestSong("title3", "artist3", "album", "genre");
            Song t4 = new TestSong("title4", "artist4", "album", "genre");

            Assert.IsTrue(cache.UpdateLyrics(t1));
            Assert.IsTrue(cache.UpdateLyrics(t2));
            Assert.IsTrue(cache.UpdateLyrics(t3));
            Assert.IsFalse(cache.UpdateLyrics(t4));

            Assert.AreEqual(s1.Lyrics, t1.Lyrics);
            Assert.AreEqual(s2.Lyrics, t2.Lyrics);
            Assert.AreEqual(s3.Lyrics, t3.Lyrics);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Team Leader
Australia Australia
Phillip has been playing with computers since the Apple II was the hottest home computer available. He learned the fine art of C programming and Guru meditation on the Amiga.

C# and Python are his languages of choice. Smalltalk is his mentor for simplicity and beauty. C++ is to programming what drills are to visits to the dentist.

He worked for longer than he cares to remember as Lead Programmer and System Architect of the Objective document management system. (www.objective.com)

He has lived for 10 years in northern Mozambique, teaching in villages.

He has developed high volume trading software, low volume FX trading software, and is currently working for Atlassian on HipChat.

Comments and Discussions