spec/player_state_spec.lua (2457 bytes)
1 local PlayerState = require("subread.player_state") 2 3 describe("PlayerState.parse", function() 4 it("reads a playing report", function() 5 local state, err = PlayerState.parse("playing=1;position=96153;speed=1.5;package=de.ph1b.audiobook") 6 assert.is_nil(err) 7 assert.is_true(state.playing) 8 assert.near(96.153, state.position, 1e-9) 9 assert.equals(1.5, state.speed) 10 assert.equals("de.ph1b.audiobook", state.package) 11 end) 12 13 it("reads a paused report", function() 14 local state = PlayerState.parse("playing=0;position=0;speed=1.0;package=p") 15 assert.is_false(state.playing) 16 assert.equals(0, state.position) 17 end) 18 19 it("gives no position for a player without one", function() 20 local state = PlayerState.parse("playing=1;position=-1;speed=1.0;package=p") 21 assert.is_true(state.playing) 22 assert.is_nil(state.position) 23 end) 24 25 it("returns the error name of a problem line", function() 26 local state, err = PlayerState.parse("error=no_player") 27 assert.is_nil(state) 28 assert.equals("no_player", err) 29 state, err = PlayerState.parse("error=no_notification_access") 30 assert.is_nil(state) 31 assert.equals("no_notification_access", err) 32 end) 33 34 it("rejects an empty or broken line", function() 35 local state, err = PlayerState.parse(nil) 36 assert.is_nil(state) 37 assert.equals("empty", err) 38 state, err = PlayerState.parse("") 39 assert.equals("empty", err) 40 state, err = PlayerState.parse("hello") 41 assert.equals("malformed", err) 42 state, err = PlayerState.parse("playing=1;position=abc") 43 assert.equals("malformed", err) 44 end) 45 46 it("falls back to speed 1 for a missing or zero speed", function() 47 assert.equals(1.0, PlayerState.parse("playing=1;position=5").speed) 48 assert.equals(1.0, PlayerState.parse("playing=1;position=5;speed=0").speed) 49 end) 50 51 it("round trips a position through the seek argument", function() 52 for _, seconds in ipairs({ 0, 0.001, 1, 96.153, 3600.5, 12345.6789 }) do 53 local argument = PlayerState.seekArgument(seconds) 54 assert.is_not_nil(argument:match("^%d+$")) 55 local back = PlayerState.parse("playing=0;position=" .. argument) 56 assert.near(seconds, back.position, 0.0005) 57 end 58 assert.equals("0", PlayerState.seekArgument(-3)) 59 end) 60 end)