From f55ccec939b766bec8edf6d4bd842ab292c17ee8 Mon Sep 17 00:00:00 2001 From: boyska Date: Sun, 26 Dec 2021 19:59:45 +0100 Subject: [PATCH] parseM3U tested --- radiomanifest.js | 44 ++++++++++++++++++---------------- test/parser-M3U.test.js | 53 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 20 deletions(-) create mode 100644 test/parser-M3U.test.js diff --git a/radiomanifest.js b/radiomanifest.js index 149c910..09351da 100644 --- a/radiomanifest.js +++ b/radiomanifest.js @@ -111,35 +111,39 @@ async function get (siteurl, options) { } catch (e) { true } - if(resp !== null) { - try { - text = await resp.text() + if(resp !== null) { + try { + text = await resp.text() - const data = JSON.parse(text) - const name = data['icy-name'] - if (name !== undefined) { - manifest.setName(name) - } - } catch (e) { - if (e instanceof SyntaxError) { - true - } else { - console.error('Error', e) - throw e - } - } - } + const data = JSON.parse(text) + const name = data['icy-name'] + if (name !== undefined) { + manifest.setName(name) + } + } catch (e) { + if (e instanceof SyntaxError) { + true + } else { + console.error('Error', e) + throw e + } + } + } // XXX: in base alle options fai fetch anche di altra roba return manifest } function parseM3U (body) { - body.split('\n').filter((e) => { - if (e.startsWith('#')) { + return body.split('\n').filter((line) => { + if (line.startsWith('#')) { return false } else { - try { new URL(e); return true } catch { return false } + try { + new URL(line); return true + } catch { + return false + } } }) } diff --git a/test/parser-M3U.test.js b/test/parser-M3U.test.js new file mode 100644 index 0000000..7823165 --- /dev/null +++ b/test/parser-M3U.test.js @@ -0,0 +1,53 @@ +const parseM3U = require('../radiomanifest.js').parsers.M3U +const chai = require('chai') +chai.use(require('chai-as-promised')) +const assert = chai.assert + +const expect = chai.expect + +describe('parseM3U parses basic M3U', () => { + describe('empty M3U', () => { + it('should return empty list', () => { + var r = parseM3U("") + assert.equal(r.length, 0) + }) + it('should discard empty lines', () => { + var r = parseM3U("\n\n\n") + assert.equal(r.length, 0) + }) + }) + describe('just some lines', () => { + it('should return appropriate list', () => { + var r = parseM3U("http://foo") + assert.equal(r.length, 1) + assert.equal(r[0], "http://foo") + }) + it('should work with longer list', () => { + var r = parseM3U("http://foo\nhttp://baz\nhttp://bar\n") + assert.equal(r.length, 3) + assert.equal(r[0], "http://foo") + assert.equal(r[1], "http://baz") + assert.equal(r[2], "http://bar") + }) + it('should discard empty lines', () => { + var r = parseM3U("http://foo\n\nhttp://baz\nhttp://bar\n\n\n") + assert.equal(r.length, 3) + assert.equal(r[0], "http://foo") + assert.equal(r[1], "http://baz") + assert.equal(r[2], "http://bar") + }) + }) + describe('comments', () => { + it('comments should be ignored', () => { + var r = parseM3U("http://foo\n#asd") + assert.equal(r.length, 1) + assert.equal(r[0], "http://foo") + }) + it('mid-line hash is not a comment', () => { + var r = parseM3U("http://foo#asd") + assert.equal(r.length, 1) + assert.equal(r[0], "http://foo#asd") + }) + }) +}) +