-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #125 from metabrainz/replace-parser
Replace parser
- Loading branch information
Showing
6 changed files
with
267 additions
and
253 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 |
---|---|---|
@@ -1,130 +1,154 @@ | ||
from uuid import UUID | ||
import unittest | ||
|
||
from troi.parse_prompt import parse, ParseError | ||
from troi.parse_prompt import PromptParser, ParseError | ||
|
||
|
||
class TestParser(unittest.TestCase): | ||
|
||
def test_basic_entities(self): | ||
r = parse("a:57baa3c6-ee43-4db3-9e6a-50bbc9792ee4") | ||
pp = PromptParser() | ||
r = pp.parse("artist:57baa3c6-ee43-4db3-9e6a-50bbc9792ee4") | ||
assert r[0] == {"entity": "artist", "values": [UUID("57baa3c6-ee43-4db3-9e6a-50bbc9792ee4")], "weight": 1, "opts": []} | ||
|
||
r = parse("artist:57baa3c6-ee43-4db3-9e6a-50bbc9792ee4") | ||
r = pp.parse("artist:57baa3c6-ee43-4db3-9e6a-50bbc9792ee4") | ||
assert r[0] == {"entity": "artist", "values": [UUID("57baa3c6-ee43-4db3-9e6a-50bbc9792ee4")], "weight": 1, "opts": []} | ||
|
||
self.assertRaises(ParseError, parse, "wrong:57baa3c6-ee43-4db3-9e6a-50bbc9792ee4") | ||
self.assertRaises(ParseError, pp.parse, "wrong:57baa3c6-ee43-4db3-9e6a-50bbc9792ee4") | ||
|
||
r = parse("artist:(the knife)") | ||
r = pp.parse("artist:(the knife)") | ||
assert r[0] == {"entity": "artist", "values": ["the knife"], "weight": 1, "opts": []} | ||
|
||
self.assertRaises(ParseError, parse, "artist:u2:nosim") | ||
self.assertRaises(ParseError, pp.parse, "artist:u2:nosim") | ||
self.assertRaises(ParseError, pp.parse, "artists:u2:nosim") | ||
|
||
def test_tags(self): | ||
r = parse("t:(abstract,rock,blues)") | ||
pp = PromptParser() | ||
r = pp.parse("tag:abstract tag:rock tag:blues") | ||
assert r[0] == {"entity": "tag", "values": ["abstract"], "weight": 1, "opts": []} | ||
assert r[1] == {"entity": "tag", "values": ["rock"], "weight": 1, "opts": []} | ||
assert r[2] == {"entity": "tag", "values": ["blues"], "weight": 1, "opts": []} | ||
|
||
r = pp.parse("tag:(abstract,rock,blues)") | ||
assert r[0] == {"entity": "tag", "values": ["abstract", "rock", "blues"], "weight": 1, "opts": []} | ||
|
||
r = parse("t:(abstract rock blues)") | ||
r = pp.parse("tag:(abstract rock blues)") | ||
assert r[0] == {"entity": "tag", "values": ["abstract rock blues"], "weight": 1, "opts": []} | ||
|
||
r = parse("tag:(abstract,rock,blues):1:or") | ||
r = pp.parse("tag:(abstract,rock,blues):1:or") | ||
assert r[0] == {"entity": "tag", "values": ["abstract", "rock", "blues"], "weight": 1, "opts": ["or"]} | ||
r = parse("t:(abstract,rock,blues)") | ||
r = pp.parse("tag:(abstract,rock,blues)") | ||
assert r[0] == {"entity": "tag", "values": ["abstract", "rock", "blues"], "weight": 1, "opts": []} | ||
|
||
r = parse('t:(trip hop, hip hop)') | ||
r = pp.parse('tag:(trip hop, hip hop)') | ||
assert r[0] == {"entity": "tag", "values": ["trip hop", "hip hop"], "weight": 1, "opts": []} | ||
|
||
r = parse("t:(r&b)") | ||
r = pp.parse("tag:(r&b)") | ||
assert r[0] == {"entity": "tag", "values": ["r&b"], "weight": 1, "opts": []} | ||
|
||
r = parse("t:(blümchen)") | ||
r = pp.parse("tag:(blümchen)") | ||
assert r[0] == {"entity": "tag", "values": ["blümchen"], "weight": 1, "opts": []} | ||
|
||
r = parse("t:(モーニング娘。)") | ||
r = pp.parse("tag:(モーニング娘。)") | ||
assert r[0] == {"entity": "tag", "values": ["モーニング娘。"], "weight": 1, "opts": []} | ||
|
||
def test_tag_errors(self): | ||
self.assertRaises(ParseError, parse, "t:(abstract rock blues):bork") | ||
self.assertRaises(ParseError, parse, "tag:(foo") | ||
self.assertRaises(ParseError, parse, "tag:foo)") | ||
self.assertRaises(ParseError, parse, 'tag:foo"') | ||
self.assertRaises(ParseError, parse, 'tag:"foo') | ||
pp = PromptParser() | ||
self.assertRaises(ParseError, pp.parse, "t:(abstract rock blues):bork") | ||
self.assertRaises(ParseError, pp.parse, "tag:(foo") | ||
self.assertRaises(ParseError, pp.parse, "tag:foo)") | ||
|
||
def test_shortcuts(self): | ||
r = parse("#abstract #rock #blues") | ||
pp = PromptParser() | ||
r = pp.parse("#abstract") | ||
assert r[0] == {"entity": "tag", "values": ["abstract"], "weight": 1, "opts": []} | ||
assert r[1] == {"entity": "tag", "values": ["rock"], "weight": 1, "opts": []} | ||
assert r[2] == {"entity": "tag", "values": ["blues"], "weight": 1, "opts": []} | ||
|
||
pp = PromptParser() | ||
r = pp.parse("u2") | ||
assert r[0] == {"entity": "artist", "values": ["u2"], "weight": 1, "opts": []} | ||
|
||
def test_compound(self): | ||
r = parse('artist:05319f96-e409-4199-b94f-3cabe7cc188a:2 #downtempo:1 tag:(trip hop, abstract):2') | ||
pp = PromptParser() | ||
r = pp.parse('artist:05319f96-e409-4199-b94f-3cabe7cc188a:2 tag:(downtempo):1 tag:(trip hop, abstract):2') | ||
assert r[0] == {"entity": "artist", "values": [UUID("05319f96-e409-4199-b94f-3cabe7cc188a")], "weight": 2, "opts": []} | ||
assert r[1] == {"entity": "tag", "values": ["downtempo"], "weight": 1, "opts": []} | ||
assert r[2] == {"entity": "tag", "values": ["trip hop", "abstract"], "weight": 2, "opts": []} | ||
|
||
def test_weights(self): | ||
r = parse("a:57baa3c6-ee43-4db3-9e6a-50bbc9792ee4:1 a:f54ba4c6-12dd-4358-9136-c64ad89420c5:2") | ||
pp = PromptParser() | ||
r = pp.parse("artist:57baa3c6-ee43-4db3-9e6a-50bbc9792ee4:1 artist:f54ba4c6-12dd-4358-9136-c64ad89420c5:2") | ||
assert r[0] == {"entity": "artist", "values": [UUID("57baa3c6-ee43-4db3-9e6a-50bbc9792ee4")], "weight": 1, "opts": []} | ||
assert r[1] == {"entity": "artist", "values": [UUID("f54ba4c6-12dd-4358-9136-c64ad89420c5")], "weight": 2, "opts": []} | ||
|
||
self.assertRaises(ParseError, parse, | ||
"a:57baa3c6-ee43-4db3-9e6a-50bbc9792ee4:1 a:f54ba4c6-12dd-4358-9136-c64ad89420c5:fussy") | ||
self.assertRaises(ParseError, parse, "a:57baa3c6-ee43-4db3-9e6a-50bbc9792ee4:1 a:f54ba4c6-12dd-4358-9136-c64ad89420c5:.5") | ||
self.assertRaises(ParseError, pp.parse, | ||
"artist:57baa3c6-ee43-4db3-9e6a-50bbc9792ee4:1 artist:f54ba4c6-12dd-4358-9136-c64ad89420c5:fussy") | ||
self.assertRaises(ParseError, pp.parse, "artist:57baa3c6-ee43-4db3-9e6a-50bbc9792ee4:1 artist:f54ba4c6-12dd-4358-9136-c64ad89420c5:.5") | ||
|
||
r = parse("a:(portishead)::easy") | ||
r = pp.parse("artist:portishead::easy") | ||
assert r[0] == {"entity": "artist", "values": ["portishead"], "weight": 1, "opts": ["easy"]} | ||
|
||
r = parse("a:57baa3c6-ee43-4db3-9e6a-50bbc9792ee4::easy") | ||
r = pp.parse("artist:57baa3c6-ee43-4db3-9e6a-50bbc9792ee4::easy") | ||
assert r[0] == {"entity": "artist", "values": [UUID("57baa3c6-ee43-4db3-9e6a-50bbc9792ee4")], "weight": 1, "opts": ["easy"]} | ||
|
||
def test_collection_playlist(self): | ||
def test_opts(self): | ||
pp = PromptParser() | ||
r = pp.parse("stats:(mr_monkey)::month") | ||
print(r[0]) | ||
assert r[0] == {"entity": "stats", "values": ["mr_monkey"], "weight": 1, "opts": ["month"]} | ||
r = pp.parse("artist:(monkey)::nosim,easy") | ||
assert r[0] == {"entity": "artist", "values": ["monkey"], "weight": 1, "opts": ["nosim", "easy"]} | ||
|
||
self.assertRaises(ParseError, pp.parse, 'artist:(meh)::nosim,') | ||
|
||
r = parse("collection:57baa3c6-ee43-4db3-9e6a-50bbc9792ee4") | ||
def test_parens(self): | ||
pp = PromptParser() | ||
self.assertRaises(ParseError, pp.parse, 'artist:adfadf(meh)') | ||
self.assertRaises(ParseError, pp.parse, 'artist:adfadf(meh') | ||
self.assertRaises(ParseError, pp.parse, 'artist:adfadf)meh') | ||
|
||
def test_collection_playlist(self): | ||
pp = PromptParser() | ||
r = pp.parse("collection:57baa3c6-ee43-4db3-9e6a-50bbc9792ee4") | ||
assert r[0] == {"entity": "collection", "values": [UUID("57baa3c6-ee43-4db3-9e6a-50bbc9792ee4")], "weight": 1, "opts": []} | ||
|
||
r = parse("playlist:57baa3c6-ee43-4db3-9e6a-50bbc9792ee4") | ||
r = pp.parse("playlist:57baa3c6-ee43-4db3-9e6a-50bbc9792ee4") | ||
assert r[0] == {"entity": "playlist", "values": [UUID("57baa3c6-ee43-4db3-9e6a-50bbc9792ee4")], "weight": 1, "opts": []} | ||
|
||
r = parse("p:57baa3c6-ee43-4db3-9e6a-50bbc9792ee4") | ||
r = pp.parse("playlist:57baa3c6-ee43-4db3-9e6a-50bbc9792ee4") | ||
assert r[0] == {"entity": "playlist", "values": [UUID("57baa3c6-ee43-4db3-9e6a-50bbc9792ee4")], "weight": 1, "opts": []} | ||
|
||
def test_stats(self): | ||
|
||
r = parse("stats:") | ||
assert r[0] == {"entity": "stats", "values": [], "weight": 1, "opts": []} | ||
|
||
r = parse("stats:mr_monkey:1:year") | ||
pp = PromptParser() | ||
r = pp.parse("stats:mr_monkey:1:year") | ||
assert r[0] == {"entity": "stats", "values": ["mr_monkey"], "weight": 1, "opts": ["year"]} | ||
|
||
r = parse("s:rob:1:week") | ||
r = pp.parse("stats:rob:1:week") | ||
assert r[0] == {"entity": "stats", "values": ["rob"], "weight": 1, "opts": ["week"]} | ||
|
||
r = parse("stats:(mr_monkey)::month") | ||
r = pp.parse("stats:(mr_monkey)::month") | ||
assert r[0] == {"entity": "stats", "values": ["mr_monkey"], "weight": 1, "opts": ["month"]} | ||
|
||
r = parse("stats:(mr_monkey):2:month") | ||
r = pp.parse("stats:(mr_monkey):2:month") | ||
assert r[0] == {"entity": "stats", "values": ["mr_monkey"], "weight": 2, "opts": ["month"]} | ||
|
||
r = parse("stats:(rob zombie)") | ||
r = pp.parse("stats:(rob zombie)") | ||
assert r[0] == {"entity": "stats", "values": ["rob zombie"], "weight": 1, "opts": []} | ||
|
||
def test_recs(self): | ||
pp = PromptParser() | ||
self.assertRaises(ParseError, pp.parse, 'recs:') | ||
|
||
r = parse("recs:") | ||
assert r[0] == {"entity": "recs", "values": [], "weight": 1, "opts": []} | ||
|
||
r = parse("recs:mr_monkey::listened") | ||
r = pp.parse("recs:mr_monkey::listened") | ||
assert r[0] == {"entity": "recs", "values": ["mr_monkey"], "weight": 1, "opts": ["listened"]} | ||
|
||
r = parse("r:rob:1:unlistened") | ||
r = pp.parse("recs:rob:1:unlistened") | ||
assert r[0] == {"entity": "recs", "values": ["rob"], "weight": 1, "opts": ["unlistened"]} | ||
|
||
r = parse("recs:(mr_monkey):1:listened") | ||
r = pp.parse("recs:(mr_monkey):1:listened") | ||
assert r[0] == {"entity": "recs", "values": ["mr_monkey"], "weight": 1, "opts": ["listened"]} | ||
|
||
r = parse("recs:(mr_monkey):2:unlistened") | ||
r = pp.parse("recs:(mr_monkey):2:unlistened") | ||
assert r[0] == {"entity": "recs", "values": ["mr_monkey"], "weight": 2, "opts": ["unlistened"]} | ||
|
||
r = parse("recs:(rob zombie)") | ||
r = pp.parse("recs:(rob zombie)") | ||
assert r[0] == {"entity": "recs", "values": ["rob zombie"], "weight": 1, "opts": []} |
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
Oops, something went wrong.